-
Notifications
You must be signed in to change notification settings - Fork 10
/
sem_inc.c
289 lines (251 loc) · 7.66 KB
/
sem_inc.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* DUMA - Red-Zone memory allocator.
* Copyright (C) 2002-2008 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
* License: GNU LGPL (GNU Lesser General Public License, see COPYING-GPL)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* FILE CONTENTS:
* internal implementation file
* contains thread safety functions (semaphore lock/release)
*/
#include "duma_config.h"
#include "duma_sem.h"
#include "print.h"
#ifndef DUMA_NO_THREAD_SAFETY
/* check for pthread library */
/* use WIN32_SEMAPHORES on Win32-Cygwin,
* with this configuration testmt.c works either with pthreads and with the
* Win32 API
*/
#if (!defined(WIN32))
#define HAVE_PTHREADS 1
#define USE_WIN32_SEMAPHORES 0
#define USE_WIN32_CRIT_SECT 0
#elif (defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__) || \
defined(__CYGWIN32__) || defined(__CYGWIN64__))
#define HAVE_PTHREADS 0
#define USE_WIN32_SEMAPHORES 0
#define USE_WIN32_CRIT_SECT 1
#else
#define HAVE_PTHREADS 0
#define USE_WIN32_SEMAPHORES 0
#define USE_WIN32_CRIT_SECT 1
#endif
#if HAVE_PTHREADS
#include <pthread.h>
#include <semaphore.h>
#elif USE_WIN32_SEMAPHORES || USE_WIN32_CRIT_SECT
#define WIN32_LEAN_AND_MEAN 1
#include <winbase.h>
#include <windows.h>
#endif
/*
* DUMA_sem is a semaphore used to allow one thread at a time into
* these routines.
* Also, we use semInited as a boolean to see if we should be
* using the semaphore.
* semThread is set to the thread id of the thread that currently
* has the semaphore so that when/if it tries to get the semaphore
* again (realloc calling malloc/free) - nothing will happen to the
* semaphore.
* semDepth is used to keep track of how many times the same thread
* gets the semaphore - so we know when it is actually freed.
*/
#if HAVE_PTHREADS
#define DUMA_thread_self() pthread_self()
#ifndef DUMA_SEMAPHORES
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_t mutextid = 0;
static int locknr = 0;
#else
static sem_t DUMA_sem = {0};
static pthread_t semThread = (pthread_t)0;
#endif
#elif USE_WIN32_SEMAPHORES
#define DUMA_thread_self() GetCurrentThreadId()
#ifndef UNICODE
#define SEM_NAME_TYPE char
#define SEM_STRCPY strcpy
#define SEM_STRCAT strcat
static char semObjectName[] = "DUMA_";
#else
#define SEM_NAME_TYPE wchar_t
#define SEM_STRCPY wcscpy
#define SEM_STRCAT wcscat
static wchar_t semObjectName[] = L"DUMA_";
#endif
static SECURITY_ATTRIBUTES semSecAttr;
static DWORD semThread = 0;
static HANDLE semHandle = 0;
#elif USE_WIN32_CRIT_SECT
/* see http://msdn.microsoft.com/en-us/library/ms682530(VS.85).aspx */
static CRITICAL_SECTION critsect;
#endif
static int semInInit = 0;
#if HAVE_PTHREADS && !defined(DUMA_SEMAPHORES)
static int semInited = 1;
static int semDepth = 0;
#elif USE_WIN32_SEMAPHORES
static int semInited = 0;
static int semDepth = 0;
#elif USE_WIN32_CRIT_SECT
static int semInited = 0;
static int semDepth = 0; /* not really used, but avoids more ifdefs below */
#endif
#if HAVE_PTHREADS
#ifndef DUMA_SEMAPHORES
static void lock() {
if (pthread_mutex_trylock(&mutex)) {
if (mutextid == pthread_self()) {
++locknr;
return;
} else {
pthread_mutex_lock(&mutex);
}
}
mutextid = pthread_self();
locknr = 1;
}
static void unlock() {
--locknr;
if (!locknr) {
mutextid = 0;
pthread_mutex_unlock(&mutex);
}
}
#endif
#endif
void DUMA_init_sem(void) {
#if USE_WIN32_SEMAPHORES
SEM_NAME_TYPE semLocalName[32];
SEM_NAME_TYPE acPID[16];
DWORD pid;
#endif
/* avoid recursive call to sem_init(),
* when sem_init() calls malloc() or other allocation function
*/
if (semInited || semInInit)
return;
semInInit = 1;
#if HAVE_PTHREADS
#ifndef DUMA_SEMAPHORES
pthread_mutex_init(&mutex, NULL);
semInited = 1;
#else
if (sem_init(&DUMA_sem, 0, 1) >= 0)
semInited = 1;
#endif
#elif USE_WIN32_SEMAPHORES
pid = GetCurrentProcessId();
SEM_STRCPY(semLocalName, semObjectName);
/* append ProcessId() to get inter-process unique semaphore name */
acPID[0] = 'A' + (SEM_NAME_TYPE)((pid >> 28) & 0x0F);
acPID[1] = 'A' + (SEM_NAME_TYPE)((pid >> 24) & 0x0F);
acPID[2] = 'A' + (SEM_NAME_TYPE)((pid >> 20) & 0x0F);
acPID[3] = 'A' + (SEM_NAME_TYPE)((pid >> 16) & 0x0F);
acPID[4] = 'A' + (SEM_NAME_TYPE)((pid >> 12) & 0x0F);
acPID[5] = 'A' + (SEM_NAME_TYPE)((pid >> 8) & 0x0F);
acPID[6] = 'A' + (SEM_NAME_TYPE)((pid >> 4) & 0x0F);
acPID[7] = 'A' + (SEM_NAME_TYPE)((pid)&0x0F);
acPID[8] = 0;
SEM_STRCAT(semLocalName, acPID);
semSecAttr.nLength = sizeof(semSecAttr);
semSecAttr.lpSecurityDescriptor = NULL;
semSecAttr.bInheritHandle = FALSE;
semHandle =
CreateSemaphore(&semSecAttr /* pointer to security attributes */
,
1 /* initial count */
,
1 /* maximum count */
,
semLocalName /* pointer to semaphore-object name */
);
semInited = 1;
#elif USE_WIN32_CRIT_SECT
InitializeCriticalSection(&critsect);
semInited = 1;
#endif
semInInit = 0;
if (!semInited)
DUMA_Abort("\nCouldn't initialise semaphore");
}
void DUMA_get_sem(void) {
if (semInInit)
return; /* avoid recursion */
if (!semInited)
DUMA_init_sem(); /* initialize if necessary */
#if HAVE_PTHREADS
#ifndef DUMA_SEMAPHORES
lock();
#else
if (semThread != DUMA_thread_self()) {
while (sem_wait(&DUMA_sem) < 0)
; /* wait for the semaphore. */
semThread =
DUMA_thread_self(); /* let everyone know who has the semaphore. */
}
#endif
++semDepth; /* increment semDepth - push one stack level */
#elif USE_WIN32_SEMAPHORES
if (semThread != DUMA_thread_self()) {
while (WaitForSingleObject(semHandle, 1000) != WAIT_OBJECT_0)
; /* wait for the semaphore. */
semThread =
DUMA_thread_self(); /* let everyone know who has the semaphore. */
}
++semDepth; /* increment semDepth - push one stack level */
#elif USE_WIN32_CRIT_SECT
EnterCriticalSection(&critsect);
#endif
}
int DUMA_rel_sem(int retval) {
if (semInInit)
return retval; /* avoid recursion */
if (!semInited)
DUMA_Abort("\nSemaphore isn't initialised");
#ifdef DUMA_SEMAPHORES
if (!semThread)
DUMA_Abort("\nSemaphore isn't owned by this thread");
#endif
#if HAVE_PTHREADS || USE_WIN32_SEMAPHORES
if (semDepth <= 0)
DUMA_Abort("\nSemaphore isn't locked");
#endif
--semDepth; /* decrement semDepth - popping one stack level */
#if HAVE_PTHREADS
#ifndef DUMA_SEMAPHORES
unlock();
#else
semThread =
(pthread_t)0; /* zero this before actually free'ing the semaphore. */
if (sem_post(&DUMA_sem) < 0)
DUMA_Abort("Failed to post the semaphore.");
#endif
#elif USE_WIN32_SEMAPHORES
semThread = 0; /* zero this before actually free'ing the semaphore. */
if (0 ==
ReleaseSemaphore(semHandle, 1 /* amount to add to current count */, NULL))
DUMA_Abort("Failed to post the semaphore.");
#elif USE_WIN32_CRIT_SECT
LeaveCriticalSection(&critsect);
#endif
return retval;
}
#else
/* for not having an empty file */
static int dummy = 0;
#endif /* DUMA_NO_THREAD_SAFETY */