From b60db68aa7546a849757c323111a51dff16eed94 Mon Sep 17 00:00:00 2001 From: erlingrj Date: Fri, 8 Mar 2024 16:44:17 +0100 Subject: [PATCH] Formatting --- core/platform/lf_windows_support.c | 303 ++++++++---------- low_level_platform/api/low_level_platform.h | 33 +- .../impl/src/lf_macos_support.c | 1 - 3 files changed, 157 insertions(+), 180 deletions(-) diff --git a/core/platform/lf_windows_support.c b/core/platform/lf_windows_support.c index 9150634ef..e6e7906fd 100644 --- a/core/platform/lf_windows_support.c +++ b/core/platform/lf_windows_support.c @@ -34,7 +34,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @see https://gist.github.com/Soroosh129/127d1893fa4c1da6d3e1db33381bb273 */ -#include // Order in which windows.h is included does matter! +#include // Order in which windows.h is included does matter! #include #include #include @@ -59,16 +59,15 @@ int _lf_use_performance_counter = 0; double _lf_frequency_to_ns = 1.0; void _lf_initialize_clock() { - // Check if the performance counter is available - LARGE_INTEGER performance_frequency; - _lf_use_performance_counter = QueryPerformanceFrequency(&performance_frequency); - if (_lf_use_performance_counter) { - _lf_frequency_to_ns = (double)performance_frequency.QuadPart / BILLION; - } else { - lf_print_error( - "High resolution performance counter is not supported on this machine."); - _lf_frequency_to_ns = 0.01; - } + // Check if the performance counter is available + LARGE_INTEGER performance_frequency; + _lf_use_performance_counter = QueryPerformanceFrequency(&performance_frequency); + if (_lf_use_performance_counter) { + _lf_frequency_to_ns = (double)performance_frequency.QuadPart / BILLION; + } else { + lf_print_error("High resolution performance counter is not supported on this machine."); + _lf_frequency_to_ns = 0.01; + } } /** @@ -80,31 +79,31 @@ void _lf_initialize_clock() { * set to EINVAL or EFAULT. */ int _lf_clock_gettime(instant_t* t) { - // Adapted from gclib/GResUsage.cpp - // (https://github.com/gpertea/gclib/blob/8aee376774ccb2f3bd3f8e3bf1c9df1528ac7c5b/GResUsage.cpp) - // License: https://github.com/gpertea/gclib/blob/master/LICENSE.txt - int result = -1; - if (t == NULL) { - // The t argument address references invalid memory - errno = EFAULT; - return result; - } - LARGE_INTEGER windows_time; - if (_lf_use_performance_counter) { - int result = QueryPerformanceCounter(&windows_time); - if ( result == 0) { - lf_print_error("_lf_clock_gettime(): Failed to read the value of the physical clock."); - return result; - } - } else { - FILETIME f; - GetSystemTimeAsFileTime(&f); - windows_time.QuadPart = f.dwHighDateTime; - windows_time.QuadPart <<= 32; - windows_time.QuadPart |= f.dwLowDateTime; + // Adapted from gclib/GResUsage.cpp + // (https://github.com/gpertea/gclib/blob/8aee376774ccb2f3bd3f8e3bf1c9df1528ac7c5b/GResUsage.cpp) + // License: https://github.com/gpertea/gclib/blob/master/LICENSE.txt + int result = -1; + if (t == NULL) { + // The t argument address references invalid memory + errno = EFAULT; + return result; + } + LARGE_INTEGER windows_time; + if (_lf_use_performance_counter) { + int result = QueryPerformanceCounter(&windows_time); + if (result == 0) { + lf_print_error("_lf_clock_gettime(): Failed to read the value of the physical clock."); + return result; } - *t = (instant_t)((double)windows_time.QuadPart / _lf_frequency_to_ns); - return (0); + } else { + FILETIME f; + GetSystemTimeAsFileTime(&f); + windows_time.QuadPart = f.dwHighDateTime; + windows_time.QuadPart <<= 32; + windows_time.QuadPart |= f.dwLowDateTime; + } + *t = (instant_t)((double)windows_time.QuadPart / _lf_frequency_to_ns); + return (0); } /** @@ -116,65 +115,62 @@ int _lf_clock_gettime(instant_t* t) { * - EINVAL: All other errors */ int lf_sleep(interval_t sleep_duration) { - /* Declarations */ - HANDLE timer; /* Timer handle */ - LARGE_INTEGER li; /* Time defintion */ - /* Create timer */ - if(!(timer = CreateWaitableTimer(NULL, TRUE, NULL))) { - return FALSE; - } - /** - * Set timer properties. - * A negative number indicates relative time to wait. - * The requested sleep duration must be in number of 100 nanoseconds. - */ - li.QuadPart = -1 * (sleep_duration / 100); - if(!SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE)){ - CloseHandle(timer); - return FALSE; - } - /* Start & wait for timer */ - WaitForSingleObject(timer, INFINITE); - /* Clean resources */ + /* Declarations */ + HANDLE timer; /* Timer handle */ + LARGE_INTEGER li; /* Time defintion */ + /* Create timer */ + if (!(timer = CreateWaitableTimer(NULL, TRUE, NULL))) { + return FALSE; + } + /** + * Set timer properties. + * A negative number indicates relative time to wait. + * The requested sleep duration must be in number of 100 nanoseconds. + */ + li.QuadPart = -1 * (sleep_duration / 100); + if (!SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE)) { CloseHandle(timer); - /* Slept without problems */ - return TRUE; + return FALSE; + } + /* Start & wait for timer */ + WaitForSingleObject(timer, INFINITE); + /* Clean resources */ + CloseHandle(timer); + /* Slept without problems */ + return TRUE; } int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup_time) { - interval_t sleep_duration = wakeup_time - lf_time_physical(); - - if (sleep_duration <= 0) { - return 0; - } else { - return lf_sleep(sleep_duration); - } -} + interval_t sleep_duration = wakeup_time - lf_time_physical(); -int lf_nanosleep(interval_t sleep_duration) { + if (sleep_duration <= 0) { + return 0; + } else { return lf_sleep(sleep_duration); + } } +int lf_nanosleep(interval_t sleep_duration) { return lf_sleep(sleep_duration); } + #if defined(LF_SINGLE_THREADED) #include "lf_os_single_threaded_support.c" #endif - #if !defined(LF_SINGLE_THREADED) int lf_available_cores() { - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - return sysinfo.dwNumberOfProcessors; + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + return sysinfo.dwNumberOfProcessors; } -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; - if(handle == 0){ - return errno; - }else{ - return 0; - } +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; + if (handle == 0) { + return errno; + } else { + return 0; + } } /** @@ -185,37 +181,29 @@ int lf_thread_create(lf_thread_t* thread, void *(*lf_thread) (void *), void* arg * @return 0 on success, EINVAL otherwise. */ int lf_thread_join(lf_thread_t thread, void** thread_return) { - DWORD retvalue = WaitForSingleObject(thread, INFINITE); - if(retvalue == WAIT_FAILED){ - return EINVAL; - } - return 0; + DWORD retvalue = WaitForSingleObject(thread, INFINITE); + if (retvalue == WAIT_FAILED) { + return EINVAL; + } + return 0; } -lf_thread_t lf_thread_self() { - return GetCurrentThread(); -} +lf_thread_t lf_thread_self() { return GetCurrentThread(); } -int lf_thread_set_cpu(lf_thread_t thread, int cpu_number) { - return -1; -} +int lf_thread_set_cpu(lf_thread_t thread, int cpu_number) { return -1; } -int lf_thread_set_priority(lf_thread_t thread, int priority) { - return -1; -} +int lf_thread_set_priority(lf_thread_t thread, int priority) { return -1; } -int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t *policy) { - return -1; -} +int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t* policy) { return -1; } int lf_mutex_init(_lf_critical_section_t* critical_section) { - // Set up a recursive mutex - InitializeCriticalSection((PCRITICAL_SECTION)critical_section); - if(critical_section != NULL){ - return 0; - }else{ - return 1; - } + // Set up a recursive mutex + InitializeCriticalSection((PCRITICAL_SECTION)critical_section); + if (critical_section != NULL) { + return 0; + } else { + return 1; + } } /** @@ -230,88 +218,79 @@ int lf_mutex_init(_lf_critical_section_t* critical_section) { * @return 0 */ int lf_mutex_lock(_lf_critical_section_t* critical_section) { - // The following Windows API does not return a value. It can - // raise a EXCEPTION_POSSIBLE_DEADLOCK. See synchapi.h. - EnterCriticalSection((PCRITICAL_SECTION)critical_section); - return 0; + // The following Windows API does not return a value. It can + // raise a EXCEPTION_POSSIBLE_DEADLOCK. See synchapi.h. + EnterCriticalSection((PCRITICAL_SECTION)critical_section); + return 0; } int lf_mutex_unlock(_lf_critical_section_t* critical_section) { - // The following Windows API does not return a value. - LeaveCriticalSection((PCRITICAL_SECTION)critical_section); - return 0; + // The following Windows API does not return a value. + LeaveCriticalSection((PCRITICAL_SECTION)critical_section); + return 0; } int lf_cond_init(lf_cond_t* cond, _lf_critical_section_t* critical_section) { - // The following Windows API does not return a value. - cond->critical_section = critical_section; - InitializeConditionVariable((PCONDITION_VARIABLE)&cond->condition); - return 0; + // The following Windows API does not return a value. + cond->critical_section = critical_section; + InitializeConditionVariable((PCONDITION_VARIABLE)&cond->condition); + return 0; } int lf_cond_broadcast(lf_cond_t* cond) { - // The following Windows API does not return a value. - WakeAllConditionVariable((PCONDITION_VARIABLE)&cond->condition); - return 0; + // The following Windows API does not return a value. + WakeAllConditionVariable((PCONDITION_VARIABLE)&cond->condition); + return 0; } int lf_cond_signal(lf_cond_t* cond) { - // The following Windows API does not return a value. - WakeConditionVariable((PCONDITION_VARIABLE)&cond->condition); - return 0; + // The following Windows API does not return a value. + WakeConditionVariable((PCONDITION_VARIABLE)&cond->condition); + return 0; } int lf_cond_wait(lf_cond_t* cond) { - // According to synchapi.h, the following Windows API returns 0 on failure, - // and non-zero on success. - int return_value = - (int)SleepConditionVariableCS( - (PCONDITION_VARIABLE)&cond->condition, - (PCRITICAL_SECTION)cond->critical_section, - INFINITE - ); - switch (return_value) { - case 0: - // Error - return 1; - break; - - default: - // Success - return 0; - break; - } + // According to synchapi.h, the following Windows API returns 0 on failure, + // and non-zero on success. + int return_value = (int)SleepConditionVariableCS((PCONDITION_VARIABLE)&cond->condition, + (PCRITICAL_SECTION)cond->critical_section, INFINITE); + switch (return_value) { + case 0: + // Error + return 1; + break; + + default: + // Success + return 0; + break; + } } int _lf_cond_timedwait(lf_cond_t* cond, instant_t wakeup_time) { - // Convert the absolute time to a relative time. - interval_t wait_duration = wakeup_time - lf_time_physical(); - if (wait_duration<= 0) { - // physical time has already caught up sufficiently and we do not need to wait anymore - return 0; - } + // Convert the absolute time to a relative time. + interval_t wait_duration = wakeup_time - lf_time_physical(); + if (wait_duration <= 0) { + // physical time has already caught up sufficiently and we do not need to wait anymore + return 0; + } + + // convert ns to ms and round up to closest full integer + DWORD wait_duration_ms = (wait_duration + 999999LL) / 1000000LL; - // convert ns to ms and round up to closest full integer - DWORD wait_duration_ms = (wait_duration + 999999LL) / 1000000LL; - - int return_value = - (int)SleepConditionVariableCS( - (PCONDITION_VARIABLE)&cond->condition, - (PCRITICAL_SECTION)cond->critical_section, - wait_duration_ms - ); - if (return_value == 0) { - // Error - if (GetLastError() == ERROR_TIMEOUT) { - return LF_TIMEOUT; - } - return -1; + int return_value = (int)SleepConditionVariableCS((PCONDITION_VARIABLE)&cond->condition, + (PCRITICAL_SECTION)cond->critical_section, wait_duration_ms); + if (return_value == 0) { + // Error + if (GetLastError() == ERROR_TIMEOUT) { + return LF_TIMEOUT; } + return -1; + } - // Success - return 0; + // Success + return 0; } #endif - #endif diff --git a/low_level_platform/api/low_level_platform.h b/low_level_platform/api/low_level_platform.h index 2955d9bbd..fd0145621 100644 --- a/low_level_platform/api/low_level_platform.h +++ b/low_level_platform/api/low_level_platform.h @@ -109,7 +109,7 @@ int lf_available_cores(); /** * Returns the thread ID of the calling thread - * + * */ lf_thread_t lf_thread_self(); @@ -138,7 +138,6 @@ int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* argum */ int lf_thread_join(lf_thread_t thread, void** thread_return); - // The following API introduce the ability to change how the LF workers are sheduled // by the underlying thread scheduling. This API is still experimental and future // changes are expected. @@ -146,34 +145,34 @@ int lf_thread_join(lf_thread_t thread, void** thread_return); #define LF_SCHED_MAX_PRIORITY 99 #define LF_SCHED_MIN_PRIORITY 0 /** - * @brief The thread scheduling policies. - * + * @brief The thread scheduling policies. + * */ typedef enum { - LF_SCHED_FAIR, // Non real-time scheduling policy. Corresponds to SCHED_OTHER - LF_SCHED_TIMESLICE, // Real-time, time-slicing priority-based policty. Corresponds to SCHED_RR. - LF_SCHED_PRIORITY, // Real-time, priority-only based scheduling. Corresponds to SCHED_FIFO. + LF_SCHED_FAIR, // Non real-time scheduling policy. Corresponds to SCHED_OTHER + LF_SCHED_TIMESLICE, // Real-time, time-slicing priority-based policty. Corresponds to SCHED_RR. + LF_SCHED_PRIORITY, // Real-time, priority-only based scheduling. Corresponds to SCHED_FIFO. } lf_scheduling_policy_type_t; typedef struct { - lf_scheduling_policy_type_t policy; + lf_scheduling_policy_type_t policy; } lf_scheduling_policy_t; typedef struct { - lf_scheduling_policy_t base; - int priority; - interval_t time_slice; + lf_scheduling_policy_t base; + int priority; + interval_t time_slice; } lf_scheduling_policy_timeslice_t; typedef struct { - lf_scheduling_policy_t base; - int priority; + lf_scheduling_policy_t base; + int priority; } lf_scheduling_policy_priority_t; /** * This pins a lf_thread to a specific CPU - * - * @param thread The thread + * + * @param thread The thread * @param cpu_number the CPU ID * @return 0 on success, platform-specific error number otherwise. */ @@ -192,10 +191,10 @@ int lf_thread_set_priority(lf_thread_t thread, int priority); /** * Sets the scheduling policy of a thread. - * + * * @return int 0 on success, platform-specific error number otherwise. */ -int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t *policy); +int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t* policy); /** * Initialize a mutex. diff --git a/low_level_platform/impl/src/lf_macos_support.c b/low_level_platform/impl/src/lf_macos_support.c index 9c36502e5..4232d5825 100644 --- a/low_level_platform/impl/src/lf_macos_support.c +++ b/low_level_platform/impl/src/lf_macos_support.c @@ -46,7 +46,6 @@ int lf_thread_set_priority(lf_thread_t thread, int priority) { return -1; } int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t* policy) { return -1; } #endif -#include "lf_unix_clock_support.h" #include "platform/lf_unix_clock_support.h" // See `man 2 clock_nanosleep` for return values