reactor-c
C Runtime for Lingua Franca
Loading...
Searching...
No Matches
low_level_platform.h
Go to the documentation of this file.
1
13#ifndef PLATFORM_H
14#define PLATFORM_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20#include "tag.h"
21#include <assert.h>
22#include "platform/lf_atomic.h"
23
24// Forward declarations
26
32
38
44
45#if defined(PLATFORM_ARDUINO)
47#elif defined(PLATFORM_ZEPHYR)
49#elif defined(PLATFORM_NRF52)
51#elif defined(PLATFORM_RP2040)
53#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
54// Windows platforms
56#elif __APPLE__
57// Apple platforms
59#elif __linux__
60// Linux
62#elif __unix__ // all unices not caught above
63// Unix
65#elif defined(_POSIX_VERSION)
66// POSIX
68#elif defined(__riscv) || defined(__riscv__)
69// RISC-V (see https://github.com/riscv/riscv-toolchain-conventions)
70#error "RISC-V not supported"
71#else
72#error "Platform not supported"
73#endif
74
75#define LF_TIMEOUT 1
76
77// Worker priorities range from 0 to 99 where 99 is the highest priority.
78#define LF_SCHED_MAX_PRIORITY 99
79#define LF_SCHED_MIN_PRIORITY 0
80
81// To support the single-threaded runtime, we need the following functions. They
82// are not required by the threaded runtime and is thus hidden behind a #ifdef.
83#if defined(LF_SINGLE_THREADED)
84typedef void* lf_mutex_t;
95
101
105#else
106// For platforms with threading support, the following functions
107// abstract the API so that the LF runtime remains portable.
108
113
118
125int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments);
126
130int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments);
131
142
146typedef enum {
147 LF_SCHED_FAIR, // Non real-time scheduling policy. Corresponds to SCHED_OTHER
148 LF_SCHED_TIMESLICE, // Real-time, time-slicing priority-based policty. Corresponds to SCHED_RR.
149 LF_SCHED_PRIORITY, // Real-time, priority-only based scheduling. Corresponds to SCHED_FIFO.
151
152typedef struct {
153 lf_scheduling_policy_type_t policy; // The scheduling policy
154 int priority; // The priority, if applicable
155 interval_t time_slice; // The time-slice allocated, if applicable.
157
166
178
190
196int lf_mutex_init(lf_mutex_t* mutex);
197
203int lf_mutex_lock(lf_mutex_t* mutex);
204
210int lf_mutex_unlock(lf_mutex_t* mutex);
211
217int lf_cond_init(lf_cond_t* cond, lf_mutex_t* mutex);
218
225
231int lf_cond_signal(lf_cond_t* cond);
232
239int lf_cond_wait(lf_cond_t* cond);
240
251
255#ifndef thread_local
256#if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
257#define thread_local _Thread_local
258#elif defined _WIN32 && (defined _MSC_VER || defined __ICL || defined __DMC__ || defined __BORLANDC__)
259#define thread_local __declspec(thread)
260/* note that ICC (linux) and Clang are covered by __GNUC__ */
261#elif defined __GNUC__ || defined __SUNPRO_C || defined __xlC__
262#define thread_local __thread
263#else
264#error "Cannot define thread_local"
265#endif
266#endif // thread_local
267
272int lf_thread_id();
273
278#endif // !defined(LF_SINGLE_THREADED)
279
284
296
303
315
319#ifdef __GNUC__
320#define DEPRECATED(X) X __attribute__((deprecated))
321#elif defined(_MSC_VER)
322#define DEPRECATED(X) __declspec(deprecated) X
323#else
324#define DEPRECATED(X) X
325#endif
326
331
332#ifdef __cplusplus
333}
334#endif
335
336#endif // PLATFORM_H
return address
Definition hashmap.h:74
void * lf_thread_t
Definition lf_arduino_support.h:120
void * lf_mutex_t
Definition lf_arduino_support.h:118
int lf_mutex_unlock(lf_mutex_t *mutex)
Definition lf_POSIX_threads_support.c:42
int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_time)
Sleep until the given wakeup time. This should not be used directly as it does not account for clock ...
int _lf_cond_timedwait(lf_cond_t *cond, instant_t wakeup_time)
Definition lf_POSIX_threads_support.c:61
int lf_thread_set_priority(lf_thread_t thread, int priority)
Set the priority of a thread. Priority ranges from 0 to 99 where a higher number indicates higher pri...
int lf_notify_of_event(environment_t *env)
Notify of new event.
Definition reactor_threaded.c:1155
int lf_thread_create(lf_thread_t *thread, void *(*lf_thread)(void *), void *arguments)
Helper function for creating a thread.
Definition lf_POSIX_threads_support.c:13
int lf_thread_id()
Cross-platform version of the C11 thread_local keyword.
Definition lf_platform_util.c:22
int lf_thread_join(lf_thread_t thread, void **thread_return)
Definition lf_POSIX_threads_support.c:19
int lf_cond_signal(lf_cond_t *cond)
Definition lf_POSIX_threads_support.c:55
int _lf_clock_gettime(instant_t *t)
lf_thread_t lf_thread_self()
Return the lf_thread_t of the calling thread.
Definition lf_POSIX_threads_support.c:17
int lf_sleep(interval_t sleep_duration)
void initialize_lf_thread_id()
Initialize the thread ID for the current thread.
Definition lf_platform_util.c:24
int lf_cond_broadcast(lf_cond_t *cond)
Definition lf_POSIX_threads_support.c:53
int lf_mutex_init(lf_mutex_t *mutex)
Definition lf_POSIX_threads_support.c:21
int lf_cond_init(lf_cond_t *cond, lf_mutex_t *mutex)
Definition lf_POSIX_threads_support.c:44
int lf_cond_wait(lf_cond_t *cond)
Definition lf_POSIX_threads_support.c:57
int lf_available_cores()
Get the number of cores on the host machine.
Definition lf_POSIX_threads_support.c:11
int lf_mutex_lock(lf_mutex_t *mutex)
Definition lf_POSIX_threads_support.c:40
int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t *policy)
Set the scheduling policy of a thread. This is based on the scheduling concept from Linux explained h...
lf_scheduling_policy_type_t
The thread scheduling policies.
Definition low_level_platform.h:146
@ LF_SCHED_PRIORITY
Definition low_level_platform.h:149
@ LF_SCHED_TIMESLICE
Definition low_level_platform.h:148
@ LF_SCHED_FAIR
Definition low_level_platform.h:147
int lf_thread_set_cpu(lf_thread_t thread, size_t cpu_number)
Pin a thread to a specific CPU.
int lf_critical_section_enter(environment_t *env)
Enter critical section within an environment.
Definition reactor_threaded.c:1164
int lf_critical_section_exit(environment_t *env)
Leave a critical section within an environment.
Definition reactor_threaded.c:1176
void _lf_initialize_clock(void)
#define DEPRECATED(X)
Definition low_level_platform.h:324
Execution environment. This struct contains information about the execution environment....
Definition environment.h:69
lf_mutex_t mutex
Definition environment.h:101
Definition lf_POSIX_threads_support.h:41
Definition low_level_platform.h:152
interval_t time_slice
Definition low_level_platform.h:155
lf_scheduling_policy_type_t policy
Definition low_level_platform.h:153
int priority
Definition low_level_platform.h:154
Time and tag definitions and functions for Lingua Franca.
int64_t instant_t
Definition tag.h:66
int64_t interval_t
Definition tag.h:71