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(PLATFORM_FLEXPRET)
55#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
56// Windows platforms
58#elif __APPLE__
59// Apple platforms
61#elif __linux__
62// Linux
64#elif __unix__ // all unices not caught above
65// Unix
67#elif defined(_POSIX_VERSION)
68// POSIX
70#elif defined(__riscv) || defined(__riscv__)
71// RISC-V (see https://github.com/riscv/riscv-toolchain-conventions)
72#error "RISC-V not supported"
73#else
74#error "Platform not supported"
75#endif
76
77#define LF_TIMEOUT 1
78
79// Worker priorities range from 0 to 99 where 99 is the highest priority.
80#define LF_SCHED_MAX_PRIORITY 99
81#define LF_SCHED_MIN_PRIORITY 0
82
83// To support the single-threaded runtime, we need the following functions. They
84// are not required by the threaded runtime and is thus hidden behind a #ifdef.
85#if defined(LF_SINGLE_THREADED)
86typedef void* lf_mutex_t;
91int lf_disable_interrupts_nested();
96int lf_enable_interrupts_nested();
97
102int _lf_single_threaded_notify_of_event();
103
107#else
108// For platforms with threading support, the following functions
109// abstract the API so that the LF runtime remains portable.
110
115
120
127int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments);
128
132int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments);
133
143int lf_thread_join(lf_thread_t thread, void** thread_return);
144
148typedef enum {
149 LF_SCHED_FAIR, // Non real-time scheduling policy. Corresponds to SCHED_OTHER
150 LF_SCHED_TIMESLICE, // Real-time, time-slicing priority-based policty. Corresponds to SCHED_RR.
151 LF_SCHED_PRIORITY, // Real-time, priority-only based scheduling. Corresponds to SCHED_FIFO.
153
154typedef struct {
155 lf_scheduling_policy_type_t policy; // The scheduling policy
156 int priority; // The priority, if applicable
157 interval_t time_slice; // The time-slice allocated, if applicable.
159
167int lf_thread_set_cpu(lf_thread_t thread, size_t cpu_number);
168
179int lf_thread_set_priority(lf_thread_t thread, int priority);
180
192
198int lf_mutex_init(lf_mutex_t* mutex);
199
205int lf_mutex_lock(lf_mutex_t* mutex);
206
212int lf_mutex_unlock(lf_mutex_t* mutex);
213
219int lf_cond_init(lf_cond_t* cond, lf_mutex_t* mutex);
220
227
233int lf_cond_signal(lf_cond_t* cond);
234
241int lf_cond_wait(lf_cond_t* cond);
242
252int _lf_cond_timedwait(lf_cond_t* cond, instant_t wakeup_time);
253
257#ifndef thread_local
258#if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
259#define thread_local _Thread_local
260#elif defined _WIN32 && (defined _MSC_VER || defined __ICL || defined __DMC__ || defined __BORLANDC__)
261#define thread_local __declspec(thread)
262/* note that ICC (linux) and Clang are covered by __GNUC__ */
263#elif defined __GNUC__ || defined __SUNPRO_C || defined __xlC__
264#define thread_local __thread
265#else
266#error "Cannot define thread_local"
267#endif
268#endif // thread_local
269
274int lf_thread_id();
275
280#endif // !defined(LF_SINGLE_THREADED)
281
286
298
304int lf_sleep(interval_t sleep_duration);
305
317
321#ifdef __GNUC__
322#define DEPRECATED(X) X __attribute__((deprecated))
323#elif defined(_MSC_VER)
324#define DEPRECATED(X) __declspec(deprecated) X
325#else
326#define DEPRECATED(X) X
327#endif
328
332DEPRECATED(int lf_nanosleep(interval_t sleep_duration));
333
334#ifdef __cplusplus
335}
336#endif
337
338#endif // PLATFORM_H
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:1169
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:148
@ LF_SCHED_PRIORITY
Definition low_level_platform.h:151
@ LF_SCHED_TIMESLICE
Definition low_level_platform.h:150
@ LF_SCHED_FAIR
Definition low_level_platform.h:149
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:1178
int lf_critical_section_exit(environment_t *env)
Leave a critical section within an environment.
Definition reactor_threaded.c:1190
void _lf_initialize_clock(void)
#define DEPRECATED(X)
Definition low_level_platform.h:326
Execution environment. This struct contains information about the execution environment....
Definition environment.h:49
lf_mutex_t mutex
Definition environment.h:81
Definition lf_POSIX_threads_support.h:41
Definition low_level_platform.h:154
interval_t time_slice
Definition low_level_platform.h:157
lf_scheduling_policy_type_t policy
Definition low_level_platform.h:155
int priority
Definition low_level_platform.h:156
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