From ea1a7bbca364fa59a6647006f00818ed5424ac45 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 10 Nov 2024 15:47:56 -0500 Subject: Added in -DLPM_NO_THREADS. --- src/lpm.c | 80 +++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/src/lpm.c b/src/lpm.c index 313e4ac..905506e 100644 --- a/src/lpm.c +++ b/src/lpm.c @@ -4,7 +4,9 @@ #include #include #else - #include + #ifndef LPM_NO_THRAEDS + #include + #endif #include #include #include @@ -80,36 +82,44 @@ typedef struct { static lpm_mutex_t* new_mutex() { lpm_mutex_t* mutex = malloc(sizeof(lpm_mutex_t)); - #if _WIN32 - mutex->mutex = CreateMutex(NULL, FALSE, NULL); - #else - pthread_mutex_init(&mutex->mutex, NULL); + #ifndef LPM_NO_THREADS + #if _WIN32 + mutex->mutex = CreateMutex(NULL, FALSE, NULL); + #else + pthread_mutex_init(&mutex->mutex, NULL); + #endif #endif return mutex; } static void free_mutex(lpm_mutex_t* mutex) { - #if _WIN32 - CloseHandle(mutex->mutex); - #else - pthread_mutex_destroy(&mutex->mutex); + #ifndef LPM_NO_THREADS + #if _WIN32 + CloseHandle(mutex->mutex); + #else + pthread_mutex_destroy(&mutex->mutex); + #endif #endif free(mutex); } static void lock_mutex(lpm_mutex_t* mutex) { - #if _WIN32 - WaitForSingleObject(mutex->mutex, INFINITE); - #else - pthread_mutex_lock(&mutex->mutex); + #ifndef LPM_NO_THREADS + #if _WIN32 + WaitForSingleObject(mutex->mutex, INFINITE); + #else + pthread_mutex_lock(&mutex->mutex); + #endif #endif } static void unlock_mutex(lpm_mutex_t* mutex) { - #if _WIN32 - ReleaseMutex(mutex->mutex); - #else - pthread_mutex_unlock(&mutex->mutex); + #ifndef LPM_NO_THREADS + #if _WIN32 + ReleaseMutex(mutex->mutex); + #else + pthread_mutex_unlock(&mutex->mutex); + #endif #endif } @@ -124,12 +134,16 @@ static DWORD windows_thread_callback(void* data) { static lpm_thread_t* create_thread(void* (*func)(void*), void* data) { lpm_thread_t* thread = malloc(sizeof(lpm_thread_t)); - #if _WIN32 - thread->func = func; - thread->data = data; - thread->thread = CreateThread(NULL, 0, windows_thread_callback, thread, 0, NULL); + #ifndef LPM_NO_THREADS + #if _WIN32 + thread->func = func; + thread->data = data; + thread->thread = CreateThread(NULL, 0, windows_thread_callback, thread, 0, NULL); + #else + pthread_create(&thread->thread, NULL, func, data); + #endif #else - pthread_create(&thread->thread, NULL, func, data); + func(data); #endif return thread; } @@ -137,11 +151,13 @@ static lpm_thread_t* create_thread(void* (*func)(void*), void* data) { static void* join_thread(lpm_thread_t* thread) { if (!thread) return NULL; - void* retval; - #if _WIN32 - WaitForSingleObject(thread->thread, INFINITE); - #else - pthread_join(thread->thread, &retval); + void* retval = NULL; + #ifndef LPM_NO_THREADS + #if _WIN32 + WaitForSingleObject(thread->thread, INFINITE); + #else + pthread_join(thread->thread, &retval); + #endif #endif free(thread); return retval; @@ -594,9 +610,13 @@ static int lpm_git_transfer_progress_cb(const git_transfer_progress *stats, void } static int lua_is_main_thread(lua_State* L) { - int is_main = lua_pushthread(L); - lua_pop(L, 1); - return is_main; + #ifndef LPM_NO_THREADS + int is_main = lua_pushthread(L); + lua_pop(L, 1); + return is_main; + #else + return 1; + #endif } static void* lpm_fetch_thread(void* ctx) { -- cgit v1.2.3