ftmemsim-valgrind/drd/tests/recursive_mutex.c
Nicholas Nethercote 07045477ca Merge the DARWIN branch onto the trunk.
I tried using 'svn merge' to do the merge but it did a terrible job and
there were bazillions of conflicts.  So instead I just took the diff between
the branch and trunk  at r10155, applied the diff to the trunk, 'svn add'ed
the added files (no files needed to be 'svn remove'd) and committed.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10156
2009-05-28 01:53:07 +00:00

80 lines
1.9 KiB
C

/** Initialize several kinds of mutexes and lock each mutex twice.
* Note: locking a regular mutex twice causes a deadlock.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include "../../config.h"
#if !defined(VGO_darwin)
static void lock_twice(pthread_mutex_t* const p)
{
pthread_mutex_lock(p);
pthread_mutex_lock(p);
pthread_mutex_unlock(p);
pthread_mutex_unlock(p);
}
#endif
int main(int argc, char** argv)
{
/* Let the program abort after 3 seconds instead of leaving it deadlocked. */
alarm(3);
#if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
{
pthread_mutex_t m = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
printf("Recursive mutex (statically initialized).\n");
lock_twice(&m);
pthread_mutex_destroy(&m);
}
#endif
#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP)
{
pthread_mutex_t m;
pthread_mutexattr_t attr;
printf("Recursive mutex (initialized via mutex attributes).\n");
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(&m, &attr);
pthread_mutexattr_destroy(&attr);
lock_twice(&m);
pthread_mutex_destroy(&m);
}
#endif
#if defined(HAVE_PTHREAD_MUTEX_ERRORCHECK_NP)
{
pthread_mutex_t m;
pthread_mutexattr_t attr;
printf("Error checking mutex.\n");
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
pthread_mutex_init(&m, &attr);
pthread_mutexattr_destroy(&attr);
lock_twice(&m);
pthread_mutex_destroy(&m);
}
#endif
// DDD: Darwin doesn't support signals yet, so the alarm() call doesn't kick
// in, which causes it to hang.
#if !defined(VGO_darwin)
{
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
printf("Non-recursive mutex.\n");
fflush(stdout);
lock_twice(&m);
}
printf("Done.\n");
#endif
return 0;
}