Multiple unfinished changes
[public/netxms.git] / include / nms_threads.h
CommitLineData
54481027
VK
1/*
2** Project X - 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
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
100inline void ConditionWait(CONDITION hCond, DWORD dwTimeOut)
101{
102 WaitForSingleObject(hCond, dwTimeOut);
103}
104
105#else /* _WIN32 */
106
107#include <pthread.h>
108
109//
110// Related datatypes and constants
111//
112
113typedef MUTEX * pthread_mutex_t;
114#define THREAD HANDLE
115
116#define INVALID_MUTEX_HANDLE NULL
117
118
119//
120// Inline functions
121//
122
123inline void ThreadSleep(int iSeconds)
124{
125 sleep(iSeconds);
126}
127
128inline void ThreadSleepMs(DWORD dwMilliseconds)
129{
130 usleep(dwMilliseconds * 1000); // Convert to microseconds
131}
132
133inline MUTEX MutexCreate(void)
134{
135 MUTEX mutex;
136
137 mutex = (MUTEX)malloc(sizeof(pthread_mutex_t));
138 if (mutex != NULL)
139 pthread_mutex_init(mutex, NULL);
140 return mutex;
141}
142
143inline void MutexDestroy(MUTEX mutex)
144{
145 if (mutex != NULL)
146 {
147 pthread_mutex_destroy(mutex);
148 free(mutex);
149 }
150}
151
152inline void MutexLock(MUTEX mutex, DWORD dwTimeOut)
153{
154}
155
156inline void MutexUnlock(MUTEX mutex)
157{
158 if (mutex != NULL)
159 pthread_mutex_unlock(mutex);
160}
161
162#endif /* _WIN32 */
163
164#endif /* _nms_threads_h_ */