Removed unneeded -lsnmp
[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{
d16cf8a5
AK
143 struct timeval tv;
144
145 tv.tv_sec = nSeconds;
146 tv.tv_usec = 0;
147
148 select(1, NULL, NULL, NULL, &tv);
54481027
VK
149}
150
151inline void ThreadSleepMs(DWORD dwMilliseconds)
152{
d16cf8a5 153 // select is a sort of overkill
54481027
VK
154 usleep(dwMilliseconds * 1000); // Convert to microseconds
155}
156
93e601e5 157inline THREAD ThreadCreate(void (*start_address )(void *), int stack_size, void *args)
7e679c4b
AK
158{
159 THREAD id;
160
93e601e5 161 if (pthread_create(&id, NULL, (void *(*)(void *))start_address, args) == 0) {
7e679c4b
AK
162 return id;
163 } else {
164 return 0;
165 }
166}
167
168inline void ThreadExit(void)
169{
521d90e7 170 pthread_exit(NULL);
7e679c4b
AK
171}
172
54481027
VK
173inline MUTEX MutexCreate(void)
174{
175 MUTEX mutex;
176
177 mutex = (MUTEX)malloc(sizeof(pthread_mutex_t));
178 if (mutex != NULL)
179 pthread_mutex_init(mutex, NULL);
180 return mutex;
181}
182
183inline void MutexDestroy(MUTEX mutex)
184{
7e679c4b 185 if (mutex != NULL) {
54481027
VK
186 pthread_mutex_destroy(mutex);
187 free(mutex);
188 }
189}
190
53c17a96 191inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
54481027 192{
d16cf8a5
AK
193 int i;
194 int ret = FALSE;
195
7e679c4b 196 if (mutex != NULL) {
951e884e
AK
197 if (dwTimeOut == INFINITE)
198 {
199 if (pthread_mutex_lock(mutex) == 0) {
d16cf8a5 200 ret = TRUE;
d16cf8a5 201 }
951e884e
AK
202 }
203 else
204 {
205 for (i = (dwTimeOut / 50) + 1; i > 0; i--) {
206 if (pthread_mutex_trylock(mutex) != EBUSY) {
207 ret = TRUE;
208 break;
209 }
02ed4cf1 210 ThreadSleepMs(50);
951e884e 211 }
d16cf8a5 212 }
7e679c4b 213 }
d16cf8a5 214 return ret;
54481027
VK
215}
216
217inline void MutexUnlock(MUTEX mutex)
218{
7e679c4b 219 if (mutex != NULL) {
54481027 220 pthread_mutex_unlock(mutex);
7e679c4b
AK
221 }
222}
223
d16cf8a5 224inline CONDITION ConditionCreate(BOOL bBroadcast)
7e679c4b
AK
225{
226 CONDITION cond;
227
d16cf8a5 228 cond = (CONDITION)malloc(sizeof(struct condition_t));
7e679c4b 229 if (cond != NULL) {
d16cf8a5
AK
230 pthread_cond_init(&cond->cond, NULL);
231 pthread_mutex_init(&cond->mutex, NULL);
232 cond->broadcast = bBroadcast;
7e679c4b 233 }
d16cf8a5 234
7e679c4b
AK
235 return cond;
236}
237
d16cf8a5 238inline void ConditionDestroy(CONDITION cond)
7e679c4b 239{
d16cf8a5
AK
240 if (cond != NULL)
241 {
242 pthread_cond_destroy(&cond->cond);
243 pthread_mutex_destroy(&cond->mutex);
244 free(cond);
7e679c4b
AK
245 }
246}
247
d16cf8a5 248inline void ConditionSet(CONDITION cond)
7e679c4b 249{
d16cf8a5
AK
250 if (cond != NULL)
251 {
252 pthread_mutex_lock(&cond->mutex);
02ed4cf1 253 if (cond->broadcast)
d16cf8a5
AK
254 {
255 pthread_cond_broadcast(&cond->cond);
256 }
257 else
258 {
259 pthread_cond_signal(&cond->cond);
260 }
261 pthread_mutex_unlock(&cond->mutex);
262 }
7e679c4b
AK
263}
264
d16cf8a5 265inline BOOL ConditionWait(CONDITION cond, DWORD dwTimeOut)
7e679c4b 266{
d16cf8a5
AK
267 BOOL ret = FALSE;
268
269 if (cond != NULL)
270 {
271 int retcode;
272
273 pthread_mutex_lock(&cond->mutex);
274
275 if (dwTimeOut != INFINITE)
276 {
277 struct timeval now;
278 struct timespec timeout;
279
280 gettimeofday(&now, NULL);
281 timeout.tv_sec = now.tv_sec + (dwTimeOut / 1000);
63d09869 282 timeout.tv_nsec = ( now.tv_usec + ( dwTimeOut % 1000 ) * 1000) * 1000;
bba4da8a 283 retcode = pthread_cond_timedwait(&cond->cond, &cond->mutex, &timeout);
d16cf8a5
AK
284 }
285 else
286 {
287 retcode = pthread_cond_wait(&cond->cond, &cond->mutex);
7e679c4b 288 }
7e679c4b 289
d16cf8a5
AK
290 pthread_mutex_unlock(&cond->mutex);
291
292 if (retcode == 0)
293 {
294 ret = TRUE;
295 }
7e679c4b 296 }
d16cf8a5
AK
297
298 return ret;
54481027
VK
299}
300
301#endif /* _WIN32 */
302
303#endif /* _nms_threads_h_ */