From e14a891d53c272f791edb060bdf6dc38da112687 Mon Sep 17 00:00:00 2001 From: Christian Menard Date: Tue, 25 Jun 2024 15:36:40 +0200 Subject: [PATCH 1/3] Fixed initialization of the master worker thread id Fixes https://github.com/lf-lang/reactor-c/issues/452 --- core/threaded/reactor_threaded.c | 1 + 1 file changed, 1 insertion(+) diff --git a/core/threaded/reactor_threaded.c b/core/threaded/reactor_threaded.c index 392f49c31..14d84fd14 100644 --- a/core/threaded/reactor_threaded.c +++ b/core/threaded/reactor_threaded.c @@ -1112,6 +1112,7 @@ int lf_reactor_c_main(int argc, const char* argv[]) { // run on the main thread, rather than creating a new thread. // This is important for bare-metal platforms, who can't // afford to have the main thread sit idle. + env->thread_ids[j] = lf_thread_self(); continue; } if (lf_thread_create(&env->thread_ids[j], worker, env) != 0) { From 7a8d18a7617a00f67df22f54e0d388dbacdbe238 Mon Sep 17 00:00:00 2001 From: Peter Donovan Date: Thu, 27 Jun 2024 12:28:50 -0700 Subject: [PATCH 2/3] Support getting current thread on Windows Also, silence undefined reference linker errors on other platforms that either have no support or that do not currently implement the API --- low_level_platform/impl/src/lf_arduino_support.c | 10 ++++++++++ low_level_platform/impl/src/lf_flexpret_support.c | 7 +++++++ low_level_platform/impl/src/lf_windows_support.c | 2 ++ 3 files changed, 19 insertions(+) diff --git a/low_level_platform/impl/src/lf_arduino_support.c b/low_level_platform/impl/src/lf_arduino_support.c index ed9391205..c56d288dc 100644 --- a/low_level_platform/impl/src/lf_arduino_support.c +++ b/low_level_platform/impl/src/lf_arduino_support.c @@ -170,6 +170,16 @@ typedef void* (*lf_function_t)(void*); */ int lf_available_cores() { return 1; } +lf_thread_t lf_thread_self() { + // Not implemented. Although Arduino mbed provides a ThisThread API and a + // get_id() function, it does not provide a way to get the current thread as a + // Thread object. + // N.B. This wrong implementation will eventually cause hard-to-debug + // segfaults, but it unblocks us from conveniently implementing features for + // other platforms, and it does not break existing features for Arduino. + return NULL; +} + int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments) { lf_thread_t t = thread_new(); long int start = thread_start(t, *lf_thread, arguments); diff --git a/low_level_platform/impl/src/lf_flexpret_support.c b/low_level_platform/impl/src/lf_flexpret_support.c index 9d83283c5..7962f9d3b 100644 --- a/low_level_platform/impl/src/lf_flexpret_support.c +++ b/low_level_platform/impl/src/lf_flexpret_support.c @@ -178,6 +178,13 @@ int lf_available_cores() { return FP_THREADS - 1; // Return the number of Flexpret HW threads } +lf_thread_t lf_thread_self() { + // N.B. This wrong implementation will eventually cause hard-to-debug + // segfaults, but it unblocks us from conveniently implementing features for + // other platforms, and it does not break existing features for FlexPRET. + return NULL; +} + int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments) { /** * Need to select between HRTT or SRTT; see diff --git a/low_level_platform/impl/src/lf_windows_support.c b/low_level_platform/impl/src/lf_windows_support.c index 1d48bc6c7..61424ac7f 100644 --- a/low_level_platform/impl/src/lf_windows_support.c +++ b/low_level_platform/impl/src/lf_windows_support.c @@ -162,6 +162,8 @@ int lf_available_cores() { return sysinfo.dwNumberOfProcessors; } +lf_thread_t lf_thread_self() { return GetCurrentThread(); } + int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments) { uintptr_t handle = _beginthreadex(NULL, 0, lf_thread, arguments, 0, NULL); *thread = (HANDLE)handle; From 5818d1521f2d35546e46c25245538524ed04558a Mon Sep 17 00:00:00 2001 From: Christian Menard Date: Fri, 28 Jun 2024 10:24:22 +0200 Subject: [PATCH 3/3] More concise comments --- low_level_platform/impl/src/lf_arduino_support.c | 3 --- low_level_platform/impl/src/lf_flexpret_support.c | 4 +--- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/low_level_platform/impl/src/lf_arduino_support.c b/low_level_platform/impl/src/lf_arduino_support.c index c56d288dc..5793bd650 100644 --- a/low_level_platform/impl/src/lf_arduino_support.c +++ b/low_level_platform/impl/src/lf_arduino_support.c @@ -174,9 +174,6 @@ lf_thread_t lf_thread_self() { // Not implemented. Although Arduino mbed provides a ThisThread API and a // get_id() function, it does not provide a way to get the current thread as a // Thread object. - // N.B. This wrong implementation will eventually cause hard-to-debug - // segfaults, but it unblocks us from conveniently implementing features for - // other platforms, and it does not break existing features for Arduino. return NULL; } diff --git a/low_level_platform/impl/src/lf_flexpret_support.c b/low_level_platform/impl/src/lf_flexpret_support.c index 7962f9d3b..cf37c1b8a 100644 --- a/low_level_platform/impl/src/lf_flexpret_support.c +++ b/low_level_platform/impl/src/lf_flexpret_support.c @@ -179,9 +179,7 @@ int lf_available_cores() { } lf_thread_t lf_thread_self() { - // N.B. This wrong implementation will eventually cause hard-to-debug - // segfaults, but it unblocks us from conveniently implementing features for - // other platforms, and it does not break existing features for FlexPRET. + // Not implemented. return NULL; }