forked from u-n-k-n-o-w-n/BonDriverProxy_Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.h
More file actions
115 lines (105 loc) · 2.08 KB
/
Common.h
File metadata and controls
115 lines (105 loc) · 2.08 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
#ifndef __COMMON_H__
#define __COMMON_H__
class cCriticalSection {
pthread_mutex_t m_m;
public:
cCriticalSection()
{
pthread_mutexattr_t attr;
::pthread_mutexattr_init(&attr);
::pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
::pthread_mutex_init(&m_m, &attr);
}
~cCriticalSection(){ ::pthread_mutex_destroy(&m_m); }
void Enter(){ ::pthread_mutex_lock(&m_m); }
void Leave(){ ::pthread_mutex_unlock(&m_m); }
};
class cLock {
cCriticalSection &m_c;
public:
cLock(cCriticalSection &ref) : m_c(ref) { m_c.Enter(); }
~cLock(){ m_c.Leave(); }
};
#define LOCK(key) cLock __Lock__(key)
class cEvent {
pthread_cond_t &m_c;
pthread_mutex_t &m_m;
BOOL m_bAutoReset;
volatile BOOL m_bActive;
public:
cEvent(pthread_cond_t &c, pthread_mutex_t &m) : m_c(c), m_m(m)
{
m_bActive = FALSE;
m_bAutoReset = FALSE;
}
~cEvent(){}
void SetAutoReset(BOOL b){ m_bAutoReset = b; }
inline BOOL IsSet(){ return m_bActive; }
BOOL Set()
{
::pthread_mutex_lock(&m_m);
m_bActive = TRUE;
::pthread_cond_broadcast(&m_c);
::pthread_mutex_unlock(&m_m);
return TRUE;
}
BOOL Reset()
{
::pthread_mutex_lock(&m_m);
m_bActive = FALSE;
::pthread_mutex_unlock(&m_m);
return TRUE;
}
DWORD Wait(cEvent *err)
{
cEvent *h[2] = { err, this };
return MultipleWait(2, h);
}
static DWORD MultipleWait(int num, cEvent **e, BOOL bAll = FALSE)
{
int i, cnt;
DWORD dwRet = 0xffffffff;
::pthread_mutex_lock(&(e[0]->m_m));
for (;;)
{
if (bAll)
{
cnt = 0;
for (i = 0; i < num; i++)
{
if (e[i]->IsSet())
cnt++;
}
if (cnt == num)
{
for (i = 0; i < num; i++)
{
if (e[i]->m_bAutoReset)
e[i]->m_bActive = FALSE;
}
dwRet = num;
break;
}
}
else
{
for (i = 0; i < num; i++)
{
if (e[i]->IsSet())
{
if (e[i]->m_bAutoReset)
e[i]->m_bActive = FALSE;
dwRet = i;
break;
}
}
if (dwRet != 0xffffffff)
break;
}
::pthread_cond_wait(&(e[0]->m_c), &(e[0]->m_m));
}
::pthread_mutex_unlock(&(e[0]->m_m));
return dwRet;
}
};
#endif // __COMMON_H__