forked from liuxw7/MessageQueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemaphore.h
220 lines (197 loc) · 5.61 KB
/
Semaphore.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#pragma once
#include "SharedMemory.h"
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <tlhelp32.h>
#define TIMEOUT_VARIABLE iRet
#define SEM_FAILED 0
#define sem_open(name, oflag, accessPermission, initValue) CreateSemaphoreA(CreateSecurityDescriptor(accessPermission), initValue, 1, name)
#define TimeWait(semaphore, timeout) WaitForSingleObject(semaphore, timeout)
#define sem_wait(semaphore) WaitForSingleObject(semaphore, INFINITE)
#define sem_post(semaphore) (!ReleaseSemaphore(semaphore, 1, NULL))
#define sem_close(semaphore) CloseHandle(semaphore)
static bool QueryThreadExist(DWORD ThreadID)
{
if (0 == ThreadID)return false;
HANDLE h_Process = OpenThread(THREAD_QUERY_INFORMATION, FALSE, ThreadID);
if (!h_Process)
{
DWORD error = GetLastError();
if (error == ERROR_SUCCESS || ERROR_ACCESS_DENIED == error)
{
//LOG_DEBUG("Thread exist\r\n");
return true;
}
else
{
//LOG_DEBUG("Thread does not exist, error:%d\r\n", error);
return false;
}
}
DWORD dwExitCode = 0;
BOOL ret = GetExitCodeThread(h_Process, &dwExitCode);
CloseHandle(h_Process);
if (0 == ret || (0 != ret && STILL_ACTIVE != dwExitCode))
return false;
return true;
}
#else
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>
#define GetCurrentThreadId() syscall(__NR_gettid)
#define WAIT_OBJECT_0 0
#define WAIT_FAILED -1
#define WAIT_TIMEOUT ETIMEDOUT
#define TIMEOUT_VARIABLE errno
static int TimeWait(sem_t *semaphore, unsigned long timeoutMilliseconds)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += timeoutMilliseconds % 1000 * 1000000;
ts.tv_sec += timeoutMilliseconds / 1000 + ts.tv_nsec/1000000000;
ts.tv_nsec %= 1000000000;
return sem_timedwait(semaphore, &ts);
}
static bool QueryThreadExist(int ThreadID)
{
if (0 == ThreadID)return false;
char buf[MAX_SHARED_MEMORY_NAME_SIZE] = { 0 };
sprintf(buf, "/proc/%d/exe", ThreadID);
FILE *fp = fopen(buf, "r");
if (fp)
{
//LOG_DEBUG("Thread exist\r\n");
fclose(fp);
return true;
}
else
{
//LOG_DEBUG("Thread does not exist, error:%d\r\n", error);
return false;
}
}
#endif
class CSemaphore
{
public:
CSemaphore(const unsigned int key, const unsigned int initValue = 1, const unsigned int accessPermission = 0666);
//CSemaphore(const char* nameSem, const unsigned int initValue = 1, const unsigned int accessPermission = 0666);
virtual ~CSemaphore();
virtual int Wait(unsigned long timeoutMilliseconds = (unsigned long)-1);
virtual int Post();
private:
static const int INTERVAL_TIMEOUT = 1000; //ºÁÃë
sem_t *m_sem;
unsigned int *m_pThreadIDSharedMemory;
};
inline CSemaphore::CSemaphore(const unsigned int key, const unsigned int initValue, const unsigned int accessPermission)
{
m_pThreadIDSharedMemory = NULL;
m_sem = NULL;
char nameSem[MAX_SHARED_MEMORY_NAME_SIZE] = { 0 };
sprintf(nameSem, "Global\\Semaphore_%u", key);
m_sem = sem_open(nameSem, O_CREAT, accessPermission, initValue);
if (SEM_FAILED == m_sem)
{
LOG_ERROR("Semaphore application failed, key=%u, error=%s\r\n", key, GetErrorStr(ERRNO));
exit(ERRNO);
}
#ifdef __linux__
sprintf(nameSem, "/dev/shm/sem.Global\\Semaphore_%u", key);
chmod(nameSem, accessPermission);
#endif
CSharedMemory sharedMemory(key, sizeof(int), accessPermission);
m_pThreadIDSharedMemory = (unsigned int*)sharedMemory.Begin();
}
//CSemaphore::CSemaphore(const char* nameSem, const unsigned int initValue, const unsigned int accessPermission)
//{
// m_sem = sem_open(nameSem, O_CREAT, accessPermission, initValue);
// if (SEM_FAILED == m_sem)
// {
// LOG_ERROR("Semaphore application failed\r\n");
// exit(-2);
// }
//}
inline CSemaphore::~CSemaphore()
{
#ifdef SHARED_MEMORY_CLEAR
if (NULL != m_sem)
sem_close(m_sem);
#endif
}
inline int CSemaphore::Wait(unsigned long timeoutMilliseconds)
{
int iRet = WAIT_OBJECT_0;
unsigned long uInterval = 0;
while (NULL == m_pThreadIDSharedMemory)Sleep(1);
while(1)
{
if (timeoutMilliseconds == (unsigned long)-1)
{
uInterval = INTERVAL_TIMEOUT;
}
else if (timeoutMilliseconds > INTERVAL_TIMEOUT)
{
timeoutMilliseconds -= INTERVAL_TIMEOUT;
uInterval = INTERVAL_TIMEOUT;
}
else
{
uInterval = timeoutMilliseconds;
timeoutMilliseconds = 0;
}
iRet = TimeWait(m_sem, uInterval);
if (iRet == WAIT_OBJECT_0)
{
*m_pThreadIDSharedMemory = GetCurrentThreadId();
return WAIT_OBJECT_0;
}
switch (TIMEOUT_VARIABLE)
{
case WAIT_TIMEOUT:
if (*m_pThreadIDSharedMemory == GetCurrentThreadId())
{
if (0 == timeoutMilliseconds)
return WAIT_TIMEOUT;
else
continue;
}
if (!QueryThreadExist(*m_pThreadIDSharedMemory))
{
*m_pThreadIDSharedMemory = GetCurrentThreadId();
Sleep(1);
if (*m_pThreadIDSharedMemory == GetCurrentThreadId())
{
LOG_DEBUG("The semaphore is abnormally locked and unlocked.\r\n");
return WAIT_OBJECT_0;
}
}
if (0 == timeoutMilliseconds)
return WAIT_TIMEOUT;
break;
case WAIT_FAILED:
LOG_ERROR("Semaphore WAIT_FAILED error, error=%s\r\n", GetErrorStr(ERRNO));
return TIMEOUT_VARIABLE;
default:
LOG_ERROR("Semaphore other error, error=%s\r\n", GetErrorStr(ERRNO));
return TIMEOUT_VARIABLE;
}
}
}
inline int CSemaphore::Post()
{
if (NULL == m_sem)
return 0;
#ifdef _WIN32
sem_post(m_sem);
return 0;
#else
int iRet = sem_post(m_sem);
if (iRet == WAIT_OBJECT_0)return WAIT_OBJECT_0;
LOG_ERROR("Semaphore error, error=%s\r\n", GetErrorStr(ERRNO));
return ERRNO;
#endif
}