-
Notifications
You must be signed in to change notification settings - Fork 1
/
OxsyLinuxEvent.h
94 lines (71 loc) · 1.38 KB
/
OxsyLinuxEvent.h
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
#ifndef _LINUX_EVENT_H
#define _LINUX_EVENT_H
// modified from original Tsinghuaeolus 'linux_syn.h'
#ifndef WIN32 // _LINUX
#include "pthread.h"
class OxsyLinuxEvent
{
private:
pthread_mutex_t m;
pthread_cond_t c;
bool m_bManualReset;
bool m_bSignalState;
public:
OxsyLinuxEvent()
{
}
void init(bool bManualReset, bool bInitialState)
{
m_bManualReset = bManualReset;
m_bSignalState = bInitialState;
pthread_mutex_init(&m, NULL);
pthread_cond_init(&c, NULL);
}
void destroy()
{
pthread_cond_destroy(&c);
pthread_mutex_destroy(&m);
}
void SetEvent()
{
pthread_mutex_lock(&m);
m_bSignalState = true;
pthread_mutex_unlock(&m);
pthread_cond_signal(&c);
}
void ResetEvent()
{
pthread_mutex_lock(&m);
m_bSignalState = false;
pthread_mutex_unlock(&m);
}
int WaitEvent(const struct timespec *abstime = NULL)
{
int rt = 0;
pthread_mutex_lock(&m);
if(!m_bSignalState)
{
if(abstime == NULL)
{
rt = pthread_cond_wait(&c, &m);
}
else
{
rt = pthread_cond_timedwait(&c, &m, abstime);
}
if (!m_bManualReset) m_bSignalState = false;
}
pthread_mutex_unlock(&m);
return rt;
}
};
template<class T> void SetEvent(T& e)
{
e.SetEvent();
}
template<class T> void ResetEvent(T& e)
{
e.ResetEvent();
}
#endif
#endif //_LINUX_EVENT_H