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