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
25typedef struct environment_t environment_t;
26
32
38
44
45#if defined(PLATFORM_ARDUINO)
47#elif defined(PLATFORM_ZEPHYR)
49#elif defined(PLATFORM_NRF52)
51#elif defined(PLATFORM_PATMOS)
53#elif defined(PLATFORM_RP2040)
55#elif defined(PLATFORM_FLEXPRET)
57#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
58// Windows platforms
60#elif __APPLE__
61// Apple platforms
63#elif __linux__
64// Linux
66#elif __unix__ // all unices not caught above
67// Unix
69#elif defined(_POSIX_VERSION)
70// POSIX
72#elif defined(__riscv) || defined(__riscv__)
73// RISC-V (see https://github.com/riscv/riscv-toolchain-conventions)
74#error "RISC-V not supported"
75#else
76#error "Platform not supported"
77#endif
78
79#define LF_TIMEOUT 1
80
81// Worker priorities range from 0 to 99 where 99 is the highest priority.
82#define LF_SCHED_MAX_PRIORITY 99
83#define LF_SCHED_MIN_PRIORITY 0
84
85// To support the single-threaded runtime, we need the following functions. They
86// are not required by the threaded runtime and is thus hidden behind a #ifdef.
87#if defined(LF_SINGLE_THREADED)
88typedef void* lf_mutex_t;
93int lf_disable_interrupts_nested();
98int lf_enable_interrupts_nested();
99
104int _lf_single_threaded_notify_of_event();
105
109#else
110// For platforms with threading support, the following functions
111// abstract the API so that the LF runtime remains portable.
112
117
122
129int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments);
130
134int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments);
135
145int lf_thread_join(lf_thread_t thread, void** thread_return);
146
150typedef enum {
151 LF_SCHED_FAIR, // Non real-time scheduling policy. Corresponds to SCHED_OTHER
152 LF_SCHED_TIMESLICE, // Real-time, time-slicing priority-based policty. Corresponds to SCHED_RR.
153 LF_SCHED_PRIORITY, // Real-time, priority-only based scheduling. Corresponds to SCHED_FIFO.
155
156typedef struct {
157 lf_scheduling_policy_type_t policy; // The scheduling policy
158 int priority; // The priority, if applicable
159 interval_t time_slice; // The time-slice allocated, if applicable.
161
169int lf_thread_set_cpu(lf_thread_t thread, size_t cpu_number);
170
181int lf_thread_set_priority(lf_thread_t thread, int priority);
182
194
200int lf_mutex_init(lf_mutex_t* mutex);
201
207int lf_mutex_lock(lf_mutex_t* mutex);
208
214int lf_mutex_unlock(lf_mutex_t* mutex);
215
221int lf_cond_init(lf_cond_t* cond, lf_mutex_t* mutex);
222
229
235int lf_cond_signal(lf_cond_t* cond);
236
243int lf_cond_wait(lf_cond_t* cond);
244
254int _lf_cond_timedwait(lf_cond_t* cond, instant_t wakeup_time);
255
259#ifndef thread_local
260#if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
261#define thread_local _Thread_local
262#elif defined _WIN32 && (defined _MSC_VER || defined __ICL || defined __DMC__ || defined __BORLANDC__)
263#define thread_local __declspec(thread)
264/* note that ICC (linux) and Clang are covered by __GNUC__ */
265#elif defined __GNUC__ || defined __SUNPRO_C || defined __xlC__
266#define thread_local __thread
267#else
268#error "Cannot define thread_local"
269#endif
270#endif // thread_local
271
276int lf_thread_id();
277
282#endif // !defined(LF_SINGLE_THREADED)
283
288
300
306int lf_sleep(interval_t sleep_duration);
307
319
323#ifdef __GNUC__
324#define DEPRECATED(X) X __attribute__((deprecated))
325#elif defined(_MSC_VER)
326#define DEPRECATED(X) __declspec(deprecated) X
327#else
328#define DEPRECATED(X) X
329#endif
330
334DEPRECATED(int lf_nanosleep(interval_t sleep_duration));
335
336#ifdef __cplusplus
337}
338#endif
339
340#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:1144
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:150
@ LF_SCHED_PRIORITY
Definition low_level_platform.h:153
@ LF_SCHED_TIMESLICE
Definition low_level_platform.h:152
@ LF_SCHED_FAIR
Definition low_level_platform.h:151
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:1153
int lf_critical_section_exit(environment_t *env)
Leave a critical section within an environment.
Definition reactor_threaded.c:1165
void _lf_initialize_clock(void)
#define DEPRECATED(X)
Definition low_level_platform.h:328
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:156
interval_t time_slice
Definition low_level_platform.h:159
lf_scheduling_policy_type_t policy
Definition low_level_platform.h:157
int priority
Definition low_level_platform.h:158
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