1
/* Copyright (C) 2000-2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* This makes a wrapper for mutex handling to make it easier to debug mutex */
18
#include <my_global.h>
19
#if defined(TARGET_OS_LINUX) && !defined (__USE_UNIX98)
20
#define __USE_UNIX98 /* To get rw locks under Linux */
22
#if defined(THREAD) && defined(SAFE_MUTEX)
23
#undef SAFE_MUTEX /* Avoid safe_mutex redefinitions */
24
#include "mysys_priv.h"
25
#include "my_static.h"
28
#ifndef DO_NOT_REMOVE_THREAD_WRAPPERS
30
#undef pthread_mutex_t
31
#undef pthread_mutex_init
32
#undef pthread_mutex_lock
33
#undef pthread_mutex_unlock
34
#undef pthread_mutex_destroy
35
#undef pthread_cond_wait
36
#undef pthread_cond_timedwait
37
#ifdef HAVE_NONPOSIX_PTHREAD_MUTEX_INIT
38
#define pthread_mutex_init(a,b) my_pthread_mutex_init((a),(b))
40
#endif /* DO_NOT_REMOVE_THREAD_WRAPPERS */
42
static pthread_mutex_t THR_LOCK_mutex;
43
static ulong safe_mutex_count= 0; /* Number of mutexes created */
44
#ifdef SAFE_MUTEX_DETECT_DESTROY
45
static struct st_safe_mutex_info_t *safe_mutex_root= NULL;
48
void safe_mutex_global_init(void)
50
pthread_mutex_init(&THR_LOCK_mutex,MY_MUTEX_INIT_FAST);
54
int safe_mutex_init(safe_mutex_t *mp,
55
const pthread_mutexattr_t *attr __attribute__((unused)),
59
bzero((char*) mp,sizeof(*mp));
60
pthread_mutex_init(&mp->global,MY_MUTEX_INIT_ERRCHK);
61
pthread_mutex_init(&mp->mutex,attr);
62
/* Mark that mutex is initialized */
66
#ifdef SAFE_MUTEX_DETECT_DESTROY
68
Monitor the freeing of mutexes. This code depends on single thread init
71
if ((mp->info= (safe_mutex_info_t *) malloc(sizeof(safe_mutex_info_t))))
73
struct st_safe_mutex_info_t *info =mp->info;
75
info->init_file= file;
76
info->init_line= line;
80
pthread_mutex_lock(&THR_LOCK_mutex);
81
if ((info->next= safe_mutex_root))
82
safe_mutex_root->prev= info;
83
safe_mutex_root= info;
85
pthread_mutex_unlock(&THR_LOCK_mutex);
88
thread_safe_increment(safe_mutex_count, &THR_LOCK_mutex);
89
#endif /* SAFE_MUTEX_DETECT_DESTROY */
94
int safe_mutex_lock(safe_mutex_t *mp, my_bool try_lock, const char *file, uint line)
100
"safe_mutex: Trying to lock unitialized mutex at %s, line %d\n",
106
pthread_mutex_lock(&mp->global);
111
pthread_mutex_unlock(&mp->global);
114
else if (pthread_equal(pthread_self(),mp->thread))
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());
124
pthread_mutex_unlock(&mp->global);
127
If we are imitating trylock(), we need to take special
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.
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
144
error= pthread_mutex_trylock(&mp->mutex);
149
error= pthread_mutex_lock(&mp->mutex);
151
if (error || (error=pthread_mutex_lock(&mp->global)))
153
fprintf(stderr,"Got error %d when trying to lock mutex at %s, line %d\n",
158
mp->thread= pthread_self();
161
fprintf(stderr,"safe_mutex: Error in thread libray: Got mutex at %s, \
162
line %d more than 1 time\n", file,line);
168
pthread_mutex_unlock(&mp->global);
173
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line)
176
pthread_mutex_lock(&mp->global);
179
fprintf(stderr,"safe_mutex: Trying to unlock mutex that wasn't locked at %s, line %d\n Last used at %s, line: %d\n",
180
file,line,mp->file ? mp->file : "",mp->line);
184
if (!pthread_equal(pthread_self(),mp->thread))
186
fprintf(stderr,"safe_mutex: Trying to unlock mutex at %s, line %d that was locked by another thread at: %s, line: %d\n",
187
file,line,mp->file,mp->line);
193
error=pthread_mutex_unlock(&mp->mutex);
196
fprintf(stderr,"safe_mutex: Got error: %d (%d) when trying to unlock mutex at %s, line %d\n", error, errno, file, line);
200
pthread_mutex_unlock(&mp->global);
205
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp, const char *file,
209
pthread_mutex_lock(&mp->global);
212
fprintf(stderr,"safe_mutex: Trying to cond_wait on a unlocked mutex at %s, line %d\n",file,line);
216
if (!pthread_equal(pthread_self(),mp->thread))
218
fprintf(stderr,"safe_mutex: Trying to cond_wait on a mutex at %s, line %d that was locked by another thread at: %s, line: %d\n",
219
file,line,mp->file,mp->line);
224
if (mp->count-- != 1)
226
fprintf(stderr,"safe_mutex: Count was %d on locked mutex at %s, line %d\n",
227
mp->count+1, file, line);
231
pthread_mutex_unlock(&mp->global);
232
error=pthread_cond_wait(cond,&mp->mutex);
233
pthread_mutex_lock(&mp->global);
236
fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_wait at %s, line %d\n", error, errno, file, line);
240
mp->thread=pthread_self();
244
"safe_mutex: Count was %d in thread 0x%lx when locking mutex at %s, line %d\n",
245
mp->count-1, my_thread_dbug_id(), file, line);
251
pthread_mutex_unlock(&mp->global);
256
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
257
struct timespec *abstime,
258
const char *file, uint line)
261
pthread_mutex_lock(&mp->global);
262
if (mp->count != 1 || !pthread_equal(pthread_self(),mp->thread))
264
fprintf(stderr,"safe_mutex: Trying to cond_wait at %s, line %d on a not hold mutex\n",file,line);
268
mp->count--; /* Mutex will be released */
269
pthread_mutex_unlock(&mp->global);
270
error=pthread_cond_timedwait(cond,&mp->mutex,abstime);
272
if (error && (error != EINTR && error != ETIMEDOUT && error != ETIME))
274
fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_timedwait at %s, line %d\n", error, errno, file, line);
277
pthread_mutex_lock(&mp->global);
278
mp->thread=pthread_self();
282
"safe_mutex: Count was %d in thread 0x%lx when locking mutex at %s, line %d (error: %d (%d))\n",
283
mp->count-1, my_thread_dbug_id(), file, line, error, error);
289
pthread_mutex_unlock(&mp->global);
294
int safe_mutex_destroy(safe_mutex_t *mp, const char *file, uint line)
300
"safe_mutex: Trying to destroy unitialized mutex at %s, line %d\n",
307
fprintf(stderr,"safe_mutex: Trying to destroy a mutex that was locked at %s, line %d at %s, line %d\n",
308
mp->file,mp->line, file, line);
312
if (pthread_mutex_destroy(&mp->global))
314
if (pthread_mutex_destroy(&mp->mutex))
316
mp->file= 0; /* Mark destroyed */
318
#ifdef SAFE_MUTEX_DETECT_DESTROY
321
struct st_safe_mutex_info_t *info= mp->info;
322
pthread_mutex_lock(&THR_LOCK_mutex);
325
info->prev->next = info->next;
327
safe_mutex_root = info->next;
329
info->next->prev = info->prev;
332
pthread_mutex_unlock(&THR_LOCK_mutex);
334
mp->info= NULL; /* Get crash if double free */
337
thread_safe_sub(safe_mutex_count, 1, &THR_LOCK_mutex);
338
#endif /* SAFE_MUTEX_DETECT_DESTROY */
344
Free global resources and check that all mutex has been destroyed
348
file Print errors on this file
351
We can't use DBUG_PRINT() here as we have in my_end() disabled
352
DBUG handling before calling this function.
354
In MySQL one may get one warning for a mutex created in my_thr_init.c
355
This is ok, as this thread may not yet have been exited.
358
void safe_mutex_end(FILE *file __attribute__((unused)))
360
if (!safe_mutex_count) /* safetly */
361
pthread_mutex_destroy(&THR_LOCK_mutex);
362
#ifdef SAFE_MUTEX_DETECT_DESTROY
366
if (safe_mutex_count)
368
fprintf(file, "Warning: Not destroyed mutex: %lu\n", safe_mutex_count);
372
struct st_safe_mutex_info_t *ptr;
373
for (ptr= safe_mutex_root ; ptr ; ptr= ptr->next)
375
fprintf(file, "\tMutex initiated at line %4u in '%s'\n",
376
ptr->init_line, ptr->init_file);
380
#endif /* SAFE_MUTEX_DETECT_DESTROY */
383
#endif /* THREAD && SAFE_MUTEX */
385
#if defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
387
#include "mysys_priv.h"
388
#include "my_static.h"
389
#include <m_string.h>
393
#include <myisampack.h>
394
#include <mysys_err.h>
397
#undef pthread_mutex_t
398
#undef pthread_mutex_init
399
#undef pthread_mutex_lock
400
#undef pthread_mutex_trylock
401
#undef pthread_mutex_unlock
402
#undef pthread_mutex_destroy
403
#undef pthread_cond_wait
404
#undef pthread_cond_timedwait
406
ulong mutex_delay(ulong delayloops)
413
for (i = 0; i < delayloops * 50; i++)
419
#define MY_PTHREAD_FASTMUTEX_SPINS 8
420
#define MY_PTHREAD_FASTMUTEX_DELAY 4
422
static int cpu_count= 0;
424
int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
425
const pthread_mutexattr_t *attr)
427
if ((cpu_count > 1) && (attr == MY_MUTEX_INIT_FAST))
428
mp->spins= MY_PTHREAD_FASTMUTEX_SPINS;
431
return pthread_mutex_init(&mp->mutex, attr);
434
int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp)
438
uint maxdelay= MY_PTHREAD_FASTMUTEX_DELAY;
440
for (i= 0; i < mp->spins; i++)
442
res= pthread_mutex_trylock(&mp->mutex);
450
mutex_delay(maxdelay);
451
maxdelay += ((double) random() / (double) RAND_MAX) *
452
MY_PTHREAD_FASTMUTEX_DELAY + 1;
454
return pthread_mutex_lock(&mp->mutex);
458
void fastmutex_global_init(void)
460
#ifdef _SC_NPROCESSORS_CONF
461
cpu_count= sysconf(_SC_NPROCESSORS_CONF);
465
#endif /* defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */