reactor-c 1.0
C Runtime for Lingua Franca
Loading...
Searching...
No Matches
low_level_platform.h
Go to the documentation of this file.
1
16
17#ifndef PLATFORM_H
18#define PLATFORM_H
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#include "tag.h"
25#include <assert.h>
26#include "platform/lf_atomic.h"
27
28// Forward declarations
29typedef struct environment_t environment_t;
30
37
44
51
52#if defined(PLATFORM_ARDUINO)
53#include "platform/lf_arduino_support.h"
54#elif defined(PLATFORM_ZEPHYR)
55#include "platform/lf_zephyr_support.h"
56#elif defined(PLATFORM_NRF52)
57#include "platform/lf_nrf52_support.h"
58#elif defined(PLATFORM_PATMOS)
59#include "platform/lf_patmos_support.h"
60#elif defined(PLATFORM_RP2040)
61#include "platform/lf_rp2040_support.h"
62#elif defined(PLATFORM_FLEXPRET)
63#include "platform/lf_flexpret_support.h"
64#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
65// Windows platforms
66#include "platform/lf_windows_support.h"
67#elif __APPLE__
68// Apple platforms
69#include "platform/lf_macos_support.h"
70#elif __linux__
71// Linux
72#include "platform/lf_linux_support.h"
73#elif __unix__ // all unices not caught above
74// Unix
75#include "platform/lf_POSIX_threads_support.h"
76#elif defined(_POSIX_VERSION)
77// POSIX
78#include "platform/lf_POSIX_threads_support.h"
79#elif defined(__riscv) || defined(__riscv__)
80// RISC-V (see https://github.com/riscv/riscv-toolchain-conventions)
81#error "RISC-V not supported"
82#else
83#error "Platform not supported"
84#endif
85
86#define LF_TIMEOUT 1
87
88// Worker priorities range from 0 to 99 where 99 is the highest priority.
89#define LF_SCHED_MAX_PRIORITY 99
90#define LF_SCHED_MIN_PRIORITY 0
91
92// To support the single-threaded runtime, we need the following functions. They
93// are not required by the threaded runtime and is thus hidden behind a #ifdef.
94#if defined(LF_SINGLE_THREADED)
95
96// For unthreaded platforms, the mutex functions below are implemented in reactor.c
97// and do nothing, returning 0.
98typedef void* lf_mutex_t;
99int lf_mutex_unlock(lf_mutex_t* mutex);
100int lf_mutex_init(lf_mutex_t* mutex);
101int lf_mutex_lock(lf_mutex_t* mutex);
102
108int lf_disable_interrupts_nested(void);
109
115int lf_enable_interrupts_nested(void);
116
122int _lf_single_threaded_notify_of_event(void);
123
124#else // defined(LF_SINGLE_THREADED)
125
126// For platforms with threading support, the following functions
127// abstract the API so that the LF runtime remains portable.
128
134
139lf_thread_t lf_thread_self(void);
140
152int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments);
153
165int lf_thread_join(lf_thread_t thread, void** thread_return);
166
171typedef enum {
172 LF_SCHED_FAIR, // Non real-time scheduling policy. Corresponds to SCHED_OTHER
173 LF_SCHED_TIMESLICE, // Real-time, time-slicing priority-based policty. Corresponds to SCHED_RR.
174 LF_SCHED_PRIORITY, // Real-time, priority-only based scheduling. Corresponds to SCHED_FIFO.
176
181typedef struct {
182 lf_scheduling_policy_type_t policy; // The scheduling policy
183 int priority; // The priority, if applicable
184 interval_t time_slice; // The time-slice allocated, if applicable.
186
195int lf_thread_set_cpu(lf_thread_t thread, size_t cpu_number);
196
209int lf_thread_set_priority(lf_thread_t thread, int priority);
210
225
233int lf_mutex_init(lf_mutex_t* mutex);
234
242int lf_mutex_lock(lf_mutex_t* mutex);
243
251int lf_mutex_unlock(lf_mutex_t* mutex);
252
260int lf_cond_init(lf_cond_t* cond, lf_mutex_t* mutex);
261
268int lf_cond_broadcast(lf_cond_t* cond);
269
276int lf_cond_signal(lf_cond_t* cond);
277
286int lf_cond_wait(lf_cond_t* cond);
287
301int _lf_cond_timedwait(lf_cond_t* cond, instant_t wakeup_time);
302
303/*
304 * Cross-platform version of the C11 thread_local keyword.
305 */
306#ifndef thread_local
307#if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
308#define thread_local _Thread_local
309#elif defined _WIN32 && (defined _MSC_VER || defined __ICL || defined __DMC__ || defined __BORLANDC__)
310#define thread_local __declspec(thread)
311/* note that ICC (linux) and Clang are covered by __GNUC__ */
312#elif defined __GNUC__ || defined __SUNPRO_C || defined __xlC__
313#define thread_local __thread
314#else
315#error "Cannot define thread_local"
316#endif
317#endif // thread_local
318
326int lf_thread_id(void);
327
333#endif // !defined(LF_SINGLE_THREADED)
334
342
356
363int lf_sleep(interval_t sleep_duration);
364
379
380/*
381 * Macros for marking function as deprecated
382 */
383#ifdef __GNUC__
384#define DEPRECATED(X) X __attribute__((deprecated))
385#elif defined(_MSC_VER)
386#define DEPRECATED(X) __declspec(deprecated) X
387#else
388#define DEPRECATED(X) X
389#endif
390
394DEPRECATED(int lf_nanosleep(interval_t sleep_duration));
395
396#ifdef __cplusplus
397}
398#endif
399
400#endif // PLATFORM_H
int64_t instant_t
Time instant.
Definition tag.h:101
int64_t interval_t
Interval of time.
Definition tag.h:107
int lf_mutex_unlock(lf_mutex_t *mutex)
Unlock the specified mutex.
int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_time)
Sleep until the given wakeup time.
int _lf_cond_timedwait(lf_cond_t *cond, instant_t wakeup_time)
Block the current thread on the condition variable with a timeout.
int lf_thread_set_priority(lf_thread_t thread, int priority)
Set the priority of a thread.
void initialize_lf_thread_id(void)
Initialize the thread ID for the current thread.
int lf_notify_of_event(environment_t *env)
Notify of new event.
int lf_thread_create(lf_thread_t *thread, void *(*lf_thread)(void *), void *arguments)
Create a new thread.
int lf_thread_join(lf_thread_t thread, void **thread_return)
Wait for the specified thread to exit.
int lf_cond_signal(lf_cond_t *cond)
Wake up one thread waiting for condition variable cond.
int _lf_clock_gettime(instant_t *t)
Get the value of an internal (and platform-specific) physical clock.
int lf_sleep(interval_t sleep_duration)
Pause execution for a given duration.
int lf_cond_broadcast(lf_cond_t *cond)
Wake up all threads waiting for condition variable cond.
lf_thread_t lf_thread_self(void)
Return the lf_thread_t of the calling thread.
int lf_mutex_init(lf_mutex_t *mutex)
Initialize a mutex.
int lf_cond_init(lf_cond_t *cond, lf_mutex_t *mutex)
Initialize a conditional variable.
int lf_cond_wait(lf_cond_t *cond)
Wait for condition variable "cond" to be signaled or broadcast.
int lf_mutex_lock(lf_mutex_t *mutex)
Lock the specified mutex.
int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t *policy)
Set the scheduling policy of a thread.
lf_scheduling_policy_type_t
The thread scheduling policies.
Definition low_level_platform.h:171
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.
int lf_available_cores(void)
Get the number of cores on the host machine.
int lf_critical_section_exit(environment_t *env)
Leave a critical section within an environment.
void _lf_initialize_clock(void)
Initialize the LF clock.
int lf_thread_id(void)
Return the ID of the current thread.
@ LF_SCHED_PRIORITY
Definition low_level_platform.h:174
@ LF_SCHED_TIMESLICE
Definition low_level_platform.h:173
@ LF_SCHED_FAIR
Definition low_level_platform.h:172
int lf_nanosleep(interval_t sleep_duration)
#define DEPRECATED(X)
Definition low_level_platform.h:388
Execution environment.
Definition environment.h:52
lf_mutex_t mutex
Mutex for synchronizing access to the environment.
Definition environment.h:295
A struct supporting thread scheduling policies.
Definition low_level_platform.h:181
interval_t time_slice
Definition low_level_platform.h:184
lf_scheduling_policy_type_t policy
Definition low_level_platform.h:182
int priority
Definition low_level_platform.h:183
Time and tag definitions and functions for Lingua Franca.