2022-10-12 14:13:20 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "fastfetch.h"
|
|
|
|
|
|
|
|
|
|
#ifdef FF_HAVE_THREADS
|
2026-04-15 16:16:46 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
#include <winternl.h>
|
|
|
|
|
#include <synchapi.h>
|
|
|
|
|
#include <process.h>
|
|
|
|
|
#include <processthreadsapi.h>
|
|
|
|
|
#define FF_THREAD_MUTEX_INITIALIZER SRWLOCK_INIT
|
2026-03-29 09:28:49 +08:00
|
|
|
typedef SRWLOCK FFThreadMutex;
|
|
|
|
|
typedef HANDLE FFThreadType;
|
|
|
|
|
static inline void ffThreadMutexLock(FFThreadMutex* mutex) {
|
|
|
|
|
AcquireSRWLockExclusive(mutex);
|
|
|
|
|
}
|
|
|
|
|
static inline void ffThreadMutexUnlock(FFThreadMutex* mutex) {
|
|
|
|
|
ReleaseSRWLockExclusive(mutex);
|
|
|
|
|
}
|
|
|
|
|
static inline FFThreadType ffThreadCreate(unsigned(__stdcall* func)(void*), void* data) {
|
|
|
|
|
return (FFThreadType) _beginthreadex(NULL, 0, func, data, 0, NULL);
|
|
|
|
|
}
|
2026-04-15 16:16:46 +08:00
|
|
|
#define FF_THREAD_ENTRY_DECL_WRAPPER(fn, paramType) \
|
2026-03-29 09:28:49 +08:00
|
|
|
static __stdcall unsigned fn##ThreadMain(void* data) { \
|
|
|
|
|
fn((paramType) data); \
|
|
|
|
|
return 0; \
|
|
|
|
|
}
|
2026-04-15 16:16:46 +08:00
|
|
|
#define FF_THREAD_ENTRY_DECL_WRAPPER_NOPARAM(fn) \
|
2026-03-29 09:28:49 +08:00
|
|
|
static __stdcall unsigned fn##ThreadMain() { \
|
|
|
|
|
fn(); \
|
|
|
|
|
return 0; \
|
|
|
|
|
}
|
|
|
|
|
static inline void ffThreadDetach(FFThreadType thread) {
|
|
|
|
|
NtClose(thread);
|
|
|
|
|
}
|
|
|
|
|
static inline bool ffThreadJoin(FFThreadType thread, uint32_t timeout) {
|
2026-03-30 10:27:23 +08:00
|
|
|
if (NtWaitForSingleObject(thread, FALSE, timeout == 0 ? NULL : &(LARGE_INTEGER) { .QuadPart = (int64_t) timeout * -10000 }) != STATUS_WAIT_0) {
|
2026-03-29 09:28:49 +08:00
|
|
|
TerminateThread(thread, (DWORD) -1);
|
|
|
|
|
NtClose(thread);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
NtClose(thread);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-04-15 16:16:46 +08:00
|
|
|
#else
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#if FF_HAVE_PTHREAD_NP
|
|
|
|
|
#include <pthread_np.h>
|
|
|
|
|
#endif
|
2026-03-29 09:28:49 +08:00
|
|
|
typedef pthread_t FFThreadType;
|
2026-04-15 16:16:46 +08:00
|
|
|
#if __APPLE__
|
|
|
|
|
#include <os/lock.h>
|
|
|
|
|
#define FF_THREAD_MUTEX_INITIALIZER OS_UNFAIR_LOCK_INIT
|
2026-03-29 09:28:49 +08:00
|
|
|
typedef os_unfair_lock FFThreadMutex;
|
|
|
|
|
static inline void ffThreadMutexLock(os_unfair_lock* mutex) {
|
|
|
|
|
os_unfair_lock_lock(mutex);
|
|
|
|
|
}
|
|
|
|
|
static inline void ffThreadMutexUnlock(os_unfair_lock* mutex) {
|
|
|
|
|
os_unfair_lock_unlock(mutex);
|
|
|
|
|
}
|
2026-04-15 16:16:46 +08:00
|
|
|
#else
|
|
|
|
|
#define FF_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
2026-03-29 09:28:49 +08:00
|
|
|
typedef pthread_mutex_t FFThreadMutex;
|
|
|
|
|
static inline void ffThreadMutexLock(FFThreadMutex* mutex) {
|
|
|
|
|
pthread_mutex_lock(mutex);
|
|
|
|
|
}
|
|
|
|
|
static inline void ffThreadMutexUnlock(FFThreadMutex* mutex) {
|
|
|
|
|
pthread_mutex_unlock(mutex);
|
|
|
|
|
}
|
2026-04-15 16:16:46 +08:00
|
|
|
#endif
|
2026-03-29 09:28:49 +08:00
|
|
|
static inline FFThreadType ffThreadCreate(void* (*func)(void*), void* data) {
|
|
|
|
|
FFThreadType newThread = 0;
|
|
|
|
|
pthread_create(&newThread, NULL, func, data);
|
|
|
|
|
return newThread;
|
|
|
|
|
}
|
2026-04-15 16:16:46 +08:00
|
|
|
#define FF_THREAD_ENTRY_DECL_WRAPPER(fn, paramType) \
|
2026-03-29 09:28:49 +08:00
|
|
|
static void* fn##ThreadMain(void* data) { \
|
|
|
|
|
fn((paramType) data); \
|
|
|
|
|
return NULL; \
|
|
|
|
|
}
|
2026-04-15 16:16:46 +08:00
|
|
|
#define FF_THREAD_ENTRY_DECL_WRAPPER_NOPARAM(fn) \
|
2026-03-29 09:28:49 +08:00
|
|
|
static void* fn##ThreadMain() { \
|
|
|
|
|
fn(); \
|
|
|
|
|
return NULL; \
|
|
|
|
|
}
|
|
|
|
|
static inline void ffThreadDetach(FFThreadType thread) {
|
|
|
|
|
pthread_detach(thread);
|
|
|
|
|
}
|
2026-04-09 16:20:59 +08:00
|
|
|
static inline bool ffThreadJoin(FFThreadType thread, FF_A_UNUSED uint32_t timeout) {
|
2026-04-15 16:16:46 +08:00
|
|
|
#if HAVE_TIMEDJOIN_NP
|
2026-03-29 09:28:49 +08:00
|
|
|
if (timeout > 0) {
|
|
|
|
|
struct timespec ts;
|
|
|
|
|
if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {
|
|
|
|
|
ts.tv_sec += timeout / 1000;
|
|
|
|
|
ts.tv_nsec += (timeout % 1000) * 1000000;
|
|
|
|
|
if (pthread_timedjoin_np(thread, NULL, &ts) != 0) {
|
|
|
|
|
pthread_kill(thread, SIGTERM);
|
2023-09-22 16:06:04 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2026-04-15 16:16:46 +08:00
|
|
|
#endif
|
2026-03-29 09:28:49 +08:00
|
|
|
pthread_join(thread, NULL);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-04-15 16:16:46 +08:00
|
|
|
#endif
|
2026-03-29 09:28:49 +08:00
|
|
|
#else // FF_HAVE_THREADS
|
2026-04-15 16:16:46 +08:00
|
|
|
#define FF_THREAD_MUTEX_INITIALIZER 0
|
2026-03-29 09:28:49 +08:00
|
|
|
typedef char FFThreadMutex;
|
2026-04-09 16:20:59 +08:00
|
|
|
static inline void ffThreadMutexLock(FF_A_UNUSED FFThreadMutex* mutex) {}
|
|
|
|
|
static inline void ffThreadMutexUnlock(FF_A_UNUSED FFThreadMutex* mutex) {}
|
2026-04-15 16:16:46 +08:00
|
|
|
#define FF_THREAD_ENTRY_DECL_WRAPPER(fn, paramType)
|
2026-03-29 09:28:49 +08:00
|
|
|
#endif // FF_HAVE_THREADS
|