[Asterisk-Dev] FreeBSD recursive mutexes - please help

Markus Wild mw at kpnqwest.ch
Tue May 4 14:28:43 MST 2004


> Just use PTHREAD_MUTEX_STATIC_INITIALIZER in FreeBSD.  That's
> the initializer that was used in the original stdtime/localtime.c
> which came out of FreeBSD!  I had to change the mutexes for Asterisk,
> because at the time, Asterisk was using non-recursive mutexes.  This
> mutex for FreeBSD should therefore already be recursive.

Uhm... you mean this one from <pthread.h> ?
#define PTHREAD_MUTEX_INITIALIZER       NULL

The problem with this is: yes, it will initialize your mutex so you
can start to use it. No, it will not statically preset it to be 
of PTHREAD_MUTEX_RECURSIVE type, but of PTHREAD_MUTEX_ERRORCHECK. Check
/usr/src/lib/libpthread/thread/thr_mutex.c:_pthread_mutex_init()

To properly solve this, we'll probably have to roll our own type
that will allow for static initialization including type. We could
probably also take care to actually implement recursive mutexes in
there for OS X.. (very similar approach to what is currently enabled
with -DDEBUG_THREADS).

To get it done quicker (not for OS X), the following will work for 
the case where _only_  recursive mutexes are used, relying on 
pthread_mutex_t being a pointer:

--- include/asterisk/lock.h     22 Apr 2004 00:20:34 -0000      1.16
+++ include/asterisk/lock.h     4 May 2004 21:22:12 -0000
@@ -79,6 +107,9 @@
 
 static inline int __ast_pthread_mutex_lock(char *filename, int lineno, char *func, ast_mutex_t *t) {
        int res;
+#ifdef __FreeBSD__
+       if (! t->mutex) ast_mutex_init(t);
+#endif
        res = pthread_mutex_lock(&t->mutex);
        if (!res) {
                t->file = filename;
@@ -99,6 +130,9 @@
 
 static inline int __ast_pthread_mutex_trylock(char *filename, int lineno, char *func, ast_mutex_t *t) {
        int res;
+#ifdef __FreeBSD__
+       if (! t->mutex) ast_mutex_init(t);
+#endif
        res = pthread_mutex_trylock(&t->mutex);
        if (!res) {
                t->file = filename;

Note: you need to have the optional

# Optional debugging parameters
DEBUG_THREADS = -DDEBUG_THREADS -DDO_CRASH 

enabled in your Makefile for this to work.

Cheers,
Markus



More information about the asterisk-dev mailing list