libUPnP  1.14.18
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, UPNP_EXPORT_SPEC */
45 #include "UpnpUniStd.h" /* for close() */
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 #include <pthread.h>
52 
53 #if defined(BSD) && !defined(__GNU__)
54  #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
55 #endif
56 
57 #if defined(PTHREAD_MUTEX_RECURSIVE) || defined(__DragonFly__)
58  /* This system has SuS2-compliant mutex attributes.
59  * E.g. on Cygwin, where we don't have the old nonportable (NP) symbols
60  */
61  #define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_NORMAL
62  #define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
63  #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK
64 #else /* PTHREAD_MUTEX_RECURSIVE */
65  #define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_FAST_NP
66  #define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE_NP
67  #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK_NP
68 #endif /* PTHREAD_MUTEX_RECURSIVE */
69 
70 #define ITHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
71 #define ITHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
72 
73 #define ITHREAD_CANCELED PTHREAD_CANCELED
74 
75 #define ITHREAD_STACK_MIN PTHREAD_STACK_MIN
76 #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 #define ithread_mutexattr_setkind_np pthread_mutexattr_settype
308 #define ithread_mutexattr_settype pthread_mutexattr_settype
309 
310 /****************************************************************************
311  * Function: ithread_mutexattr_getkind_np
312  *
313  * Description:
314  * Gets the mutex type in the attribute.
315  * Valid types are: ITHREAD_MUTEX_FAST_NP
316  * ITHREAD_MUTEX_RECURSIVE_NP
317  * ITHREAD_MUTEX_ERRORCHECK_NP
318  *
319  * Parameters:
320  * ithread_mutexattr_t * attr (must be valid non NULL pointer to
321  * pthread_mutexattr_t)
322  * int *kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP
323  * or ITHREAD_MUTEX_ERRORCHECK_NP)
324  * Returns:
325  * 0 on success. Nonzero on failure.
326  * Always returns 0.
327  * See man page for pthread_mutexattr_getkind_np
328  *****************************************************************************/
329 #define ithread_mutexattr_getkind_np pthread_mutexattr_gettype
330 #define ithread_mutexattr_gettype pthread_mutexattr_gettype
331 
332 /****************************************************************************
333  * Function: ithread_mutex_init
334  *
335  * Description:
336  * Initializes mutex.
337  * Must be called before use.
338  *
339  * Parameters:
340  * ithread_mutex_t *mutex (must be valid non NULL pointer to
341  * pthread_mutex_t)
342  * const ithread_mutexattr_t *mutex_attr
343  *
344  * Returns:
345  * 0 on success,
346  * Nonzero on failure.
347  * Always returns 0.
348  * See man page for pthread_mutex_init
349  *****************************************************************************/
350 #define ithread_mutex_init pthread_mutex_init
351 
352 /****************************************************************************
353  * Function: ithread_mutex_lock
354  *
355  * Description:
356  * Locks mutex.
357  *
358  * Parameters:
359  * ithread_mutex_t * mutex (must be valid non NULL pointer to
360  * pthread_mutex_t). The mutex must be initialized.
361  *
362  * Returns:
363  * 0 on success, Nonzero on failure.
364  * Always returns 0.
365  * See man page for pthread_mutex_lock
366  *****************************************************************************/
367 #define ithread_mutex_lock pthread_mutex_lock
368 
369 /****************************************************************************
370  * Function: ithread_mutex_unlock
371  *
372  * Description:
373  * Unlocks mutex.
374  *
375  * Parameters:
376  * ithread_mutex_t * mutex (must be valid non NULL pointer to
377  * pthread_mutex_t). The mutex must be initialized.
378  *
379  * Returns:
380  * 0 on success, Nonzero on failure.
381  * Always returns 0.
382  * See man page for pthread_mutex_unlock
383  *****************************************************************************/
384 #define ithread_mutex_unlock pthread_mutex_unlock
385 
386 /****************************************************************************
387  * Function: ithread_mutex_destroy
388  *
389  * Description:
390  * Releases any resources held by the mutex.
391  * Mutex can no longer be used after this call.
392  * Mutex is only destroyed when there are no longer any threads
393  * waiting on it. Mutex cannot be destroyed if it is locked.
394  * Parameters:
395  * ithread_mutex_t * mutex (must be valid non NULL pointer to
396  * pthread_mutex_t). The mutex must be initialized.
397  * Returns:
398  * 0 on success.
399  * Nonzero on failure.
400  * Always returns 0.
401  * See man page for pthread_mutex_destroy
402  *****************************************************************************/
403 #define ithread_mutex_destroy pthread_mutex_destroy
404 
405 /****************************************************************************
406  * Function: ithread_rwlockattr_init
407  *
408  * Description:
409  * Initializes a rwlock attribute variable to default values.
410  * Parameters:
411  * const ithread_rwlockattr_init *attr (must be valid non NULL pointer to
412  * pthread_rwlockattr_t)
413  * Returns:
414  * 0 on success, Nonzero on failure.
415  * Always returns 0.
416  * See man page for pthread_rwlockattr_init
417  ***************************************************************************/
418 #if UPNP_USE_RWLOCK
419  #define ithread_rwlockattr_init pthread_rwlockattr_init
420 #endif /* UPNP_USE_RWLOCK */
421 
422 /****************************************************************************
423  * Function: ithread_rwlockattr_destroy
424  *
425  * Description:
426  * Releases any resources held by the rwlock attribute.
427  * Parameters:
428  * ithread_rwlockattr_t *attr (must be valid non NULL pointer to
429  * pthread_rwlockattr_t)
430  * Returns:
431  * 0 on success, Nonzero on failure.
432  * Always returns 0.
433  * See man page for pthread_rwlockattr_destroy
434  ***************************************************************************/
435 #if UPNP_USE_RWLOCK
436  #define ithread_rwlockattr_destroy pthread_rwlockattr_destroy
437 #endif /* UPNP_USE_RWLOCK */
438 
439 /****************************************************************************
440  * Function: ithread_rwlockatttr_setpshared
441  *
442  * Description:
443  * Sets the rwlock type in the attribute.
444  * Valid types are: ITHREAD_PROCESS_PRIVATE
445  * ITHREAD_PROCESS_SHARED
446  *
447  * Parameters:
448  * ithread_rwlockattr_t * attr (must be valid non NULL pointer to
449  * ithread_rwlockattr_t)
450  * int kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED)
451  *
452  * Returns:
453  * 0 on success. Nonzero on failure.
454  * Returns EINVAL if the kind is not supported.
455  * See man page for pthread_rwlockattr_setkind_np
456  *****************************************************************************/
457 #if UPNP_USE_RWLOCK
458  #define ithread_rwlockatttr_setpshared pthread_rwlockatttr_setpshared
459 #endif /* UPNP_USE_RWLOCK */
460 
461 /****************************************************************************
462  * Function: ithread_rwlockatttr_getpshared
463  *
464  * Description:
465  * Gets the rwlock type in the attribute.
466  * Valid types are: ITHREAD_PROCESS_PRIVATE
467  * ITHREAD_PROCESS_SHARED
468  *
469  * Parameters:
470  * ithread_rwlockattr_t * attr (must be valid non NULL pointer to
471  * pthread_rwlockattr_t)
472  * int *kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED)
473  *
474  * Returns:
475  * 0 on success. Nonzero on failure.
476  * Always returns 0.
477  * See man page for pthread_rwlockatttr_getpshared
478  *****************************************************************************/
479 #if UPNP_USE_RWLOCK
480  #define ithread_rwlockatttr_getpshared pthread_rwlockatttr_getpshared
481 #endif /* UPNP_USE_RWLOCK */
482 
483 /****************************************************************************
484  * Function: ithread_rwlock_init
485  *
486  * Description:
487  * Initializes rwlock.
488  * Must be called before use.
489  *
490  * Parameters:
491  * ithread_rwlock_t *rwlock (must be valid non NULL pointer to
492  * pthread_rwlock_t) const ithread_rwlockattr_t *rwlock_attr
493  *
494  * Returns:
495  * 0 on success
496  * Nonzero on failure.
497  * Always returns 0.
498  * See man page for pthread_rwlock_init
499  *****************************************************************************/
500 #if UPNP_USE_RWLOCK
501  #define ithread_rwlock_init pthread_rwlock_init
502 #else
503  /* Read-write locks aren't available: use mutex instead. */
504  #define ithread_rwlock_init ithread_mutex_init
505 #endif
506 
507 /****************************************************************************
508  * Function: ithread_rwlock_rdlock
509  *
510  * Description:
511  * Locks rwlock for reading.
512  * Parameters:
513  * ithread_rwlock_t *rwlock (must be valid non NULL pointer to
514  * pthread_rwlock_t). The rwlock must be initialized.
515  *
516  * Returns:
517  * 0 on success, Nonzero on failure.
518  * Always returns 0.
519  * See man page for pthread_rwlock_rdlock
520  *****************************************************************************/
521 #if UPNP_USE_RWLOCK
522  #define ithread_rwlock_rdlock pthread_rwlock_rdlock
523 #else
524  /* Read-write locks aren't available: use mutex instead. */
525  #define ithread_rwlock_rdlock ithread_mutex_lock
526 #endif /* UPNP_USE_RWLOCK */
527 
528 /****************************************************************************
529  * Function: ithread_rwlock_wrlock
530  *
531  * Description:
532  * Locks rwlock for writting.
533  * Parameters:
534  * ithread_rwlock_t *rwlock (must be valid non NULL pointer to
535  * pthread_rwlock_t) rwlock must be initialized.
536  *
537  * Returns:
538  * 0 on success, Nonzero on failure.
539  * Always returns 0.
540  * See man page for pthread_rwlock_wrlock
541  *****************************************************************************/
542 #if UPNP_USE_RWLOCK
543  #define ithread_rwlock_wrlock pthread_rwlock_wrlock
544 #else
545  /* Read-write locks aren't available: use mutex instead. */
546  #define ithread_rwlock_wrlock ithread_mutex_lock
547 #endif /* UPNP_USE_RWLOCK */
548 
549 /****************************************************************************
550  * Function: ithread_rwlock_unlock
551  *
552  * Description:
553  * Unlocks rwlock.
554  *
555  * Parameters:
556  * ithread_rwlock_t *rwlock (must be valid non NULL pointer to
557  * pthread_rwlock_t) rwlock must be initialized.
558  *
559  * Returns:
560  * 0 on success, Nonzero on failure.
561  * Always returns 0.
562  * See man page for pthread_rwlock_unlock
563  *****************************************************************************/
564 #if UPNP_USE_RWLOCK
565  #define ithread_rwlock_unlock pthread_rwlock_unlock
566 #else
567  /* Read-write locks aren't available: use mutex instead. */
568  #define ithread_rwlock_unlock ithread_mutex_unlock
569 #endif /* UPNP_USE_RWLOCK */
570 
571 /****************************************************************************
572  * Function: ithread_rwlock_destroy
573  *
574  * Description:
575  * Releases any resources held by the rwlock.
576  * rwlock can no longer be used after this call.
577  * rwlock is only destroyed when there are no longer any threads
578  * waiting on it. rwlock cannot be destroyed if it is locked.
579  *
580  * Parameters:
581  * ithread_rwlock_t *rwlock (must be valid non NULL pointer to
582  * pthread_rwlock_t) rwlock must be initialized.
583  *
584  * Returns:
585  * 0 on success.
586  * Nonzero on failure.
587  * Always returns 0.
588  * See man page for pthread_rwlock_destroy
589  *****************************************************************************/
590 #if UPNP_USE_RWLOCK
591  #define ithread_rwlock_destroy pthread_rwlock_destroy
592 #else
593  /* Read-write locks aren't available: use mutex instead. */
594  #define ithread_rwlock_destroy ithread_mutex_destroy
595 #endif /* UPNP_USE_RWLOCK */
596 
597 /****************************************************************************
598  * Function: ithread_cond_init
599  *
600  * Description:
601  * Initializes condition variable.
602  * Must be called before use.
603  * Parameters:
604  * ithread_cond_t *cond (must be valid non NULL pointer to pthread_cond_t)
605  * const ithread_condattr_t *cond_attr (ignored)
606  * Returns:
607  * 0 on success, Nonzero on failure.
608  * See man page for pthread_cond_init
609  *****************************************************************************/
610 #define ithread_cond_init pthread_cond_init
611 
612 /****************************************************************************
613  * Function: ithread_cond_signal
614  *
615  * Description:
616  * Wakes up exactly one thread waiting on condition.
617  * Associated mutex MUST be locked by thread before entering this call.
618  * Parameters:
619  * ithread_cond_t *cond (must be valid non NULL pointer to
620  * ithread_cond_t)
621  * cond must be initialized
622  * Returns:
623  * 0 on success, Nonzero on failure.
624  * See man page for pthread_cond_signal
625  *****************************************************************************/
626 #define ithread_cond_signal pthread_cond_signal
627 
628 /****************************************************************************
629  * Function: ithread_cond_broadcast
630  *
631  * Description:
632  * Wakes up all threads waiting on condition.
633  * Associated mutex MUST be locked by thread before entering this call.
634  * Parameters:
635  * ithread_cond_t *cond (must be valid non NULL pointer to
636  * ithread_cond_t)
637  * cond must be initialized
638  * Returns:
639  * 0 on success, Nonzero on failure.
640  * See man page for pthread_cond_broadcast
641  *****************************************************************************/
642 #define ithread_cond_broadcast pthread_cond_broadcast
643 
644 /****************************************************************************
645  * Function: ithread_cond_wait
646  *
647  * Description:
648  * Atomically releases mutex and waits on condition.
649  * Associated mutex MUST be locked by thread before entering this call.
650  * Mutex is reacquired when call returns.
651  * Parameters:
652  * ithread_cond_t *cond (must be valid non NULL pointer to
653  * ithread_cond_t)
654  * cond must be initialized
655  * ithread_mutex_t *mutex (must be valid non NULL pointer to
656  * ithread_mutex_t)
657  * Mutex must be locked.
658  * Returns:
659  * 0 on success, Nonzero on failure.
660  * See man page for pthread_cond_wait
661  *****************************************************************************/
662 #define ithread_cond_wait pthread_cond_wait
663 
664 /****************************************************************************
665  * Function: pthread_cond_timedwait
666  *
667  * Description:
668  * Atomically releases the associated mutex and waits on the condition.
669  * If the condition is not signaled in the specified time than the call
670  * times out and returns. Associated mutex MUST be locked by thread before
671  * entering this call. Mutex is reacquired when call returns.
672  * Parameters:
673  * ithread_cond_t *cond (must be valid non NULL pointer to ithread_cond_t)
674  * cond must be initialized
675  * ithread_mutex_t *mutex (must be valid non NULL pointer to
676  * ithread_mutex_t) Mutex must be locked.
677  * const struct timespec *abstime(absolute time, measured from Jan 1, 1970)
678  * Returns:
679  * 0 on success.
680  * ETIMEDOUT on timeout.
681  * Nonzero on failure.
682  * See man page for pthread_cond_timedwait
683  ***************************************************************************/
684 
685 #define ithread_cond_timedwait pthread_cond_timedwait
686 
687 /****************************************************************************
688  * Function: ithread_cond_destroy
689  *
690  * Description:
691  * Releases any resources held by the condition variable.
692  * Condition variable can no longer be used after this call.
693  * Parameters:
694  * ithread_cond_t *cond (must be valid non NULL pointer to
695  * ithread_cond_t)
696  * cond must be initialized.
697  * Returns:
698  * 0 on success. Nonzero on failure.
699  * See man page for pthread_cond_destroy
700  ***************************************************************************/
701 #define ithread_cond_destroy pthread_cond_destroy
702 
703 /****************************************************************************
704  * Function: ithread_attr_init
705  *
706  * Description:
707  * Initialises thread attribute object.
708  * Parameters:
709  * ithread_attr_t *attr (must be valid non NULL pointer to
710  * ithread_attr_t)
711  * Returns:
712  * 0 on success. Nonzero on failure.
713  * See man page for pthread_attr_init
714  ***************************************************************************/
715 #define ithread_attr_init pthread_attr_init
716 
717 /****************************************************************************
718  * Function: ithread_attr_destroy
719  *
720  * Description:
721  * Destroys thread attribute object.
722  * Parameters:
723  * ithread_attr_t *attr (must be valid non NULL pointer to
724  * ithread_attr_t)
725  * Returns:
726  * 0 on success. Nonzero on failure.
727  * See man page for pthread_attr_destroy
728  ***************************************************************************/
729 #define ithread_attr_destroy pthread_attr_destroy
730 
731 /****************************************************************************
732  * Function: ithread_attr_setstacksize
733  *
734  * Description:
735  * Sets stack size of a thread attribute object.
736  * Parameters:
737  * ithread_attr_t *attr (must be valid non NULL pointer to
738  * ithread_attr_t)
739  * size_t stacksize (value of stacksize must be greater than
740  * ITHREAD_STACK_MIN and lower than system-imposed limits
741  * Returns:
742  * 0 on success. Nonzero on failure.
743  * See man page for pthread_attr_setstacksize
744  ***************************************************************************/
745 #define ithread_attr_setstacksize pthread_attr_setstacksize
746 
747 /****************************************************************************
748  * Function: ithread_attr_setdetachstate
749  *
750  * Description:
751  * Sets detach state of a thread attribute object.
752  * Parameters:
753  * ithread_attr_t *attr (must be valid non NULL pointer to
754  * ithread_attr_t)
755  * int detachstate (value of detachstate must be ITHREAD_CREATE_DETACHED
756  * or ITHREAD_CREATE_JOINABLE)
757  * Returns:
758  * 0 on success. Nonzero on failure.
759  * See man page for pthread_attr_setdetachstate
760  ***************************************************************************/
761 #define ithread_attr_setdetachstate pthread_attr_setdetachstate
762 
763 /****************************************************************************
764  * Function: ithread_create
765  *
766  * Description:
767  * Creates a thread with the given start routine
768  * and argument.
769  * Parameters:
770  * ithread_t * thread (must be valid non NULL pointer to pthread_t)
771  * ithread_attr_t *attr
772  * void * (start_routine) (void *arg) (start routine)
773  * void * arg - argument.
774  * Returns:
775  * 0 on success. Nonzero on failure.
776  * Returns EAGAIN if a new thread can not be created.
777  * Returns EINVAL if there is a problem with the arguments.
778  * See man page fore pthread_create
779  ***************************************************************************/
780 #define ithread_create pthread_create
781 
782 /****************************************************************************
783  * Function: ithread_cancel
784  *
785  * Description:
786  * Cancels a thread.
787  * Parameters:
788  * ithread_t * thread (must be valid non NULL pointer to ithread_t)
789  * Returns:
790  * 0 on success. Nonzero on failure.
791  * See man page for pthread_cancel
792  ***************************************************************************/
793 #define ithread_cancel pthread_cancel
794 
795 /****************************************************************************
796  * Function: ithread_exit
797  *
798  * Description:
799  * Returns a return code from a thread.
800  * Implicitly called when the start routine returns.
801  * Parameters:
802  * void * return_code return code to return
803  * See man page for pthread_exit
804  ***************************************************************************/
805 #define ithread_exit pthread_exit
806 
807 /****************************************************************************
808  * Function: ithread_get_current_thread_id
809  *
810  * Description:
811  * Returns the handle of the currently running thread.
812  * Returns:
813  * The handle of the currently running thread.
814  * See man page for pthread_self
815  ***************************************************************************/
816 #define ithread_get_current_thread_id pthread_self
817 
818 /****************************************************************************
819  * Function: ithread_self
820  *
821  * Description:
822  * Returns the handle of the currently running thread.
823  * Returns:
824  * The handle of the currently running thread.
825  * See man page for pthread_self
826  ***************************************************************************/
827 #define ithread_self pthread_self
828 
829 /****************************************************************************
830  * Function: ithread_detach
831  *
832  * Description:
833  * Makes a thread's resources reclaimed immediately
834  * after it finishes
835  * execution.
836  * Returns:
837  * 0 on success, Nonzero on failure.
838  * See man page for pthread_detach
839  ***************************************************************************/
840 #define ithread_detach pthread_detach
841 
842 /****************************************************************************
843  * Function: ithread_join
844  *
845  * Description:
846  * Suspends the currently running thread until the
847  * specified thread
848  * has finished.
849  * Returns the return code of the thread, or ITHREAD_CANCELED
850  * if the thread has been canceled.
851  * Parameters:
852  * ithread_t *thread (valid non null thread identifier)
853  * void ** return (space for return code)
854  * Returns:
855  * 0 on success, Nonzero on failure.
856  * See man page for pthread_join
857  ***************************************************************************/
858 #define ithread_join pthread_join
859 
860 /****************************************************************************
861  * Function: isleep
862  *
863  * Description:
864  * Suspends the currently running thread for the specified number
865  * of seconds
866  * Always returns 0.
867  * Parameters:
868  * unsigned int seconds - number of seconds to sleep.
869  * Returns:
870  * 0 on success, Nonzero on failure.
871  * See man page for sleep (man 3 sleep)
872  *****************************************************************************/
873 #ifdef _WIN32
874  #define isleep(x) Sleep((x)*1000)
875 #else
876  #define isleep sleep
877 #endif
878 
879 /****************************************************************************
880  * Function: isleep
881  *
882  * Description:
883  * Suspends the currently running thread for the specified number
884  * of milliseconds
885  * Always returns 0.
886  * Parameters:
887  * unsigned int milliseconds - number of milliseconds to sleep.
888  * Returns:
889  * 0 on success, Nonzero on failure.
890  * See man page for sleep (man 3 sleep)
891  *****************************************************************************/
892 #ifdef _WIN32
893  #define imillisleep Sleep
894 #else
895  #if _POSIX_C_SOURCE < 200809L
896  #define imillisleep(x) usleep(1000 * x)
897  #else
898  #define imillisleep(x) \
899  do { \
900  const struct timespec req = { \
901  0, x * 1000 * 1000}; \
902  nanosleep(&req, NULL); \
903  } while (0)
904  #endif
905 #endif
906 
907 #if !defined(PTHREAD_MUTEX_RECURSIVE) && !defined(__DragonFly__) && \
908  !defined(UPNP_USE_MSVCPP)
909 /* !defined(UPNP_USE_MSVCPP) should probably also have pthreads version check -
910  * but it's not clear if that is possible */
911 /* NK: Added for satisfying the gcc compiler */
912 UPNP_EXPORT_SPEC int pthread_mutexattr_setkind_np(
913  pthread_mutexattr_t *attr, int kind);
914 #endif
915 
916 #ifdef __cplusplus
917 }
918 #endif
919 
920 #endif /* ITHREAD_H */
#define UPNP_EXPORT_SPEC
Export functions on WIN32 DLLs.
Definition: UpnpGlobal.h:98
#define UPNP_INLINE
Declares an inline function.
Definition: UpnpGlobal.h:112
Defines constants that for some reason are not defined on some systems.