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