Copyright (C) 2008
CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
+ Copyright (C) 2008 Theobroma Systems Ltd.
This file is part of CACAO.
#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");
+ }
}
*/
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");
+ }
}
*/
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");
+ }
}
*/
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