|
a/src/utils/ptmutex.h |
|
b/src/utils/ptmutex.h |
|
... |
|
... |
20 |
#include <pthread.h>
|
20 |
#include <pthread.h>
|
21 |
|
21 |
|
22 |
/// A trivial wrapper/helper for pthread mutex locks
|
22 |
/// A trivial wrapper/helper for pthread mutex locks
|
23 |
|
23 |
|
24 |
|
24 |
|
25 |
/// Init lock. Used as a single PTMutexInit static object.
|
25 |
/// Lock storage with auto-initialization. Must be created before any
|
|
|
26 |
/// lock-using thread of course (possibly as a static object).
|
26 |
class PTMutexInit {
|
27 |
class PTMutexInit {
|
27 |
public:
|
28 |
public:
|
28 |
pthread_mutex_t m_mutex;
|
29 |
pthread_mutex_t m_mutex;
|
29 |
PTMutexInit()
|
30 |
PTMutexInit()
|
30 |
{
|
31 |
{
|
31 |
pthread_mutex_init(&m_mutex, 0);
|
32 |
pthread_mutex_init(&m_mutex, 0);
|
32 |
}
|
33 |
}
|
33 |
};
|
34 |
};
|
34 |
|
35 |
|
35 |
/// Take the lock when constructed, release when deleted
|
36 |
/// Take the lock when constructed, release when deleted. Can be disabled
|
|
|
37 |
/// by constructor params for conditional use.
|
36 |
class PTMutexLocker {
|
38 |
class PTMutexLocker {
|
37 |
public:
|
39 |
public:
|
|
|
40 |
// The nolock arg enables conditional locking
|
38 |
PTMutexLocker(PTMutexInit& l) : m_lock(l)
|
41 |
PTMutexLocker(PTMutexInit& l, bool nolock = false)
|
|
|
42 |
: m_lock(l), m_status(-1)
|
39 |
{
|
43 |
{
|
|
|
44 |
if (!nolock)
|
40 |
m_status = pthread_mutex_lock(&m_lock.m_mutex);
|
45 |
m_status = pthread_mutex_lock(&m_lock.m_mutex);
|
41 |
}
|
46 |
}
|
42 |
~PTMutexLocker()
|
47 |
~PTMutexLocker()
|
43 |
{
|
48 |
{
|
|
|
49 |
if (m_status == 0)
|
44 |
pthread_mutex_unlock(&m_lock.m_mutex);
|
50 |
pthread_mutex_unlock(&m_lock.m_mutex);
|
45 |
}
|
51 |
}
|
46 |
int ok() {return m_status == 0;}
|
52 |
int ok() {return m_status == 0;}
|
47 |
private:
|
53 |
private:
|
48 |
PTMutexInit& m_lock;
|
54 |
PTMutexInit& m_lock;
|
49 |
int m_status;
|
55 |
int m_status;
|