6 Mutex::Mutex(char *title, int recursive)
9 pthread_mutexattr_t attr;
10 pthread_mutexattr_init(&attr);
11 pthread_mutex_init(&mutex, &attr);
12 pthread_mutex_init(&recursive_lock, &attr);
14 this->recursive = recursive;
21 pthread_mutex_destroy(&mutex);
22 pthread_mutex_destroy(&recursive_lock);
24 UNSET_ALL_LOCKS(this);
28 int Mutex::lock(char *location)
30 // Test recursive owner and give up if we already own it
33 pthread_mutex_lock(&recursive_lock);
34 if(thread_id_valid && pthread_self() == thread_id)
37 pthread_mutex_unlock(&recursive_lock);
40 pthread_mutex_unlock(&recursive_lock);
45 SET_LOCK(this, title, location);
47 if(pthread_mutex_lock(&mutex)) perror("Mutex::lock");
51 // Update recursive status for the first lock
54 pthread_mutex_lock(&recursive_lock);
56 thread_id = pthread_self();
58 pthread_mutex_unlock(&recursive_lock);
74 // Remove from recursive status
77 pthread_mutex_lock(&recursive_lock);
82 pthread_mutex_unlock(&recursive_lock);
88 pthread_mutex_unlock(&recursive_lock);
98 if(pthread_mutex_unlock(&mutex)) perror("Mutex::unlock");
104 return pthread_mutex_trylock(&mutex);
107 int Mutex::is_locked()
114 pthread_mutex_destroy(&mutex);
115 pthread_mutexattr_t attr;
116 pthread_mutexattr_init(&attr);
117 pthread_mutex_init(&mutex, &attr);
122 UNSET_ALL_LOCKS(this)