Skip to content

Commit 2362261

Browse files
author
mats@kindahl-laptop.dnsalias.net
committed
Merge kindahl-laptop.dnsalias.net:/home/bk/b30992-mysql-5.0-rpl
into kindahl-laptop.dnsalias.net:/home/bk/b30992-mysql-5.0-runtime
2 parents ef3bcaf + 5dad55c commit 2362261

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

include/my_pthread.h

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

117117
void win_pthread_init(void);
118118
int win_pthread_setspecific(void *A,void *B,uint length);
119+
int win_pthread_mutex_trylock(pthread_mutex_t *mutex);
119120
int pthread_create(pthread_t *,pthread_attr_t *,pthread_handler,void *);
120121
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
121122
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
@@ -176,7 +177,7 @@ extern int pthread_mutex_destroy (pthread_mutex_t *);
176177
#else
177178
#define pthread_mutex_init(A,B) (InitializeCriticalSection(A),0)
178179
#define pthread_mutex_lock(A) (EnterCriticalSection(A),0)
179-
#define pthread_mutex_trylock(A) (WaitForSingleObject((A), 0) == WAIT_TIMEOUT)
180+
#define pthread_mutex_trylock(A) win_pthread_mutex_trylock((A))
180181
#define pthread_mutex_unlock(A) LeaveCriticalSection(A)
181182
#define pthread_mutex_destroy(A) DeleteCriticalSection(A)
182183
#define my_pthread_setprio(A,B) SetThreadPriority(GetCurrentThread(), (B))
@@ -574,7 +575,7 @@ typedef struct st_safe_mutex_info_t
574575

575576
int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
576577
const char *file, uint line);
577-
int safe_mutex_lock(safe_mutex_t *mp,const char *file, uint line);
578+
int safe_mutex_lock(safe_mutex_t *mp, my_bool try_lock, const char *file, uint line);
578579
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line);
579580
int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint line);
580581
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
@@ -597,12 +598,12 @@ void safe_mutex_end(FILE *file);
597598
#undef pthread_cond_timedwait
598599
#undef pthread_mutex_trylock
599600
#define pthread_mutex_init(A,B) safe_mutex_init((A),(B),__FILE__,__LINE__)
600-
#define pthread_mutex_lock(A) safe_mutex_lock((A),__FILE__,__LINE__)
601+
#define pthread_mutex_lock(A) safe_mutex_lock((A), FALSE, __FILE__, __LINE__)
601602
#define pthread_mutex_unlock(A) safe_mutex_unlock((A),__FILE__,__LINE__)
602603
#define pthread_mutex_destroy(A) safe_mutex_destroy((A),__FILE__,__LINE__)
603604
#define pthread_cond_wait(A,B) safe_cond_wait((A),(B),__FILE__,__LINE__)
604605
#define pthread_cond_timedwait(A,B,C) safe_cond_timedwait((A),(B),(C),__FILE__,__LINE__)
605-
#define pthread_mutex_trylock(A) pthread_mutex_lock(A)
606+
#define pthread_mutex_trylock(A) safe_mutex_lock((A), TRUE, __FILE__, __LINE__)
606607
#define pthread_mutex_t safe_mutex_t
607608
#define safe_mutex_assert_owner(mp) \
608609
DBUG_ASSERT((mp)->count > 0 && \

mysys/my_winthread.c

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

43+
/**
44+
Adapter to @c pthread_mutex_trylock()
45+
46+
@retval 0 Mutex was acquired
47+
@retval EBUSY Mutex was already locked by a thread
48+
@retval EINVAL Mutex could not be acquired due to other error
49+
*/
50+
int
51+
win_pthread_mutex_trylock(pthread_mutex_t *mutex)
52+
{
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 */
64+
return 0;
65+
}
66+
}
67+
4368
/*
4469
** We have tried to use '_beginthreadex' instead of '_beginthread' here
4570
** 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)