/src/pupnp/upnp/inc/ithread.h
Line | Count | Source |
1 | | #ifndef ITHREAD_H |
2 | | #define ITHREAD_H |
3 | | |
4 | | /******************************************************************************* |
5 | | * |
6 | | * Copyright (c) 2000-2003 Intel Corporation |
7 | | * All rights reserved. |
8 | | * Copyright (c) 2012 France Telecom All rights reserved. |
9 | | * |
10 | | * Redistribution and use in source and binary forms, with or without |
11 | | * modification, are permitted provided that the following conditions are met: |
12 | | * |
13 | | * * Redistributions of source code must retain the above copyright notice, |
14 | | * this list of conditions and the following disclaimer. |
15 | | * * Redistributions in binary form must reproduce the above copyright notice, |
16 | | * this list of conditions and the following disclaimer in the documentation |
17 | | * and/or other materials provided with the distribution. |
18 | | * * Neither name of Intel Corporation nor the names of its contributors |
19 | | * may be used to endorse or promote products derived from this software |
20 | | * without specific prior written permission. |
21 | | * |
22 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
23 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
24 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
25 | | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR |
26 | | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
27 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
28 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
29 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
30 | | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
31 | | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
32 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
33 | | * |
34 | | ******************************************************************************/ |
35 | | |
36 | | /*! |
37 | | * \file |
38 | | */ |
39 | | |
40 | | #if !defined(_WIN32) |
41 | | #include <sys/param.h> |
42 | | #endif |
43 | | |
44 | | #include "UpnpGlobal.h" /* For UPNP_INLINE, UPNP_EXPORT_SPEC */ |
45 | | #include "UpnpUniStd.h" /* for close() */ |
46 | | |
47 | | #ifdef __cplusplus |
48 | | extern "C" { |
49 | | #endif |
50 | | |
51 | | #include <pthread.h> |
52 | | |
53 | | #if defined(BSD) && !defined(__GNU__) |
54 | | #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE |
55 | | #endif |
56 | | |
57 | | #if defined(PTHREAD_MUTEX_RECURSIVE) || defined(__DragonFly__) |
58 | | /* This system has SuS2-compliant mutex attributes. |
59 | | * E.g. on Cygwin, where we don't have the old nonportable (NP) symbols |
60 | | */ |
61 | | #define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_NORMAL |
62 | | #define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE |
63 | | #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK |
64 | | #else /* PTHREAD_MUTEX_RECURSIVE */ |
65 | | #define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_FAST_NP |
66 | | #define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE_NP |
67 | | #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK_NP |
68 | | #endif /* PTHREAD_MUTEX_RECURSIVE */ |
69 | | |
70 | | #define ITHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE |
71 | | #define ITHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED |
72 | | |
73 | | #define ITHREAD_CANCELED PTHREAD_CANCELED |
74 | | |
75 | | #define ITHREAD_STACK_MIN PTHREAD_STACK_MIN |
76 | 0 | #define ITHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED |
77 | | #define ITHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE |
78 | | |
79 | | /*************************************************************************** |
80 | | * Name: ithread_t |
81 | | * |
82 | | * Description: |
83 | | * Thread handle. |
84 | | * typedef to pthread_t. |
85 | | * Internal Use Only. |
86 | | ***************************************************************************/ |
87 | | typedef pthread_t ithread_t; |
88 | | |
89 | | /**************************************************************************** |
90 | | * Name: ithread_attr_t |
91 | | * |
92 | | * Description: |
93 | | * Thread attribute. |
94 | | * typedef to pthread_attr_t |
95 | | * Internal Use Only |
96 | | ***************************************************************************/ |
97 | | typedef pthread_attr_t ithread_attr_t; |
98 | | |
99 | | /**************************************************************************** |
100 | | * Name: start_routine |
101 | | * |
102 | | * Description: |
103 | | * Thread start routine |
104 | | * Internal Use Only. |
105 | | ***************************************************************************/ |
106 | | typedef void (*start_routine)(void *arg); |
107 | | |
108 | | /**************************************************************************** |
109 | | * Name: ithread_cond_t |
110 | | * |
111 | | * Description: |
112 | | * condition variable. |
113 | | * typedef to pthread_cond_t |
114 | | * Internal Use Only. |
115 | | ***************************************************************************/ |
116 | | typedef pthread_cond_t ithread_cond_t; |
117 | | |
118 | | /**************************************************************************** |
119 | | * Name: ithread_mutexattr_t |
120 | | * |
121 | | * Description: |
122 | | * Mutex attribute. |
123 | | * typedef to pthread_mutexattr_t |
124 | | * Internal Use Only |
125 | | ***************************************************************************/ |
126 | | typedef pthread_mutexattr_t ithread_mutexattr_t; |
127 | | |
128 | | /**************************************************************************** |
129 | | * Name: ithread_mutex_t |
130 | | * |
131 | | * Description: |
132 | | * Mutex. |
133 | | * typedef to pthread_mutex_t |
134 | | * Internal Use Only. |
135 | | ***************************************************************************/ |
136 | | typedef pthread_mutex_t ithread_mutex_t; |
137 | | |
138 | | /**************************************************************************** |
139 | | * Name: ithread_condattr_t |
140 | | * |
141 | | * Description: |
142 | | * Condition attribute. |
143 | | * typedef to pthread_condattr_t |
144 | | * NOT USED |
145 | | * Internal Use Only |
146 | | ***************************************************************************/ |
147 | | typedef pthread_condattr_t ithread_condattr_t; |
148 | | |
149 | | /* UPNP_USE_RWLOCK is defined in autoconfig.h, pulled in by config.h. |
150 | | * Include config.h now if the caller has not done so: without it |
151 | | * UPNP_USE_RWLOCK is 0 and ithread_rwlock_t silently becomes |
152 | | * pthread_mutex_t, which causes a deadlock when used in place of the |
153 | | * pthread_rwlock_t that upnpapi.c uses for GlobalHndRWLock. */ |
154 | | #ifndef INTERNAL_CONFIG_H |
155 | | #include "config.h" /* IWYU pragma: keep */ |
156 | | #endif |
157 | | |
158 | | /**************************************************************************** |
159 | | * Name: ithread_rwlockattr_t |
160 | | * |
161 | | * Description: |
162 | | * Mutex attribute. |
163 | | * typedef to pthread_rwlockattr_t |
164 | | * Internal Use Only |
165 | | ***************************************************************************/ |
166 | | #if UPNP_USE_RWLOCK |
167 | | typedef pthread_rwlockattr_t ithread_rwlockattr_t; |
168 | | #endif /* UPNP_USE_RWLOCK */ |
169 | | |
170 | | /**************************************************************************** |
171 | | * Name: ithread_rwlock_t |
172 | | * |
173 | | * Description: |
174 | | * Condition attribute. |
175 | | * typedef to pthread_rwlock_t |
176 | | * Internal Use Only |
177 | | ***************************************************************************/ |
178 | | #if UPNP_USE_RWLOCK |
179 | | typedef pthread_rwlock_t ithread_rwlock_t; |
180 | | #else |
181 | | /* Read-write locks aren't available on this platform: use mutex instead. */ |
182 | | typedef ithread_mutex_t ithread_rwlock_t; |
183 | | #endif /* UPNP_USE_RWLOCK */ |
184 | | |
185 | | /**************************************************************************** |
186 | | * Function: ithread_initialize_library |
187 | | * |
188 | | * Description: |
189 | | * Initializes the library. Does nothing in all implementations, except |
190 | | * when statically linked for WIN32. |
191 | | * Parameters: |
192 | | * none. |
193 | | * Returns: |
194 | | * 0 on success, Nonzero on failure. |
195 | | ***************************************************************************/ |
196 | | static UPNP_INLINE int ithread_initialize_library(void) |
197 | 0 | { |
198 | 0 | int ret = 0; |
199 | |
|
200 | 0 | return ret; |
201 | 0 | } Unexecuted instantiation: httpparser.c:ithread_initialize_library Unexecuted instantiation: uri.c:ithread_initialize_library Unexecuted instantiation: upnpapi.c:ithread_initialize_library Unexecuted instantiation: upnpdebug.c:ithread_initialize_library Unexecuted instantiation: miniserver.c:ithread_initialize_library Unexecuted instantiation: httpreadwrite.c:ithread_initialize_library Unexecuted instantiation: statcodes.c:ithread_initialize_library Unexecuted instantiation: webserver.c:ithread_initialize_library Unexecuted instantiation: ThreadPool.c:ithread_initialize_library Unexecuted instantiation: TimerThread.c:ithread_initialize_library Unexecuted instantiation: gena_callback2.c:ithread_initialize_library Unexecuted instantiation: gena_ctrlpt.c:ithread_initialize_library Unexecuted instantiation: gena_device.c:ithread_initialize_library Unexecuted instantiation: soap_ctrlpt.c:ithread_initialize_library Unexecuted instantiation: soap_device.c:ithread_initialize_library Unexecuted instantiation: ssdp_ctrlpt.c:ithread_initialize_library Unexecuted instantiation: ssdp_server.c:ithread_initialize_library Unexecuted instantiation: uuid.c:ithread_initialize_library Unexecuted instantiation: client_table.c:ithread_initialize_library Unexecuted instantiation: ssdp_device.c:ithread_initialize_library Unexecuted instantiation: sysdep.c:ithread_initialize_library |
202 | | |
203 | | /**************************************************************************** |
204 | | * Function: ithread_cleanup_library |
205 | | * |
206 | | * Description: |
207 | | * Clean up library resources. Does nothing in all implementations, except |
208 | | * when statically linked for WIN32. |
209 | | * Parameters: |
210 | | * none. |
211 | | * Returns: |
212 | | * 0 on success, Nonzero on failure. |
213 | | ***************************************************************************/ |
214 | | static UPNP_INLINE int ithread_cleanup_library(void) |
215 | 0 | { |
216 | 0 | int ret = 0; |
217 | |
|
218 | 0 | return ret; |
219 | 0 | } Unexecuted instantiation: httpparser.c:ithread_cleanup_library Unexecuted instantiation: uri.c:ithread_cleanup_library Unexecuted instantiation: upnpapi.c:ithread_cleanup_library Unexecuted instantiation: upnpdebug.c:ithread_cleanup_library Unexecuted instantiation: miniserver.c:ithread_cleanup_library Unexecuted instantiation: httpreadwrite.c:ithread_cleanup_library Unexecuted instantiation: statcodes.c:ithread_cleanup_library Unexecuted instantiation: webserver.c:ithread_cleanup_library Unexecuted instantiation: ThreadPool.c:ithread_cleanup_library Unexecuted instantiation: TimerThread.c:ithread_cleanup_library Unexecuted instantiation: gena_callback2.c:ithread_cleanup_library Unexecuted instantiation: gena_ctrlpt.c:ithread_cleanup_library Unexecuted instantiation: gena_device.c:ithread_cleanup_library Unexecuted instantiation: soap_ctrlpt.c:ithread_cleanup_library Unexecuted instantiation: soap_device.c:ithread_cleanup_library Unexecuted instantiation: ssdp_ctrlpt.c:ithread_cleanup_library Unexecuted instantiation: ssdp_server.c:ithread_cleanup_library Unexecuted instantiation: uuid.c:ithread_cleanup_library Unexecuted instantiation: client_table.c:ithread_cleanup_library Unexecuted instantiation: ssdp_device.c:ithread_cleanup_library Unexecuted instantiation: sysdep.c:ithread_cleanup_library |
220 | | |
221 | | /**************************************************************************** |
222 | | * Function: ithread_initialize_thread |
223 | | * |
224 | | * Description: |
225 | | * Initializes the thread. Does nothing in all implementations, except |
226 | | * when statically linked for WIN32. |
227 | | * Parameters: |
228 | | * none. |
229 | | * Returns: |
230 | | * 0 on success, Nonzero on failure. |
231 | | ***************************************************************************/ |
232 | | static UPNP_INLINE int ithread_initialize_thread(void) |
233 | 0 | { |
234 | 0 | int ret = 0; |
235 | |
|
236 | | #if defined(_WIN32) && defined(PTW32_STATIC_LIB) |
237 | | ret = !pthread_win32_thread_attach_np(); |
238 | | #endif |
239 | |
|
240 | 0 | return ret; |
241 | 0 | } Unexecuted instantiation: httpparser.c:ithread_initialize_thread Unexecuted instantiation: uri.c:ithread_initialize_thread Unexecuted instantiation: upnpapi.c:ithread_initialize_thread Unexecuted instantiation: upnpdebug.c:ithread_initialize_thread Unexecuted instantiation: miniserver.c:ithread_initialize_thread Unexecuted instantiation: httpreadwrite.c:ithread_initialize_thread Unexecuted instantiation: statcodes.c:ithread_initialize_thread Unexecuted instantiation: webserver.c:ithread_initialize_thread Unexecuted instantiation: ThreadPool.c:ithread_initialize_thread Unexecuted instantiation: TimerThread.c:ithread_initialize_thread Unexecuted instantiation: gena_callback2.c:ithread_initialize_thread Unexecuted instantiation: gena_ctrlpt.c:ithread_initialize_thread Unexecuted instantiation: gena_device.c:ithread_initialize_thread Unexecuted instantiation: soap_ctrlpt.c:ithread_initialize_thread Unexecuted instantiation: soap_device.c:ithread_initialize_thread Unexecuted instantiation: ssdp_ctrlpt.c:ithread_initialize_thread Unexecuted instantiation: ssdp_server.c:ithread_initialize_thread Unexecuted instantiation: uuid.c:ithread_initialize_thread Unexecuted instantiation: client_table.c:ithread_initialize_thread Unexecuted instantiation: ssdp_device.c:ithread_initialize_thread Unexecuted instantiation: sysdep.c:ithread_initialize_thread |
242 | | |
243 | | /**************************************************************************** |
244 | | * Function: ithread_cleanup_thread |
245 | | * |
246 | | * Description: |
247 | | * Clean up thread resources. Does nothing in all implementations, except |
248 | | * when statically linked for WIN32. |
249 | | * Parameters: |
250 | | * none. |
251 | | * Returns: |
252 | | * 0 on success, Nonzero on failure. |
253 | | ***************************************************************************/ |
254 | | static UPNP_INLINE int ithread_cleanup_thread(void) |
255 | 0 | { |
256 | 0 | int ret = 0; |
257 | |
|
258 | | #if defined(_WIN32) && defined(PTW32_STATIC_LIB) |
259 | | ret = !pthread_win32_thread_detach_np(); |
260 | | #endif |
261 | |
|
262 | 0 | return ret; |
263 | 0 | } Unexecuted instantiation: httpparser.c:ithread_cleanup_thread Unexecuted instantiation: uri.c:ithread_cleanup_thread Unexecuted instantiation: upnpapi.c:ithread_cleanup_thread Unexecuted instantiation: upnpdebug.c:ithread_cleanup_thread Unexecuted instantiation: miniserver.c:ithread_cleanup_thread Unexecuted instantiation: httpreadwrite.c:ithread_cleanup_thread Unexecuted instantiation: statcodes.c:ithread_cleanup_thread Unexecuted instantiation: webserver.c:ithread_cleanup_thread Unexecuted instantiation: ThreadPool.c:ithread_cleanup_thread Unexecuted instantiation: TimerThread.c:ithread_cleanup_thread Unexecuted instantiation: gena_callback2.c:ithread_cleanup_thread Unexecuted instantiation: gena_ctrlpt.c:ithread_cleanup_thread Unexecuted instantiation: gena_device.c:ithread_cleanup_thread Unexecuted instantiation: soap_ctrlpt.c:ithread_cleanup_thread Unexecuted instantiation: soap_device.c:ithread_cleanup_thread Unexecuted instantiation: ssdp_ctrlpt.c:ithread_cleanup_thread Unexecuted instantiation: ssdp_server.c:ithread_cleanup_thread Unexecuted instantiation: uuid.c:ithread_cleanup_thread Unexecuted instantiation: client_table.c:ithread_cleanup_thread Unexecuted instantiation: ssdp_device.c:ithread_cleanup_thread Unexecuted instantiation: sysdep.c:ithread_cleanup_thread |
264 | | |
265 | | /**************************************************************************** |
266 | | * Function: ithread_mutexattr_init |
267 | | * |
268 | | * Description: |
269 | | * Initializes a mutex attribute variable. |
270 | | * Used to set the type of the mutex. |
271 | | * Parameters: |
272 | | * ithread_mutexattr_init * attr (must be valid non NULL pointer to |
273 | | * pthread_mutexattr_t) |
274 | | * Returns: |
275 | | * 0 on success, Nonzero on failure. |
276 | | * Always returns 0. |
277 | | * See man page for pthread_mutexattr_init |
278 | | ***************************************************************************/ |
279 | | #define ithread_mutexattr_init pthread_mutexattr_init |
280 | | |
281 | | /**************************************************************************** |
282 | | * Function: ithread_mutexattr_destroy |
283 | | * |
284 | | * Description: |
285 | | * Releases any resources held by the mutex attribute. |
286 | | * Currently there are no resources associated with the attribute |
287 | | * Parameters: |
288 | | * ithread_mutexattr_t * attr (must be valid non NULL pointer to |
289 | | * pthread_mutexattr_t) |
290 | | * Returns: |
291 | | * 0 on success, Nonzero on failure. |
292 | | * Always returns 0. |
293 | | * See man page for pthread_mutexattr_destroy |
294 | | ***************************************************************************/ |
295 | | #define ithread_mutexattr_destroy pthread_mutexattr_destroy |
296 | | |
297 | | /**************************************************************************** |
298 | | * Function: ithread_mutexattr_setkind_np |
299 | | * |
300 | | * Description: |
301 | | * Sets the mutex type in the attribute. |
302 | | * Valid types are: ITHREAD_MUTEX_FAST_NP |
303 | | * ITHREAD_MUTEX_RECURSIVE_NP |
304 | | * ITHREAD_MUTEX_ERRORCHECK_NP |
305 | | * |
306 | | * Parameters: |
307 | | * ithread_mutexattr_t * attr (must be valid non NULL pointer to |
308 | | * ithread_mutexattr_t) |
309 | | * int kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP |
310 | | * or ITHREAD_MUTEX_ERRORCHECK_NP) |
311 | | * Returns: |
312 | | * 0 on success. Nonzero on failure. |
313 | | * Returns EINVAL if the kind is not supported. |
314 | | * See man page for pthread_mutexattr_setkind_np |
315 | | *****************************************************************************/ |
316 | | #define ithread_mutexattr_setkind_np pthread_mutexattr_settype |
317 | | #define ithread_mutexattr_settype pthread_mutexattr_settype |
318 | | |
319 | | /**************************************************************************** |
320 | | * Function: ithread_mutexattr_getkind_np |
321 | | * |
322 | | * Description: |
323 | | * Gets the mutex type in the attribute. |
324 | | * Valid types are: ITHREAD_MUTEX_FAST_NP |
325 | | * ITHREAD_MUTEX_RECURSIVE_NP |
326 | | * ITHREAD_MUTEX_ERRORCHECK_NP |
327 | | * |
328 | | * Parameters: |
329 | | * ithread_mutexattr_t * attr (must be valid non NULL pointer to |
330 | | * pthread_mutexattr_t) |
331 | | * int *kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP |
332 | | * or ITHREAD_MUTEX_ERRORCHECK_NP) |
333 | | * Returns: |
334 | | * 0 on success. Nonzero on failure. |
335 | | * Always returns 0. |
336 | | * See man page for pthread_mutexattr_getkind_np |
337 | | *****************************************************************************/ |
338 | | #define ithread_mutexattr_getkind_np pthread_mutexattr_gettype |
339 | | #define ithread_mutexattr_gettype pthread_mutexattr_gettype |
340 | | |
341 | | /**************************************************************************** |
342 | | * Function: ithread_mutex_init |
343 | | * |
344 | | * Description: |
345 | | * Initializes mutex. |
346 | | * Must be called before use. |
347 | | * |
348 | | * Parameters: |
349 | | * ithread_mutex_t *mutex (must be valid non NULL pointer to |
350 | | * pthread_mutex_t) |
351 | | * const ithread_mutexattr_t *mutex_attr |
352 | | * |
353 | | * Returns: |
354 | | * 0 on success, |
355 | | * Nonzero on failure. |
356 | | * Always returns 0. |
357 | | * See man page for pthread_mutex_init |
358 | | *****************************************************************************/ |
359 | 0 | #define ithread_mutex_init pthread_mutex_init |
360 | | |
361 | | /**************************************************************************** |
362 | | * Function: ithread_mutex_lock |
363 | | * |
364 | | * Description: |
365 | | * Locks mutex. |
366 | | * |
367 | | * Parameters: |
368 | | * ithread_mutex_t * mutex (must be valid non NULL pointer to |
369 | | * pthread_mutex_t). The mutex must be initialized. |
370 | | * |
371 | | * Returns: |
372 | | * 0 on success, Nonzero on failure. |
373 | | * Always returns 0. |
374 | | * See man page for pthread_mutex_lock |
375 | | *****************************************************************************/ |
376 | 0 | #define ithread_mutex_lock pthread_mutex_lock |
377 | | |
378 | | /**************************************************************************** |
379 | | * Function: ithread_mutex_unlock |
380 | | * |
381 | | * Description: |
382 | | * Unlocks mutex. |
383 | | * |
384 | | * Parameters: |
385 | | * ithread_mutex_t * mutex (must be valid non NULL pointer to |
386 | | * pthread_mutex_t). The mutex must be initialized. |
387 | | * |
388 | | * Returns: |
389 | | * 0 on success, Nonzero on failure. |
390 | | * Always returns 0. |
391 | | * See man page for pthread_mutex_unlock |
392 | | *****************************************************************************/ |
393 | 0 | #define ithread_mutex_unlock pthread_mutex_unlock |
394 | | |
395 | | /**************************************************************************** |
396 | | * Function: ithread_mutex_destroy |
397 | | * |
398 | | * Description: |
399 | | * Releases any resources held by the mutex. |
400 | | * Mutex can no longer be used after this call. |
401 | | * Mutex is only destroyed when there are no longer any threads |
402 | | * waiting on it. Mutex cannot be destroyed if it is locked. |
403 | | * Parameters: |
404 | | * ithread_mutex_t * mutex (must be valid non NULL pointer to |
405 | | * pthread_mutex_t). The mutex must be initialized. |
406 | | * Returns: |
407 | | * 0 on success. |
408 | | * Nonzero on failure. |
409 | | * Always returns 0. |
410 | | * See man page for pthread_mutex_destroy |
411 | | *****************************************************************************/ |
412 | 0 | #define ithread_mutex_destroy pthread_mutex_destroy |
413 | | |
414 | | /**************************************************************************** |
415 | | * Function: ithread_rwlockattr_init |
416 | | * |
417 | | * Description: |
418 | | * Initializes a rwlock attribute variable to default values. |
419 | | * Parameters: |
420 | | * const ithread_rwlockattr_init *attr (must be valid non NULL pointer to |
421 | | * pthread_rwlockattr_t) |
422 | | * Returns: |
423 | | * 0 on success, Nonzero on failure. |
424 | | * Always returns 0. |
425 | | * See man page for pthread_rwlockattr_init |
426 | | ***************************************************************************/ |
427 | | #if UPNP_USE_RWLOCK |
428 | | #define ithread_rwlockattr_init pthread_rwlockattr_init |
429 | | #endif /* UPNP_USE_RWLOCK */ |
430 | | |
431 | | /**************************************************************************** |
432 | | * Function: ithread_rwlockattr_destroy |
433 | | * |
434 | | * Description: |
435 | | * Releases any resources held by the rwlock attribute. |
436 | | * Parameters: |
437 | | * ithread_rwlockattr_t *attr (must be valid non NULL pointer to |
438 | | * pthread_rwlockattr_t) |
439 | | * Returns: |
440 | | * 0 on success, Nonzero on failure. |
441 | | * Always returns 0. |
442 | | * See man page for pthread_rwlockattr_destroy |
443 | | ***************************************************************************/ |
444 | | #if UPNP_USE_RWLOCK |
445 | | #define ithread_rwlockattr_destroy pthread_rwlockattr_destroy |
446 | | #endif /* UPNP_USE_RWLOCK */ |
447 | | |
448 | | /**************************************************************************** |
449 | | * Function: ithread_rwlockatttr_setpshared |
450 | | * |
451 | | * Description: |
452 | | * Sets the rwlock type in the attribute. |
453 | | * Valid types are: ITHREAD_PROCESS_PRIVATE |
454 | | * ITHREAD_PROCESS_SHARED |
455 | | * |
456 | | * Parameters: |
457 | | * ithread_rwlockattr_t * attr (must be valid non NULL pointer to |
458 | | * ithread_rwlockattr_t) |
459 | | * int kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED) |
460 | | * |
461 | | * Returns: |
462 | | * 0 on success. Nonzero on failure. |
463 | | * Returns EINVAL if the kind is not supported. |
464 | | * See man page for pthread_rwlockattr_setkind_np |
465 | | *****************************************************************************/ |
466 | | #if UPNP_USE_RWLOCK |
467 | | #define ithread_rwlockatttr_setpshared pthread_rwlockatttr_setpshared |
468 | | #endif /* UPNP_USE_RWLOCK */ |
469 | | |
470 | | /**************************************************************************** |
471 | | * Function: ithread_rwlockatttr_getpshared |
472 | | * |
473 | | * Description: |
474 | | * Gets the rwlock type in the attribute. |
475 | | * Valid types are: ITHREAD_PROCESS_PRIVATE |
476 | | * ITHREAD_PROCESS_SHARED |
477 | | * |
478 | | * Parameters: |
479 | | * ithread_rwlockattr_t * attr (must be valid non NULL pointer to |
480 | | * pthread_rwlockattr_t) |
481 | | * int *kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED) |
482 | | * |
483 | | * Returns: |
484 | | * 0 on success. Nonzero on failure. |
485 | | * Always returns 0. |
486 | | * See man page for pthread_rwlockatttr_getpshared |
487 | | *****************************************************************************/ |
488 | | #if UPNP_USE_RWLOCK |
489 | | #define ithread_rwlockatttr_getpshared pthread_rwlockatttr_getpshared |
490 | | #endif /* UPNP_USE_RWLOCK */ |
491 | | |
492 | | /**************************************************************************** |
493 | | * Function: ithread_rwlock_init |
494 | | * |
495 | | * Description: |
496 | | * Initializes rwlock. |
497 | | * Must be called before use. |
498 | | * |
499 | | * Parameters: |
500 | | * ithread_rwlock_t *rwlock (must be valid non NULL pointer to |
501 | | * pthread_rwlock_t) const ithread_rwlockattr_t *rwlock_attr |
502 | | * |
503 | | * Returns: |
504 | | * 0 on success |
505 | | * Nonzero on failure. |
506 | | * Always returns 0. |
507 | | * See man page for pthread_rwlock_init |
508 | | *****************************************************************************/ |
509 | | #if UPNP_USE_RWLOCK |
510 | 0 | #define ithread_rwlock_init pthread_rwlock_init |
511 | | #else |
512 | | /* Read-write locks aren't available: use mutex instead. */ |
513 | | #define ithread_rwlock_init ithread_mutex_init |
514 | | #endif |
515 | | |
516 | | /**************************************************************************** |
517 | | * Function: ithread_rwlock_rdlock |
518 | | * |
519 | | * Description: |
520 | | * Locks rwlock for reading. |
521 | | * Parameters: |
522 | | * ithread_rwlock_t *rwlock (must be valid non NULL pointer to |
523 | | * pthread_rwlock_t). The rwlock must be initialized. |
524 | | * |
525 | | * Returns: |
526 | | * 0 on success, Nonzero on failure. |
527 | | * Always returns 0. |
528 | | * See man page for pthread_rwlock_rdlock |
529 | | *****************************************************************************/ |
530 | | #if UPNP_USE_RWLOCK |
531 | 0 | #define ithread_rwlock_rdlock pthread_rwlock_rdlock |
532 | | #else |
533 | | /* Read-write locks aren't available: use mutex instead. */ |
534 | | #define ithread_rwlock_rdlock ithread_mutex_lock |
535 | | #endif /* UPNP_USE_RWLOCK */ |
536 | | |
537 | | /**************************************************************************** |
538 | | * Function: ithread_rwlock_wrlock |
539 | | * |
540 | | * Description: |
541 | | * Locks rwlock for writting. |
542 | | * Parameters: |
543 | | * ithread_rwlock_t *rwlock (must be valid non NULL pointer to |
544 | | * pthread_rwlock_t) rwlock must be initialized. |
545 | | * |
546 | | * Returns: |
547 | | * 0 on success, Nonzero on failure. |
548 | | * Always returns 0. |
549 | | * See man page for pthread_rwlock_wrlock |
550 | | *****************************************************************************/ |
551 | | #if UPNP_USE_RWLOCK |
552 | 0 | #define ithread_rwlock_wrlock pthread_rwlock_wrlock |
553 | | #else |
554 | | /* Read-write locks aren't available: use mutex instead. */ |
555 | | #define ithread_rwlock_wrlock ithread_mutex_lock |
556 | | #endif /* UPNP_USE_RWLOCK */ |
557 | | |
558 | | /**************************************************************************** |
559 | | * Function: ithread_rwlock_unlock |
560 | | * |
561 | | * Description: |
562 | | * Unlocks rwlock. |
563 | | * |
564 | | * Parameters: |
565 | | * ithread_rwlock_t *rwlock (must be valid non NULL pointer to |
566 | | * pthread_rwlock_t) rwlock must be initialized. |
567 | | * |
568 | | * Returns: |
569 | | * 0 on success, Nonzero on failure. |
570 | | * Always returns 0. |
571 | | * See man page for pthread_rwlock_unlock |
572 | | *****************************************************************************/ |
573 | | #if UPNP_USE_RWLOCK |
574 | 0 | #define ithread_rwlock_unlock pthread_rwlock_unlock |
575 | | #else |
576 | | /* Read-write locks aren't available: use mutex instead. */ |
577 | | #define ithread_rwlock_unlock ithread_mutex_unlock |
578 | | #endif /* UPNP_USE_RWLOCK */ |
579 | | |
580 | | /**************************************************************************** |
581 | | * Function: ithread_rwlock_destroy |
582 | | * |
583 | | * Description: |
584 | | * Releases any resources held by the rwlock. |
585 | | * rwlock can no longer be used after this call. |
586 | | * rwlock is only destroyed when there are no longer any threads |
587 | | * waiting on it. rwlock cannot be destroyed if it is locked. |
588 | | * |
589 | | * Parameters: |
590 | | * ithread_rwlock_t *rwlock (must be valid non NULL pointer to |
591 | | * pthread_rwlock_t) rwlock must be initialized. |
592 | | * |
593 | | * Returns: |
594 | | * 0 on success. |
595 | | * Nonzero on failure. |
596 | | * Always returns 0. |
597 | | * See man page for pthread_rwlock_destroy |
598 | | *****************************************************************************/ |
599 | | #if UPNP_USE_RWLOCK |
600 | 0 | #define ithread_rwlock_destroy pthread_rwlock_destroy |
601 | | #else |
602 | | /* Read-write locks aren't available: use mutex instead. */ |
603 | | #define ithread_rwlock_destroy ithread_mutex_destroy |
604 | | #endif /* UPNP_USE_RWLOCK */ |
605 | | |
606 | | /**************************************************************************** |
607 | | * Function: ithread_cond_init |
608 | | * |
609 | | * Description: |
610 | | * Initializes condition variable. |
611 | | * Must be called before use. |
612 | | * Parameters: |
613 | | * ithread_cond_t *cond (must be valid non NULL pointer to pthread_cond_t) |
614 | | * const ithread_condattr_t *cond_attr (ignored) |
615 | | * Returns: |
616 | | * 0 on success, Nonzero on failure. |
617 | | * See man page for pthread_cond_init |
618 | | *****************************************************************************/ |
619 | 0 | #define ithread_cond_init pthread_cond_init |
620 | | |
621 | | /**************************************************************************** |
622 | | * Function: ithread_cond_signal |
623 | | * |
624 | | * Description: |
625 | | * Wakes up exactly one thread waiting on condition. |
626 | | * Associated mutex MUST be locked by thread before entering this call. |
627 | | * Parameters: |
628 | | * ithread_cond_t *cond (must be valid non NULL pointer to |
629 | | * ithread_cond_t) |
630 | | * cond must be initialized |
631 | | * Returns: |
632 | | * 0 on success, Nonzero on failure. |
633 | | * See man page for pthread_cond_signal |
634 | | *****************************************************************************/ |
635 | 0 | #define ithread_cond_signal pthread_cond_signal |
636 | | |
637 | | /**************************************************************************** |
638 | | * Function: ithread_cond_broadcast |
639 | | * |
640 | | * Description: |
641 | | * Wakes up all threads waiting on condition. |
642 | | * Associated mutex MUST be locked by thread before entering this call. |
643 | | * Parameters: |
644 | | * ithread_cond_t *cond (must be valid non NULL pointer to |
645 | | * ithread_cond_t) |
646 | | * cond must be initialized |
647 | | * Returns: |
648 | | * 0 on success, Nonzero on failure. |
649 | | * See man page for pthread_cond_broadcast |
650 | | *****************************************************************************/ |
651 | 0 | #define ithread_cond_broadcast pthread_cond_broadcast |
652 | | |
653 | | /**************************************************************************** |
654 | | * Function: ithread_cond_wait |
655 | | * |
656 | | * Description: |
657 | | * Atomically releases mutex and waits on condition. |
658 | | * Associated mutex MUST be locked by thread before entering this call. |
659 | | * Mutex is reacquired when call returns. |
660 | | * Parameters: |
661 | | * ithread_cond_t *cond (must be valid non NULL pointer to |
662 | | * ithread_cond_t) |
663 | | * cond must be initialized |
664 | | * ithread_mutex_t *mutex (must be valid non NULL pointer to |
665 | | * ithread_mutex_t) |
666 | | * Mutex must be locked. |
667 | | * Returns: |
668 | | * 0 on success, Nonzero on failure. |
669 | | * See man page for pthread_cond_wait |
670 | | *****************************************************************************/ |
671 | 0 | #define ithread_cond_wait pthread_cond_wait |
672 | | |
673 | | /**************************************************************************** |
674 | | * Function: pthread_cond_timedwait |
675 | | * |
676 | | * Description: |
677 | | * Atomically releases the associated mutex and waits on the condition. |
678 | | * If the condition is not signaled in the specified time than the call |
679 | | * times out and returns. Associated mutex MUST be locked by thread before |
680 | | * entering this call. Mutex is reacquired when call returns. |
681 | | * Parameters: |
682 | | * ithread_cond_t *cond (must be valid non NULL pointer to ithread_cond_t) |
683 | | * cond must be initialized |
684 | | * ithread_mutex_t *mutex (must be valid non NULL pointer to |
685 | | * ithread_mutex_t) Mutex must be locked. |
686 | | * const struct timespec *abstime(absolute time, measured from Jan 1, 1970) |
687 | | * Returns: |
688 | | * 0 on success. |
689 | | * ETIMEDOUT on timeout. |
690 | | * Nonzero on failure. |
691 | | * See man page for pthread_cond_timedwait |
692 | | ***************************************************************************/ |
693 | | |
694 | 0 | #define ithread_cond_timedwait pthread_cond_timedwait |
695 | | |
696 | | /**************************************************************************** |
697 | | * Function: ithread_cond_destroy |
698 | | * |
699 | | * Description: |
700 | | * Releases any resources held by the condition variable. |
701 | | * Condition variable can no longer be used after this call. |
702 | | * Parameters: |
703 | | * ithread_cond_t *cond (must be valid non NULL pointer to |
704 | | * ithread_cond_t) |
705 | | * cond must be initialized. |
706 | | * Returns: |
707 | | * 0 on success. Nonzero on failure. |
708 | | * See man page for pthread_cond_destroy |
709 | | ***************************************************************************/ |
710 | 0 | #define ithread_cond_destroy pthread_cond_destroy |
711 | | |
712 | | /**************************************************************************** |
713 | | * Function: ithread_attr_init |
714 | | * |
715 | | * Description: |
716 | | * Initialises thread attribute object. |
717 | | * Parameters: |
718 | | * ithread_attr_t *attr (must be valid non NULL pointer to |
719 | | * ithread_attr_t) |
720 | | * Returns: |
721 | | * 0 on success. Nonzero on failure. |
722 | | * See man page for pthread_attr_init |
723 | | ***************************************************************************/ |
724 | 0 | #define ithread_attr_init pthread_attr_init |
725 | | |
726 | | /**************************************************************************** |
727 | | * Function: ithread_attr_destroy |
728 | | * |
729 | | * Description: |
730 | | * Destroys thread attribute object. |
731 | | * Parameters: |
732 | | * ithread_attr_t *attr (must be valid non NULL pointer to |
733 | | * ithread_attr_t) |
734 | | * Returns: |
735 | | * 0 on success. Nonzero on failure. |
736 | | * See man page for pthread_attr_destroy |
737 | | ***************************************************************************/ |
738 | 0 | #define ithread_attr_destroy pthread_attr_destroy |
739 | | |
740 | | /**************************************************************************** |
741 | | * Function: ithread_attr_setstacksize |
742 | | * |
743 | | * Description: |
744 | | * Sets stack size of a thread attribute object. |
745 | | * Parameters: |
746 | | * ithread_attr_t *attr (must be valid non NULL pointer to |
747 | | * ithread_attr_t) |
748 | | * size_t stacksize (value of stacksize must be greater than |
749 | | * ITHREAD_STACK_MIN and lower than system-imposed limits |
750 | | * Returns: |
751 | | * 0 on success. Nonzero on failure. |
752 | | * See man page for pthread_attr_setstacksize |
753 | | ***************************************************************************/ |
754 | 0 | #define ithread_attr_setstacksize pthread_attr_setstacksize |
755 | | |
756 | | /**************************************************************************** |
757 | | * Function: ithread_attr_setdetachstate |
758 | | * |
759 | | * Description: |
760 | | * Sets detach state of a thread attribute object. |
761 | | * Parameters: |
762 | | * ithread_attr_t *attr (must be valid non NULL pointer to |
763 | | * ithread_attr_t) |
764 | | * int detachstate (value of detachstate must be ITHREAD_CREATE_DETACHED |
765 | | * or ITHREAD_CREATE_JOINABLE) |
766 | | * Returns: |
767 | | * 0 on success. Nonzero on failure. |
768 | | * See man page for pthread_attr_setdetachstate |
769 | | ***************************************************************************/ |
770 | 0 | #define ithread_attr_setdetachstate pthread_attr_setdetachstate |
771 | | |
772 | | /**************************************************************************** |
773 | | * Function: ithread_create |
774 | | * |
775 | | * Description: |
776 | | * Creates a thread with the given start routine |
777 | | * and argument. |
778 | | * Parameters: |
779 | | * ithread_t * thread (must be valid non NULL pointer to pthread_t) |
780 | | * ithread_attr_t *attr |
781 | | * void * (start_routine) (void *arg) (start routine) |
782 | | * void * arg - argument. |
783 | | * Returns: |
784 | | * 0 on success. Nonzero on failure. |
785 | | * Returns EAGAIN if a new thread can not be created. |
786 | | * Returns EINVAL if there is a problem with the arguments. |
787 | | * See man page fore pthread_create |
788 | | ***************************************************************************/ |
789 | 0 | #define ithread_create pthread_create |
790 | | |
791 | | /**************************************************************************** |
792 | | * Function: ithread_cancel |
793 | | * |
794 | | * Description: |
795 | | * Cancels a thread. |
796 | | * Parameters: |
797 | | * ithread_t * thread (must be valid non NULL pointer to ithread_t) |
798 | | * Returns: |
799 | | * 0 on success. Nonzero on failure. |
800 | | * See man page for pthread_cancel |
801 | | ***************************************************************************/ |
802 | | #define ithread_cancel pthread_cancel |
803 | | |
804 | | /**************************************************************************** |
805 | | * Function: ithread_exit |
806 | | * |
807 | | * Description: |
808 | | * Returns a return code from a thread. |
809 | | * Implicitly called when the start routine returns. |
810 | | * Parameters: |
811 | | * void * return_code return code to return |
812 | | * See man page for pthread_exit |
813 | | ***************************************************************************/ |
814 | | #define ithread_exit pthread_exit |
815 | | |
816 | | /**************************************************************************** |
817 | | * Function: ithread_get_current_thread_id |
818 | | * |
819 | | * Description: |
820 | | * Returns the handle of the currently running thread. |
821 | | * Returns: |
822 | | * The handle of the currently running thread. |
823 | | * See man page for pthread_self |
824 | | ***************************************************************************/ |
825 | 0 | #define ithread_get_current_thread_id pthread_self |
826 | | |
827 | | /**************************************************************************** |
828 | | * Function: ithread_self |
829 | | * |
830 | | * Description: |
831 | | * Returns the handle of the currently running thread. |
832 | | * Returns: |
833 | | * The handle of the currently running thread. |
834 | | * See man page for pthread_self |
835 | | ***************************************************************************/ |
836 | 0 | #define ithread_self pthread_self |
837 | | |
838 | | /**************************************************************************** |
839 | | * Function: ithread_detach |
840 | | * |
841 | | * Description: |
842 | | * Makes a thread's resources reclaimed immediately |
843 | | * after it finishes |
844 | | * execution. |
845 | | * Returns: |
846 | | * 0 on success, Nonzero on failure. |
847 | | * See man page for pthread_detach |
848 | | ***************************************************************************/ |
849 | | #define ithread_detach pthread_detach |
850 | | |
851 | | /**************************************************************************** |
852 | | * Function: ithread_join |
853 | | * |
854 | | * Description: |
855 | | * Suspends the currently running thread until the |
856 | | * specified thread |
857 | | * has finished. |
858 | | * Returns the return code of the thread, or ITHREAD_CANCELED |
859 | | * if the thread has been canceled. |
860 | | * Parameters: |
861 | | * ithread_t *thread (valid non null thread identifier) |
862 | | * void ** return (space for return code) |
863 | | * Returns: |
864 | | * 0 on success, Nonzero on failure. |
865 | | * See man page for pthread_join |
866 | | ***************************************************************************/ |
867 | | #define ithread_join pthread_join |
868 | | |
869 | | /**************************************************************************** |
870 | | * Function: isleep |
871 | | * |
872 | | * Description: |
873 | | * Suspends the currently running thread for the specified number |
874 | | * of seconds |
875 | | * Always returns 0. |
876 | | * Parameters: |
877 | | * unsigned int seconds - number of seconds to sleep. |
878 | | * Returns: |
879 | | * 0 on success, Nonzero on failure. |
880 | | * See man page for sleep (man 3 sleep) |
881 | | *****************************************************************************/ |
882 | | #ifdef _WIN32 |
883 | | #define isleep(x) Sleep((x) * 1000) |
884 | | #else |
885 | 0 | #define isleep sleep |
886 | | #endif |
887 | | |
888 | | /**************************************************************************** |
889 | | * Function: isleep |
890 | | * |
891 | | * Description: |
892 | | * Suspends the currently running thread for the specified number |
893 | | * of milliseconds |
894 | | * Always returns 0. |
895 | | * Parameters: |
896 | | * unsigned int milliseconds - number of milliseconds to sleep. |
897 | | * Returns: |
898 | | * 0 on success, Nonzero on failure. |
899 | | * See man page for sleep (man 3 sleep) |
900 | | *****************************************************************************/ |
901 | | #ifdef _WIN32 |
902 | | #define imillisleep Sleep |
903 | | #else |
904 | | #if _POSIX_C_SOURCE < 200809L |
905 | | #define imillisleep(x) usleep(1000 * x) |
906 | | #else |
907 | | #define imillisleep(x) \ |
908 | 0 | do { \ |
909 | 0 | const struct timespec req = { \ |
910 | 0 | 0, x * 1000 * 1000}; \ |
911 | 0 | nanosleep(&req, NULL); \ |
912 | 0 | } while (0) |
913 | | #endif |
914 | | #endif |
915 | | |
916 | | #if !defined(PTHREAD_MUTEX_RECURSIVE) && !defined(__DragonFly__) && \ |
917 | | !defined(UPNP_USE_MSVCPP) |
918 | | /* !defined(UPNP_USE_MSVCPP) should probably also have pthreads version check - |
919 | | * but it's not clear if that is possible */ |
920 | | /* NK: Added for satisfying the gcc compiler */ |
921 | | UPNP_EXPORT_SPEC int pthread_mutexattr_setkind_np( |
922 | | pthread_mutexattr_t *attr, int kind); |
923 | | #endif |
924 | | |
925 | | #ifdef __cplusplus |
926 | | } |
927 | | #endif |
928 | | |
929 | | #endif /* ITHREAD_H */ |