reactor-c
C Runtime for Lingua Franca
Loading...
Searching...
No Matches
platform.h
Go to the documentation of this file.
1/* Platform API support for the C target of Lingua Franca. */
2
3
36#ifndef PLATFORM_H
37#define PLATFORM_H
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43#include "tag.h"
44#include <assert.h>
45
46// Forward declarations
48
54
60
66
67#if defined(PLATFORM_ARDUINO)
69#elif defined(PLATFORM_ZEPHYR)
71#elif defined(PLATFORM_NRF52)
73#elif defined(PLATFORM_RP2040)
75#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
76 // Windows platforms
77 #include "lf_windows_support.h"
78#elif __APPLE__
79 // Apple platforms
80 #include "lf_macos_support.h"
81#elif __linux__
82 // Linux
83 #include "lf_linux_support.h"
84#elif __unix__ // all unices not caught above
85 // Unix
87#elif defined(_POSIX_VERSION)
88 // POSIX
90#elif defined(__riscv) || defined(__riscv__)
91 // RISC-V (see https://github.com/riscv/riscv-toolchain-conventions)
92 #error "RISC-V not supported"
93#else
94#error "Platform not supported"
95#endif
96
97#define LF_TIMEOUT 1
98
99
100// To support the single-threaded runtime, we need the following functions. They
101// are not required by the threaded runtime and is thus hidden behind a #ifdef.
102#if defined (LF_SINGLE_THREADED)
103 typedef void lf_mutex_t;
109 int lf_disable_interrupts_nested();
115 int lf_enable_interrupts_nested();
116
122 int _lf_single_threaded_notify_of_event();
123#else
124// For platforms with threading support, the following functions
125// abstract the API so that the LF runtime remains portable.
126
127
132
140int lf_thread_create(lf_thread_t* thread, void *(*lf_thread) (void *), void* arguments);
141
151int lf_thread_join(lf_thread_t thread, void** thread_return);
152
159
166
173
180
187
193int lf_cond_signal(lf_cond_t* cond);
194
201int lf_cond_wait(lf_cond_t* cond);
202
211int lf_cond_timedwait(lf_cond_t* cond, instant_t absolute_time_ns);
212
213/*
214 * Atomically increment the variable that ptr points to by the given value, and return the original value of the variable.
215 * @param ptr A pointer to a variable. The value of this variable will be replaced with the result of the operation.
216 * @param value The value to be added to the variable pointed to by the ptr parameter.
217 * @return The original value of the variable that ptr points to (i.e., from before the application of this operation).
218 */
219#if defined(PLATFORM_ZEPHYR)
220#define lf_atomic_fetch_add(ptr, value) _zephyr_atomic_fetch_add((int*) ptr, value)
221#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
222// Assume that an integer is 32 bits.
223#define lf_atomic_fetch_add(ptr, value) InterlockedExchangeAdd(ptr, value)
224#elif defined(__GNUC__) || defined(__clang__)
225#define lf_atomic_fetch_add(ptr, value) __sync_fetch_and_add(ptr, value)
226#else
227#error "Compiler not supported"
228#endif
229
230/*
231 * Atomically increment the variable that ptr points to by the given value, and return the new value of the variable.
232 * @param ptr A pointer to a variable. The value of this variable will be replaced with the result of the operation.
233 * @param value The value to be added to the variable pointed to by the ptr parameter.
234 * @return The new value of the variable that ptr points to (i.e., from before the application of this operation).
235 */
236#if defined(PLATFORM_ZEPHYR)
237#define lf_atomic_add_fetch(ptr, value) _zephyr_atomic_add_fetch((int*) ptr, value)
238#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
239// Assume that an integer is 32 bits.
240#define lf_atomic_add_fetch(ptr, value) InterlockedAdd(ptr, value)
241#elif defined(__GNUC__) || defined(__clang__)
242#define lf_atomic_add_fetch(ptr, value) __sync_add_and_fetch(ptr, value)
243#else
244#error "Compiler not supported"
245#endif
246
247/*
248 * Atomically compare the variable that ptr points to against oldval. If the
249 * current value is oldval, then write newval into *ptr.
250 * @param ptr A pointer to a variable.
251 * @param oldval The value to compare against.
252 * @param newval The value to assign to *ptr if comparison is successful.
253 * @return True if comparison was successful. False otherwise.
254 */
255#if defined(PLATFORM_ZEPHYR)
256#define lf_bool_compare_and_swap(ptr, value, newval) _zephyr_bool_compare_and_swap((bool*) ptr, value, newval)
257#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
258// Assume that a boolean is represented with a 32-bit integer.
259#define lf_bool_compare_and_swap(ptr, oldval, newval) (InterlockedCompareExchange(ptr, newval, oldval) == oldval)
260#elif defined(__GNUC__) || defined(__clang__)
261#define lf_bool_compare_and_swap(ptr, oldval, newval) __sync_bool_compare_and_swap(ptr, oldval, newval)
262#else
263#error "Compiler not supported"
264#endif
265
266/*
267 * Atomically compare the 32-bit value that ptr points to against oldval. If the
268 * current value is oldval, then write newval into *ptr.
269 * @param ptr A pointer to a variable.
270 * @param oldval The value to compare against.
271 * @param newval The value to assign to *ptr if comparison is successful.
272 * @return The initial value of *ptr.
273 */
274#if defined(PLATFORM_ZEPHYR)
275#define lf_val_compare_and_swap(ptr, value, newval) _zephyr_val_compare_and_swap((int*) ptr, value, newval)
276#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
277#define lf_val_compare_and_swap(ptr, oldval, newval) InterlockedCompareExchange(ptr, newval, oldval)
278#elif defined(__GNUC__) || defined(__clang__)
279#define lf_val_compare_and_swap(ptr, oldval, newval) __sync_val_compare_and_swap(ptr, oldval, newval)
280#else
281#error "Compiler not supported"
282#endif
283
284#endif
285
290
301
307int lf_sleep(interval_t sleep_duration);
308
318
322#ifdef __GNUC__
323 #define DEPRECATED(X) X __attribute__((deprecated))
324#elif defined(_MSC_VER)
325 #define DEPRECATED(X) __declspec(deprecated) X
326#else
327 #define DEPRECATED(X) X
328#endif
329
333DEPRECATED(int lf_nanosleep(interval_t sleep_duration));
334
335#ifdef __cplusplus
336}
337#endif
338
339#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_C11_threads_support.c:27
int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_time)
Sleep until the given wakeup time. Assumes the lock for the given environment is held.
int lf_notify_of_event(environment_t *env)
Notify of new event.
Definition reactor_threaded.c:1230
int lf_thread_create(lf_thread_t *thread, void *(*lf_thread)(void *), void *arguments)
Definition lf_C11_threads_support.c:9
int lf_cond_timedwait(lf_cond_t *cond, instant_t absolute_time_ns)
Definition lf_C11_threads_support.c:48
int lf_thread_join(lf_thread_t thread, void **thread_return)
Definition lf_C11_threads_support.c:13
int lf_cond_signal(lf_cond_t *cond)
Definition lf_C11_threads_support.c:40
int _lf_clock_now(instant_t *t)
int lf_sleep(interval_t sleep_duration)
int lf_cond_broadcast(lf_cond_t *cond)
Definition lf_C11_threads_support.c:36
int lf_mutex_init(lf_mutex_t *mutex)
Definition lf_C11_threads_support.c:18
int lf_cond_init(lf_cond_t *cond, lf_mutex_t *mutex)
Definition lf_C11_threads_support.c:31
int lf_cond_wait(lf_cond_t *cond)
Definition lf_C11_threads_support.c:44
int lf_available_cores()
Get the number of cores on the host machine.
int lf_mutex_lock(lf_mutex_t *mutex)
Definition lf_C11_threads_support.c:23
int lf_critical_section_enter(environment_t *env)
Enter critical section by disabling interrupts.
Definition reactor_threaded.c:1239
int lf_critical_section_exit(environment_t *env)
Leave a critical section by enabling interrupts.
Definition reactor_threaded.c:1251
void _lf_initialize_clock(void)
#define DEPRECATED(X)
Definition platform.h:327
Execution environment. This struct contains information about the execution environment....
Definition environment.h:68
lf_mutex_t mutex
Definition environment.h:99
Definition lf_C11_threads_support.h:38
Time and tag definitions and functions for Lingua Franca.
int64_t instant_t
Definition tag.h:58
int64_t interval_t
Definition tag.h:63