- Fixed NetWare compatibility issues
[public/netxms.git] / include / nms_threads.h
CommitLineData
54481027 1/*
4997be5c 2** NetXMS - Network Management System
54481027
VK
3** Copyright (C) 2003 Victor Kirhenshtein
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** $module: nms_threads.h
20**
21**/
22
23#ifndef _nms_threads_h_
24#define _nms_threads_h_
25
b71bff93
VK
26#ifdef __cplusplus
27
28#define NMS_THREADS_H_INCLUDED
29
54481027
VK
30#ifdef _WIN32
31
018fda4d
AK
32#ifndef UNDER_CE
33# include <process.h>
34#endif
54481027
VK
35
36//
37// Related datatypes and constants
38//
39
dbe67493
VK
40typedef HANDLE MUTEX;
41typedef HANDLE THREAD;
42typedef HANDLE CONDITION;
54481027 43
449e3da9
VK
44#define INVALID_MUTEX_HANDLE INVALID_HANDLE_VALUE
45#define INVALID_CONDITION_HANDLE INVALID_HANDLE_VALUE
ccdbbb52
VK
46#define INVALID_THREAD_HANDLE (NULL)
47
288ddda4
VK
48#ifdef UNDER_CE
49typedef DWORD THREAD_RESULT;
50typedef DWORD THREAD_ID;
51#else
ccdbbb52 52typedef unsigned int THREAD_RESULT;
288ddda4
VK
53typedef unsigned int THREAD_ID;
54#endif
ccdbbb52
VK
55
56#define THREAD_OK 0
288ddda4
VK
57
58#ifdef UNDER_CE
59#define THREAD_CALL
60#else
ccdbbb52 61#define THREAD_CALL __stdcall
288ddda4 62#endif
54481027
VK
63
64
65//
66// Inline functions
67//
68
69inline void ThreadSleep(int iSeconds)
70{
71 Sleep((DWORD)iSeconds * 1000); // Convert to milliseconds
72}
73
74inline void ThreadSleepMs(DWORD dwMilliseconds)
75{
76 Sleep(dwMilliseconds);
77}
78
ccdbbb52 79inline BOOL ThreadCreate(THREAD_RESULT (THREAD_CALL *start_address )(void *), int stack_size, void *args)
54481027 80{
ccdbbb52 81 HANDLE hThread;
288ddda4 82 THREAD_ID dwThreadId;
ccdbbb52 83
f2fdf1b5 84#ifdef UNDER_CE
288ddda4 85 hThread = CreateThread(NULL, (DWORD)stack_size, start_address, args, 0, &dwThreadId);
f2fdf1b5 86#else
ccdbbb52 87 hThread = (HANDLE)_beginthreadex(NULL, stack_size, start_address, args, 0, &dwThreadId);
f2fdf1b5 88#endif
ccdbbb52
VK
89 if (hThread != NULL)
90 CloseHandle(hThread);
91 return (hThread != NULL);
92}
93
94inline THREAD ThreadCreateEx(THREAD_RESULT (THREAD_CALL *start_address )(void *), int stack_size, void *args)
95{
288ddda4 96 THREAD_ID dwThreadId;
ccdbbb52 97
288ddda4
VK
98#ifdef UNDER_CE
99 return CreateThread(NULL, (DWORD)stack_size, start_address, args, 0, &dwThreadId);
631ec742 100#else
288ddda4 101 return (HANDLE)_beginthreadex(NULL, stack_size, start_address, args, 0, &dwThreadId);
631ec742 102#endif
54481027
VK
103}
104
105inline void ThreadExit(void)
106{
f2fdf1b5 107#ifdef UNDER_CE
018fda4d 108 ExitThread(0);
f2fdf1b5
VK
109#else
110 _endthreadex(0);
018fda4d 111#endif
54481027
VK
112}
113
ccdbbb52
VK
114inline void ThreadJoin(THREAD hThread)
115{
116 if (hThread != INVALID_THREAD_HANDLE)
117 {
118 WaitForSingleObject(hThread, INFINITE);
119 CloseHandle(hThread);
120 }
121}
122
54481027
VK
123inline MUTEX MutexCreate(void)
124{
125 return CreateMutex(NULL, FALSE, NULL);
126}
127
128inline void MutexDestroy(MUTEX mutex)
129{
130 CloseHandle(mutex);
131}
132
133inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
134{
135 return WaitForSingleObject(mutex, dwTimeOut) == WAIT_OBJECT_0;
136}
137
138inline void MutexUnlock(MUTEX mutex)
139{
140 ReleaseMutex(mutex);
141}
142
d16cf8a5 143inline CONDITION ConditionCreate(BOOL bBroadcast)
54481027 144{
646d58de 145 return CreateEvent(NULL, bBroadcast, FALSE, NULL);
54481027
VK
146}
147
148inline void ConditionDestroy(CONDITION hCond)
149{
150 CloseHandle(hCond);
151}
152
153inline void ConditionSet(CONDITION hCond)
5f743326
VK
154{
155 SetEvent(hCond);
156}
157
158inline void ConditionReset(CONDITION hCond)
159{
160 ResetEvent(hCond);
161}
162
163inline void ConditionPulse(CONDITION hCond)
54481027 164{
39d7a7ed 165 PulseEvent(hCond);
54481027
VK
166}
167
c7f4f5a9 168inline BOOL ConditionWait(CONDITION hCond, DWORD dwTimeOut)
54481027 169{
c7f4f5a9 170 return WaitForSingleObject(hCond, dwTimeOut) == WAIT_OBJECT_0;
54481027
VK
171}
172
173#else /* _WIN32 */
174
d16cf8a5
AK
175/****************************************************************************/
176/* unix part */
177/****************************************************************************/
178
ae82f88f
VK
179#if HAVE__USR_INCLUDE_PTHREAD_H
180#include </usr/include/pthread.h>
181#else
54481027 182#include <pthread.h>
ae82f88f
VK
183#endif
184
d16cf8a5
AK
185#include <errno.h>
186#include <sys/time.h>
54481027
VK
187
188//
189// Related datatypes and constants
190//
191
7e679c4b
AK
192typedef pthread_t THREAD;
193typedef pthread_mutex_t * MUTEX;
d16cf8a5
AK
194struct condition_t
195{
196 pthread_cond_t cond;
197 pthread_mutex_t mutex;
198 BOOL broadcast;
5f743326 199 BOOL isSet;
d16cf8a5
AK
200};
201typedef struct condition_t * CONDITION;
54481027 202
449e3da9
VK
203#define INVALID_MUTEX_HANDLE (NULL)
204#define INVALID_CONDITION_HANDLE (NULL)
ccdbbb52 205#define INVALID_THREAD_HANDLE 0
54481027 206
7e679c4b
AK
207#ifndef INFINITE
208# define INFINITE 0
209#endif
54481027 210
ccdbbb52
VK
211typedef void *THREAD_RESULT;
212
213#define THREAD_OK ((void *)0)
214#define THREAD_CALL
215
216
54481027
VK
217//
218// Inline functions
219//
220
d16cf8a5 221inline void ThreadSleep(int nSeconds)
54481027 222{
22412a01
VK
223#ifdef _NETWARE
224 sleep(nSeconds);
225#else
d16cf8a5
AK
226 struct timeval tv;
227
228 tv.tv_sec = nSeconds;
229 tv.tv_usec = 0;
230
231 select(1, NULL, NULL, NULL, &tv);
22412a01 232#endif
54481027
VK
233}
234
235inline void ThreadSleepMs(DWORD dwMilliseconds)
236{
d16cf8a5 237 // select is a sort of overkill
54481027
VK
238 usleep(dwMilliseconds * 1000); // Convert to microseconds
239}
240
ccdbbb52 241inline BOOL ThreadCreate(THREAD_RESULT (THREAD_CALL *start_address )(void *), int stack_size, void *args)
7e679c4b
AK
242{
243 THREAD id;
244
ccdbbb52 245 if (pthread_create(&id, NULL, start_address, args) == 0)
d1fbf6ba
VK
246 {
247 pthread_detach(id);
ccdbbb52
VK
248 return TRUE;
249 }
250 else
251 {
252 return FALSE;
253 }
254}
255
256inline THREAD ThreadCreateEx(THREAD_RESULT (THREAD_CALL *start_address )(void *), int stack_size, void *args)
257{
35a3a09e 258 THREAD id;
ccdbbb52 259
35a3a09e 260 if (pthread_create(&id, NULL, start_address, args) == 0)
ccdbbb52 261 {
35a3a09e
VK
262 return id;
263 }
d1fbf6ba
VK
264 else
265 {
35a3a09e
VK
266 return INVALID_THREAD_HANDLE;
267 }
7e679c4b
AK
268}
269
270inline void ThreadExit(void)
271{
521d90e7 272 pthread_exit(NULL);
7e679c4b
AK
273}
274
ccdbbb52
VK
275inline void ThreadJoin(THREAD hThread)
276{
277 if (hThread != INVALID_THREAD_HANDLE)
35a3a09e 278 pthread_join(hThread, NULL);
ccdbbb52
VK
279}
280
54481027
VK
281inline MUTEX MutexCreate(void)
282{
283 MUTEX mutex;
284
285 mutex = (MUTEX)malloc(sizeof(pthread_mutex_t));
286 if (mutex != NULL)
287 pthread_mutex_init(mutex, NULL);
288 return mutex;
289}
290
291inline void MutexDestroy(MUTEX mutex)
292{
7e679c4b 293 if (mutex != NULL) {
54481027
VK
294 pthread_mutex_destroy(mutex);
295 free(mutex);
296 }
297}
298
53c17a96 299inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
54481027 300{
d16cf8a5
AK
301 int i;
302 int ret = FALSE;
303
7e679c4b 304 if (mutex != NULL) {
951e884e
AK
305 if (dwTimeOut == INFINITE)
306 {
307 if (pthread_mutex_lock(mutex) == 0) {
d16cf8a5 308 ret = TRUE;
d16cf8a5 309 }
951e884e
AK
310 }
311 else
312 {
313 for (i = (dwTimeOut / 50) + 1; i > 0; i--) {
8a435919
VK
314 if (pthread_mutex_trylock(mutex) == 0)
315 {
951e884e
AK
316 ret = TRUE;
317 break;
318 }
02ed4cf1 319 ThreadSleepMs(50);
951e884e 320 }
d16cf8a5 321 }
7e679c4b 322 }
d16cf8a5 323 return ret;
54481027
VK
324}
325
326inline void MutexUnlock(MUTEX mutex)
327{
8a435919
VK
328 if (mutex != NULL)
329 {
54481027 330 pthread_mutex_unlock(mutex);
7e679c4b
AK
331 }
332}
333
d16cf8a5 334inline CONDITION ConditionCreate(BOOL bBroadcast)
7e679c4b
AK
335{
336 CONDITION cond;
337
d16cf8a5 338 cond = (CONDITION)malloc(sizeof(struct condition_t));
8a435919
VK
339 if (cond != NULL)
340 {
d16cf8a5
AK
341 pthread_cond_init(&cond->cond, NULL);
342 pthread_mutex_init(&cond->mutex, NULL);
343 cond->broadcast = bBroadcast;
5f743326 344 cond->isSet = FALSE;
7e679c4b 345 }
d16cf8a5 346
7e679c4b
AK
347 return cond;
348}
349
d16cf8a5 350inline void ConditionDestroy(CONDITION cond)
7e679c4b 351{
5f743326 352 if (cond != INVALID_CONDITION_HANDLE)
d16cf8a5
AK
353 {
354 pthread_cond_destroy(&cond->cond);
355 pthread_mutex_destroy(&cond->mutex);
356 free(cond);
7e679c4b
AK
357 }
358}
359
d16cf8a5 360inline void ConditionSet(CONDITION cond)
7e679c4b 361{
5f743326 362 if (cond != INVALID_CONDITION_HANDLE)
d16cf8a5
AK
363 {
364 pthread_mutex_lock(&cond->mutex);
5f743326 365 cond->isSet = TRUE;
02ed4cf1 366 if (cond->broadcast)
d16cf8a5
AK
367 {
368 pthread_cond_broadcast(&cond->cond);
369 }
370 else
371 {
372 pthread_cond_signal(&cond->cond);
373 }
374 pthread_mutex_unlock(&cond->mutex);
375 }
7e679c4b
AK
376}
377
5f743326
VK
378inline void ConditionReset(CONDITION cond)
379{
380 if (cond != INVALID_CONDITION_HANDLE)
381 {
382 pthread_mutex_lock(&cond->mutex);
383 cond->isSet = FALSE;
384 pthread_mutex_unlock(&cond->mutex);
385 }
386}
387
388inline void ConditionPulse(CONDITION cond)
389{
390 if (cond != INVALID_CONDITION_HANDLE)
391 {
392 pthread_mutex_lock(&cond->mutex);
393 if (cond->broadcast)
394 {
395 pthread_cond_broadcast(&cond->cond);
396 }
397 else
398 {
399 pthread_cond_signal(&cond->cond);
400 }
401 cond->isSet = FALSE;
402 pthread_mutex_unlock(&cond->mutex);
403 }
404}
405
d16cf8a5 406inline BOOL ConditionWait(CONDITION cond, DWORD dwTimeOut)
7e679c4b 407{
d16cf8a5
AK
408 BOOL ret = FALSE;
409
410 if (cond != NULL)
411 {
412 int retcode;
413
414 pthread_mutex_lock(&cond->mutex);
5f743326
VK
415 if (cond->isSet)
416 {
417 ret = TRUE;
418 if (!cond->broadcast)
419 cond->isSet = FALSE;
420 }
421 else
422 {
423 if (dwTimeOut != INFINITE)
424 {
696fc54f 425#if HAVE_PTHREAD_COND_RELTIMEDWAIT_NP
5f743326 426 struct timespec timeout;
696fc54f 427
5f743326
VK
428 timeout.tv_sec = dwTimeOut / 1000;
429 timeout.tv_nsec = (dwTimeOut % 1000) * 1000000;
430 retcode = pthread_cond_reltimedwait_np(&cond->cond, &cond->mutex, &timeout);
696fc54f 431#else
5f743326
VK
432 struct timeval now;
433 struct timespec timeout;
d16cf8a5 434
5f743326
VK
435 // note.
436 // mili - 10^-3
437 // micro - 10^-6
438 // nano - 10^-9
ed452de5 439
5f743326
VK
440 // FIXME there should be more accurate way
441 gettimeofday(&now, NULL);
442 timeout.tv_sec = now.tv_sec + (dwTimeOut / 1000);
1216cc73 443
5f743326
VK
444 now.tv_usec += (dwTimeOut % 1000) * 1000;
445 timeout.tv_sec += now.tv_usec / 1000000;
446 timeout.tv_nsec = (now.tv_usec % 1000000) * 1000;
ed452de5 447
5f743326 448 retcode = pthread_cond_timedwait(&cond->cond, &cond->mutex, &timeout);
696fc54f 449#endif
5f743326
VK
450 }
451 else
452 {
453 retcode = pthread_cond_wait(&cond->cond, &cond->mutex);
454 }
455
456 if (retcode == 0)
457 {
633907a4
VK
458 if (!cond->broadcast)
459 cond->isSet = FALSE;
5f743326
VK
460 ret = TRUE;
461 }
462 }
7e679c4b 463
d16cf8a5 464 pthread_mutex_unlock(&cond->mutex);
7e679c4b 465 }
d16cf8a5
AK
466
467 return ret;
54481027
VK
468}
469
bff234b8
VK
470inline DWORD GetCurrentProcessId(void)
471{
472 return getpid();
473}
474
54481027
VK
475#endif /* _WIN32 */
476
b71bff93
VK
477#endif /* __cplusplus */
478
54481027 479#endif /* _nms_threads_h_ */