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 "mysys_priv.h"
20
#if defined(TARGET_OS_LINUX) && !defined (__USE_UNIX98)
21
#define __USE_UNIX98 /* To get rw locks under Linux */
23
#if defined(SAFE_MUTEX)
24
#undef SAFE_MUTEX /* Avoid safe_mutex redefinitions */
25
#include "mysys_priv.h"
26
#include "my_static.h"
29
#ifndef DO_NOT_REMOVE_THREAD_WRAPPERS
31
#undef pthread_mutex_t
32
#undef pthread_mutex_init
33
#undef pthread_mutex_lock
34
#undef pthread_mutex_unlock
35
#undef pthread_mutex_destroy
36
#undef pthread_cond_wait
37
#undef pthread_cond_timedwait
38
#ifdef HAVE_NONPOSIX_PTHREAD_MUTEX_INIT
39
#define pthread_mutex_init(a,b) my_pthread_mutex_init((a),(b))
41
#endif /* DO_NOT_REMOVE_THREAD_WRAPPERS */
43
static pthread_mutex_t THR_LOCK_mutex;
44
static uint32_t safe_mutex_count= 0; /* Number of mutexes created */
45
#ifdef SAFE_MUTEX_DETECT_DESTROY
46
static struct st_safe_mutex_info_t *safe_mutex_root= NULL;
49
void safe_mutex_global_init(void)
51
pthread_mutex_init(&THR_LOCK_mutex,MY_MUTEX_INIT_FAST);
55
int safe_mutex_init(safe_mutex_t *mp,
56
const pthread_mutexattr_t *attr __attribute__((unused)),
60
memset(mp, 0, sizeof(*mp));
61
pthread_mutex_init(&mp->global,MY_MUTEX_INIT_ERRCHK);
62
pthread_mutex_init(&mp->mutex,attr);
63
/* Mark that mutex is initialized */
67
#ifdef SAFE_MUTEX_DETECT_DESTROY
69
Monitor the freeing of mutexes. This code depends on single thread init
72
if ((mp->info= (safe_mutex_info_t *) malloc(sizeof(safe_mutex_info_t))))
74
struct st_safe_mutex_info_t *info =mp->info;
76
info->init_file= file;
77
info->init_line= line;
81
pthread_mutex_lock(&THR_LOCK_mutex);
82
if ((info->next= safe_mutex_root))
83
safe_mutex_root->prev= info;
84
safe_mutex_root= info;
86
pthread_mutex_unlock(&THR_LOCK_mutex);
89
thread_safe_increment(safe_mutex_count, &THR_LOCK_mutex);
90
#endif /* SAFE_MUTEX_DETECT_DESTROY */
95
int safe_mutex_lock(safe_mutex_t *mp, bool try_lock, const char *file, uint32_t line)
101
"safe_mutex: Trying to lock unitialized mutex at %s, line %d\n",
107
pthread_mutex_lock(&mp->global);
112
pthread_mutex_unlock(&mp->global);
115
else if (pthread_equal(pthread_self(),mp->thread))
118
"safe_mutex: Trying to lock mutex at %s, line %d, when the"
119
" mutex was already locked at %s, line %d in thread %s\n",
120
file,line,mp->file, mp->line, my_thread_name());
125
pthread_mutex_unlock(&mp->global);
128
If we are imitating trylock(), we need to take special
131
- We cannot use pthread_mutex_lock() only since another thread can
132
overtake this thread and take the lock before this thread
133
causing pthread_mutex_trylock() to hang. In this case, we should
134
just return EBUSY. Hence, we use pthread_mutex_trylock() to be
135
able to return immediately.
137
- We cannot just use trylock() and continue execution below, since
138
this would generate an error and abort execution if the thread
139
was overtaken and trylock() returned EBUSY . In this case, we
140
instead just return EBUSY, since this is the expected behaviour
145
error= pthread_mutex_trylock(&mp->mutex);
150
error= pthread_mutex_lock(&mp->mutex);
152
if (error || (error=pthread_mutex_lock(&mp->global)))
154
fprintf(stderr,"Got error %d when trying to lock mutex at %s, line %d\n",
159
mp->thread= pthread_self();
162
fprintf(stderr,"safe_mutex: Error in thread libray: Got mutex at %s, \
163
line %d more than 1 time\n", file,line);
169
pthread_mutex_unlock(&mp->global);
174
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint32_t line)
177
pthread_mutex_lock(&mp->global);
180
fprintf(stderr,"safe_mutex: Trying to unlock mutex that wasn't locked at %s, line %d\n Last used at %s, line: %d\n",
181
file,line,mp->file ? mp->file : "",mp->line);
185
if (!pthread_equal(pthread_self(),mp->thread))
187
fprintf(stderr,"safe_mutex: Trying to unlock mutex at %s, line %d that was locked by another thread at: %s, line: %d\n",
188
file,line,mp->file,mp->line);
194
error=pthread_mutex_unlock(&mp->mutex);
197
fprintf(stderr,"safe_mutex: Got error: %d (%d) when trying to unlock mutex at %s, line %d\n", error, errno, file, line);
201
pthread_mutex_unlock(&mp->global);
206
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp, const char *file,
210
pthread_mutex_lock(&mp->global);
213
fprintf(stderr,"safe_mutex: Trying to cond_wait on a unlocked mutex at %s, line %d\n",file,line);
217
if (!pthread_equal(pthread_self(),mp->thread))
219
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",
220
file,line,mp->file,mp->line);
225
if (mp->count-- != 1)
227
fprintf(stderr,"safe_mutex: Count was %d on locked mutex at %s, line %d\n",
228
mp->count+1, file, line);
232
pthread_mutex_unlock(&mp->global);
233
error=pthread_cond_wait(cond,&mp->mutex);
234
pthread_mutex_lock(&mp->global);
237
fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_wait at %s, line %d\n", error, errno, file, line);
241
mp->thread=pthread_self();
245
"safe_mutex: Count was %d in thread 0x%lx when locking mutex at %s, line %d\n",
246
mp->count-1, my_thread_dbug_id(), file, line);
252
pthread_mutex_unlock(&mp->global);
257
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
258
struct timespec *abstime,
259
const char *file, uint32_t line)
262
pthread_mutex_lock(&mp->global);
263
if (mp->count != 1 || !pthread_equal(pthread_self(),mp->thread))
265
fprintf(stderr,"safe_mutex: Trying to cond_wait at %s, line %d on a not hold mutex\n",file,line);
269
mp->count--; /* Mutex will be released */
270
pthread_mutex_unlock(&mp->global);
271
error=pthread_cond_timedwait(cond,&mp->mutex,abstime);
273
if (error && (error != EINTR && error != ETIMEDOUT && error != ETIME))
275
fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_timedwait at %s, line %d\n", error, errno, file, line);
278
pthread_mutex_lock(&mp->global);
279
mp->thread=pthread_self();
283
"safe_mutex: Count was %d in thread 0x%lx when locking mutex at %s, line %d (error: %d (%d))\n",
284
mp->count-1, my_thread_dbug_id(), file, line, error, error);
290
pthread_mutex_unlock(&mp->global);
295
int safe_mutex_destroy(safe_mutex_t *mp, const char *file, uint32_t line)
301
"safe_mutex: Trying to destroy unitialized mutex at %s, line %d\n",
308
fprintf(stderr,"safe_mutex: Trying to destroy a mutex that was locked at %s, line %d at %s, line %d\n",
309
mp->file,mp->line, file, line);
313
if (pthread_mutex_destroy(&mp->global))
315
if (pthread_mutex_destroy(&mp->mutex))
317
mp->file= 0; /* Mark destroyed */
319
#ifdef SAFE_MUTEX_DETECT_DESTROY
322
struct st_safe_mutex_info_t *info= mp->info;
323
pthread_mutex_lock(&THR_LOCK_mutex);
326
info->prev->next = info->next;
328
safe_mutex_root = info->next;
330
info->next->prev = info->prev;
333
pthread_mutex_unlock(&THR_LOCK_mutex);
335
mp->info= NULL; /* Get crash if double free */
338
thread_safe_sub(safe_mutex_count, 1, &THR_LOCK_mutex);
339
#endif /* SAFE_MUTEX_DETECT_DESTROY */
345
Free global resources and check that all mutex has been destroyed
349
file Print errors on this file
353
In MySQL one may get one warning for a mutex created in my_thr_init.c
354
This is ok, as this thread may not yet have been exited.
357
void safe_mutex_end(int *file __attribute__((unused)))
359
if (!safe_mutex_count) /* safetly */
360
pthread_mutex_destroy(&THR_LOCK_mutex);
361
#ifdef SAFE_MUTEX_DETECT_DESTROY
365
if (safe_mutex_count)
367
fprintf(file, "Warning: Not destroyed mutex: %lu\n", safe_mutex_count);
371
struct st_safe_mutex_info_t *ptr;
372
for (ptr= safe_mutex_root ; ptr ; ptr= ptr->next)
374
fprintf(file, "\tMutex initiated at line %4u in '%s'\n",
375
ptr->init_line, ptr->init_file);
379
#endif /* SAFE_MUTEX_DETECT_DESTROY */
382
#endif /* THREAD && SAFE_MUTEX */
384
#if defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
386
#include "mysys_priv.h"
387
#include "my_static.h"
388
#include <m_string.h>
391
#include <myisampack.h>
392
#include <mysys_err.h>
395
#undef pthread_mutex_t
396
#undef pthread_mutex_init
397
#undef pthread_mutex_lock
398
#undef pthread_mutex_trylock
399
#undef pthread_mutex_unlock
400
#undef pthread_mutex_destroy
401
#undef pthread_cond_wait
402
#undef pthread_cond_timedwait
404
uint32_t mutex_delay(uint32_t delayloops)
411
for (i = 0; i < delayloops * 50; i++)
417
#define MY_PTHREAD_FASTMUTEX_SPINS 8
418
#define MY_PTHREAD_FASTMUTEX_DELAY 4
420
static int cpu_count= 0;
422
int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
423
const pthread_mutexattr_t *attr)
425
if ((cpu_count > 1) && (attr == MY_MUTEX_INIT_FAST))
426
mp->spins= MY_PTHREAD_FASTMUTEX_SPINS;
429
return pthread_mutex_init(&mp->mutex, attr);
432
int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp)
436
uint32_t maxdelay= MY_PTHREAD_FASTMUTEX_DELAY;
438
for (i= 0; i < mp->spins; i++)
440
res= pthread_mutex_trylock(&mp->mutex);
448
mutex_delay(maxdelay);
449
maxdelay += ((double) random() / (double) RAND_MAX) *
450
MY_PTHREAD_FASTMUTEX_DELAY + 1;
452
return pthread_mutex_lock(&mp->mutex);
456
void fastmutex_global_init(void)
458
#ifdef _SC_NPROCESSORS_CONF
459
cpu_count= sysconf(_SC_NPROCESSORS_CONF);
463
#endif /* defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */