3750f9796ea9ed480061684f1fab0e9c4ca9b35e
[public/netxms.git] / include / nms_threads.h
1 /*
2 ** NetXMS - Network Management System
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
38 #define INVALID_MUTEX_HANDLE INVALID_HANDLE_VALUE
39
40
41 //
42 // Inline functions
43 //
44
45 inline void ThreadSleep(int iSeconds)
46 {
47 Sleep((DWORD)iSeconds * 1000); // Convert to milliseconds
48 }
49
50 inline void ThreadSleepMs(DWORD dwMilliseconds)
51 {
52 Sleep(dwMilliseconds);
53 }
54
55 inline THREAD ThreadCreate(void (__cdecl *start_address )(void *), int stack_size, void *args)
56 {
57 return (THREAD)_beginthread(start_address, stack_size, args);
58 }
59
60 inline void ThreadExit(void)
61 {
62 _endthread();
63 }
64
65 inline MUTEX MutexCreate(void)
66 {
67 return CreateMutex(NULL, FALSE, NULL);
68 }
69
70 inline void MutexDestroy(MUTEX mutex)
71 {
72 CloseHandle(mutex);
73 }
74
75 inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
76 {
77 return WaitForSingleObject(mutex, dwTimeOut) == WAIT_OBJECT_0;
78 }
79
80 inline void MutexUnlock(MUTEX mutex)
81 {
82 ReleaseMutex(mutex);
83 }
84
85 inline CONDITION ConditionCreate(void)
86 {
87 return CreateEvent(NULL, FALSE, FALSE, NULL);
88 }
89
90 inline void ConditionDestroy(CONDITION hCond)
91 {
92 CloseHandle(hCond);
93 }
94
95 inline void ConditionSet(CONDITION hCond)
96 {
97 SetEvent(hCond);
98 }
99
100 inline BOOL ConditionWait(CONDITION hCond, DWORD dwTimeOut)
101 {
102 return WaitForSingleObject(hCond, dwTimeOut) == WAIT_OBJECT_0;
103 }
104
105 #else /* _WIN32 */
106
107 #include <pthread.h>
108
109 //
110 // Related datatypes and constants
111 //
112
113 typedef pthread_t THREAD;
114 typedef pthread_mutex_t * MUTEX;
115 typedef pthread_cond_t * CONDITION;
116
117 #define INVALID_MUTEX_HANDLE NULL
118
119 #ifndef INFINITE
120 # define INFINITE 0
121 #endif
122
123 //
124 // Inline functions
125 //
126
127 inline void ThreadSleep(int iSeconds)
128 {
129 sleep(iSeconds);
130 }
131
132 inline void ThreadSleepMs(DWORD dwMilliseconds)
133 {
134 usleep(dwMilliseconds * 1000); // Convert to microseconds
135 }
136
137 inline THREAD ThreadCreate(void (*start_address )(void *), int stack_size, void *args)
138 {
139 THREAD id;
140
141 if (pthread_create(&id, NULL, (void *(*)(void *))start_address, args) == 0) {
142 return id;
143 } else {
144 return 0;
145 }
146 }
147
148 inline void ThreadExit(void)
149 {
150 pthread_kill(pthread_self(), 15); // 9?
151 }
152
153 inline MUTEX MutexCreate(void)
154 {
155 MUTEX mutex;
156
157 mutex = (MUTEX)malloc(sizeof(pthread_mutex_t));
158 if (mutex != NULL)
159 pthread_mutex_init(mutex, NULL);
160 return mutex;
161 }
162
163 inline void MutexDestroy(MUTEX mutex)
164 {
165 if (mutex != NULL) {
166 pthread_mutex_destroy(mutex);
167 free(mutex);
168 }
169 }
170
171 inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
172 {
173 if (mutex != NULL) {
174 pthread_mutex_lock(mutex); /* TODO: add timeout here */
175 }
176 return TRUE;
177 }
178
179 inline void MutexUnlock(MUTEX mutex)
180 {
181 if (mutex != NULL) {
182 pthread_mutex_unlock(mutex);
183 }
184 }
185
186 inline CONDITION ConditionCreate(void)
187 {
188 CONDITION cond;
189
190 abort(); // trap
191
192 cond = (CONDITION)malloc(sizeof(pthread_cond_t));
193 if (cond != NULL) {
194 pthread_cond_init(cond, NULL);
195 }
196 return cond;
197 }
198
199 inline void ConditionDestroy(CONDITION hCond)
200 {
201 abort(); // trap
202
203 if (hCond != NULL) {
204 pthread_cond_destroy(hCond);
205 free(hCond);
206 }
207 }
208
209 inline void ConditionSet(CONDITION hCond)
210 {
211 abort(); // trap
212 }
213
214 inline BOOL ConditionWait(CONDITION hCond, DWORD dwTimeOut)
215 {
216 abort(); // trap
217
218 if (hCond != NULL) {
219 /* struct timeval now;
220 struct timespec timeout;
221
222 pthread_mutex_lock(&hMutex);
223 gettimeofday(&now);
224 timeout.tv_sec = now.tv_sec + dwTimeOut; // TODO: dwTimeOut - in seconds?
225 timeout.tv_nsec = now.tv_usec * 1000;
226 retcode = 0;
227 while (retcode != ETIMEDOUT) {
228 retcode = pthread_cond_timedwait(hCond, &hMutex, &timeout);
229 }
230 pthread_mutex_unlock(&hMutex);*/
231
232 return FALSE;
233 } else {
234 return FALSE;
235 }
236 }
237
238 #endif /* _WIN32 */
239
240 #endif /* _nms_threads_h_ */