diff --git a/src/common/init.c b/src/common/init.c index 7f986818e..962bde9fe 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -305,13 +305,13 @@ FF_THREAD_ENTRY_DECL_WRAPPER(ffDetectGTK4, FFinstance*) void startDetectionThreads(FFinstance* instance) { #ifdef FF_HAVE_THREADS - ffThreadCreateAndDetach(ffConnectDisplayServerThreadMain, instance); + ffThreadDetach(ffThreadCreate(ffConnectDisplayServerThreadMain, instance)); #ifdef FF_DETECT_QT_GTK - ffThreadCreateAndDetach(ffDetectQtThreadMain, instance); - ffThreadCreateAndDetach(ffDetectGTK2ThreadMain, instance); - ffThreadCreateAndDetach(ffDetectGTK3ThreadMain, instance); - ffThreadCreateAndDetach(ffDetectGTK4ThreadMain, instance); + ffThreadDetach(ffThreadCreate(ffDetectQtThreadMain, instance)); + ffThreadDetach(ffThreadCreate(ffDetectGTK2ThreadMain, instance)); + ffThreadDetach(ffThreadCreate(ffDetectGTK3ThreadMain, instance)); + ffThreadDetach(ffThreadCreate(ffDetectGTK4ThreadMain, instance)); #endif #else diff --git a/src/common/thread.h b/src/common/thread.h index 035ff9350..74c536cf2 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -9,35 +9,40 @@ #if defined(_WIN32) #include #include - #include // Win32 isn't available on MSYS2 + #include #define FF_THREAD_MUTEX_INITIALIZER SRWLOCK_INIT typedef SRWLOCK FFThreadMutex; + typedef HANDLE FFThreadType; static inline void ffThreadMutexLock(FFThreadMutex* mutex) { AcquireSRWLockExclusive(mutex); } static inline void ffThreadMutexUnlock(FFThreadMutex* mutex) { ReleaseSRWLockExclusive(mutex); } - static inline void ffThreadCreateAndDetach(unsigned (__stdcall* func)(void*), void* data) { - uintptr_t newThread = _beginthreadex(NULL, 0, func, data, 0, NULL); - if(newThread) - CloseHandle((HANDLE)newThread); + static inline FFThreadType ffThreadCreate(unsigned (__stdcall* func)(void*), void* data) { + return (FFThreadType)_beginthreadex(NULL, 0, func, data, 0, NULL); } #define FF_THREAD_ENTRY_DECL_WRAPPER(fn, paramType) static __stdcall unsigned fn ## ThreadMain (void* data) { fn((paramType)data); return 0; } + static inline void ffThreadDetach(FFThreadType thread) { CloseHandle(thread); } + static inline void ffThreadJoin(FFThreadType thread) { WaitForSingleObject(thread, INFINITE); } #else #include #define FF_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER typedef pthread_mutex_t FFThreadMutex; + typedef pthread_t FFThreadType; static inline void ffThreadMutexLock(FFThreadMutex* mutex) { pthread_mutex_lock(mutex); } static inline void ffThreadMutexUnlock(FFThreadMutex* mutex) { pthread_mutex_unlock(mutex); } - static inline void ffThreadCreateAndDetach(void* (* func)(void*), void* data) { - pthread_t newThread; - if(pthread_create(&newThread, NULL, func, data) == 0) - pthread_detach(newThread); + static inline FFThreadType ffThreadCreate(void* (* func)(void*), void* data) { + FFThreadType newThread = NULL; + pthread_create(&newThread, NULL, func, data); + return newThread; } #define FF_THREAD_ENTRY_DECL_WRAPPER(fn, paramType) static void* fn ## ThreadMain (void* data) { fn((paramType)data); return NULL; } + static inline void ffThreadDetach(FFThreadType thread) { pthread_detach(thread); } + static inline void ffThreadJoin(FFThreadType thread) { pthread_join(thread, NULL); } #endif #else //FF_HAVE_THREADS #define FF_THREAD_MUTEX_INITIALIZER 0 typedef char FFThreadMutex; static inline void ffThreadMutexLock(FFThreadMutex* mutex) { FF_UNUSED(mutex) } static inline void ffThreadMutexUnlock(FFThreadMutex* mutex) { FF_UNUSED(mutex) } + #define FF_THREAD_ENTRY_DECL_WRAPPER(fn, paramType) #endif //FF_HAVE_THREADS #endif