From: Eric Bollengier Date: Mon, 26 Apr 2010 09:39:30 +0000 (+0200) Subject: Add lmgr_is_locked() function to test if a thread own a mutex X-Git-Tag: Release-5.0.2~9 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=7e93fcc0ac7ea68573d36882b446d700c7342929;p=bacula%2Fbacula Add lmgr_is_locked() function to test if a thread own a mutex --- 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 */