* src/threads/posix/mutex-posix.hpp (Mutex): Added _attr.
[cacao.git] / src / threads / posix / mutex-posix.hpp
index 98adf5756b0446e102903d65ab7a21ecfd3224d5..db7b2d52840cd0246d8f38a1e79d616706b61cd9 100644 (file)
@@ -2,6 +2,7 @@
 
    Copyright (C) 2008
    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
+   Copyright (C) 2008 Theobroma Systems Ltd.
 
    This file is part of CACAO.
 
@@ -30,7 +31,6 @@
 
 #include <pthread.h>
 
-#include "vm/vm.hpp"
 
 #ifdef __cplusplus
 
 class Mutex {
 private:
        // POSIX mutex structure.
-       pthread_mutex_t _mutex;
+       pthread_mutex_t     _mutex;
+       pthread_mutexattr_t _attr;
 
        // Condition class needs to access _mutex for wait() and
        // timedwait().
        friend class Condition;
        
 public:
-       Mutex();
-       ~Mutex();
+       inline Mutex();
+       inline ~Mutex();
 
-       void lock();
-       void unlock();
+       inline void lock();
+       inline void unlock();
 };
 
+#endif
+
+
+// Includes.
+#include "vm/vm.hpp"
+
+
+#ifdef __cplusplus
 
 /**
  * Initializes the given mutex object and checks for errors.
  */
 inline Mutex::Mutex()
 {
-       int result;
+       int result = pthread_mutexattr_init(&_attr);
+
+       if (result != 0) {
+               vm_abort_errnum(result, "Mutex::Mutex(): pthread_mutexattr_init failed");
+       }
+
+       result = pthread_mutexattr_settype(&_attr, PTHREAD_MUTEX_RECURSIVE);
+
+       if (result != 0) {
+               vm_abort_errnum(result, "Mutex::Mutex(): pthread_mutexattr_settype failed");
+       }
 
-       result = pthread_mutex_init(&_mutex, NULL);
+       result = pthread_mutex_init(&_mutex, &_attr);
 
-       if (result != 0)
+       if (result != 0) {
                vm_abort_errnum(result, "Mutex::Mutex(): pthread_mutex_init failed");
+       }
 }
 
 
@@ -74,12 +94,17 @@ inline Mutex::Mutex()
  */
 inline Mutex::~Mutex()
 {
-       int result;
+       int result = pthread_mutexattr_destroy(&_attr);
+
+       if (result != 0) {
+               vm_abort_errnum(result, "Mutex::~Mutex(): pthread_mutexattr_destroy failed");
+       }
 
        result = pthread_mutex_destroy(&_mutex);
 
-       if (result != 0)
+       if (result != 0) {
                vm_abort_errnum(result, "Mutex::~Mutex(): pthread_mutex_destroy failed");
+       }
 }
 
 
@@ -94,12 +119,11 @@ inline Mutex::~Mutex()
  */
 inline void Mutex::lock()
 {
-       int result;
-
-       result = pthread_mutex_lock(&_mutex);
+       int result = pthread_mutex_lock(&_mutex);
 
-       if (result != 0)
+       if (result != 0) {
                vm_abort_errnum(result, "Mutex::lock(): pthread_mutex_lock failed");
+       }
 }
 
 
@@ -109,12 +133,11 @@ inline void Mutex::lock()
  */
 inline void Mutex::unlock()
 {
-       int result;
-
-       result = pthread_mutex_unlock(&_mutex);
+       int result = pthread_mutex_unlock(&_mutex);
 
-       if (result != 0)
+       if (result != 0) {
                vm_abort_errnum(result, "Mutex::unlock: pthread_mutex_unlock failed");
+       }
 }
 
 #else