forked from mapsme/omim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.hpp
103 lines (86 loc) · 2.09 KB
/
mutex.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once
#include "base/assert.hpp"
#include "std/target_os.hpp"
#if defined(OMIM_OS_WINDOWS_NATIVE)
#include "std/windows.hpp"
#else
#include <pthread.h>
#endif
namespace threads
{
class Condition;
namespace impl
{
class ConditionImpl;
class ImplWinVista;
}
/// Mutex primitive, used only for synchronizing this process threads
/// based on Critical Section under Win32 and pthreads under Linux
/// @author Siarhei Rachytski
/// @deprecated As the MacOS implementation doesn't support recursive mutexes we should emulate them by ourselves.
/// The code is taken from @a http://www.omnigroup.com/mailman/archive/macosx-dev/2002-March/036465.html
class Mutex
{
private:
#if defined(OMIM_OS_WINDOWS_NATIVE)
CRITICAL_SECTION m_Mutex;
#else
pthread_mutex_t m_Mutex;
#endif
Mutex & operator=(Mutex const &);
Mutex(Mutex const &);
friend class threads::impl::ConditionImpl;
friend class threads::impl::ImplWinVista;
friend class threads::Condition;
public:
Mutex()
{
#if defined(OMIM_OS_WINDOWS_NATIVE)
::InitializeCriticalSection(&m_Mutex);
#else
::pthread_mutex_init(&m_Mutex, 0);
#endif
}
~Mutex()
{
#if defined(OMIM_OS_WINDOWS_NATIVE)
::DeleteCriticalSection(&m_Mutex);
#else
::pthread_mutex_destroy(&m_Mutex);
#endif
}
void Lock()
{
#if defined(OMIM_OS_WINDOWS_NATIVE)
::EnterCriticalSection(&m_Mutex);
#else
VERIFY(0 == ::pthread_mutex_lock(&m_Mutex), ());
#endif
}
bool TryLock()
{
#if defined(OMIM_OS_WINDOWS_NATIVE)
return (TRUE == ::TryEnterCriticalSection(&m_Mutex));
#else
return (0 == ::pthread_mutex_trylock(&m_Mutex));
#endif
}
void Unlock()
{
#if defined(OMIM_OS_WINDOWS_NATIVE)
::LeaveCriticalSection(&m_Mutex);
#else
VERIFY(0 == ::pthread_mutex_unlock(&m_Mutex), ());
#endif
}
};
/// ScopeGuard wrapper around mutex
class MutexGuard
{
public:
MutexGuard(Mutex & mutex): m_Mutex(mutex) { m_Mutex.Lock(); }
~MutexGuard() { m_Mutex.Unlock(); }
private:
Mutex & m_Mutex;
};
} // namespace threads