Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
erlingrj committed Mar 8, 2024
1 parent c36a0e9 commit b60db68
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 180 deletions.
303 changes: 141 additions & 162 deletions core/platform/lf_windows_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <windows.h> // Order in which windows.h is included does matter!
#include <windows.h> // Order in which windows.h is included does matter!
#include <errno.h>
#include <process.h>
#include <sysinfoapi.h>
Expand All @@ -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;
}
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}
}

/**
Expand All @@ -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;
}
}

/**
Expand All @@ -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
Loading

0 comments on commit b60db68

Please sign in to comment.