Skip to content

Commit 18c6118

Browse files
author
msvensson@pilot.mysql.com
committed
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
1 parent 2362261 commit 18c6118

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

include/config-win.h

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
/* We have to do this define before including windows.h to get the AWE API
2020
functions */
2121
#define _WIN32_WINNT 0x0500
22+
#else
23+
/* Get NT 4.0 functions */
24+
#define _WIN32_WINNT 0x0400
2225
#endif
2326

2427
#if defined(_MSC_VER) && _MSC_VER >= 1400

mysys/my_winthread.c

+10-12
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,29 @@ void win_pthread_init(void)
4040
pthread_mutex_init(&THR_LOCK_thread,MY_MUTEX_INIT_FAST);
4141
}
4242

43+
4344
/**
4445
Adapter to @c pthread_mutex_trylock()
4546
4647
@retval 0 Mutex was acquired
4748
@retval EBUSY Mutex was already locked by a thread
48-
@retval EINVAL Mutex could not be acquired due to other error
4949
*/
5050
int
5151
win_pthread_mutex_trylock(pthread_mutex_t *mutex)
5252
{
53-
switch (WaitForSingleObject(mutex, 0)) {
54-
case WAIT_TIMEOUT:
55-
return EBUSY;
56-
57-
default:
58-
case WAIT_FAILURE:
59-
return EINVAL;
60-
61-
case WAIT_OBJECT_0:
62-
case WAIT_ABANDONED: /* The mutex was acquired because it was
63-
* abandoned */
53+
if (TryEnterCriticalSection(mutex))
54+
{
55+
/* Don't allow recursive lock */
56+
if (mutex->RecursionCount > 1){
57+
LeaveCriticalSection(mutex);
58+
return EBUSY;
59+
}
6460
return 0;
6561
}
62+
return EBUSY;
6663
}
6764

65+
6866
/*
6967
** We have tried to use '_beginthreadex' instead of '_beginthread' here
7068
** but in this case the program leaks about 512 characters for each

0 commit comments

Comments
 (0)