Finished alarm configuration in policy editor
[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
26#ifdef _WIN32
27
28#include <process.h>
29
30//
31// Related datatypes and constants
32//
33
34#define MUTEX HANDLE
35#define THREAD HANDLE
36#define CONDITION HANDLE
37
449e3da9
VK
38#define INVALID_MUTEX_HANDLE INVALID_HANDLE_VALUE
39#define INVALID_CONDITION_HANDLE INVALID_HANDLE_VALUE
54481027
VK
40
41
42//
43// Inline functions
44//
45
46inline void ThreadSleep(int iSeconds)
47{
48 Sleep((DWORD)iSeconds * 1000); // Convert to milliseconds
49}
50
51inline void ThreadSleepMs(DWORD dwMilliseconds)
52{
53 Sleep(dwMilliseconds);
54}
55
56inline THREAD ThreadCreate(void (__cdecl *start_address )(void *), int stack_size, void *args)
57{
58 return (THREAD)_beginthread(start_address, stack_size, args);
59}
60
61inline void ThreadExit(void)
62{
63 _endthread();
64}
65
66inline MUTEX MutexCreate(void)
67{
68 return CreateMutex(NULL, FALSE, NULL);
69}
70
71inline void MutexDestroy(MUTEX mutex)
72{
73 CloseHandle(mutex);
74}
75
76inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
77{
78 return WaitForSingleObject(mutex, dwTimeOut) == WAIT_OBJECT_0;
79}
80
81inline void MutexUnlock(MUTEX mutex)
82{
83 ReleaseMutex(mutex);
84}
85
d16cf8a5 86inline CONDITION ConditionCreate(BOOL bBroadcast)
54481027 87{
646d58de 88 return CreateEvent(NULL, bBroadcast, FALSE, NULL);
54481027
VK
89}
90
91inline void ConditionDestroy(CONDITION hCond)
92{
93 CloseHandle(hCond);
94}
95
96inline void ConditionSet(CONDITION hCond)
97{
98 SetEvent(hCond);
99}
100
c7f4f5a9 101inline BOOL ConditionWait(CONDITION hCond, DWORD dwTimeOut)
54481027 102{
c7f4f5a9 103 return WaitForSingleObject(hCond, dwTimeOut) == WAIT_OBJECT_0;
54481027
VK
104}
105
106#else /* _WIN32 */
107
d16cf8a5
AK
108/****************************************************************************/
109/* unix part */
110/****************************************************************************/
111
54481027 112#include <pthread.h>
d16cf8a5
AK
113#include <errno.h>
114#include <sys/time.h>
54481027
VK
115
116//
117// Related datatypes and constants
118//
119
7e679c4b
AK
120typedef pthread_t THREAD;
121typedef pthread_mutex_t * MUTEX;
d16cf8a5
AK
122struct condition_t
123{
124 pthread_cond_t cond;
125 pthread_mutex_t mutex;
126 BOOL broadcast;
127};
128typedef struct condition_t * CONDITION;
54481027 129
449e3da9
VK
130#define INVALID_MUTEX_HANDLE (NULL)
131#define INVALID_CONDITION_HANDLE (NULL)
54481027 132
7e679c4b
AK
133#ifndef INFINITE
134# define INFINITE 0
135#endif
54481027
VK
136
137//
138// Inline functions
139//
140
d16cf8a5 141inline void ThreadSleep(int nSeconds)
54481027 142{
22412a01
VK
143#ifdef _NETWARE
144 sleep(nSeconds);
145#else
d16cf8a5
AK
146 struct timeval tv;
147
148 tv.tv_sec = nSeconds;
149 tv.tv_usec = 0;
150
151 select(1, NULL, NULL, NULL, &tv);
22412a01 152#endif
54481027
VK
153}
154
155inline void ThreadSleepMs(DWORD dwMilliseconds)
156{
d16cf8a5 157 // select is a sort of overkill
54481027
VK
158 usleep(dwMilliseconds * 1000); // Convert to microseconds
159}
160
93e601e5 161inline THREAD ThreadCreate(void (*start_address )(void *), int stack_size, void *args)
7e679c4b
AK
162{
163 THREAD id;
164
93e601e5 165 if (pthread_create(&id, NULL, (void *(*)(void *))start_address, args) == 0) {
7e679c4b
AK
166 return id;
167 } else {
168 return 0;
169 }
170}
171
172inline void ThreadExit(void)
173{
521d90e7 174 pthread_exit(NULL);
7e679c4b
AK
175}
176
54481027
VK
177inline MUTEX MutexCreate(void)
178{
179 MUTEX mutex;
180
181 mutex = (MUTEX)malloc(sizeof(pthread_mutex_t));
182 if (mutex != NULL)
183 pthread_mutex_init(mutex, NULL);
184 return mutex;
185}
186
187inline void MutexDestroy(MUTEX mutex)
188{
7e679c4b 189 if (mutex != NULL) {
54481027
VK
190 pthread_mutex_destroy(mutex);
191 free(mutex);
192 }
193}
194
53c17a96 195inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
54481027 196{
d16cf8a5
AK
197 int i;
198 int ret = FALSE;
199
7e679c4b 200 if (mutex != NULL) {
951e884e
AK
201 if (dwTimeOut == INFINITE)
202 {
203 if (pthread_mutex_lock(mutex) == 0) {
d16cf8a5 204 ret = TRUE;
d16cf8a5 205 }
951e884e
AK
206 }
207 else
208 {
209 for (i = (dwTimeOut / 50) + 1; i > 0; i--) {
210 if (pthread_mutex_trylock(mutex) != EBUSY) {
211 ret = TRUE;
212 break;
213 }
02ed4cf1 214 ThreadSleepMs(50);
951e884e 215 }
d16cf8a5 216 }
7e679c4b 217 }
d16cf8a5 218 return ret;
54481027
VK
219}
220
221inline void MutexUnlock(MUTEX mutex)
222{
7e679c4b 223 if (mutex != NULL) {
54481027 224 pthread_mutex_unlock(mutex);
7e679c4b
AK
225 }
226}
227
d16cf8a5 228inline CONDITION ConditionCreate(BOOL bBroadcast)
7e679c4b
AK
229{
230 CONDITION cond;
231
d16cf8a5 232 cond = (CONDITION)malloc(sizeof(struct condition_t));
7e679c4b 233 if (cond != NULL) {
d16cf8a5
AK
234 pthread_cond_init(&cond->cond, NULL);
235 pthread_mutex_init(&cond->mutex, NULL);
236 cond->broadcast = bBroadcast;
7e679c4b 237 }
d16cf8a5 238
7e679c4b
AK
239 return cond;
240}
241
d16cf8a5 242inline void ConditionDestroy(CONDITION cond)
7e679c4b 243{
d16cf8a5
AK
244 if (cond != NULL)
245 {
246 pthread_cond_destroy(&cond->cond);
247 pthread_mutex_destroy(&cond->mutex);
248 free(cond);
7e679c4b
AK
249 }
250}
251
d16cf8a5 252inline void ConditionSet(CONDITION cond)
7e679c4b 253{
d16cf8a5
AK
254 if (cond != NULL)
255 {
256 pthread_mutex_lock(&cond->mutex);
02ed4cf1 257 if (cond->broadcast)
d16cf8a5
AK
258 {
259 pthread_cond_broadcast(&cond->cond);
260 }
261 else
262 {
263 pthread_cond_signal(&cond->cond);
264 }
265 pthread_mutex_unlock(&cond->mutex);
266 }
7e679c4b
AK
267}
268
d16cf8a5 269inline BOOL ConditionWait(CONDITION cond, DWORD dwTimeOut)
7e679c4b 270{
d16cf8a5
AK
271 BOOL ret = FALSE;
272
273 if (cond != NULL)
274 {
275 int retcode;
276
277 pthread_mutex_lock(&cond->mutex);
278
279 if (dwTimeOut != INFINITE)
280 {
696fc54f
VK
281#if HAVE_PTHREAD_COND_RELTIMEDWAIT_NP
282 struct timespec timeout;
283
284 timeout.tv_sec = dwTimeOut / 1000;
285 timeout.tv_nsec = (dwTimeOut % 1000) * 1000000;
286 retcode = pthread_cond_reltimedwait_np(&cond->cond, &cond->mutex, &timeout);
287#else
d16cf8a5
AK
288 struct timeval now;
289 struct timespec timeout;
290
291 gettimeofday(&now, NULL);
292 timeout.tv_sec = now.tv_sec + (dwTimeOut / 1000);
63d09869 293 timeout.tv_nsec = ( now.tv_usec + ( dwTimeOut % 1000 ) * 1000) * 1000;
bba4da8a 294 retcode = pthread_cond_timedwait(&cond->cond, &cond->mutex, &timeout);
696fc54f 295#endif
d16cf8a5
AK
296 }
297 else
298 {
299 retcode = pthread_cond_wait(&cond->cond, &cond->mutex);
7e679c4b 300 }
7e679c4b 301
d16cf8a5
AK
302 pthread_mutex_unlock(&cond->mutex);
303
304 if (retcode == 0)
305 {
306 ret = TRUE;
307 }
7e679c4b 308 }
d16cf8a5
AK
309
310 return ret;
54481027
VK
311}
312
313#endif /* _WIN32 */
314
315#endif /* _nms_threads_h_ */