2 This file is part of libmspframework
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
6 #ifndef MSP_FRAMEWORK_MUTEX_H_
7 #define MSP_FRAMEWORK_MUTEX_H_
10 #include <msp/refcount.h>
17 Mutex() { pthread_mutex_init(&mutex, 0); }
18 int lock() { return pthread_mutex_lock(&mutex); }
19 int trylock() { return pthread_mutex_trylock(&mutex); }
20 int unlock() { return pthread_mutex_unlock(&mutex); }
21 ~Mutex() { pthread_mutex_destroy(&mutex); }
23 pthread_mutex_t mutex;
25 friend class Semaphore;
31 MutexLock(Mutex &m): mutex(m) { mutex.lock(); }
32 ~MutexLock() { mutex.unlock(); }
39 class MutexPtr: public RefCount
42 MutexPtr(T *d, Mutex &m): mutex(m), data(d) { mutex.lock(); }
43 MutexPtr(const MutexPtr<T> &p): RefCount(p), mutex(p.mutex), data(p.data) { }
44 T &operator*() const { return *data; }
45 T *operator->() const { return data; }
46 void clear() { decref(); data=0; }
47 ~MutexPtr() { decref(); }
54 if(!RefCount::decref())