blob: 25afb9d19b22db24dc44f39e93490c0107afd4a8 (
plain)
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
|
#pragma once
#include "../std/target_os.hpp"
#if defined(OMIM_OS_BADA)
#include <FBaseRtThreadMutex.h>
#elif 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_BADA)
Osp::Base::Runtime::Mutex m_Mutex;
#elif 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_BADA)
m_Mutex.Create();
#elif 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);
#elif !defined(OMIM_OS_BADA)
::pthread_mutex_destroy(&m_Mutex);
#endif
}
void Lock()
{
#if defined(OMIM_OS_BADA)
m_Mutex.Acquire();
#elif defined(OMIM_OS_WINDOWS_NATIVE)
::EnterCriticalSection(&m_Mutex);
#else
::pthread_mutex_lock(&m_Mutex);
#endif
}
void Unlock()
{
#if defined(OMIM_OS_BADA)
m_Mutex.Release();
#elif defined(OMIM_OS_WINDOWS_NATIVE)
::LeaveCriticalSection(&m_Mutex);
#else
::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
|