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 the lifetime of the object.
45 MutexLock(const MutexLock &);
47 MutexLock(Mutex &m, bool l = true): mutex(m) { if(l) mutex.lock(); }
48 ~MutexLock() { mutex.unlock(); }
50 void lock() { mutex.lock(); }
54 Protects a pointer with a mutex. As long as the MutexPtr (or a copy of it)
55 exists, the mutex will stay locked.
61 RefPtr<MutexLock> mutex;
65 MutexPtr(T *d, Mutex &m): mutex(new MutexLock(m)), data(d) { }
67 T &operator*() const { return *data; }
68 T *operator->() const { return data; }
69 void clear() { mutex = 0; data = 0; }