-
Notifications
You must be signed in to change notification settings - Fork 0
/
Concurrency.cpp
162 lines (158 loc) · 3.21 KB
/
Concurrency.cpp
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
#include "Concurrency.h"
#include <stdexcept>
#ifdef _WIN32
#include <process.h>
#else
#include <sys/time.h>
#endif
namespace dtglib
{
#ifdef _WIN32
unsigned __stdcall C_Thread::M_ThreadInit(void* args)
{
C_Thread::C_Data* d=(C_Thread::C_Data*)args;
d->m_Func(d->m_Arg);
return 0;
}
C_Thread::C_Thread(t_ThreadFunc f, void* args) : m_Thread(0), m_Data(C_Data(f, args))
{
m_Thread=(HANDLE)_beginthreadex(NULL,0,M_ThreadInit,&m_Data,0,NULL);
if(!m_Thread)
{
throw std::runtime_error("Error creating a thread.");
}
}
#else
void* C_Thread::M_ThreadInit(void* args)
{
C_Thread::C_Data* d=(C_Thread::C_Data*)args;
d->m_Func(d->m_Arg);
return NULL;
}
C_Thread::C_Thread(t_ThreadFunc f, void* args) : m_Thread(0), m_Data(C_Data(f, args))
{
int ret=pthread_create(&m_Thread, NULL, M_ThreadInit, &m_Data);
if(ret)
{
throw std::runtime_error("Error creating a thread.");
}
}
void C_Thread::M_Start(t_ThreadFunc f, void* args)
{
m_Thread=0;
m_Data=C_Data(f, args);
int ret=pthread_create(&m_Thread, NULL, M_ThreadInit, &m_Data);
if(ret)
{
throw std::runtime_error("Error creating a thread.");
}
}
#endif
void C_Thread::M_Join()
{
#ifdef _WIN32
WaitForSingleObject(m_Thread, INFINITE);
#else
pthread_join(m_Thread, NULL);
#endif
}
C_Thread::~C_Thread()
{
#ifdef _WIN32
if(m_Thread)
{
M_Join();
CloseHandle(m_Thread);
m_Thread=NULL;
}
#else
if(m_Thread)
{
M_Join();
pthread_detach(m_Thread);
m_Thread=0;
}
#endif
}
C_Mutex::C_Mutex()
{
#ifdef _WIN32
InitializeCriticalSection(&m_Mutex);
#else
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&m_Mutex, &attr);
pthread_mutexattr_destroy(&attr);
#endif
}
C_Mutex::~C_Mutex()
{
#ifdef _WIN32
DeleteCriticalSection(&m_Mutex);
#else
pthread_mutex_destroy(&m_Mutex);
#endif
}
void C_Mutex::M_Lock()
{
#ifdef _WIN32
EnterCriticalSection(&m_Mutex);
#else
pthread_mutex_lock(&m_Mutex);
#endif
}
void C_Mutex::M_Unlock()
{
#ifdef _WIN32
LeaveCriticalSection(&m_Mutex);
#else
pthread_mutex_unlock(&m_Mutex);
#endif
}
C_CondVar::C_CondVar()
{
#ifdef _WIN32
InitializeConditionVariable(&m_Cond);
#else
pthread_cond_init(&m_Cond,NULL);
#endif
}
void C_CondVar::M_Wait(uint timeoutms)
{
#ifdef _WIN32
m_Mutex.M_Lock();
SleepConditionVariableCS(&m_Cond, &m_Mutex.m_Mutex, timeoutms==~0U?INFINITE:timeoutms);
m_Mutex.M_Unlock();
#else
m_Mutex.M_Lock();
if(timeoutms==~0U) pthread_cond_wait(&m_Cond, &m_Mutex.m_Mutex);
else
{
struct timeval tv;
struct timespec ts;
gettimeofday(&tv, NULL);
ts.tv_sec=tv.tv_sec+(timeoutms/1000);
ts.tv_nsec=((tv.tv_usec*1000)+((timeoutms%1000)*1000000));
pthread_cond_timedwait(&m_Cond, &m_Mutex.m_Mutex, &ts);
}
m_Mutex.M_Unlock();
#endif
}
void C_CondVar::M_SignalOne()
{
#ifdef _WIN32
WakeConditionVariable(&m_Cond);
#else
pthread_cond_signal(&m_Cond);
#endif
}
void C_CondVar::M_Signal()
{
#ifdef _WIN32
WakeAllConditionVariable(&m_Cond);
#else
pthread_cond_broadcast(&m_Cond);
#endif
}
}