From 7e93fcc0ac7ea68573d36882b446d700c7342929 Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Mon, 26 Apr 2010 11:39:30 +0200 Subject: [PATCH] Add lmgr_is_locked() function to test if a thread own a mutex --- bacula/src/lib/lockmgr.c | 21 +++++++++++++++++++++ bacula/src/lib/lockmgr.h | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/bacula/src/lib/lockmgr.c b/bacula/src/lib/lockmgr.c index 97e99889ef..9af26d831f 100644 --- a/bacula/src/lib/lockmgr.c +++ b/bacula/src/lib/lockmgr.c @@ -820,6 +820,25 @@ int bthread_cond_timedwait_p(pthread_cond_t *cond, return ret; } +/* Test if this mutex is locked by the current thread + * returns: + * 0 - unlocked + * 1 - locked by the current thread + * 2 - locked by an other thread + */ +int lmgr_mutex_is_locked(void *m) +{ + lmgr_thread_t *self = lmgr_get_thread_info(); + + for(int i=0; i <= self->current; i++) { + if (self->lock_list[i].lock == m) { + return 1; /* locked by us */ + } + } + + return 0; /* not locked by us */ +} + /* * Use this function when the caller handle the mutex directly * @@ -1189,7 +1208,9 @@ int main(int argc, char **argv) P(mutex4); P(mutex5); P(mutex6); + ok(lmgr_mutex_is_locked(&mutex6) == 1, "Check if mutex is locked"); V(mutex6); + ok(lmgr_mutex_is_locked(&mutex6) == 0, "Check if mutex is locked"); V(mutex5); V(mutex4); diff --git a/bacula/src/lib/lockmgr.h b/bacula/src/lib/lockmgr.h index ec5e0dcdd1..883a8c084e 100644 --- a/bacula/src/lib/lockmgr.h +++ b/bacula/src/lib/lockmgr.h @@ -86,6 +86,12 @@ int bthread_mutex_lock_p(bthread_mutex_t *m, int bthread_mutex_unlock_p(bthread_mutex_t *m, const char *file="*unknown*", int line=0); +/* Test if this mutex is locked by the current thread + * 0 - not locked by the current thread + * 1 - locked by the current thread + */ +int lmgr_mutex_is_locked(void *m); + /* * Use them when you want use your lock yourself (ie rwlock) */ @@ -208,6 +214,8 @@ int lmgr_thread_create(pthread_t *thread, # define BTHREAD_MUTEX_PRIORITY(p) PTHREAD_MUTEX_INITIALIZER # define BTHREAD_MUTEX_NO_PRIORITY PTHREAD_MUTEX_INITIALIZER # define BTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER +# define lmgr_mutex_is_locked(m) (1) + #endif /* _USE_LOCKMGR */ #endif /* _LOCKMGR_H */ -- 2.39.5