libUPnP  1.6.17
ithread.h
Go to the documentation of this file.
00001 #ifndef ITHREAD_H
00002 #define ITHREAD_H
00003 
00004 /*******************************************************************************
00005  *
00006  * Copyright (c) 2000-2003 Intel Corporation 
00007  * All rights reserved. 
00008  * Copyright (c) 2012 France Telecom All rights reserved. 
00009  *
00010  * Redistribution and use in source and binary forms, with or without 
00011  * modification, are permitted provided that the following conditions are met: 
00012  *
00013  * * Redistributions of source code must retain the above copyright notice, 
00014  * this list of conditions and the following disclaimer. 
00015  * * Redistributions in binary form must reproduce the above copyright notice, 
00016  * this list of conditions and the following disclaimer in the documentation 
00017  * and/or other materials provided with the distribution. 
00018  * * Neither name of Intel Corporation nor the names of its contributors 
00019  * may be used to endorse or promote products derived from this software 
00020  * without specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
00025  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 
00026  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00028  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
00030  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  ******************************************************************************/
00035 
00040 #if !defined(WIN32)
00041         #include <sys/param.h>
00042 #endif
00043 
00044 #include "UpnpGlobal.h" /* For UPNP_INLINE, EXPORT_SPEC */
00045 #include "UpnpUniStd.h" /* for close() */
00046 
00047 #ifdef __cplusplus
00048 extern "C" {
00049 #endif
00050 
00051 #include <pthread.h>
00052 
00053 #if defined(BSD)
00054         #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
00055 #endif
00056 
00057 
00058 #if defined(PTHREAD_MUTEX_RECURSIVE) || defined(__DragonFly__)
00059         /* This system has SuS2-compliant mutex attributes.
00060          * E.g. on Cygwin, where we don't have the old nonportable (NP) symbols
00061          */
00062         #define ITHREAD_MUTEX_FAST_NP       PTHREAD_MUTEX_NORMAL
00063         #define ITHREAD_MUTEX_RECURSIVE_NP  PTHREAD_MUTEX_RECURSIVE
00064         #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK
00065 #else /* PTHREAD_MUTEX_RECURSIVE */
00066         #define ITHREAD_MUTEX_FAST_NP       PTHREAD_MUTEX_FAST_NP
00067         #define ITHREAD_MUTEX_RECURSIVE_NP  PTHREAD_MUTEX_RECURSIVE_NP
00068         #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK_NP
00069 #endif /* PTHREAD_MUTEX_RECURSIVE */
00070 
00071 
00072 #define ITHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
00073 #define ITHREAD_PROCESS_SHARED  PTHREAD_PROCESS_SHARED
00074 
00075 
00076 #define ITHREAD_CANCELED PTHREAD_CANCELED
00077 
00078 
00079 #define ITHREAD_STACK_MIN PTHREAD_STACK_MIN
00080 #define ITHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
00081 #define ITHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
00082 
00083 /***************************************************************************
00084  * Name: ithread_t
00085  *
00086  *  Description:
00087  *      Thread handle.
00088  *      typedef to pthread_t.
00089  *      Internal Use Only.
00090  ***************************************************************************/
00091 typedef pthread_t ithread_t;
00092 
00093   
00094 /****************************************************************************
00095  * Name: ithread_attr_t
00096  *
00097  *  Description:
00098  *      Thread attribute.
00099  *      typedef to pthread_attr_t
00100  *      Internal Use Only
00101  ***************************************************************************/
00102 typedef pthread_attr_t ithread_attr_t;  
00103 
00104 
00105 /****************************************************************************
00106  * Name: start_routine
00107  *
00108  *  Description:
00109  *      Thread start routine 
00110  *      Internal Use Only.
00111  ***************************************************************************/
00112 typedef void *(*start_routine)(void *arg);
00113 
00114   
00115 /****************************************************************************
00116  * Name: ithread_cond_t
00117  *
00118  *  Description:
00119  *      condition variable.
00120  *      typedef to pthread_cond_t
00121  *      Internal Use Only.
00122  ***************************************************************************/
00123 typedef pthread_cond_t ithread_cond_t;
00124 
00125 
00126 /****************************************************************************
00127  * Name: ithread_mutexattr_t
00128  *
00129  *  Description:
00130  *      Mutex attribute.
00131  *      typedef to pthread_mutexattr_t
00132  *      Internal Use Only
00133  ***************************************************************************/
00134 typedef pthread_mutexattr_t ithread_mutexattr_t;        
00135 
00136 
00137 /****************************************************************************
00138  * Name: ithread_mutex_t
00139  *
00140  *  Description:
00141  *      Mutex.
00142  *      typedef to pthread_mutex_t
00143  *      Internal Use Only.
00144  ***************************************************************************/
00145 typedef pthread_mutex_t ithread_mutex_t;
00146 
00147 
00148 /****************************************************************************
00149  * Name: ithread_condattr_t
00150  *
00151  *  Description:
00152  *      Condition attribute.
00153  *      typedef to pthread_condattr_t
00154  *      NOT USED
00155  *      Internal Use Only
00156  ***************************************************************************/
00157 typedef pthread_condattr_t ithread_condattr_t;  
00158 
00159 
00160 /****************************************************************************
00161  * Name: ithread_rwlockattr_t
00162  *
00163  *  Description:
00164  *      Mutex attribute.
00165  *      typedef to pthread_rwlockattr_t
00166  *      Internal Use Only
00167  ***************************************************************************/
00168 #if UPNP_USE_RWLOCK
00169 typedef pthread_rwlockattr_t ithread_rwlockattr_t;      
00170 #endif /* UPNP_USE_RWLOCK */
00171 
00172 
00173 /****************************************************************************
00174  * Name: ithread_rwlock_t
00175  *
00176  *  Description:
00177  *      Condition attribute.
00178  *      typedef to pthread_rwlock_t
00179  *      Internal Use Only
00180  ***************************************************************************/
00181 #if UPNP_USE_RWLOCK
00182         typedef pthread_rwlock_t ithread_rwlock_t;
00183 #else
00184         /* Read-write locks aren't available: use mutex instead. */
00185         typedef ithread_mutex_t ithread_rwlock_t;
00186 #endif /* UPNP_USE_RWLOCK */
00187 
00188 
00189 /****************************************************************************
00190  * Function: ithread_initialize_library
00191  *
00192  *  Description:
00193  *      Initializes the library. Does nothing in all implementations, except
00194  *      when statically linked for WIN32.
00195  *  Parameters:
00196  *      none.
00197  *  Returns:
00198  *      0 on success, Nonzero on failure.
00199  ***************************************************************************/
00200 static UPNP_INLINE int ithread_initialize_library(void) {
00201         int ret = 0;
00202 
00203 #if defined(WIN32) && defined(PTW32_STATIC_LIB)
00204         ret = !pthread_win32_process_attach_np();
00205 #endif
00206 
00207         return ret;
00208 }
00209 
00210 
00211 /****************************************************************************
00212  * Function: ithread_cleanup_library
00213  *
00214  *  Description:
00215  *      Clean up library resources. Does nothing in all implementations, except
00216  *      when statically linked for WIN32.
00217  *  Parameters:
00218  *      none.
00219  *  Returns:
00220  *      0 on success, Nonzero on failure.
00221  ***************************************************************************/
00222 static UPNP_INLINE int ithread_cleanup_library(void) {
00223         int ret = 0;
00224 
00225 #if defined(WIN32) && defined(PTW32_STATIC_LIB)
00226         ret = !pthread_win32_process_detach_np();
00227 #endif
00228 
00229         return ret;
00230 }
00231 
00232 
00233 /****************************************************************************
00234  * Function: ithread_initialize_thread
00235  *
00236  *  Description:
00237  *      Initializes the thread. Does nothing in all implementations, except
00238  *      when statically linked for WIN32.
00239  *  Parameters:
00240  *      none.
00241  *  Returns:
00242  *      0 on success, Nonzero on failure.
00243  ***************************************************************************/
00244 static UPNP_INLINE int ithread_initialize_thread(void) {
00245         int ret = 0;
00246 
00247 #if defined(WIN32) && defined(PTW32_STATIC_LIB)
00248         ret = !pthread_win32_thread_attach_np();
00249 #endif
00250 
00251         return ret;
00252 }
00253 
00254 
00255 /****************************************************************************
00256  * Function: ithread_cleanup_thread
00257  *
00258  *  Description:
00259  *      Clean up thread resources. Does nothing in all implementations, except
00260  *      when statically linked for WIN32.
00261  *  Parameters:
00262  *      none.
00263  *  Returns:
00264  *      0 on success, Nonzero on failure.
00265  ***************************************************************************/
00266 static UPNP_INLINE int ithread_cleanup_thread(void) {
00267         int ret = 0;
00268 
00269 #if defined(WIN32) && defined(PTW32_STATIC_LIB)
00270         ret = !pthread_win32_thread_detach_np();
00271 #endif
00272 
00273         return ret;
00274 }
00275 
00276 
00277 /****************************************************************************
00278  * Function: ithread_mutexattr_init
00279  *
00280  *  Description:
00281  *      Initializes a mutex attribute variable.
00282  *      Used to set the type of the mutex.
00283  *  Parameters:
00284  *      ithread_mutexattr_init * attr (must be valid non NULL pointer to 
00285  *                                     pthread_mutexattr_t)
00286  *  Returns:
00287  *      0 on success, Nonzero on failure.
00288  *      Always returns 0.
00289  *      See man page for pthread_mutexattr_init
00290  ***************************************************************************/
00291 #define ithread_mutexattr_init pthread_mutexattr_init
00292 
00293 
00294 /****************************************************************************
00295  * Function: ithread_mutexattr_destroy
00296  *
00297  *  Description:
00298  *      Releases any resources held by the mutex attribute.
00299  *      Currently there are no resources associated with the attribute
00300  *  Parameters:
00301  *      ithread_mutexattr_t * attr (must be valid non NULL pointer to 
00302  *                                  pthread_mutexattr_t)
00303  *  Returns:
00304  *      0 on success, Nonzero on failure.
00305  *      Always returns 0.
00306  *      See man page for pthread_mutexattr_destroy
00307  ***************************************************************************/
00308 #define ithread_mutexattr_destroy pthread_mutexattr_destroy
00309   
00310   
00311 /****************************************************************************
00312  * Function: ithread_mutexattr_setkind_np
00313  *
00314  *  Description:
00315  *      Sets the mutex type in the attribute.
00316  *      Valid types are: ITHREAD_MUTEX_FAST_NP 
00317  *                       ITHREAD_MUTEX_RECURSIVE_NP 
00318  *                       ITHREAD_MUTEX_ERRORCHECK_NP
00319  *
00320  *  Parameters:
00321  *      ithread_mutexattr_t * attr (must be valid non NULL pointer to 
00322  *                                   ithread_mutexattr_t)
00323  *      int kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP
00324  *                or ITHREAD_MUTEX_ERRORCHECK_NP)
00325  *  Returns:
00326  *      0 on success. Nonzero on failure.
00327  *      Returns EINVAL if the kind is not supported.
00328  *      See man page for pthread_mutexattr_setkind_np
00329  *****************************************************************************/
00330 #if defined(PTHREAD_MUTEX_RECURSIVE) || defined(__DragonFly__)
00331         #define ithread_mutexattr_setkind_np pthread_mutexattr_settype
00332 #else
00333         #define ithread_mutexattr_setkind_np pthread_mutexattr_setkind_np
00334 #endif /* UPNP_USE_RWLOCK */
00335 
00336 /****************************************************************************
00337  * Function: ithread_mutexattr_getkind_np
00338  *
00339  *  Description:
00340  *      Gets the mutex type in the attribute.
00341  *      Valid types are: ITHREAD_MUTEX_FAST_NP 
00342  *                       ITHREAD_MUTEX_RECURSIVE_NP 
00343  *                       ITHREAD_MUTEX_ERRORCHECK_NP
00344  *
00345  *  Parameters:
00346  *      ithread_mutexattr_t * attr (must be valid non NULL pointer to 
00347  *                                   pthread_mutexattr_t)
00348  *      int *kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP
00349  *                or ITHREAD_MUTEX_ERRORCHECK_NP)
00350  *  Returns:
00351  *      0 on success. Nonzero on failure.
00352  *      Always returns 0.
00353  *      See man page for pthread_mutexattr_getkind_np
00354  *****************************************************************************/
00355 #if defined(PTHREAD_MUTEX_RECURSIVE) || defined(__DragonFly__)
00356         #define ithread_mutexattr_getkind_np pthread_mutexattr_gettype
00357 #else
00358         #define ithread_mutexattr_getkind_np pthread_mutexattr_getkind_np
00359 #endif /* UPNP_USE_RWLOCK */
00360 
00361   
00362 /****************************************************************************
00363  * Function: ithread_mutex_init
00364  *
00365  *  Description:
00366  *      Initializes mutex.
00367  *      Must be called before use.
00368  *      
00369  *  Parameters:
00370  *      ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t)
00371  *      const ithread_mutexattr_t * mutex_attr 
00372  *  Returns:
00373  *      0 on success, Nonzero on failure.
00374  *      Always returns 0.
00375  *      See man page for pthread_mutex_init
00376  *****************************************************************************/
00377 #define ithread_mutex_init pthread_mutex_init
00378 
00379 
00380 /****************************************************************************
00381  * Function: ithread_mutex_lock
00382  *
00383  *  Description:
00384  *      Locks mutex.
00385  *  Parameters:
00386  *      ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t)
00387  *      mutex must be initialized.
00388  *      
00389  *  Returns:
00390  *      0 on success, Nonzero on failure.
00391  *      Always returns 0.
00392  *      See man page for pthread_mutex_lock
00393  *****************************************************************************/
00394 #define ithread_mutex_lock pthread_mutex_lock
00395   
00396 
00397 /****************************************************************************
00398  * Function: ithread_mutex_unlock
00399  *
00400  *  Description:
00401  *      Unlocks mutex.
00402  *
00403  *  Parameters:
00404  *      ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t)
00405  *      mutex must be initialized.
00406  *      
00407  *  Returns:
00408  *      0 on success, Nonzero on failure.
00409  *      Always returns 0.
00410  *      See man page for pthread_mutex_unlock
00411  *****************************************************************************/
00412 #define ithread_mutex_unlock pthread_mutex_unlock
00413 
00414 
00415 /****************************************************************************
00416  * Function: ithread_mutex_destroy
00417  *
00418  *  Description:
00419  *      Releases any resources held by the mutex. 
00420  *              Mutex can no longer be used after this call.
00421  *              Mutex is only destroyed when there are no longer any threads waiting on it. 
00422  *              Mutex cannot be destroyed if it is locked.
00423  *  Parameters:
00424  *      ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t)
00425  *      mutex must be initialized.
00426  *  Returns:
00427  *      0 on success. Nonzero on failure.
00428  *      Always returns 0.
00429  *      See man page for pthread_mutex_destroy
00430  *****************************************************************************/
00431 #define ithread_mutex_destroy pthread_mutex_destroy
00432 
00433 
00434 /****************************************************************************
00435  * Function: ithread_rwlockattr_init
00436  *
00437  *  Description:
00438  *      Initializes a rwlock attribute variable to default values.
00439  *  Parameters:
00440  *      const ithread_rwlockattr_init *attr (must be valid non NULL pointer to 
00441  *                                           pthread_rwlockattr_t)
00442  *  Returns:
00443  *      0 on success, Nonzero on failure.
00444  *      Always returns 0.
00445  *      See man page for pthread_rwlockattr_init
00446  ***************************************************************************/
00447 #if UPNP_USE_RWLOCK
00448         #define ithread_rwlockattr_init pthread_rwlockattr_init
00449 #endif /* UPNP_USE_RWLOCK */
00450 
00451 
00452 /****************************************************************************
00453  * Function: ithread_rwlockattr_destroy
00454  *
00455  *  Description:
00456  *      Releases any resources held by the rwlock attribute.
00457  *  Parameters:
00458  *      ithread_rwlockattr_t *attr (must be valid non NULL pointer to 
00459  *                                  pthread_rwlockattr_t)
00460  *  Returns:
00461  *      0 on success, Nonzero on failure.
00462  *      Always returns 0.
00463  *      See man page for pthread_rwlockattr_destroy
00464  ***************************************************************************/
00465 #if UPNP_USE_RWLOCK
00466         #define ithread_rwlockattr_destroy pthread_rwlockattr_destroy
00467 #endif /* UPNP_USE_RWLOCK */
00468   
00469   
00470 /****************************************************************************
00471  * Function: ithread_rwlockatttr_setpshared
00472  *
00473  *  Description:
00474  *      Sets the rwlock type in the attribute.
00475  *      Valid types are: ITHREAD_PROCESS_PRIVATE 
00476  *                       ITHREAD_PROCESS_SHARED
00477  *
00478  *  Parameters:
00479  *      ithread_rwlockattr_t * attr (must be valid non NULL pointer to 
00480  *                                   ithread_rwlockattr_t)
00481  *      int kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED)
00482  *
00483  *  Returns:
00484  *      0 on success. Nonzero on failure.
00485  *      Returns EINVAL if the kind is not supported.
00486  *      See man page for pthread_rwlockattr_setkind_np
00487  *****************************************************************************/
00488 #if UPNP_USE_RWLOCK
00489         #define ithread_rwlockatttr_setpshared pthread_rwlockatttr_setpshared
00490 #endif /* UPNP_USE_RWLOCK */
00491 
00492 
00493 /****************************************************************************
00494  * Function: ithread_rwlockatttr_getpshared
00495  *
00496  *  Description:
00497  *      Gets the rwlock type in the attribute.
00498  *      Valid types are: ITHREAD_PROCESS_PRIVATE 
00499  *                       ITHREAD_PROCESS_SHARED 
00500  *
00501  *  Parameters:
00502  *      ithread_rwlockattr_t * attr (must be valid non NULL pointer to 
00503  *                                   pthread_rwlockattr_t)
00504  *      int *kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED)
00505  *
00506  *  Returns:
00507  *      0 on success. Nonzero on failure.
00508  *      Always returns 0.
00509  *      See man page for pthread_rwlockatttr_getpshared
00510  *****************************************************************************/
00511 #if UPNP_USE_RWLOCK
00512         #define ithread_rwlockatttr_getpshared pthread_rwlockatttr_getpshared
00513 #endif /* UPNP_USE_RWLOCK */
00514 
00515   
00516 /****************************************************************************
00517  * Function: ithread_rwlock_init
00518  *
00519  *  Description:
00520  *      Initializes rwlock.
00521  *      Must be called before use.
00522  *      
00523  *  Parameters:
00524  *      ithread_rwlock_t *rwlock (must be valid non NULL pointer to pthread_rwlock_t)
00525  *      const ithread_rwlockattr_t *rwlock_attr 
00526  *  Returns:
00527  *      0 on success, Nonzero on failure.
00528  *      Always returns 0.
00529  *      See man page for pthread_rwlock_init
00530  *****************************************************************************/
00531 #if UPNP_USE_RWLOCK
00532         #define ithread_rwlock_init pthread_rwlock_init
00533 #else
00534         /* Read-write locks aren't available: use mutex instead. */
00535         #define ithread_rwlock_init ithread_mutex_init
00536 #endif
00537 
00538 /****************************************************************************
00539  * Function: ithread_rwlock_rdlock
00540  *
00541  *  Description:
00542  *      Locks rwlock for reading.
00543  *  Parameters:
00544  *      ithread_rwlock_t *rwlock (must be valid non NULL pointer to pthread_rwlock_t)
00545  *      rwlock must be initialized.
00546  *      
00547  *  Returns:
00548  *      0 on success, Nonzero on failure.
00549  *      Always returns 0.
00550  *      See man page for pthread_rwlock_rdlock
00551  *****************************************************************************/
00552 #if UPNP_USE_RWLOCK
00553         #define ithread_rwlock_rdlock pthread_rwlock_rdlock
00554 #else
00555         /* Read-write locks aren't available: use mutex instead. */
00556         #define ithread_rwlock_rdlock ithread_mutex_lock
00557 #endif /* UPNP_USE_RWLOCK */
00558 
00559 /****************************************************************************
00560  * Function: ithread_rwlock_wrlock
00561  *
00562  *  Description:
00563  *      Locks rwlock for writting.
00564  *  Parameters:
00565  *      ithread_rwlock_t *rwlock (must be valid non NULL pointer to pthread_rwlock_t)
00566  *      rwlock must be initialized.
00567  *      
00568  *  Returns:
00569  *      0 on success, Nonzero on failure.
00570  *      Always returns 0.
00571  *      See man page for pthread_rwlock_wrlock
00572  *****************************************************************************/
00573 #if UPNP_USE_RWLOCK
00574         #define ithread_rwlock_wrlock pthread_rwlock_wrlock
00575 #else
00576         /* Read-write locks aren't available: use mutex instead. */
00577         #define ithread_rwlock_wrlock ithread_mutex_lock
00578 #endif /* UPNP_USE_RWLOCK */
00579 
00580 
00581 /****************************************************************************
00582  * Function: ithread_rwlock_unlock
00583  *
00584  *  Description:
00585  *      Unlocks rwlock.
00586  *
00587  *  Parameters:
00588  *      ithread_rwlock_t *rwlock (must be valid non NULL pointer to pthread_rwlock_t)
00589  *      rwlock must be initialized.
00590  *      
00591  *  Returns:
00592  *      0 on success, Nonzero on failure.
00593  *      Always returns 0.
00594  *      See man page for pthread_rwlock_unlock
00595  *****************************************************************************/
00596 #if UPNP_USE_RWLOCK
00597         #define ithread_rwlock_unlock pthread_rwlock_unlock
00598 #else
00599         /* Read-write locks aren't available: use mutex instead. */
00600         #define ithread_rwlock_unlock ithread_mutex_unlock
00601 #endif /* UPNP_USE_RWLOCK */
00602 
00603 
00604 /****************************************************************************
00605  * Function: ithread_rwlock_destroy
00606  *
00607  *  Description:
00608  *      Releases any resources held by the rwlock. 
00609  *              rwlock can no longer be used after this call.
00610  *              rwlock is only destroyed when there are no longer any threads waiting on it. 
00611  *              rwlock cannot be destroyed if it is locked.
00612  *  Parameters:
00613  *      ithread_rwlock_t *rwlock (must be valid non NULL pointer to pthread_rwlock_t)
00614  *      rwlock must be initialized.
00615  *  Returns:
00616  *      0 on success. Nonzero on failure.
00617  *      Always returns 0.
00618  *      See man page for pthread_rwlock_destroy
00619  *****************************************************************************/
00620 #if UPNP_USE_RWLOCK
00621         #define ithread_rwlock_destroy pthread_rwlock_destroy
00622 #else
00623         /* Read-write locks aren't available: use mutex instead. */
00624         #define ithread_rwlock_destroy ithread_mutex_destroy
00625 #endif /* UPNP_USE_RWLOCK */
00626 
00627 
00628 /****************************************************************************
00629  * Function: ithread_cond_init
00630  *
00631  *  Description:
00632  *      Initializes condition variable.
00633  *      Must be called before use.
00634  *  Parameters:
00635  *      ithread_cond_t *cond (must be valid non NULL pointer to pthread_cond_t)
00636  *      const ithread_condattr_t *cond_attr (ignored)
00637  *  Returns:
00638  *      0 on success, Nonzero on failure.
00639  *      See man page for pthread_cond_init
00640  *****************************************************************************/
00641 #define ithread_cond_init pthread_cond_init
00642 
00643 
00644 /****************************************************************************
00645  * Function: ithread_cond_signal
00646  *
00647  *  Description:
00648  *      Wakes up exactly one thread waiting on condition.
00649  *      Associated mutex MUST be locked by thread before entering this call.
00650  *  Parameters:
00651  *      ithread_cond_t *cond (must be valid non NULL pointer to 
00652  *      ithread_cond_t)
00653  *      cond must be initialized
00654  *  Returns:
00655  *      0 on success, Nonzero on failure.
00656  *      See man page for pthread_cond_signal
00657  *****************************************************************************/
00658 #define ithread_cond_signal pthread_cond_signal
00659 
00660 
00661 /****************************************************************************
00662  * Function: ithread_cond_broadcast
00663  *
00664  *  Description:
00665  *      Wakes up all threads waiting on condition.
00666  *      Associated mutex MUST be locked by thread before entering this call.
00667  *  Parameters:
00668  *      ithread_cond_t *cond (must be valid non NULL pointer to 
00669  *      ithread_cond_t)
00670  *      cond must be initialized
00671  *  Returns:
00672  *      0 on success, Nonzero on failure.
00673  *      See man page for pthread_cond_broadcast
00674  *****************************************************************************/
00675 #define ithread_cond_broadcast pthread_cond_broadcast
00676   
00677 
00678 /****************************************************************************
00679  * Function: ithread_cond_wait
00680  *
00681  *  Description:
00682  *      Atomically releases mutex and waits on condition.
00683  *      Associated mutex MUST be locked by thread before entering this call.
00684  *      Mutex is reacquired when call returns.
00685  *  Parameters:
00686  *      ithread_cond_t *cond (must be valid non NULL pointer to 
00687  *      ithread_cond_t)
00688  *      cond must be initialized
00689  *      ithread_mutex_t *mutex (must be valid non NULL pointer to 
00690  *      ithread_mutex_t)
00691  *      Mutex must be locked.
00692  *  Returns:
00693  *      0 on success, Nonzero on failure.
00694  *      See man page for pthread_cond_wait
00695  *****************************************************************************/
00696 #define ithread_cond_wait pthread_cond_wait
00697   
00698 
00699   /****************************************************************************
00700    * Function: pthread_cond_timedwait
00701    *
00702    *    Description:      
00703    *            Atomically releases the associated mutex and waits on the
00704    *    condition.
00705    *            If the condition is not signaled in the specified time than the
00706    *    call times out and returns.
00707    *            Associated mutex MUST be locked by thread before entering this call.
00708    *            Mutex is reacquired when call returns.
00709    *  Parameters:
00710    *      ithread_cond_t *cond (must be valid non NULL pointer to ithread_cond_t)
00711    *            cond must be initialized
00712    *      ithread_mutex_t *mutex (must be valid non NULL pointer to ithread_mutex_t)
00713    *            Mutex must be locked.
00714    *      const struct timespec *abstime (absolute time, measured from Jan 1, 1970)
00715    *  Returns:
00716    *      0 on success. ETIMEDOUT on timeout. Nonzero on failure.
00717    *      See man page for pthread_cond_timedwait
00718    ***************************************************************************/
00719  
00720 #define ithread_cond_timedwait pthread_cond_timedwait
00721   
00722 
00723   /****************************************************************************
00724    * Function: ithread_cond_destroy
00725    *
00726    *  Description:
00727    *      Releases any resources held by the condition variable. 
00728    *            Condition variable can no longer be used after this call.       
00729    *  Parameters:
00730    *      ithread_cond_t *cond (must be valid non NULL pointer to 
00731    *      ithread_cond_t)
00732    *      cond must be initialized.
00733    *  Returns:
00734    *      0 on success. Nonzero on failure.
00735    *      See man page for pthread_cond_destroy
00736    ***************************************************************************/
00737 #define ithread_cond_destroy pthread_cond_destroy
00738 
00739   /****************************************************************************
00740    * Function: ithread_attr_init
00741    *
00742    *  Description:
00743    *      Initialises thread attribute object.
00744    *  Parameters:
00745    *      ithread_attr_t *attr (must be valid non NULL pointer to
00746    *      ithread_attr_t)
00747    *  Returns:
00748    *      0 on success. Nonzero on failure.
00749    *      See man page for pthread_attr_init
00750    ***************************************************************************/
00751 #define ithread_attr_init pthread_attr_init
00752 
00753   /****************************************************************************
00754    * Function: ithread_attr_destroy
00755    *
00756    *  Description:
00757    *      Destroys thread attribute object.
00758    *  Parameters:
00759    *      ithread_attr_t *attr (must be valid non NULL pointer to
00760    *      ithread_attr_t)
00761    *  Returns:
00762    *      0 on success. Nonzero on failure.
00763    *      See man page for pthread_attr_destroy
00764    ***************************************************************************/
00765 #define ithread_attr_destroy pthread_attr_destroy
00766 
00767   /****************************************************************************
00768    * Function: ithread_attr_setstacksize
00769    *
00770    *  Description:
00771    *      Sets stack size of a thread attribute object.
00772    *  Parameters:
00773    *      ithread_attr_t *attr (must be valid non NULL pointer to
00774    *      ithread_attr_t)
00775    *      size_t stacksize (value of stacksize must be greater than
00776    *      ITHREAD_STACK_MIN and lower than system-imposed limits
00777    *  Returns:
00778    *      0 on success. Nonzero on failure.
00779    *      See man page for pthread_attr_setstacksize
00780    ***************************************************************************/
00781 #define ithread_attr_setstacksize pthread_attr_setstacksize
00782 
00783   /****************************************************************************
00784    * Function: ithread_attr_setdetachstate
00785    *
00786    *  Description:
00787    *      Sets detach state of a thread attribute object.
00788    *  Parameters:
00789    *      ithread_attr_t *attr (must be valid non NULL pointer to
00790    *      ithread_attr_t)
00791    *      int detachstate (value of detachstate must be ITHREAD_CREATE_DETACHED
00792    *      or ITHREAD_CREATE_JOINABLE)
00793    *  Returns:
00794    *      0 on success. Nonzero on failure.
00795    *      See man page for pthread_attr_setdetachstate
00796    ***************************************************************************/
00797 #define ithread_attr_setdetachstate pthread_attr_setdetachstate
00798 
00799   /****************************************************************************
00800    * Function: ithread_create
00801    *
00802    *  Description:
00803    *            Creates a thread with the given start routine
00804    *      and argument.
00805    *  Parameters:
00806    *      ithread_t * thread (must be valid non NULL pointer to pthread_t)
00807    *      ithread_attr_t *attr
00808    *      void * (start_routine) (void *arg) (start routine)
00809    *      void * arg - argument.
00810    *  Returns:
00811    *      0 on success. Nonzero on failure.
00812    *        Returns EAGAIN if a new thread can not be created.
00813    *      Returns EINVAL if there is a problem with the arguments.
00814    *      See man page fore pthread_create
00815    ***************************************************************************/
00816 #define ithread_create pthread_create
00817 
00818 
00819   /****************************************************************************
00820    * Function: ithread_cancel
00821    *
00822    *  Description:
00823    *            Cancels a thread.
00824    *  Parameters:
00825    *      ithread_t * thread (must be valid non NULL pointer to ithread_t)
00826    *  Returns:
00827    *      0 on success. Nonzero on failure.
00828    *      See man page for pthread_cancel
00829    ***************************************************************************/
00830 #define ithread_cancel pthread_cancel
00831   
00832 
00833   /****************************************************************************
00834    * Function: ithread_exit
00835    *
00836    *  Description:
00837    *            Returns a return code from a thread.
00838    *      Implicitly called when the start routine returns.
00839    *  Parameters:
00840    *      void  * return_code return code to return
00841    *      See man page for pthread_exit
00842    ***************************************************************************/
00843 #define ithread_exit pthread_exit
00844 
00845 
00846 /****************************************************************************
00847    * Function: ithread_get_current_thread_id
00848    *
00849    *  Description:
00850    *            Returns the handle of the currently running thread.
00851    *  Returns:
00852    *            The handle of the currently running thread.
00853    *              See man page for pthread_self
00854    ***************************************************************************/
00855 #define ithread_get_current_thread_id pthread_self
00856 
00857 
00858   /****************************************************************************
00859    * Function: ithread_self
00860    *
00861    *  Description:
00862    *            Returns the handle of the currently running thread.
00863    *  Returns:
00864    *            The handle of the currently running thread.
00865    *              See man page for pthread_self
00866    ***************************************************************************/
00867 #define ithread_self pthread_self
00868 
00869 
00870   /****************************************************************************
00871    * Function: ithread_detach
00872    *
00873    *  Description:
00874    *            Makes a thread's resources reclaimed immediately 
00875    *            after it finishes
00876    *            execution.  
00877    *  Returns:
00878    *            0 on success, Nonzero on failure.
00879    *      See man page for pthread_detach
00880    ***************************************************************************/
00881 #define ithread_detach pthread_detach  
00882 
00883 
00884   /****************************************************************************
00885    * Function: ithread_join
00886    *
00887    *  Description:
00888    *            Suspends the currently running thread until the 
00889    * specified thread
00890    *      has finished. 
00891    *      Returns the return code of the thread, or ITHREAD_CANCELED 
00892    *      if the thread has been canceled.
00893    *  Parameters:
00894    *      ithread_t *thread (valid non null thread identifier)
00895    *      void ** return (space for return code) 
00896    *  Returns:
00897    *            0 on success, Nonzero on failure.
00898    *     See man page for pthread_join
00899    ***************************************************************************/
00900 #define ithread_join pthread_join
00901   
00902 
00903 /****************************************************************************
00904  * Function: isleep
00905  *
00906  *  Description:
00907  *              Suspends the currently running thread for the specified number 
00908  *      of seconds
00909  *      Always returns 0.
00910  *  Parameters:
00911  *      unsigned int seconds - number of seconds to sleep.
00912  *  Returns:
00913  *              0 on success, Nonzero on failure.
00914  *              See man page for sleep (man 3 sleep)
00915  *****************************************************************************/
00916 #ifdef WIN32
00917         #define isleep(x) Sleep((x)*1000)
00918 #else
00919         #define isleep sleep
00920 #endif
00921 
00922 
00923 /****************************************************************************
00924  * Function: isleep
00925  *
00926  *  Description:
00927  *              Suspends the currently running thread for the specified number 
00928  *      of milliseconds
00929  *      Always returns 0.
00930  *  Parameters:
00931  *      unsigned int milliseconds - number of milliseconds to sleep.
00932  *  Returns:
00933  *              0 on success, Nonzero on failure.
00934  *              See man page for sleep (man 3 sleep)
00935  *****************************************************************************/
00936 #ifdef WIN32
00937         #define imillisleep Sleep
00938 #else
00939         #define imillisleep(x) usleep(1000*x)
00940 #endif
00941 
00942 
00943 #if !defined(PTHREAD_MUTEX_RECURSIVE) && !defined(__DragonFly__) && !defined(UPNP_USE_MSVCPP)
00944 /* !defined(UPNP_USE_MSVCPP) should probably also have pthreads version check - but it's not clear if that is possible */
00945 /* NK: Added for satisfying the gcc compiler */
00946 EXPORT_SPEC int pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind);
00947 #endif
00948 
00949 
00950 #ifdef __cplusplus
00951 }
00952 #endif
00953 
00954 
00955 #endif /* ITHREAD_H */
00956