summaryrefslogtreecommitdiff
path: root/mysys/thr_mutex.c
diff options
context:
space:
mode:
Diffstat (limited to 'mysys/thr_mutex.c')
-rw-r--r--mysys/thr_mutex.c54
1 files changed, 26 insertions, 28 deletions
diff --git a/mysys/thr_mutex.c b/mysys/thr_mutex.c
index 9421a3b2d98..86a7ce9684b 100644
--- a/mysys/thr_mutex.c
+++ b/mysys/thr_mutex.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc
+/* Copyright (C) 2000-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -19,12 +19,12 @@
#if defined(TARGET_OS_LINUX) && !defined (__USE_UNIX98)
#define __USE_UNIX98 /* To get rw locks under Linux */
#endif
+
#ifdef SAFE_MUTEX
#define SAFE_MUTEX_DEFINED
+#undef SAFE_MUTEX /* Avoid safe_mutex redefinitions */
#endif
-#if defined(THREAD)
-#undef SAFE_MUTEX /* Avoid safe_mutex redefinitions */
#include "mysys_priv.h"
#include "my_static.h"
#include <m_string.h>
@@ -52,6 +52,7 @@ pthread_mutexattr_t my_fast_mutexattr;
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
pthread_mutexattr_t my_errorcheck_mutexattr;
#endif
+
#ifdef SAFE_MUTEX_DEFINED
static pthread_mutex_t THR_LOCK_mutex;
static ulong safe_mutex_count= 0; /* Number of mutexes created */
@@ -170,12 +171,12 @@ static int safe_mutex_lazy_init_deadlock_detection(safe_mutex_t *mp)
pthread_mutex_lock(&THR_LOCK_mutex);
mp->id= ++safe_mutex_id;
pthread_mutex_unlock(&THR_LOCK_mutex);
- hash_init2(mp->locked_mutex, 64, &my_charset_bin,
+ my_hash_init2(mp->locked_mutex, 64, &my_charset_bin,
128,
offsetof(safe_mutex_deadlock_t, id),
sizeof(mp->id),
0, 0, HASH_UNIQUE);
- hash_init2(mp->used_mutex, 64, &my_charset_bin,
+ my_hash_init2(mp->used_mutex, 64, &my_charset_bin,
128,
offsetof(safe_mutex_t, id),
sizeof(mp->id),
@@ -185,10 +186,7 @@ static int safe_mutex_lazy_init_deadlock_detection(safe_mutex_t *mp)
int safe_mutex_init(safe_mutex_t *mp,
const pthread_mutexattr_t *attr __attribute__((unused)),
- const char *name,
- myf my_flags,
- const char *file,
- uint line)
+ const char *name, const char *file, uint line)
{
DBUG_ENTER("safe_mutex_init");
DBUG_PRINT("enter",("mutex: 0x%lx name: %s", (ulong) mp, name));
@@ -201,11 +199,9 @@ int safe_mutex_init(safe_mutex_t *mp,
/* Skip the very common '&' prefix from the autogenerated name */
mp->name= name[0] == '&' ? name + 1 : name;
- if (!safe_mutex_deadlock_detector)
- my_flags|= MYF_NO_DEADLOCK_DETECTION;
/* Deadlock detection is initialised only lazily, on first use. */
- mp->create_flags= my_flags;
+ mp->create_flags= safe_mutex_deadlock_detector ? MYF_NO_DEADLOCK_DETECTION : 0;
#ifdef SAFE_MUTEX_DETECT_DESTROY
/*
@@ -229,7 +225,9 @@ int safe_mutex_init(safe_mutex_t *mp,
pthread_mutex_unlock(&THR_LOCK_mutex);
}
#else
- thread_safe_increment(safe_mutex_count, &THR_LOCK_mutex);
+ pthread_mutex_lock(&THR_LOCK_mutex);
+ safe_mutex_count++;
+ pthread_mutex_unlock(&THR_LOCK_mutex);
#endif /* SAFE_MUTEX_DETECT_DESTROY */
DBUG_RETURN(0);
}
@@ -339,7 +337,7 @@ int safe_mutex_lock(safe_mutex_t *mp, myf my_flags, const char *file,
*/
pthread_mutex_lock(&THR_LOCK_mutex);
- if (!hash_search(mutex_root->locked_mutex, (uchar*) &mp->id, 0))
+ if (!my_hash_search(mutex_root->locked_mutex, (uchar*) &mp->id, 0))
{
safe_mutex_deadlock_t *deadlock;
safe_mutex_t *mutex;
@@ -359,7 +357,7 @@ int safe_mutex_lock(safe_mutex_t *mp, myf my_flags, const char *file,
mutex= mutex_root;
do
{
- if (hash_search(mp->locked_mutex, (uchar*) &mutex->id, 0))
+ if (my_hash_search(mp->locked_mutex, (uchar*) &mutex->id, 0))
{
print_deadlock_warning(mp, mutex);
/* Mark wrong usage to avoid future warnings for same error */
@@ -527,8 +525,8 @@ int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp, const char *file,
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
- const struct timespec *abstime,
- const char *file, uint line)
+ const struct timespec *abstime,
+ const char *file, uint line)
{
int error;
safe_mutex_t save_state;
@@ -637,7 +635,9 @@ int safe_mutex_destroy(safe_mutex_t *mp, const char *file, uint line)
mp->info= NULL; /* Get crash if double free */
}
#else
- thread_safe_sub(safe_mutex_count, 1, &THR_LOCK_mutex);
+ pthread_mutex_lock(&THR_LOCK_mutex);
+ safe_mutex_count--;
+ pthread_mutex_unlock(&THR_LOCK_mutex);
#endif /* SAFE_MUTEX_DETECT_DESTROY */
DBUG_RETURN(error);
}
@@ -664,9 +664,9 @@ void safe_mutex_free_deadlock_data(safe_mutex_t *mp)
mp);
pthread_mutex_unlock(&THR_LOCK_mutex);
- hash_free(mp->used_mutex);
- hash_free(mp->locked_mutex);
- my_free(mp->locked_mutex, 0);
+ my_hash_free(mp->used_mutex);
+ my_hash_free(mp->locked_mutex);
+ my_free(mp->locked_mutex);
mp->create_flags|= MYF_NO_DEADLOCK_DETECTION;
}
}
@@ -711,7 +711,6 @@ void safe_mutex_end(FILE *file __attribute__((unused)))
#endif /* SAFE_MUTEX_DETECT_DESTROY */
}
-
static my_bool add_used_to_locked_mutex(safe_mutex_t *used_mutex,
safe_mutex_deadlock_t *locked_mutex)
{
@@ -774,17 +773,17 @@ static my_bool remove_from_locked_mutex(safe_mutex_t *mp,
(ulong) delete_mutex, (ulong) mp,
delete_mutex->id, mp->id));
- found= (safe_mutex_deadlock_t *) hash_search(mp->locked_mutex,
+ found= (safe_mutex_deadlock_t *) my_hash_search(mp->locked_mutex,
(uchar*) &delete_mutex->id, 0);
DBUG_ASSERT(found);
if (found)
{
- if (hash_delete(mp->locked_mutex, (uchar*) found))
+ if (my_hash_delete(mp->locked_mutex, (uchar*) found))
{
DBUG_ASSERT(0);
}
if (!--found->count)
- my_free(found, MYF(0));
+ my_free(found);
}
DBUG_RETURN(0);
}
@@ -796,12 +795,12 @@ static my_bool remove_from_used_mutex(safe_mutex_deadlock_t *locked_mutex,
DBUG_PRINT("enter", ("delete_mutex: 0x%lx mutex: 0x%lx (id: %lu <- %lu)",
(ulong) mutex, (ulong) locked_mutex,
mutex->id, locked_mutex->id));
- if (hash_delete(locked_mutex->mutex->used_mutex, (uchar*) mutex))
+ if (my_hash_delete(locked_mutex->mutex->used_mutex, (uchar*) mutex))
{
DBUG_ASSERT(0);
}
if (!--locked_mutex->count)
- my_free(locked_mutex, MYF(0));
+ my_free(locked_mutex);
DBUG_RETURN(0);
}
@@ -924,4 +923,3 @@ void fastmutex_global_init(void)
}
#endif /* defined(MY_PTHREAD_FASTMUTEX) */
-#endif /* THREAD */