diff options
author | unknown <msvensson@pilot.mysql.com> | 2007-10-03 21:38:32 +0200 |
---|---|---|
committer | unknown <msvensson@pilot.mysql.com> | 2007-10-03 21:38:32 +0200 |
commit | 662fb20f1e03a5d4146a67b5e73c828f5ab476e4 (patch) | |
tree | c537085b7010b4e17f0b3c2e7bc1d3f2e8bf2311 /mysys/my_winthread.c | |
parent | 6a5c7fc3b86cf7df5ce5abba99a7008911964461 (diff) | |
download | mariadb-git-662fb20f1e03a5d4146a67b5e73c828f5ab476e4.tar.gz |
Bug#30992 Wrong implementation of pthread_mutex_trylock()
It's not possible to use WaitForSingleObject to wait
on a CRITICAL_SECTION, instead use the TryEnterCriticalSection function.
- if "mutex" was already taken => return EBUSY
- if "mutex" was aquired => return 0
include/config-win.h:
Make windows.h define TryEnterCriticalSection
mysys/my_winthread.c:
Use the TryEnterCriticalSection function to implement pthread_mutex_trylock
Prevent recursive behaviour by looking at the RecursionCount variable
in the CRITICAL_SECTION struct and return EBUSY from function if
the mutex was already locked once.
Diffstat (limited to 'mysys/my_winthread.c')
-rw-r--r-- | mysys/my_winthread.c | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/mysys/my_winthread.c b/mysys/my_winthread.c index e9fface0521..e94369bec32 100644 --- a/mysys/my_winthread.c +++ b/mysys/my_winthread.c @@ -40,31 +40,29 @@ void win_pthread_init(void) pthread_mutex_init(&THR_LOCK_thread,MY_MUTEX_INIT_FAST); } + /** Adapter to @c pthread_mutex_trylock() @retval 0 Mutex was acquired @retval EBUSY Mutex was already locked by a thread - @retval EINVAL Mutex could not be acquired due to other error */ int win_pthread_mutex_trylock(pthread_mutex_t *mutex) { - switch (WaitForSingleObject(mutex, 0)) { - case WAIT_TIMEOUT: - return EBUSY; - - default: - case WAIT_FAILURE: - return EINVAL; - - case WAIT_OBJECT_0: - case WAIT_ABANDONED: /* The mutex was acquired because it was - * abandoned */ + if (TryEnterCriticalSection(mutex)) + { + /* Don't allow recursive lock */ + if (mutex->RecursionCount > 1){ + LeaveCriticalSection(mutex); + return EBUSY; + } return 0; } + return EBUSY; } + /* ** We have tried to use '_beginthreadex' instead of '_beginthread' here ** but in this case the program leaks about 512 characters for each |