libUPnP 22.0.4
ithread.h
Go to the documentation of this file.
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
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
48extern "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#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 ***************************************************************************/
87typedef 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 ***************************************************************************/
97typedef pthread_attr_t ithread_attr_t;
98
99/****************************************************************************
100 * Name: start_routine
101 *
102 * Description:
103 * Thread start routine
104 * Internal Use Only.
105 ***************************************************************************/
106typedef 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 ***************************************************************************/
116typedef 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 ***************************************************************************/
126typedef 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 ***************************************************************************/
136typedef 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 ***************************************************************************/
147typedef pthread_condattr_t ithread_condattr_t;
148
149/****************************************************************************
150 * Name: ithread_rwlockattr_t
151 *
152 * Description:
153 * Mutex attribute.
154 * typedef to pthread_rwlockattr_t
155 * Internal Use Only
156 ***************************************************************************/
157#if UPNP_USE_RWLOCK
158typedef pthread_rwlockattr_t ithread_rwlockattr_t;
159#endif /* UPNP_USE_RWLOCK */
160
161/****************************************************************************
162 * Name: ithread_rwlock_t
163 *
164 * Description:
165 * Condition attribute.
166 * typedef to pthread_rwlock_t
167 * Internal Use Only
168 ***************************************************************************/
169/* UPNP_USE_RWLOCK is defined in autoconfig.h, pulled in by config.h.
170 * Include config.h now if the caller has not done so: without it
171 * UPNP_USE_RWLOCK is 0 and ithread_rwlock_t silently becomes
172 * pthread_mutex_t, which causes a deadlock when used in place of the
173 * pthread_rwlock_t that upnpapi.c uses for GlobalHndRWLock. */
174#ifndef INTERNAL_CONFIG_H
175 #include "config.h" /* IWYU pragma: keep */
176#endif
177#if UPNP_USE_RWLOCK
178typedef pthread_rwlock_t ithread_rwlock_t;
179#else
180/* Read-write locks aren't available on this platform: use mutex instead. */
181typedef ithread_mutex_t ithread_rwlock_t;
182#endif /* UPNP_USE_RWLOCK */
183
184/****************************************************************************
185 * Function: ithread_initialize_library
186 *
187 * Description:
188 * Initializes the library. Does nothing in all implementations, except
189 * when statically linked for WIN32.
190 * Parameters:
191 * none.
192 * Returns:
193 * 0 on success, Nonzero on failure.
194 ***************************************************************************/
195static UPNP_INLINE int ithread_initialize_library(void)
196{
197 int ret = 0;
198
199 return ret;
200}
201
202/****************************************************************************
203 * Function: ithread_cleanup_library
204 *
205 * Description:
206 * Clean up library resources. Does nothing in all implementations, except
207 * when statically linked for WIN32.
208 * Parameters:
209 * none.
210 * Returns:
211 * 0 on success, Nonzero on failure.
212 ***************************************************************************/
213static UPNP_INLINE int ithread_cleanup_library(void)
214{
215 int ret = 0;
216
217 return ret;
218}
219
220/****************************************************************************
221 * Function: ithread_initialize_thread
222 *
223 * Description:
224 * Initializes the thread. Does nothing in all implementations, except
225 * when statically linked for WIN32.
226 * Parameters:
227 * none.
228 * Returns:
229 * 0 on success, Nonzero on failure.
230 ***************************************************************************/
231static UPNP_INLINE int ithread_initialize_thread(void)
232{
233 int ret = 0;
234
235#if defined(_WIN32) && defined(PTW32_STATIC_LIB)
236 ret = !pthread_win32_thread_attach_np();
237#endif
238
239 return ret;
240}
241
242/****************************************************************************
243 * Function: ithread_cleanup_thread
244 *
245 * Description:
246 * Clean up thread resources. Does nothing in all implementations, except
247 * when statically linked for WIN32.
248 * Parameters:
249 * none.
250 * Returns:
251 * 0 on success, Nonzero on failure.
252 ***************************************************************************/
253static UPNP_INLINE int ithread_cleanup_thread(void)
254{
255 int ret = 0;
256
257#if defined(_WIN32) && defined(PTW32_STATIC_LIB)
258 ret = !pthread_win32_thread_detach_np();
259#endif
260
261 return ret;
262}
263
264/****************************************************************************
265 * Function: ithread_mutexattr_init
266 *
267 * Description:
268 * Initializes a mutex attribute variable.
269 * Used to set the type of the mutex.
270 * Parameters:
271 * ithread_mutexattr_init * attr (must be valid non NULL pointer to
272 * pthread_mutexattr_t)
273 * Returns:
274 * 0 on success, Nonzero on failure.
275 * Always returns 0.
276 * See man page for pthread_mutexattr_init
277 ***************************************************************************/
278#define ithread_mutexattr_init pthread_mutexattr_init
279
280/****************************************************************************
281 * Function: ithread_mutexattr_destroy
282 *
283 * Description:
284 * Releases any resources held by the mutex attribute.
285 * Currently there are no resources associated with the attribute
286 * Parameters:
287 * ithread_mutexattr_t * attr (must be valid non NULL pointer to
288 * pthread_mutexattr_t)
289 * Returns:
290 * 0 on success, Nonzero on failure.
291 * Always returns 0.
292 * See man page for pthread_mutexattr_destroy
293 ***************************************************************************/
294#define ithread_mutexattr_destroy pthread_mutexattr_destroy
295
296/****************************************************************************
297 * Function: ithread_mutexattr_setkind_np
298 *
299 * Description:
300 * Sets the mutex type in the attribute.
301 * Valid types are: ITHREAD_MUTEX_FAST_NP
302 * ITHREAD_MUTEX_RECURSIVE_NP
303 * ITHREAD_MUTEX_ERRORCHECK_NP
304 *
305 * Parameters:
306 * ithread_mutexattr_t * attr (must be valid non NULL pointer to
307 * ithread_mutexattr_t)
308 * int kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP
309 * or ITHREAD_MUTEX_ERRORCHECK_NP)
310 * Returns:
311 * 0 on success. Nonzero on failure.
312 * Returns EINVAL if the kind is not supported.
313 * See man page for pthread_mutexattr_setkind_np
314 *****************************************************************************/
315#define ithread_mutexattr_setkind_np pthread_mutexattr_settype
316#define ithread_mutexattr_settype pthread_mutexattr_settype
317
318/****************************************************************************
319 * Function: ithread_mutexattr_getkind_np
320 *
321 * Description:
322 * Gets the mutex type in the attribute.
323 * Valid types are: ITHREAD_MUTEX_FAST_NP
324 * ITHREAD_MUTEX_RECURSIVE_NP
325 * ITHREAD_MUTEX_ERRORCHECK_NP
326 *
327 * Parameters:
328 * ithread_mutexattr_t * attr (must be valid non NULL pointer to
329 * pthread_mutexattr_t)
330 * int *kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP
331 * or ITHREAD_MUTEX_ERRORCHECK_NP)
332 * Returns:
333 * 0 on success. Nonzero on failure.
334 * Always returns 0.
335 * See man page for pthread_mutexattr_getkind_np
336 *****************************************************************************/
337#define ithread_mutexattr_getkind_np pthread_mutexattr_gettype
338#define ithread_mutexattr_gettype pthread_mutexattr_gettype
339
340/****************************************************************************
341 * Function: ithread_mutex_init
342 *
343 * Description:
344 * Initializes mutex.
345 * Must be called before use.
346 *
347 * Parameters:
348 * ithread_mutex_t *mutex (must be valid non NULL pointer to
349 * pthread_mutex_t)
350 * const ithread_mutexattr_t *mutex_attr
351 *
352 * Returns:
353 * 0 on success,
354 * Nonzero on failure.
355 * Always returns 0.
356 * See man page for pthread_mutex_init
357 *****************************************************************************/
358#define ithread_mutex_init pthread_mutex_init
359
360/****************************************************************************
361 * Function: ithread_mutex_lock
362 *
363 * Description:
364 * Locks mutex.
365 *
366 * Parameters:
367 * ithread_mutex_t * mutex (must be valid non NULL pointer to
368 * pthread_mutex_t). The mutex must be initialized.
369 *
370 * Returns:
371 * 0 on success, Nonzero on failure.
372 * Always returns 0.
373 * See man page for pthread_mutex_lock
374 *****************************************************************************/
375#define ithread_mutex_lock pthread_mutex_lock
376
377/****************************************************************************
378 * Function: ithread_mutex_unlock
379 *
380 * Description:
381 * Unlocks mutex.
382 *
383 * Parameters:
384 * ithread_mutex_t * mutex (must be valid non NULL pointer to
385 * pthread_mutex_t). The mutex must be initialized.
386 *
387 * Returns:
388 * 0 on success, Nonzero on failure.
389 * Always returns 0.
390 * See man page for pthread_mutex_unlock
391 *****************************************************************************/
392#define ithread_mutex_unlock pthread_mutex_unlock
393
394/****************************************************************************
395 * Function: ithread_mutex_destroy
396 *
397 * Description:
398 * Releases any resources held by the mutex.
399 * Mutex can no longer be used after this call.
400 * Mutex is only destroyed when there are no longer any threads
401 * waiting on it. Mutex cannot be destroyed if it is locked.
402 * Parameters:
403 * ithread_mutex_t * mutex (must be valid non NULL pointer to
404 * pthread_mutex_t). The mutex must be initialized.
405 * Returns:
406 * 0 on success.
407 * Nonzero on failure.
408 * Always returns 0.
409 * See man page for pthread_mutex_destroy
410 *****************************************************************************/
411#define ithread_mutex_destroy pthread_mutex_destroy
412
413/****************************************************************************
414 * Function: ithread_rwlockattr_init
415 *
416 * Description:
417 * Initializes a rwlock attribute variable to default values.
418 * Parameters:
419 * const ithread_rwlockattr_init *attr (must be valid non NULL pointer to
420 * pthread_rwlockattr_t)
421 * Returns:
422 * 0 on success, Nonzero on failure.
423 * Always returns 0.
424 * See man page for pthread_rwlockattr_init
425 ***************************************************************************/
426#if UPNP_USE_RWLOCK
427 #define ithread_rwlockattr_init pthread_rwlockattr_init
428#endif /* UPNP_USE_RWLOCK */
429
430/****************************************************************************
431 * Function: ithread_rwlockattr_destroy
432 *
433 * Description:
434 * Releases any resources held by the rwlock attribute.
435 * Parameters:
436 * ithread_rwlockattr_t *attr (must be valid non NULL pointer to
437 * pthread_rwlockattr_t)
438 * Returns:
439 * 0 on success, Nonzero on failure.
440 * Always returns 0.
441 * See man page for pthread_rwlockattr_destroy
442 ***************************************************************************/
443#if UPNP_USE_RWLOCK
444 #define ithread_rwlockattr_destroy pthread_rwlockattr_destroy
445#endif /* UPNP_USE_RWLOCK */
446
447/****************************************************************************
448 * Function: ithread_rwlockatttr_setpshared
449 *
450 * Description:
451 * Sets the rwlock type in the attribute.
452 * Valid types are: ITHREAD_PROCESS_PRIVATE
453 * ITHREAD_PROCESS_SHARED
454 *
455 * Parameters:
456 * ithread_rwlockattr_t * attr (must be valid non NULL pointer to
457 * ithread_rwlockattr_t)
458 * int kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED)
459 *
460 * Returns:
461 * 0 on success. Nonzero on failure.
462 * Returns EINVAL if the kind is not supported.
463 * See man page for pthread_rwlockattr_setkind_np
464 *****************************************************************************/
465#if UPNP_USE_RWLOCK
466 #define ithread_rwlockatttr_setpshared pthread_rwlockatttr_setpshared
467#endif /* UPNP_USE_RWLOCK */
468
469/****************************************************************************
470 * Function: ithread_rwlockatttr_getpshared
471 *
472 * Description:
473 * Gets the rwlock type in the attribute.
474 * Valid types are: ITHREAD_PROCESS_PRIVATE
475 * ITHREAD_PROCESS_SHARED
476 *
477 * Parameters:
478 * ithread_rwlockattr_t * attr (must be valid non NULL pointer to
479 * pthread_rwlockattr_t)
480 * int *kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED)
481 *
482 * Returns:
483 * 0 on success. Nonzero on failure.
484 * Always returns 0.
485 * See man page for pthread_rwlockatttr_getpshared
486 *****************************************************************************/
487#if UPNP_USE_RWLOCK
488 #define ithread_rwlockatttr_getpshared pthread_rwlockatttr_getpshared
489#endif /* UPNP_USE_RWLOCK */
490
491/****************************************************************************
492 * Function: ithread_rwlock_init
493 *
494 * Description:
495 * Initializes rwlock.
496 * Must be called before use.
497 *
498 * Parameters:
499 * ithread_rwlock_t *rwlock (must be valid non NULL pointer to
500 * pthread_rwlock_t) const ithread_rwlockattr_t *rwlock_attr
501 *
502 * Returns:
503 * 0 on success
504 * Nonzero on failure.
505 * Always returns 0.
506 * See man page for pthread_rwlock_init
507 *****************************************************************************/
508#if UPNP_USE_RWLOCK
509 #define ithread_rwlock_init pthread_rwlock_init
510#else
511 /* Read-write locks aren't available: use mutex instead. */
512 #define ithread_rwlock_init ithread_mutex_init
513#endif
514
515/****************************************************************************
516 * Function: ithread_rwlock_rdlock
517 *
518 * Description:
519 * Locks rwlock for reading.
520 * Parameters:
521 * ithread_rwlock_t *rwlock (must be valid non NULL pointer to
522 * pthread_rwlock_t). The rwlock must be initialized.
523 *
524 * Returns:
525 * 0 on success, Nonzero on failure.
526 * Always returns 0.
527 * See man page for pthread_rwlock_rdlock
528 *****************************************************************************/
529#if UPNP_USE_RWLOCK
530 #define ithread_rwlock_rdlock pthread_rwlock_rdlock
531#else
532 /* Read-write locks aren't available: use mutex instead. */
533 #define ithread_rwlock_rdlock ithread_mutex_lock
534#endif /* UPNP_USE_RWLOCK */
535
536/****************************************************************************
537 * Function: ithread_rwlock_wrlock
538 *
539 * Description:
540 * Locks rwlock for writting.
541 * Parameters:
542 * ithread_rwlock_t *rwlock (must be valid non NULL pointer to
543 * pthread_rwlock_t) rwlock must be initialized.
544 *
545 * Returns:
546 * 0 on success, Nonzero on failure.
547 * Always returns 0.
548 * See man page for pthread_rwlock_wrlock
549 *****************************************************************************/
550#if UPNP_USE_RWLOCK
551 #define ithread_rwlock_wrlock pthread_rwlock_wrlock
552#else
553 /* Read-write locks aren't available: use mutex instead. */
554 #define ithread_rwlock_wrlock ithread_mutex_lock
555#endif /* UPNP_USE_RWLOCK */
556
557/****************************************************************************
558 * Function: ithread_rwlock_unlock
559 *
560 * Description:
561 * Unlocks rwlock.
562 *
563 * Parameters:
564 * ithread_rwlock_t *rwlock (must be valid non NULL pointer to
565 * pthread_rwlock_t) rwlock must be initialized.
566 *
567 * Returns:
568 * 0 on success, Nonzero on failure.
569 * Always returns 0.
570 * See man page for pthread_rwlock_unlock
571 *****************************************************************************/
572#if UPNP_USE_RWLOCK
573 #define ithread_rwlock_unlock pthread_rwlock_unlock
574#else
575 /* Read-write locks aren't available: use mutex instead. */
576 #define ithread_rwlock_unlock ithread_mutex_unlock
577#endif /* UPNP_USE_RWLOCK */
578
579/****************************************************************************
580 * Function: ithread_rwlock_destroy
581 *
582 * Description:
583 * Releases any resources held by the rwlock.
584 * rwlock can no longer be used after this call.
585 * rwlock is only destroyed when there are no longer any threads
586 * waiting on it. rwlock cannot be destroyed if it is locked.
587 *
588 * Parameters:
589 * ithread_rwlock_t *rwlock (must be valid non NULL pointer to
590 * pthread_rwlock_t) rwlock must be initialized.
591 *
592 * Returns:
593 * 0 on success.
594 * Nonzero on failure.
595 * Always returns 0.
596 * See man page for pthread_rwlock_destroy
597 *****************************************************************************/
598#if UPNP_USE_RWLOCK
599 #define ithread_rwlock_destroy pthread_rwlock_destroy
600#else
601 /* Read-write locks aren't available: use mutex instead. */
602 #define ithread_rwlock_destroy ithread_mutex_destroy
603#endif /* UPNP_USE_RWLOCK */
604
605/****************************************************************************
606 * Function: ithread_cond_init
607 *
608 * Description:
609 * Initializes condition variable.
610 * Must be called before use.
611 * Parameters:
612 * ithread_cond_t *cond (must be valid non NULL pointer to pthread_cond_t)
613 * const ithread_condattr_t *cond_attr (ignored)
614 * Returns:
615 * 0 on success, Nonzero on failure.
616 * See man page for pthread_cond_init
617 *****************************************************************************/
618#define ithread_cond_init pthread_cond_init
619
620/****************************************************************************
621 * Function: ithread_cond_signal
622 *
623 * Description:
624 * Wakes up exactly one thread waiting on condition.
625 * Associated mutex MUST be locked by thread before entering this call.
626 * Parameters:
627 * ithread_cond_t *cond (must be valid non NULL pointer to
628 * ithread_cond_t)
629 * cond must be initialized
630 * Returns:
631 * 0 on success, Nonzero on failure.
632 * See man page for pthread_cond_signal
633 *****************************************************************************/
634#define ithread_cond_signal pthread_cond_signal
635
636/****************************************************************************
637 * Function: ithread_cond_broadcast
638 *
639 * Description:
640 * Wakes up all threads waiting on condition.
641 * Associated mutex MUST be locked by thread before entering this call.
642 * Parameters:
643 * ithread_cond_t *cond (must be valid non NULL pointer to
644 * ithread_cond_t)
645 * cond must be initialized
646 * Returns:
647 * 0 on success, Nonzero on failure.
648 * See man page for pthread_cond_broadcast
649 *****************************************************************************/
650#define ithread_cond_broadcast pthread_cond_broadcast
651
652/****************************************************************************
653 * Function: ithread_cond_wait
654 *
655 * Description:
656 * Atomically releases mutex and waits on condition.
657 * Associated mutex MUST be locked by thread before entering this call.
658 * Mutex is reacquired when call returns.
659 * Parameters:
660 * ithread_cond_t *cond (must be valid non NULL pointer to
661 * ithread_cond_t)
662 * cond must be initialized
663 * ithread_mutex_t *mutex (must be valid non NULL pointer to
664 * ithread_mutex_t)
665 * Mutex must be locked.
666 * Returns:
667 * 0 on success, Nonzero on failure.
668 * See man page for pthread_cond_wait
669 *****************************************************************************/
670#define ithread_cond_wait pthread_cond_wait
671
672/****************************************************************************
673 * Function: pthread_cond_timedwait
674 *
675 * Description:
676 * Atomically releases the associated mutex and waits on the condition.
677 * If the condition is not signaled in the specified time than the call
678 * times out and returns. Associated mutex MUST be locked by thread before
679 * entering this call. Mutex is reacquired when call returns.
680 * Parameters:
681 * ithread_cond_t *cond (must be valid non NULL pointer to ithread_cond_t)
682 * cond must be initialized
683 * ithread_mutex_t *mutex (must be valid non NULL pointer to
684 * ithread_mutex_t) Mutex must be locked.
685 * const struct timespec *abstime(absolute time, measured from Jan 1, 1970)
686 * Returns:
687 * 0 on success.
688 * ETIMEDOUT on timeout.
689 * Nonzero on failure.
690 * See man page for pthread_cond_timedwait
691 ***************************************************************************/
692
693#define ithread_cond_timedwait pthread_cond_timedwait
694
695/****************************************************************************
696 * Function: ithread_cond_destroy
697 *
698 * Description:
699 * Releases any resources held by the condition variable.
700 * Condition variable can no longer be used after this call.
701 * Parameters:
702 * ithread_cond_t *cond (must be valid non NULL pointer to
703 * ithread_cond_t)
704 * cond must be initialized.
705 * Returns:
706 * 0 on success. Nonzero on failure.
707 * See man page for pthread_cond_destroy
708 ***************************************************************************/
709#define ithread_cond_destroy pthread_cond_destroy
710
711/****************************************************************************
712 * Function: ithread_attr_init
713 *
714 * Description:
715 * Initialises thread attribute object.
716 * Parameters:
717 * ithread_attr_t *attr (must be valid non NULL pointer to
718 * ithread_attr_t)
719 * Returns:
720 * 0 on success. Nonzero on failure.
721 * See man page for pthread_attr_init
722 ***************************************************************************/
723#define ithread_attr_init pthread_attr_init
724
725/****************************************************************************
726 * Function: ithread_attr_destroy
727 *
728 * Description:
729 * Destroys thread attribute object.
730 * Parameters:
731 * ithread_attr_t *attr (must be valid non NULL pointer to
732 * ithread_attr_t)
733 * Returns:
734 * 0 on success. Nonzero on failure.
735 * See man page for pthread_attr_destroy
736 ***************************************************************************/
737#define ithread_attr_destroy pthread_attr_destroy
738
739/****************************************************************************
740 * Function: ithread_attr_setstacksize
741 *
742 * Description:
743 * Sets stack size of a thread attribute object.
744 * Parameters:
745 * ithread_attr_t *attr (must be valid non NULL pointer to
746 * ithread_attr_t)
747 * size_t stacksize (value of stacksize must be greater than
748 * ITHREAD_STACK_MIN and lower than system-imposed limits
749 * Returns:
750 * 0 on success. Nonzero on failure.
751 * See man page for pthread_attr_setstacksize
752 ***************************************************************************/
753#define ithread_attr_setstacksize pthread_attr_setstacksize
754
755/****************************************************************************
756 * Function: ithread_attr_setdetachstate
757 *
758 * Description:
759 * Sets detach state of a thread attribute object.
760 * Parameters:
761 * ithread_attr_t *attr (must be valid non NULL pointer to
762 * ithread_attr_t)
763 * int detachstate (value of detachstate must be ITHREAD_CREATE_DETACHED
764 * or ITHREAD_CREATE_JOINABLE)
765 * Returns:
766 * 0 on success. Nonzero on failure.
767 * See man page for pthread_attr_setdetachstate
768 ***************************************************************************/
769#define ithread_attr_setdetachstate pthread_attr_setdetachstate
770
771/****************************************************************************
772 * Function: ithread_create
773 *
774 * Description:
775 * Creates a thread with the given start routine
776 * and argument.
777 * Parameters:
778 * ithread_t * thread (must be valid non NULL pointer to pthread_t)
779 * ithread_attr_t *attr
780 * void * (start_routine) (void *arg) (start routine)
781 * void * arg - argument.
782 * Returns:
783 * 0 on success. Nonzero on failure.
784 * Returns EAGAIN if a new thread can not be created.
785 * Returns EINVAL if there is a problem with the arguments.
786 * See man page fore pthread_create
787 ***************************************************************************/
788#define ithread_create pthread_create
789
790/****************************************************************************
791 * Function: ithread_cancel
792 *
793 * Description:
794 * Cancels a thread.
795 * Parameters:
796 * ithread_t * thread (must be valid non NULL pointer to ithread_t)
797 * Returns:
798 * 0 on success. Nonzero on failure.
799 * See man page for pthread_cancel
800 ***************************************************************************/
801#define ithread_cancel pthread_cancel
802
803/****************************************************************************
804 * Function: ithread_exit
805 *
806 * Description:
807 * Returns a return code from a thread.
808 * Implicitly called when the start routine returns.
809 * Parameters:
810 * void * return_code return code to return
811 * See man page for pthread_exit
812 ***************************************************************************/
813#define ithread_exit pthread_exit
814
815/****************************************************************************
816 * Function: ithread_get_current_thread_id
817 *
818 * Description:
819 * Returns the handle of the currently running thread.
820 * Returns:
821 * The handle of the currently running thread.
822 * See man page for pthread_self
823 ***************************************************************************/
824#define ithread_get_current_thread_id pthread_self
825
826/****************************************************************************
827 * Function: ithread_self
828 *
829 * Description:
830 * Returns the handle of the currently running thread.
831 * Returns:
832 * The handle of the currently running thread.
833 * See man page for pthread_self
834 ***************************************************************************/
835#define ithread_self pthread_self
836
837/****************************************************************************
838 * Function: ithread_detach
839 *
840 * Description:
841 * Makes a thread's resources reclaimed immediately
842 * after it finishes
843 * execution.
844 * Returns:
845 * 0 on success, Nonzero on failure.
846 * See man page for pthread_detach
847 ***************************************************************************/
848#define ithread_detach pthread_detach
849
850/****************************************************************************
851 * Function: ithread_join
852 *
853 * Description:
854 * Suspends the currently running thread until the
855 * specified thread
856 * has finished.
857 * Returns the return code of the thread, or ITHREAD_CANCELED
858 * if the thread has been canceled.
859 * Parameters:
860 * ithread_t *thread (valid non null thread identifier)
861 * void ** return (space for return code)
862 * Returns:
863 * 0 on success, Nonzero on failure.
864 * See man page for pthread_join
865 ***************************************************************************/
866#define ithread_join pthread_join
867
868/****************************************************************************
869 * Function: isleep
870 *
871 * Description:
872 * Suspends the currently running thread for the specified number
873 * of seconds
874 * Always returns 0.
875 * Parameters:
876 * unsigned int seconds - number of seconds to sleep.
877 * Returns:
878 * 0 on success, Nonzero on failure.
879 * See man page for sleep (man 3 sleep)
880 *****************************************************************************/
881#ifdef _WIN32
882 #define isleep(x) Sleep((x) * 1000)
883#else
884 #define isleep sleep
885#endif
886
887/****************************************************************************
888 * Function: isleep
889 *
890 * Description:
891 * Suspends the currently running thread for the specified number
892 * of milliseconds
893 * Always returns 0.
894 * Parameters:
895 * unsigned int milliseconds - number of milliseconds to sleep.
896 * Returns:
897 * 0 on success, Nonzero on failure.
898 * See man page for sleep (man 3 sleep)
899 *****************************************************************************/
900#ifdef _WIN32
901 #define imillisleep Sleep
902#else
903 #if _POSIX_C_SOURCE < 200809L
904 #define imillisleep(x) usleep(1000 * x)
905 #else
906 #define imillisleep(x) \
907 do { \
908 const struct timespec req = { \
909 0, x * 1000 * 1000}; \
910 nanosleep(&req, NULL); \
911 } while (0)
912 #endif
913#endif
914
915#if !defined(PTHREAD_MUTEX_RECURSIVE) && !defined(__DragonFly__) && \
916 !defined(UPNP_USE_MSVCPP)
917/* !defined(UPNP_USE_MSVCPP) should probably also have pthreads version check -
918 * but it's not clear if that is possible */
919/* NK: Added for satisfying the gcc compiler */
920UPNP_EXPORT_SPEC int pthread_mutexattr_setkind_np(
921 pthread_mutexattr_t *attr, int kind);
922#endif
923
924#ifdef __cplusplus
925}
926#endif
927
928#endif /* ITHREAD_H */
Defines constants that for some reason are not defined on some systems.
#define UPNP_EXPORT_SPEC
Export functions on WIN32 DLLs.
Definition UpnpGlobal.h:101
#define UPNP_INLINE
Declares an inline function.
Definition UpnpGlobal.h:115