1 #ifndef MSP_CORE_MUTEX_H_
2 #define MSP_CORE_MUTEX_H_
9 A class for controlling mutually exclusive access to a resource. Only one
10 thread can hold a lock on the mutex at a time.
14 friend class Semaphore;
25 /** Locks the mutex. If the mutex is already locked, waits until it becomes
29 /** Attempts to lock the mutex. Returns true if the lock was acquired,
30 otherwise returns false. */
33 /** Unlocks the mutex. */
38 Locks the mutex for te lifetime of the object.
46 MutexLock(Mutex &m, bool l = true): mutex(m) { if(l) mutex.lock(); }
47 ~MutexLock() { mutex.unlock(); }
49 void lock() { mutex.lock(); }
51 MutexLock(const MutexLock &);
52 MutexLock &operator=(const MutexLock &);
56 Protects a pointer with a mutex. As long as the MutexPtr (or a copy of it)
57 exists, the mutex will stay locked.
63 MutexPtr(T *d, Mutex &m): mutex(new MutexLock(m)), data(d) { }
65 T &operator*() const { return *data; }
66 T *operator->() const { return data; }
67 void clear() { mutex=0; data = 0; }
69 RefPtr<MutexLock> mutex;
73 /*template<typename T>
74 class MutexPtr: public RefCount
77 MutexPtr(T *d, Mutex &m): mutex(m), data(d) { mutex.lock(); }
78 MutexPtr(const MutexPtr<T> &p): RefCount(p), mutex(p.mutex), data(p.data) { }
79 T &operator*() const { return *data; }
80 T *operator->() const { return data; }
81 void clear() { decref(); data = 0; }
82 ~MutexPtr() { decref(); }
89 if(!RefCount::decref())