From 7a8d18a7617a00f67df22f54e0d388dbacdbe238 Mon Sep 17 00:00:00 2001 From: Peter Donovan Date: Thu, 27 Jun 2024 12:28:50 -0700 Subject: [PATCH] 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;