#include #include "thread.h" NTSTATUS(__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval); void nn_thread_sleep(nn_os_tstamp delay) { NtDelayExecution(false, &((LARGE_INTEGER){ .QuadPart = -(LONGLONG)(delay * 1e7L) })); } void nn_thread_create(struct nn_thread *thread, nn_thread_func func, void *userdata) { thread->thread = CreateThread(NULL, 0, func, userdata, 0, NULL); } void nn_thread_set_priority(s32 policy, s32 priority) { (void)policy; (void)priority; } void nn_thread_set_name(const char *name) { (void)name; } void nn_thread_join(struct nn_thread *thread) { WaitForSingleObject(thread->thread, INFINITE); CloseHandle(thread->thread); } #ifdef NAUNET_HAS_THREAD_CANCEL void nn_thread_cancel(struct nn_thread *thread) { (void)thread; } void nn_thread_setcanceltype(s32 type) { (void)type; } #endif /* void nn_thread_detach(struct nn_thread *thread) { } */ /* void nn_mutex_init(struct nn_mutex *mutex) { mutex->mutex = CreateMutex(NULL, FALSE, NULL); } void nn_mutex_lock(struct nn_mutex *mutex) { WaitForSingleObject(mutex->mutex, INFINITE); } void nn_mutex_unlock(struct nn_mutex *mutex) { ReleaseMutex(mutex->mutex); } void nn_mutex_destroy(struct nn_mutex *mutex) { CloseHandle(mutex->mutex); } */ void nn_mutex_init(struct nn_mutex *mutex) { InitializeCriticalSection(&mutex->mutex); } s32 nn_mutex_lock(struct nn_mutex *mutex) { EnterCriticalSection(&mutex->mutex); return 0; } s32 nn_mutex_unlock(struct nn_mutex *mutex) { LeaveCriticalSection(&mutex->mutex); return 0; } void nn_mutex_destroy(struct nn_mutex *mutex) { DeleteCriticalSection(&mutex->mutex); } #ifdef NAUNET_WIN32_COMPAT_MODE void nn_cond_init(struct nn_cond *cond) { cond->condition = true; cond->event = CreateEvent(NULL, TRUE, FALSE, NULL); } void nn_cond_wait(struct nn_cond *cond, struct nn_mutex *mutex) { al_assert(cond->condition); cond->condition = false; nn_mutex_unlock(mutex); WaitForSingleObject(cond->event, INFINITE); ResetEvent(cond->event); nn_mutex_lock(mutex); } bool nn_cond_is_waiting(struct nn_cond *cond) { return !cond->condition; } void nn_cond_signal(struct nn_cond *cond) { al_assert(!cond->condition); cond->condition = true; SetEvent(cond->event); } void nn_cond_destroy(struct nn_cond *cond) { CloseHandle(cond->event); } #else void nn_cond_init(struct nn_cond *cond) { cond->condition = true; InitializeConditionVariable(&cond->cond); } void nn_cond_wait(struct nn_cond *cond, struct nn_mutex *mutex) { cond->condition = false; while (!cond->condition) { SleepConditionVariableCS(&cond->cond, &mutex->mutex, INFINITE); } } bool nn_cond_is_waiting(struct nn_cond *cond) { return !cond->condition; } void nn_cond_signal(struct nn_cond *cond) { cond->condition = true; WakeConditionVariable(&cond->cond); } void nn_cond_destroy(struct nn_cond *cond) { (void)cond; } #endif