E-Mail sending implemented
[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{
39d7a7ed 98 PulseEvent(hCond);
54481027
VK
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--) {
8a435919
VK
210 if (pthread_mutex_trylock(mutex) == 0)
211 {
951e884e
AK
212 ret = TRUE;
213 break;
214 }
02ed4cf1 215 ThreadSleepMs(50);
951e884e 216 }
d16cf8a5 217 }
7e679c4b 218 }
d16cf8a5 219 return ret;
54481027
VK
220}
221
222inline void MutexUnlock(MUTEX mutex)
223{
8a435919
VK
224 if (mutex != NULL)
225 {
54481027 226 pthread_mutex_unlock(mutex);
7e679c4b
AK
227 }
228}
229
d16cf8a5 230inline CONDITION ConditionCreate(BOOL bBroadcast)
7e679c4b
AK
231{
232 CONDITION cond;
233
d16cf8a5 234 cond = (CONDITION)malloc(sizeof(struct condition_t));
8a435919
VK
235 if (cond != NULL)
236 {
d16cf8a5
AK
237 pthread_cond_init(&cond->cond, NULL);
238 pthread_mutex_init(&cond->mutex, NULL);
239 cond->broadcast = bBroadcast;
7e679c4b 240 }
d16cf8a5 241
7e679c4b
AK
242 return cond;
243}
244
d16cf8a5 245inline void ConditionDestroy(CONDITION cond)
7e679c4b 246{
d16cf8a5
AK
247 if (cond != NULL)
248 {
249 pthread_cond_destroy(&cond->cond);
250 pthread_mutex_destroy(&cond->mutex);
251 free(cond);
7e679c4b
AK
252 }
253}
254
d16cf8a5 255inline void ConditionSet(CONDITION cond)
7e679c4b 256{
d16cf8a5
AK
257 if (cond != NULL)
258 {
259 pthread_mutex_lock(&cond->mutex);
02ed4cf1 260 if (cond->broadcast)
d16cf8a5
AK
261 {
262 pthread_cond_broadcast(&cond->cond);
263 }
264 else
265 {
266 pthread_cond_signal(&cond->cond);
267 }
268 pthread_mutex_unlock(&cond->mutex);
269 }
7e679c4b
AK
270}
271
d16cf8a5 272inline BOOL ConditionWait(CONDITION cond, DWORD dwTimeOut)
7e679c4b 273{
d16cf8a5
AK
274 BOOL ret = FALSE;
275
276 if (cond != NULL)
277 {
278 int retcode;
279
280 pthread_mutex_lock(&cond->mutex);
281
282 if (dwTimeOut != INFINITE)
283 {
696fc54f
VK
284#if HAVE_PTHREAD_COND_RELTIMEDWAIT_NP
285 struct timespec timeout;
286
287 timeout.tv_sec = dwTimeOut / 1000;
288 timeout.tv_nsec = (dwTimeOut % 1000) * 1000000;
289 retcode = pthread_cond_reltimedwait_np(&cond->cond, &cond->mutex, &timeout);
290#else
d16cf8a5
AK
291 struct timeval now;
292 struct timespec timeout;
293
294 gettimeofday(&now, NULL);
295 timeout.tv_sec = now.tv_sec + (dwTimeOut / 1000);
63d09869 296 timeout.tv_nsec = ( now.tv_usec + ( dwTimeOut % 1000 ) * 1000) * 1000;
bba4da8a 297 retcode = pthread_cond_timedwait(&cond->cond, &cond->mutex, &timeout);
696fc54f 298#endif
d16cf8a5
AK
299 }
300 else
301 {
302 retcode = pthread_cond_wait(&cond->cond, &cond->mutex);
7e679c4b 303 }
7e679c4b 304
d16cf8a5
AK
305 pthread_mutex_unlock(&cond->mutex);
306
307 if (retcode == 0)
308 {
309 ret = TRUE;
310 }
7e679c4b 311 }
d16cf8a5
AK
312
313 return ret;
54481027
VK
314}
315
316#endif /* _WIN32 */
317
318#endif /* _nms_threads_h_ */