62
62
Mutex(const char *n, bool r = false, bool ld=true, bool bt=false) :
63
63
name(n), id(-1), recursive(r), lockdep(ld), backtrace(bt), nlock(0) {
65
// Mutexes of type PTHREAD_MUTEX_RECURSIVE do all the same checks as
66
// mutexes of type PTHREAD_MUTEX_ERRORCHECK.
65
67
pthread_mutexattr_t attr;
66
68
pthread_mutexattr_init(&attr);
67
69
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
68
70
pthread_mutex_init(&_m,&attr);
69
71
pthread_mutexattr_destroy(&attr);
76
// If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking
77
// shall be provided. If a thread attempts to relock a mutex that it
78
// has already locked, an error shall be returned. If a thread
79
// attempts to unlock a mutex that it has not locked or a mutex which
80
// is unlocked, an error shall be returned.
81
pthread_mutexattr_t attr;
82
pthread_mutexattr_init(&attr);
83
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
84
pthread_mutex_init(&_m, &attr);
89
// If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection
90
// shall not be provided. Attempting to relock the mutex causes
91
// deadlock. If a thread attempts to unlock a mutex that it has not
92
// locked or a mutex which is unlocked, undefined behavior results.
71
93
pthread_mutex_init(&_m, NULL);
73
if (lockdep && g_lockdep) _register();
76
97
assert(nlock == 0);