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-7.0.0~1869 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=a920fc22d18b8aaf9539f28309f1841ebbced353;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 bbbce8e34a..e95c92c24f 100644 --- a/bacula/src/lib/lockmgr.c +++ b/bacula/src/lib/lockmgr.c @@ -823,6 +823,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 * @@ -1192,7 +1211,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 64ccbd31fe..a0bd869492 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 */