Changes in object update code
[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
38#define INVALID_MUTEX_HANDLE INVALID_HANDLE_VALUE
39
40
41//
42// Inline functions
43//
44
45inline void ThreadSleep(int iSeconds)
46{
47 Sleep((DWORD)iSeconds * 1000); // Convert to milliseconds
48}
49
50inline void ThreadSleepMs(DWORD dwMilliseconds)
51{
52 Sleep(dwMilliseconds);
53}
54
55inline THREAD ThreadCreate(void (__cdecl *start_address )(void *), int stack_size, void *args)
56{
57 return (THREAD)_beginthread(start_address, stack_size, args);
58}
59
60inline void ThreadExit(void)
61{
62 _endthread();
63}
64
65inline MUTEX MutexCreate(void)
66{
67 return CreateMutex(NULL, FALSE, NULL);
68}
69
70inline void MutexDestroy(MUTEX mutex)
71{
72 CloseHandle(mutex);
73}
74
75inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
76{
77 return WaitForSingleObject(mutex, dwTimeOut) == WAIT_OBJECT_0;
78}
79
80inline void MutexUnlock(MUTEX mutex)
81{
82 ReleaseMutex(mutex);
83}
84
85inline CONDITION ConditionCreate(void)
86{
87 return CreateEvent(NULL, FALSE, FALSE, NULL);
88}
89
90inline void ConditionDestroy(CONDITION hCond)
91{
92 CloseHandle(hCond);
93}
94
95inline void ConditionSet(CONDITION hCond)
96{
97 SetEvent(hCond);
98}
99
c7f4f5a9 100inline BOOL ConditionWait(CONDITION hCond, DWORD dwTimeOut)
54481027 101{
c7f4f5a9 102 return WaitForSingleObject(hCond, dwTimeOut) == WAIT_OBJECT_0;
54481027
VK
103}
104
105#else /* _WIN32 */
106
107#include <pthread.h>
108
109//
110// Related datatypes and constants
111//
112
7e679c4b
AK
113typedef pthread_t THREAD;
114typedef pthread_mutex_t * MUTEX;
115typedef pthread_cond_t * CONDITION;
54481027
VK
116
117#define INVALID_MUTEX_HANDLE NULL
118
7e679c4b
AK
119#ifndef INFINITE
120# define INFINITE 0
121#endif
54481027
VK
122
123//
124// Inline functions
125//
126
127inline void ThreadSleep(int iSeconds)
128{
129 sleep(iSeconds);
130}
131
132inline void ThreadSleepMs(DWORD dwMilliseconds)
133{
134 usleep(dwMilliseconds * 1000); // Convert to microseconds
135}
136
93e601e5 137inline THREAD ThreadCreate(void (*start_address )(void *), int stack_size, void *args)
7e679c4b
AK
138{
139 THREAD id;
140
93e601e5 141 if (pthread_create(&id, NULL, (void *(*)(void *))start_address, args) == 0) {
7e679c4b
AK
142 return id;
143 } else {
144 return 0;
145 }
146}
147
148inline void ThreadExit(void)
149{
150 pthread_kill(pthread_self(), 15); // 9?
151}
152
54481027
VK
153inline 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
163inline void MutexDestroy(MUTEX mutex)
164{
7e679c4b 165 if (mutex != NULL) {
54481027
VK
166 pthread_mutex_destroy(mutex);
167 free(mutex);
168 }
169}
170
53c17a96 171inline BOOL MutexLock(MUTEX mutex, DWORD dwTimeOut)
54481027 172{
7e679c4b 173 if (mutex != NULL) {
53c17a96 174 pthread_mutex_lock(mutex); /* TODO: add timeout here */
7e679c4b 175 }
53c17a96 176 return TRUE;
54481027
VK
177}
178
179inline void MutexUnlock(MUTEX mutex)
180{
7e679c4b 181 if (mutex != NULL) {
54481027 182 pthread_mutex_unlock(mutex);
7e679c4b
AK
183 }
184}
185
186inline 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
199inline void ConditionDestroy(CONDITION hCond)
200{
201 abort(); // trap
202
203 if (hCond != NULL) {
204 pthread_cond_destroy(hCond);
205 free(hCond);
206 }
207}
208
209inline void ConditionSet(CONDITION hCond)
210{
211 abort(); // trap
212}
213
214inline 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 }
54481027
VK
236}
237
238#endif /* _WIN32 */
239
240#endif /* _nms_threads_h_ */