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