[Asterisk-Dev] ast_mutex_lock: How to test?

Matthew Boehm mboehm at cytelcom.com
Thu Oct 21 12:01:50 MST 2004


I'm trying to get some mutex lock statuses. Here is the code im using.

/usr/src/locktest/lock.c

#include <asterisk/lock.h>
#include <pthread.h>

AST_MUTEX_DEFINE_STATIC(lock);

int main(int argc, char **argv)
{
        int res;

        printf("Locking fresh...");
        res = ast_mutex_lock(&lock);
        printf("%i\n",res);

        printf("Locking locked...");
        res = ast_mutex_lock(&lock);
        printf("%i\n",res);

        printf("Locking locked...");
        res = ast_mutex_lock(&lock);
        printf("%i\n",res);

        printf("TryLocking locked...");
        res = ast_mutex_trylock(&lock);
        printf("%i\n",res);

        printf("Unlocking locked..");
        res = ast_mutex_unlock(&lock);
        printf("%i\n",res);

        printf("Unlocking unlocked..");
        res = ast_mutex_unlock(&lock);
        printf("%i\n",res);

        return 0;
}

compile with the following line:

gcc -pipe  -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarat
ions -g  -I../asterisk -I../asterisk/include -D_REENTRANT -D_GNU_SOURCE  -O6
 -march=i686 -DDEBUG_THREADS -o lock lock.c -lpthread

It compiles fine. And I get the following output:

[root at dev-asterisk locktest]# ./lock
Locking fresh...0
Locking locked...0
Locking locked...0
TryLocking locked...0
Unlocking locked..0
Unlocking unlocked..0

?? Is that right? If so, what is the correct way for the same thread to test
if there is a lock already?

I would expect that attempting to lock an already locked mutex would return
something other than SUCCESS.

Thanks,
-Matthew




More information about the asterisk-dev mailing list