55
54
int safe_mutex_init(safe_mutex_t *mp,
56
const pthread_mutexattr_t *attr __attribute__((unused)),
55
const pthread_mutexattr_t *, const char *file, uint32_t line)
60
57
memset(mp, 0, sizeof(*mp));
61
58
pthread_mutex_init(&mp->global,MY_MUTEX_INIT_ERRCHK);
95
int safe_mutex_lock(safe_mutex_t *mp, bool try_lock, const char *file, uint line)
92
int safe_mutex_lock(safe_mutex_t *mp, bool try_lock, const char *file, uint32_t line)
174
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line)
171
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint32_t line)
177
174
pthread_mutex_lock(&mp->global);
257
254
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
258
255
struct timespec *abstime,
259
const char *file, uint line)
256
const char *file, uint32_t line)
262
259
pthread_mutex_lock(&mp->global);
354
351
This is ok, as this thread may not yet have been exited.
357
void safe_mutex_end(FILE *file __attribute__((unused)))
354
void safe_mutex_end(int *)
359
356
if (!safe_mutex_count) /* safetly */
360
357
pthread_mutex_destroy(&THR_LOCK_mutex);
382
379
#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>
392
#include <myisampack.h>
393
#include <mysys_err.h>
396
#undef pthread_mutex_t
397
#undef pthread_mutex_init
398
#undef pthread_mutex_lock
399
#undef pthread_mutex_trylock
400
#undef pthread_mutex_unlock
401
#undef pthread_mutex_destroy
402
#undef pthread_cond_wait
403
#undef pthread_cond_timedwait
405
uint32_t mutex_delay(uint32_t delayloops)
412
for (i = 0; i < delayloops * 50; i++)
418
#define MY_PTHREAD_FASTMUTEX_SPINS 8
419
#define MY_PTHREAD_FASTMUTEX_DELAY 4
421
static int cpu_count= 0;
423
int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
424
const pthread_mutexattr_t *attr)
426
if ((cpu_count > 1) && (attr == MY_MUTEX_INIT_FAST))
427
mp->spins= MY_PTHREAD_FASTMUTEX_SPINS;
430
return pthread_mutex_init(&mp->mutex, attr);
433
int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp)
437
uint maxdelay= MY_PTHREAD_FASTMUTEX_DELAY;
439
for (i= 0; i < mp->spins; i++)
441
res= pthread_mutex_trylock(&mp->mutex);
449
mutex_delay(maxdelay);
450
maxdelay += ((double) random() / (double) RAND_MAX) *
451
MY_PTHREAD_FASTMUTEX_DELAY + 1;
453
return pthread_mutex_lock(&mp->mutex);
457
void fastmutex_global_init(void)
459
#ifdef _SC_NPROCESSORS_CONF
460
cpu_count= sysconf(_SC_NPROCESSORS_CONF);
464
#endif /* defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */