Fixed packaging error
[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 32#ifndef UNDER_CE
df7156b3 33#include <process.h>
018fda4d 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
a3c76aba
VK
128inline MUTEX MutexCreateRecursive(void)
129{
130 return CreateMutex(NULL, FALSE, NULL);
131}
132
54481027
VK
133inline void MutexDestroy(MUTEX mutex)
134{
135 CloseHandle(mutex);
136}
137
138inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
139{
140 return WaitForSingleObject(mutex, dwTimeOut) == WAIT_OBJECT_0;
141}
142
143inline void MutexUnlock(MUTEX mutex)
144{
145 ReleaseMutex(mutex);
146}
147
d16cf8a5 148inline CONDITION ConditionCreate(BOOL bBroadcast)
54481027 149{
646d58de 150 return CreateEvent(NULL, bBroadcast, FALSE, NULL);
54481027
VK
151}
152
153inline void ConditionDestroy(CONDITION hCond)
154{
155 CloseHandle(hCond);
156}
157
158inline void ConditionSet(CONDITION hCond)
159{
5f743326
VK
160 SetEvent(hCond);
161}
162
163inline void ConditionReset(CONDITION hCond)
164{
165 ResetEvent(hCond);
166}
167
168inline void ConditionPulse(CONDITION hCond)
169{
39d7a7ed 170 PulseEvent(hCond);
54481027
VK
171}
172
c7f4f5a9 173inline BOOL ConditionWait(CONDITION hCond, DWORD dwTimeOut)
54481027 174{
c7f4f5a9 175 return WaitForSingleObject(hCond, dwTimeOut) == WAIT_OBJECT_0;
54481027
VK
176}
177
178#else /* _WIN32 */
179
d16cf8a5
AK
180/****************************************************************************/
181/* unix part */
182/****************************************************************************/
183
54481027 184#include <pthread.h>
d16cf8a5
AK
185#include <errno.h>
186#include <sys/time.h>
54481027 187
6cd41ceb
VK
188#if HAVE_PTHREAD_NP_H
189#include <pthread_np.h>
190#endif
191
a0537937 192#if (HAVE_PTHREAD_MUTEXATTR_SETTYPE || HAVE_PTHREAD_MUTEXATTR_SETKIND_NP) && \
6cd41ceb
VK
193 (HAVE_DECL_PTHREAD_MUTEX_RECURSIVE || \
194 HAVE_DECL_PTHREAD_MUTEX_RECURSIVE_NP || \
195 HAVE_DECL_MUTEX_TYPE_COUNTING_FAST)
a0537937
VK
196
197#define HAVE_RECURSIVE_MUTEXES 1
198
199#if HAVE_DECL_PTHREAD_MUTEX_RECURSIVE
200#define MUTEX_RECURSIVE_FLAG PTHREAD_MUTEX_RECURSIVE
6cd41ceb 201#elif HAVE_DECL_PTHREAD_MUTEX_RECURSIVE_NP
a0537937 202#define MUTEX_RECURSIVE_FLAG PTHREAD_MUTEX_RECURSIVE_NP
6cd41ceb
VK
203#elif HAVE_DECL_MUTEX_TYPE_COUNTING_FAST
204#define MUTEX_RECURSIVE_FLAG MUTEX_TYPE_COUNTING_FAST
205#else
206#error Constant used to declare recursive mutex is not known
a0537937
VK
207#endif
208
209#if HAVE_PTHREAD_MUTEXATTR_SETTYPE
210#define MUTEXATTR_SETTYPE pthread_mutexattr_settype
211#else
212#define MUTEXATTR_SETTYPE pthread_mutexattr_setkind_np
213#endif
214
215#endif
216
54481027
VK
217//
218// Related datatypes and constants
219//
220
7e679c4b 221typedef pthread_t THREAD;
a3c76aba
VK
222struct netxms_mutex_t
223{
224 pthread_mutex_t mutex;
225#ifndef HAVE_RECURSIVE_MUTEXES
226 BOOL isRecursive;
227 pthread_t owner;
228#endif
229};
230typedef netxms_mutex_t * MUTEX;
231struct netxms_condition_t
d16cf8a5
AK
232{
233 pthread_cond_t cond;
234 pthread_mutex_t mutex;
235 BOOL broadcast;
5f743326 236 BOOL isSet;
d16cf8a5 237};
a3c76aba 238typedef struct netxms_condition_t * CONDITION;
54481027 239
449e3da9
VK
240#define INVALID_MUTEX_HANDLE (NULL)
241#define INVALID_CONDITION_HANDLE (NULL)
ccdbbb52 242#define INVALID_THREAD_HANDLE 0
54481027 243
7e679c4b
AK
244#ifndef INFINITE
245# define INFINITE 0
246#endif
54481027 247
ccdbbb52
VK
248typedef void *THREAD_RESULT;
249
250#define THREAD_OK ((void *)0)
251#define THREAD_CALL
252
253
54481027
VK
254//
255// Inline functions
256//
257
d16cf8a5 258inline void ThreadSleep(int nSeconds)
54481027 259{
22412a01
VK
260#ifdef _NETWARE
261 sleep(nSeconds);
262#else
d16cf8a5
AK
263 struct timeval tv;
264
265 tv.tv_sec = nSeconds;
266 tv.tv_usec = 0;
267
268 select(1, NULL, NULL, NULL, &tv);
22412a01 269#endif
54481027
VK
270}
271
272inline void ThreadSleepMs(DWORD dwMilliseconds)
273{
642bb2d8
VK
274#if HAVE_NANOSLEEP
275 struct timespec interval, remainder;
276
277 interval.tv_sec = dwMilliseconds / 1000;
278 interval.tv_nsec = (dwMilliseconds % 1000) * 1000000; // milli -> nano
279 nanosleep(&interval, &remainder);
280#else
281 usleep(dwMilliseconds * 1000); // Convert to microseconds
282#endif
54481027
VK
283}
284
ccdbbb52 285inline BOOL ThreadCreate(THREAD_RESULT (THREAD_CALL *start_address )(void *), int stack_size, void *args)
7e679c4b
AK
286{
287 THREAD id;
288
ccdbbb52 289 if (pthread_create(&id, NULL, start_address, args) == 0)
d1fbf6ba
VK
290 {
291 pthread_detach(id);
ccdbbb52
VK
292 return TRUE;
293 }
294 else
295 {
296 return FALSE;
297 }
298}
299
300inline THREAD ThreadCreateEx(THREAD_RESULT (THREAD_CALL *start_address )(void *), int stack_size, void *args)
301{
35a3a09e 302 THREAD id;
ccdbbb52 303
35a3a09e 304 if (pthread_create(&id, NULL, start_address, args) == 0)
ccdbbb52 305 {
35a3a09e
VK
306 return id;
307 }
d1fbf6ba
VK
308 else
309 {
35a3a09e
VK
310 return INVALID_THREAD_HANDLE;
311 }
7e679c4b
AK
312}
313
314inline void ThreadExit(void)
315{
521d90e7 316 pthread_exit(NULL);
7e679c4b
AK
317}
318
ccdbbb52
VK
319inline void ThreadJoin(THREAD hThread)
320{
321 if (hThread != INVALID_THREAD_HANDLE)
35a3a09e 322 pthread_join(hThread, NULL);
ccdbbb52
VK
323}
324
54481027
VK
325inline MUTEX MutexCreate(void)
326{
327 MUTEX mutex;
328
a3c76aba 329 mutex = (MUTEX)malloc(sizeof(netxms_mutex_t));
54481027 330 if (mutex != NULL)
8118ecaf 331 pthread_mutex_init(&mutex->mutex, NULL);
a3c76aba
VK
332 return mutex;
333}
334
335inline MUTEX MutexCreateRecursive(void)
336{
337 MUTEX mutex;
338
339 mutex = (MUTEX)malloc(sizeof(netxms_mutex_t));
340 if (mutex != NULL)
341 {
342#ifdef HAVE_RECURSIVE_MUTEXES
343 pthread_mutexattr_t a;
344
345 pthread_mutexattr_init(&a);
a0537937 346 MUTEXATTR_SETTYPE(&a, MUTEX_RECURSIVE_FLAG);
a3c76aba
VK
347 pthread_mutex_init(&mutex->mutex, &a);
348 pthread_mutexattr_destroy(&a);
349#else
350#error FIXME: implement recursive mutexes
351#endif
352 }
54481027
VK
353 return mutex;
354}
355
356inline void MutexDestroy(MUTEX mutex)
357{
a3c76aba
VK
358 if (mutex != NULL)
359 {
360 pthread_mutex_destroy(&mutex->mutex);
54481027
VK
361 free(mutex);
362 }
363}
364
53c17a96 365inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
54481027 366{
d16cf8a5
AK
367 int i;
368 int ret = FALSE;
369
a3c76aba
VK
370 if (mutex != NULL)
371 {
951e884e
AK
372 if (dwTimeOut == INFINITE)
373 {
a3c76aba
VK
374 if (pthread_mutex_lock(&mutex->mutex) == 0)
375 {
d16cf8a5 376 ret = TRUE;
d16cf8a5 377 }
951e884e
AK
378 }
379 else
380 {
a3c76aba
VK
381 for (i = (dwTimeOut / 50) + 1; i > 0; i--)
382 {
383 if (pthread_mutex_trylock(&mutex->mutex) == 0)
8a435919 384 {
951e884e
AK
385 ret = TRUE;
386 break;
387 }
02ed4cf1 388 ThreadSleepMs(50);
951e884e 389 }
d16cf8a5 390 }
7e679c4b 391 }
d16cf8a5 392 return ret;
54481027
VK
393}
394
395inline void MutexUnlock(MUTEX mutex)
396{
8a435919
VK
397 if (mutex != NULL)
398 {
a3c76aba 399 pthread_mutex_unlock(&mutex->mutex);
7e679c4b
AK
400 }
401}
402
d16cf8a5 403inline CONDITION ConditionCreate(BOOL bBroadcast)
7e679c4b
AK
404{
405 CONDITION cond;
406
a3c76aba 407 cond = (CONDITION)malloc(sizeof(struct netxms_condition_t));
8a435919
VK
408 if (cond != NULL)
409 {
d16cf8a5
AK
410 pthread_cond_init(&cond->cond, NULL);
411 pthread_mutex_init(&cond->mutex, NULL);
412 cond->broadcast = bBroadcast;
5f743326 413 cond->isSet = FALSE;
7e679c4b 414 }
d16cf8a5 415
7e679c4b
AK
416 return cond;
417}
418
d16cf8a5 419inline void ConditionDestroy(CONDITION cond)
7e679c4b 420{
5f743326 421 if (cond != INVALID_CONDITION_HANDLE)
d16cf8a5
AK
422 {
423 pthread_cond_destroy(&cond->cond);
424 pthread_mutex_destroy(&cond->mutex);
425 free(cond);
7e679c4b
AK
426 }
427}
428
d16cf8a5 429inline void ConditionSet(CONDITION cond)
7e679c4b 430{
5f743326 431 if (cond != INVALID_CONDITION_HANDLE)
d16cf8a5
AK
432 {
433 pthread_mutex_lock(&cond->mutex);
5f743326 434 cond->isSet = TRUE;
02ed4cf1 435 if (cond->broadcast)
d16cf8a5
AK
436 {
437 pthread_cond_broadcast(&cond->cond);
438 }
439 else
440 {
441 pthread_cond_signal(&cond->cond);
442 }
443 pthread_mutex_unlock(&cond->mutex);
444 }
7e679c4b
AK
445}
446
5f743326
VK
447inline void ConditionReset(CONDITION cond)
448{
449 if (cond != INVALID_CONDITION_HANDLE)
450 {
451 pthread_mutex_lock(&cond->mutex);
452 cond->isSet = FALSE;
453 pthread_mutex_unlock(&cond->mutex);
454 }
455}
456
457inline void ConditionPulse(CONDITION cond)
458{
459 if (cond != INVALID_CONDITION_HANDLE)
460 {
461 pthread_mutex_lock(&cond->mutex);
462 if (cond->broadcast)
463 {
464 pthread_cond_broadcast(&cond->cond);
465 }
466 else
467 {
468 pthread_cond_signal(&cond->cond);
469 }
470 cond->isSet = FALSE;
471 pthread_mutex_unlock(&cond->mutex);
472 }
473}
474
d16cf8a5 475inline BOOL ConditionWait(CONDITION cond, DWORD dwTimeOut)
7e679c4b 476{
d16cf8a5
AK
477 BOOL ret = FALSE;
478
479 if (cond != NULL)
480 {
481 int retcode;
482
483 pthread_mutex_lock(&cond->mutex);
5f743326
VK
484 if (cond->isSet)
485 {
486 ret = TRUE;
487 if (!cond->broadcast)
488 cond->isSet = FALSE;
489 }
490 else
491 {
492 if (dwTimeOut != INFINITE)
493 {
58e7e86f 494#if HAVE_PTHREAD_COND_RELTIMEDWAIT_NP || defined(_NETWARE)
5f743326 495 struct timespec timeout;
696fc54f 496
5f743326
VK
497 timeout.tv_sec = dwTimeOut / 1000;
498 timeout.tv_nsec = (dwTimeOut % 1000) * 1000000;
58e7e86f
VK
499#ifdef _NETWARE
500 retcode = pthread_cond_timedwait(&cond->cond, &cond->mutex, &timeout);
501#else
5f743326 502 retcode = pthread_cond_reltimedwait_np(&cond->cond, &cond->mutex, &timeout);
58e7e86f 503#endif
696fc54f 504#else
5f743326
VK
505 struct timeval now;
506 struct timespec timeout;
d16cf8a5 507
5f743326
VK
508 // note.
509 // mili - 10^-3
510 // micro - 10^-6
511 // nano - 10^-9
ed452de5 512
5f743326
VK
513 // FIXME there should be more accurate way
514 gettimeofday(&now, NULL);
515 timeout.tv_sec = now.tv_sec + (dwTimeOut / 1000);
1216cc73 516
5f743326
VK
517 now.tv_usec += (dwTimeOut % 1000) * 1000;
518 timeout.tv_sec += now.tv_usec / 1000000;
519 timeout.tv_nsec = (now.tv_usec % 1000000) * 1000;
ed452de5 520
5f743326 521 retcode = pthread_cond_timedwait(&cond->cond, &cond->mutex, &timeout);
696fc54f 522#endif
5f743326
VK
523 }
524 else
525 {
526 retcode = pthread_cond_wait(&cond->cond, &cond->mutex);
527 }
528
529 if (retcode == 0)
530 {
633907a4
VK
531 if (!cond->broadcast)
532 cond->isSet = FALSE;
5f743326
VK
533 ret = TRUE;
534 }
535 }
7e679c4b 536
d16cf8a5 537 pthread_mutex_unlock(&cond->mutex);
7e679c4b 538 }
d16cf8a5
AK
539
540 return ret;
54481027
VK
541}
542
bff234b8
VK
543inline DWORD GetCurrentProcessId(void)
544{
545 return getpid();
546}
547
d8250de7 548inline THREAD GetCurrentThreadId(void)
5e3c403d 549{
05f0165e 550 return pthread_self();
5e3c403d
VK
551}
552
54481027
VK
553#endif /* _WIN32 */
554
df7156b3
VK
555#include <rwlock.h>
556
b71bff93
VK
557#endif /* __cplusplus */
558
54481027 559#endif /* _nms_threads_h_ */