diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2014-04-01 06:16:04 +0000 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2014-04-01 06:16:04 +0000 |
commit | 60d4fbee43fdb6abb2f122017bdb0e084fa4a9c6 (patch) | |
tree | 35ae0402e7d59c1e1defec2836dc3c3f6333060a /src/include | |
parent | 36394bd3bd84951b685f45ff105d256550619211 (diff) | |
download | libmicrohttpd-60d4fbee43fdb6abb2f122017bdb0e084fa4a9c6.tar.gz libmicrohttpd-60d4fbee43fdb6abb2f122017bdb0e084fa4a9c6.zip |
Implement wrapper macros for mutex manipulations, support W32 native mutex, require at least WinXP headers on W32
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/platform.h | 10 | ||||
-rw-r--r-- | src/include/platform_interface.h | 112 |
2 files changed, 121 insertions, 1 deletions
diff --git a/src/include/platform.h b/src/include/platform.h index 18d93b16..e97ab9f1 100644 --- a/src/include/platform.h +++ b/src/include/platform.h | |||
@@ -51,6 +51,16 @@ | |||
51 | #define _LP64 | 51 | #define _LP64 |
52 | #endif | 52 | #endif |
53 | 53 | ||
54 | #if defined(_WIN32) | ||
55 | #ifndef _WIN32_WINNT | ||
56 | #define _WIN32_WINNT 0x0501 | ||
57 | #else // _WIN32_WINNT | ||
58 | #if _WIN32_WINNT < 0x0501 | ||
59 | #error "Headers for Windows XP or later are required" | ||
60 | #endif // _WIN32_WINNT < 0x0501 | ||
61 | #endif // _WIN32_WINNT | ||
62 | #endif // _WIN32 | ||
63 | |||
54 | #include <stdio.h> | 64 | #include <stdio.h> |
55 | #include <stdlib.h> | 65 | #include <stdlib.h> |
56 | #include <stdint.h> | 66 | #include <stdint.h> |
diff --git a/src/include/platform_interface.h b/src/include/platform_interface.h index 4b016039..97c238a7 100644 --- a/src/include/platform_interface.h +++ b/src/include/platform_interface.h | |||
@@ -18,7 +18,7 @@ | |||
18 | */ | 18 | */ |
19 | 19 | ||
20 | /** | 20 | /** |
21 | * @file platform/platfrom_interface.h | 21 | * @file include/platfrom_interface.h |
22 | * @brief internal platform abstraction functions | 22 | * @brief internal platform abstraction functions |
23 | * @author Karlson2k (Evgeny Grin) | 23 | * @author Karlson2k (Evgeny Grin) |
24 | */ | 24 | */ |
@@ -139,4 +139,114 @@ | |||
139 | #define MHD_random_() MHD_W32_random_() | 139 | #define MHD_random_() MHD_W32_random_() |
140 | #endif | 140 | #endif |
141 | 141 | ||
142 | #if defined(_WIN32) && !defined(__CYGWIN__) | ||
143 | #define MHD_W32_MUTEX_ 1 | ||
144 | /* 'void*' is the same as 'HANDLE' | ||
145 | * this way allow typedef without including "windows.h" */ | ||
146 | typedef void* MHD_mutex_; | ||
147 | #elif defined(HAVE_PTHREAD_H) | ||
148 | #define MHD_PTHREAD_MUTEX_ 1 | ||
149 | typedef pthread_mutex_t MHD_mutex_; | ||
150 | #else | ||
151 | #error "No base mutex API is available." | ||
152 | #endif | ||
153 | |||
154 | #if defined(MHD_PTHREAD_MUTEX_) | ||
155 | /** | ||
156 | * Create new mutex. | ||
157 | * @param mutex pointer to the mutex | ||
158 | * @return #MHD_YES on success, #MHD_NO on failure | ||
159 | */ | ||
160 | #define MHD_mutex_create_(mutex) \ | ||
161 | ((0 == pthread_mutex_init ((mutex), NULL)) ? MHD_YES : MHD_NO) | ||
162 | #elif defined(MHD_W32_MUTEX_) | ||
163 | /** | ||
164 | * Create new mutex. | ||
165 | * @param mutex pointer to mutex | ||
166 | * @return #MHD_YES on success, #MHD_NO on failure | ||
167 | */ | ||
168 | #define MHD_mutex_create_(mutex) \ | ||
169 | ((NULL != (mutex) && NULL != (*(mutex) = CreateMutex(NULL, FALSE, NULL))) ? MHD_YES : MHD_NO) | ||
170 | #endif | ||
171 | |||
172 | #if defined(MHD_PTHREAD_MUTEX_) | ||
173 | /** | ||
174 | * Destroy previously created mutex. | ||
175 | * @param mutex pointer to mutex | ||
176 | * @return #MHD_YES on success, #MHD_NO on failure | ||
177 | */ | ||
178 | #define MHD_mutex_destroy_(mutex) \ | ||
179 | ((0 == pthread_mutex_destroy ((mutex), NULL)) ? MHD_YES : MHD_NO) | ||
180 | #elif defined(MHD_W32_MUTEX_) | ||
181 | /** | ||
182 | * Destroy previously created mutex. | ||
183 | * @param mutex pointer to mutex | ||
184 | * @return #MHD_YES on success, #MHD_NO on failure | ||
185 | */ | ||
186 | #define MHD_mutex_destroy_(mutex) \ | ||
187 | ((NULL != (mutex) && 0 != CloseHandle(*(mutex)) && NULL == (*(mutex) = NULL)) ? MHD_YES : MHD_NO) | ||
188 | #endif | ||
189 | |||
190 | #if defined(MHD_PTHREAD_MUTEX_) | ||
191 | /** | ||
192 | * Acquire lock on previously created mutex. | ||
193 | * If mutex was already locked by other thread, function | ||
194 | * blocks until mutex becomes available. | ||
195 | * @param mutex pointer to mutex | ||
196 | * @return #MHD_YES on success, #MHD_NO on failure | ||
197 | */ | ||
198 | #define MHD_mutex_lock_(mutex) \ | ||
199 | ((0 == pthread_mutex_lock((mutex), NULL)) ? MHD_YES : MHD_NO) | ||
200 | #elif defined(MHD_W32_MUTEX_) | ||
201 | /** | ||
202 | * Acquire lock on previously created mutex. | ||
203 | * If mutex was already locked by other thread, function | ||
204 | * blocks until mutex becomes available. | ||
205 | * @param mutex pointer to mutex | ||
206 | * @return #MHD_YES on success, #MHD_NO on failure | ||
207 | */ | ||
208 | #define MHD_mutex_lock_(mutex) \ | ||
209 | ((NULL != (mutex) && WAIT_OBJECT_0 == WaitForSingleObject(*(mutex), INFINITE)) ? MHD_YES : MHD_NO) | ||
210 | #endif | ||
211 | |||
212 | #if defined(MHD_PTHREAD_MUTEX_) | ||
213 | /** | ||
214 | * Try to acquire lock on previously created mutex. | ||
215 | * Function returns immediately. | ||
216 | * @param mutex pointer to mutex | ||
217 | * @return #MHD_YES if mutex is locked, #MHD_NO if | ||
218 | * mutex was not locked. | ||
219 | */ | ||
220 | #define MHD_mutex_trylock_(mutex) \ | ||
221 | ((0 == pthread_mutex_trylock((mutex), NULL)) ? MHD_YES : MHD_NO) | ||
222 | #elif defined(MHD_W32_MUTEX_) | ||
223 | /** | ||
224 | * Try to acquire lock on previously created mutex. | ||
225 | * Function returns immediately. | ||
226 | * @param mutex pointer to mutex | ||
227 | * @return #MHD_YES if mutex is locked, #MHD_NO if | ||
228 | * mutex was not locked. | ||
229 | */ | ||
230 | #define MHD_mutex_trylock_(mutex) \ | ||
231 | ((NULL != (mutex) && WAIT_OBJECT_0 == WaitForSingleObject(*(mutex), 0)) ? MHD_YES : MHD_NO) | ||
232 | #endif | ||
233 | |||
234 | #if defined(MHD_PTHREAD_MUTEX_) | ||
235 | /** | ||
236 | * Unlock previously created and locked mutex. | ||
237 | * @param mutex pointer to mutex | ||
238 | * @return #MHD_YES on success, #MHD_NO on failure | ||
239 | */ | ||
240 | #define MHD_mutex_unlock_(mutex) \ | ||
241 | ((0 == pthread_mutex_unlock((mutex), NULL)) ? MHD_YES : MHD_NO) | ||
242 | #elif defined(MHD_W32_MUTEX_) | ||
243 | /** | ||
244 | * Unlock previously created and locked mutex. | ||
245 | * @param mutex pointer to mutex | ||
246 | * @return #MHD_YES on success, #MHD_NO on failure | ||
247 | */ | ||
248 | #define MHD_mutex_unlock_(mutex) \ | ||
249 | ((NULL != (mutex) && 0 != ReleaseMutex(*(mutex))) ? MHD_YES : MHD_NO) | ||
250 | #endif | ||
251 | |||
142 | #endif // MHD_PLATFORM_INTERFACE_H | 252 | #endif // MHD_PLATFORM_INTERFACE_H |