-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConcurrency.cpp
More file actions
193 lines (157 loc) · 3.8 KB
/
Copy pathConcurrency.cpp
File metadata and controls
193 lines (157 loc) · 3.8 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <SFNUL/Concurrency.hpp>
#include <SFNUL/ConfigInternal.hpp>
#include <SFNUL/MakeUnique.hpp>
#if defined( SFNUL_USE_STD_THREAD )
#include <thread>
#include <mutex>
#elif defined( SFNUL_WIN32_THREADS )
#include <windows.h>
#include <process.h>
#elif defined( SFNUL_PTHREADS )
#include <pthread.h>
#else
#error No supported threading facility available. Review CMake configuration for more information.
#endif
/// @cond
namespace sfn {
#if defined( __GNUG__ )
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
#if !defined( SFNUL_USE_STD_THREAD )
#if defined( SFNUL_WIN32_THREADS )
unsigned __stdcall run( void* data );
#elif defined( SFNUL_PTHREADS )
void* run( void* data );
#endif
#endif
class ThreadImpl {
public:
#if defined( SFNUL_USE_STD_THREAD )
ThreadImpl( std::function<void()> thread_function ) :
thread{ thread_function }
{
}
~ThreadImpl() {
thread.join();
}
mutable std::thread thread;
#elif defined( SFNUL_WIN32_THREADS )
ThreadImpl( std::function<void()> thread_function ) :
function{ thread_function }
{
thread = reinterpret_cast<HANDLE>( _beginthreadex( nullptr, 0, &run, this, 0, nullptr ) );
}
~ThreadImpl() {
if( thread ) {
WaitForSingleObject( thread, INFINITE );
CloseHandle( thread );
}
}
mutable HANDLE thread{};
#elif defined( SFNUL_PTHREADS )
ThreadImpl( std::function<void()> thread_function ) :
function{ thread_function }
{
pthread_create( &thread, nullptr, &run, this );
}
~ThreadImpl() {
pthread_join( thread, nullptr );
}
mutable pthread_t thread{};
#endif
std::function<void()> function;
};
#if !defined( SFNUL_USE_STD_THREAD )
#if defined( SFNUL_WIN32_THREADS )
unsigned __stdcall run( void* data ) {
static_cast<sfn::ThreadImpl*>( data )->function();
_endthread();
return 0;
}
#elif defined( SFNUL_PTHREADS )
void* run( void* data ) {
static_cast<sfn::ThreadImpl*>( data )->function();
return nullptr;
}
#endif
#endif
Thread::Thread( std::function<void()> thread_function ) :
m_impl{ make_unique<ThreadImpl>( thread_function ) }
{
}
Thread::~Thread() = default;
class Atomic::AtomicImpl {
public:
#if defined( SFNUL_USE_STD_THREAD )
void Lock() const {
mutex.lock();
}
void Unlock() const {
mutex.unlock();
}
mutable std::recursive_mutex mutex;
#elif defined( SFNUL_WIN32_THREADS )
AtomicImpl() {
InitializeCriticalSection( &mutex );
}
~AtomicImpl() {
DeleteCriticalSection( &mutex );
}
void Lock() const {
EnterCriticalSection( &mutex );
}
void Unlock() const {
LeaveCriticalSection( &mutex );
}
mutable CRITICAL_SECTION mutex;
#elif defined( SFNUL_PTHREADS )
AtomicImpl() {
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init( &mutex_attr );
pthread_mutexattr_settype( &mutex_attr, PTHREAD_MUTEX_RECURSIVE );
pthread_mutex_init( &mutex, &mutex_attr );
pthread_mutexattr_destroy( &mutex_attr );
}
~AtomicImpl() {
pthread_mutex_destroy( &mutex );
}
void Lock() const {
pthread_mutex_lock( &mutex );
}
void Unlock() const {
pthread_mutex_unlock( &mutex );
}
mutable pthread_mutex_t mutex;
#endif
};
Atomic::Atomic() :
m_impl{ make_unique<AtomicImpl>() }
{
}
Atomic::~Atomic() = default;
void Atomic::LockMutex() const {
m_impl->Lock();
}
void Atomic::UnlockMutex() const {
m_impl->Unlock();
}
Atomic::ScopedLock::ScopedLock( Atomic* atomic ) :
m_atomic{ atomic }
{
m_atomic->LockMutex();
}
Atomic::ScopedLock::~ScopedLock() {
m_atomic->UnlockMutex();
}
Atomic::ScopedLock Atomic::AcquireLock() const {
return ScopedLock{ const_cast<Atomic*>( this ) };
}
#if defined( __GNUG__ )
#pragma GCC diagnostic pop
#endif
}
/// @endcond