Skip to content

Commit efdd32c

Browse files
author
davi@moksha.local
committed
Merge moksha.local:/Users/davi/mysql/push/mysql-5.0-runtime
into moksha.local:/Users/davi/mysql/push/mysql-5.1-runtime
2 parents 27ee6db + 18c6118 commit efdd32c

File tree

4 files changed

+73
-11
lines changed

4 files changed

+73
-11
lines changed

include/config-win.h

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
/* We have to do this define before including windows.h to get the AWE API
2222
functions */
2323
#define _WIN32_WINNT 0x0500
24+
#else
25+
/* Get NT 4.0 functions */
26+
#define _WIN32_WINNT 0x0400
2427
#endif
2528

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

include/my_pthread.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ struct timespec {
101101

102102
void win_pthread_init(void);
103103
int win_pthread_setspecific(void *A,void *B,uint length);
104+
int win_pthread_mutex_trylock(pthread_mutex_t *mutex);
104105
int pthread_create(pthread_t *,pthread_attr_t *,pthread_handler,void *);
105106
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
106107
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
@@ -156,7 +157,7 @@ void pthread_exit(void *a); /* was #define pthread_exit(A) ExitThread(A)*/
156157
#define pthread_equal(A,B) ((A) == (B))
157158
#define pthread_mutex_init(A,B) (InitializeCriticalSection(A),0)
158159
#define pthread_mutex_lock(A) (EnterCriticalSection(A),0)
159-
#define pthread_mutex_trylock(A) (WaitForSingleObject((A), 0) == WAIT_TIMEOUT)
160+
#define pthread_mutex_trylock(A) win_pthread_mutex_trylock((A))
160161
#define pthread_mutex_unlock(A) LeaveCriticalSection(A)
161162
#define pthread_mutex_destroy(A) DeleteCriticalSection(A)
162163
#define my_pthread_setprio(A,B) SetThreadPriority(GetCurrentThread(), (B))
@@ -472,7 +473,7 @@ typedef struct st_safe_mutex_info_t
472473

473474
int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
474475
const char *file, uint line);
475-
int safe_mutex_lock(safe_mutex_t *mp,const char *file, uint line);
476+
int safe_mutex_lock(safe_mutex_t *mp, my_bool try_lock, const char *file, uint line);
476477
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line);
477478
int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint line);
478479
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
@@ -495,12 +496,12 @@ void safe_mutex_end(FILE *file);
495496
#undef pthread_cond_timedwait
496497
#undef pthread_mutex_trylock
497498
#define pthread_mutex_init(A,B) safe_mutex_init((A),(B),__FILE__,__LINE__)
498-
#define pthread_mutex_lock(A) safe_mutex_lock((A),__FILE__,__LINE__)
499+
#define pthread_mutex_lock(A) safe_mutex_lock((A), FALSE, __FILE__, __LINE__)
499500
#define pthread_mutex_unlock(A) safe_mutex_unlock((A),__FILE__,__LINE__)
500501
#define pthread_mutex_destroy(A) safe_mutex_destroy((A),__FILE__,__LINE__)
501502
#define pthread_cond_wait(A,B) safe_cond_wait((A),(B),__FILE__,__LINE__)
502503
#define pthread_cond_timedwait(A,B,C) safe_cond_timedwait((A),(B),(C),__FILE__,__LINE__)
503-
#define pthread_mutex_trylock(A) pthread_mutex_lock(A)
504+
#define pthread_mutex_trylock(A) safe_mutex_lock((A), TRUE, __FILE__, __LINE__)
504505
#define pthread_mutex_t safe_mutex_t
505506
#define safe_mutex_assert_owner(mp) \
506507
DBUG_ASSERT((mp)->count > 0 && \

mysys/my_winthread.c

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

43+
44+
/**
45+
Adapter to @c pthread_mutex_trylock()
46+
47+
@retval 0 Mutex was acquired
48+
@retval EBUSY Mutex was already locked by a thread
49+
*/
50+
int
51+
win_pthread_mutex_trylock(pthread_mutex_t *mutex)
52+
{
53+
if (TryEnterCriticalSection(mutex))
54+
{
55+
/* Don't allow recursive lock */
56+
if (mutex->RecursionCount > 1){
57+
LeaveCriticalSection(mutex);
58+
return EBUSY;
59+
}
60+
return 0;
61+
}
62+
return EBUSY;
63+
}
64+
65+
4366
/*
4467
** We have tried to use '_beginthreadex' instead of '_beginthread' here
4568
** but in this case the program leaks about 512 characters for each

mysys/thr_mutex.c

+42-7
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ int safe_mutex_init(safe_mutex_t *mp,
9191
}
9292

9393

94-
int safe_mutex_lock(safe_mutex_t *mp,const char *file, uint line)
94+
int safe_mutex_lock(safe_mutex_t *mp, my_bool try_lock, const char *file, uint line)
9595
{
9696
int error;
9797
if (!mp->file)
@@ -104,15 +104,50 @@ int safe_mutex_lock(safe_mutex_t *mp,const char *file, uint line)
104104
}
105105

106106
pthread_mutex_lock(&mp->global);
107-
if (mp->count > 0 && pthread_equal(pthread_self(),mp->thread))
107+
if (mp->count > 0)
108108
{
109-
fprintf(stderr,"safe_mutex: Trying to lock mutex at %s, line %d, when the mutex was already locked at %s, line %d in thread %s\n",
110-
file,line,mp->file, mp->line, my_thread_name());
111-
fflush(stderr);
112-
abort();
109+
if (try_lock)
110+
{
111+
pthread_mutex_unlock(&mp->global);
112+
return EBUSY;
113+
}
114+
else if (pthread_equal(pthread_self(),mp->thread))
115+
{
116+
fprintf(stderr,
117+
"safe_mutex: Trying to lock mutex at %s, line %d, when the"
118+
" mutex was already locked at %s, line %d in thread %s\n",
119+
file,line,mp->file, mp->line, my_thread_name());
120+
fflush(stderr);
121+
abort();
122+
}
113123
}
114124
pthread_mutex_unlock(&mp->global);
115-
error=pthread_mutex_lock(&mp->mutex);
125+
126+
/*
127+
If we are imitating trylock(), we need to take special
128+
precautions.
129+
130+
- We cannot use pthread_mutex_lock() only since another thread can
131+
overtake this thread and take the lock before this thread
132+
causing pthread_mutex_trylock() to hang. In this case, we should
133+
just return EBUSY. Hence, we use pthread_mutex_trylock() to be
134+
able to return immediately.
135+
136+
- We cannot just use trylock() and continue execution below, since
137+
this would generate an error and abort execution if the thread
138+
was overtaken and trylock() returned EBUSY . In this case, we
139+
instead just return EBUSY, since this is the expected behaviour
140+
of trylock().
141+
*/
142+
if (try_lock)
143+
{
144+
error= pthread_mutex_trylock(&mp->mutex);
145+
if (error == EBUSY)
146+
return error;
147+
}
148+
else
149+
error= pthread_mutex_lock(&mp->mutex);
150+
116151
if (error || (error=pthread_mutex_lock(&mp->global)))
117152
{
118153
fprintf(stderr,"Got error %d when trying to lock mutex at %s, line %d\n",

0 commit comments

Comments
 (0)