From fb0b498de29563a9a4c138c2fb0f7ff5e50ba760 Mon Sep 17 00:00:00 2001 From: janwas Date: Tue, 24 Aug 2004 17:27:51 +0000 Subject: [PATCH] pthread_mutex update - now uses CRITICAL_SECTIONs. quite hacky; awaiting word from simon how he'd like it :) This was SVN commit r1049. --- source/lib/sysdep/win/wposix.cpp | 111 +++++++++++++++++++------------ source/lib/sysdep/win/wposix.h | 7 +- 2 files changed, 74 insertions(+), 44 deletions(-) diff --git a/source/lib/sysdep/win/wposix.cpp b/source/lib/sysdep/win/wposix.cpp index d6528503ad..eb39f0339f 100755 --- a/source/lib/sysdep/win/wposix.cpp +++ b/source/lib/sysdep/win/wposix.cpp @@ -365,58 +365,83 @@ int pthread_create(pthread_t* thread, const void* attr, void*(*func)(void*), voi } + +// DeleteCriticalSection currently doesn't complain if we double-free +// (e.g. user calls destroy() and static initializer atexit runs), +// and dox are ambiguous. +/* +struct CS +{ + CRITICAL_SECTION cs; + CS() + { + InitializeCriticalSection(&cs); + } + ~CS() + { + DeleteCriticalSection(&cs); + } +};*/ + +cassert(sizeof(CRITICAL_SECTION) == sizeof(pthread_mutex_t)); +/* +static std::list mutexes; + +static void destroy_mutexes() +{ + mutexes.clear(); +} +*/ + pthread_mutex_t pthread_mutex_initializer() { - HANDLE h = CreateMutex(0, 0, 0); - if(h == INVALID_HANDLE_VALUE) - return 0; - // app is responsible for freeing via pthread_destroy_mutex! - return h; -} - -int pthread_mutex_init(pthread_mutex_t* m, const pthread_mutexattr_t*) -{ - if(!m) - return -1; - *m = pthread_mutex_initializer(); - return *m? 0 : -1; -} - -int pthread_mutex_lock(pthread_mutex_t* m) -{ - return WaitForSingleObject(*m, INFINITE) == WAIT_OBJECT_0? 0 : -1; -} - -int pthread_mutex_trylock(pthread_mutex_t* m) -{ - return WaitForSingleObject(*m, 0) == WAIT_OBJECT_0? 0 : -1; -} - -int pthread_mutex_unlock(pthread_mutex_t* m) -{ - return ReleaseMutex(*m)? 0 : -1; -} - -int pthread_mutex_timedlock(pthread_mutex_t* m, const struct timespec* abs_timeout) -{ - DWORD ms_timeout = 0; - if(abs_timeout) - { - struct timespec cur_ts; - clock_gettime(CLOCK_REALTIME, &cur_ts); - ms_timeout = DWORD((cur_ts.tv_sec - abs_timeout->tv_sec ) * 1000 + - (cur_ts.tv_nsec - abs_timeout->tv_nsec) / 1000000); - } - - return WaitForSingleObject(*m, ms_timeout) == WAIT_OBJECT_0? 0 : -1; + CRITICAL_SECTION cs; + InitializeCriticalSection(&cs); + return *(pthread_mutex_t*)&cs; } int pthread_mutex_destroy(pthread_mutex_t* m) { - CloseHandle(*m); + DeleteCriticalSection((CRITICAL_SECTION*)m); + return 0; +/* + CS* cs = (CS*)m; + mutexes.erase(cs); + delete cs; + return 0; +*/ +} + +int pthread_mutex_init(pthread_mutex_t* m, const pthread_mutexattr_t*) +{ + InitializeCriticalSection((CRITICAL_SECTION*)m); return 0; } +int pthread_mutex_lock(pthread_mutex_t* m) +{ + EnterCriticalSection((CRITICAL_SECTION*)m); + return 0; +} + +int pthread_mutex_trylock(pthread_mutex_t* m) +{ + BOOL got_it = TryEnterCriticalSection((CRITICAL_SECTION*)m); + return got_it? 0 : -1; +} + +int pthread_mutex_unlock(pthread_mutex_t* m) +{ + LeaveCriticalSection((CRITICAL_SECTION*)m); + return 0; +} + +int pthread_mutex_timedlock(pthread_mutex_t* m, const struct timespec* abs_timeout) +{ + return -ENOSYS; +} + + ////////////////////////////////////////////////////////////////////////////// // // memory mapping diff --git a/source/lib/sysdep/win/wposix.h b/source/lib/sysdep/win/wposix.h index 71512718a9..e6b6217fea 100755 --- a/source/lib/sysdep/win/wposix.h +++ b/source/lib/sysdep/win/wposix.h @@ -367,7 +367,12 @@ extern int pthread_getschedparam(pthread_t thread, int* policy, struct sched_par extern int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param* param); extern int pthread_create(pthread_t* thread, const void* attr, void*(*func)(void*), void* arg); -typedef void* pthread_mutex_t; +//typedef void* pthread_mutex_t; +typedef struct +{ + char opaque[24]; +} +pthread_mutex_t; typedef void pthread_mutexattr_t; extern pthread_mutex_t pthread_mutex_initializer();