From 83ee32afb78687a55184bd38814cceca29a90f40 Mon Sep 17 00:00:00 2001 From: Marcelo Roberto Jimenez Date: Tue, 6 Nov 2007 02:04:38 +0000 Subject: [PATCH] Added support for rwlocks. git-svn-id: https://pupnp.svn.sourceforge.net/svnroot/pupnp/trunk@232 119443c7-1b9e-41f8-b6fc-b9c35fce742c --- ChangeLog | 5 +- threadutil/inc/ithread.h | 415 ++++++++++++++++++++++++++++----------- 2 files changed, 307 insertions(+), 113 deletions(-) diff --git a/ChangeLog b/ChangeLog index d4ee21c..be4c899 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,7 +2,10 @@ Version 1.6.1 ******************************************************************************* -2007-05-26 Marcelo Jimenez +2007-11-06 Marcelo Jimenez + * Added support for rwlocks. + +2007-11-05 Marcelo Jimenez * SF Bug Tracker [ 1825929 ] woker thread still alive after UpnpFinish() Submitted By: Luke Kim - nereusuj Worker thread still alive after calling UpnpFinish() because diff --git a/threadutil/inc/ithread.h b/threadutil/inc/ithread.h index 90d69f3..46947d3 100644 --- a/threadutil/inc/ithread.h +++ b/threadutil/inc/ithread.h @@ -42,131 +42,158 @@ extern "C" { #endif #ifdef __FreeBSD__ -#define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE + #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE #endif #ifdef PTHREAD_MUTEX_RECURSIVE -/* This system has SuS2-compliant mutex attributes. - * E.g. on Cygwin, where we don't have the old nonportable (NP) symbols - */ -#define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_NORMAL -#define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE -#define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK -#else -#define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_FAST_NP -#define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE_NP -#define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK_NP -#endif + /* This system has SuS2-compliant mutex attributes. + * E.g. on Cygwin, where we don't have the old nonportable (NP) symbols + */ + #define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_NORMAL + #define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE + #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK +#else /* PTHREAD_MUTEX_RECURSIVE */ + #define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_FAST_NP + #define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE_NP + #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK_NP +#endif /* PTHREAD_MUTEX_RECURSIVE */ + + +#define ITHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE +#define ITHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED + #define ITHREAD_CANCELED PTHREAD_CANCELED - /*************************************************************************** - * Name: ithread_t - * - * Description: - * Thread handle. - * typedef to pthread_t. - * Internal Use Only. - ***************************************************************************/ - typedef pthread_t ithread_t; +/*************************************************************************** + * Name: ithread_t + * + * Description: + * Thread handle. + * typedef to pthread_t. + * Internal Use Only. + ***************************************************************************/ +typedef pthread_t ithread_t; - /**************************************************************************** - * Name: ithread_attr_t - * - * Description: - * Thread attribute. - * typedef to pthread_attr_t - * Internal Use Only - ***************************************************************************/ - typedef pthread_attr_t ithread_attr_t; +/**************************************************************************** + * Name: ithread_attr_t + * + * Description: + * Thread attribute. + * typedef to pthread_attr_t + * Internal Use Only + ***************************************************************************/ +typedef pthread_attr_t ithread_attr_t; - /**************************************************************************** - * Name: start_routine - * - * Description: - * Thread start routine - * Internal Use Only. - ***************************************************************************/ - typedef void * (*start_routine) (void *arg); +/**************************************************************************** + * Name: start_routine + * + * Description: + * Thread start routine + * Internal Use Only. + ***************************************************************************/ +typedef void * (*start_routine) (void *arg); - /**************************************************************************** - * Name: ithread_cond_t - * - * Description: - * condition variable. - * typedef to pthread_cond_t - * Internal Use Only. - ***************************************************************************/ - typedef pthread_cond_t ithread_cond_t; +/**************************************************************************** + * Name: ithread_cond_t + * + * Description: + * condition variable. + * typedef to pthread_cond_t + * Internal Use Only. + ***************************************************************************/ +typedef pthread_cond_t ithread_cond_t; - /**************************************************************************** - * Name: ithread_mutexattr_t - * - * Description: - * Mutex attribute. - * typedef to pthread_mutexattr_t - * Internal Use Only - ***************************************************************************/ - typedef pthread_mutexattr_t ithread_mutexattr_t; +/**************************************************************************** + * Name: ithread_mutexattr_t + * + * Description: + * Mutex attribute. + * typedef to pthread_mutexattr_t + * Internal Use Only + ***************************************************************************/ +typedef pthread_mutexattr_t ithread_mutexattr_t; - /**************************************************************************** - * Name: ithread_mutex_t - * - * Description: - * Mutex. - * typedef to pthread_mutex_t - * Internal Use Only. - ***************************************************************************/ - typedef pthread_mutex_t ithread_mutex_t; +/**************************************************************************** + * Name: ithread_mutex_t + * + * Description: + * Mutex. + * typedef to pthread_mutex_t + * Internal Use Only. + ***************************************************************************/ +typedef pthread_mutex_t ithread_mutex_t; - /**************************************************************************** - * Name: ithread_condattr_t - * - * Description: - * Condition attribute. - * typedef to pthread_condattr_t - * NOT USED - * Internal Use Only - ***************************************************************************/ - typedef pthread_condattr_t ithread_condattr_t; +/**************************************************************************** + * Name: ithread_condattr_t + * + * Description: + * Condition attribute. + * typedef to pthread_condattr_t + * NOT USED + * Internal Use Only + ***************************************************************************/ +typedef pthread_condattr_t ithread_condattr_t; - /**************************************************************************** - * Function: ithread_mutexattr_init - * - * Description: - * Initializes a mutex attribute variable. - * Used to set the type of the mutex. - * Parameters: - * ithread_mutexattr_init * attr (must be valid non NULL pointer to - * pthread_mutexattr_t) - * Returns: - * 0 on success, Nonzero on failure. - * Always returns 0. - * See man page for pthread_mutexattr_init - ***************************************************************************/ - + +/**************************************************************************** + * Name: ithread_rwlockattr_t + * + * Description: + * Mutex attribute. + * typedef to pthread_rwlockattr_t + * Internal Use Only + ***************************************************************************/ +typedef pthread_rwlockattr_t ithread_rwlockattr_t; + + +/**************************************************************************** + * Name: ithread_rwlock_t + * + * Description: + * Condition attribute. + * typedef to pthread_rwlock_t + * Internal Use Only + ***************************************************************************/ +typedef pthread_rwlock_t ithread_rwlock_t; + +/**************************************************************************** + * Function: ithread_mutexattr_init + * + * Description: + * Initializes a mutex attribute variable. + * Used to set the type of the mutex. + * Parameters: + * ithread_mutexattr_init * attr (must be valid non NULL pointer to + * pthread_mutexattr_t) + * Returns: + * 0 on success, Nonzero on failure. + * Always returns 0. + * See man page for pthread_mutexattr_init + ***************************************************************************/ #define ithread_mutexattr_init pthread_mutexattr_init - - /**************************************************************************** - * Function: ithread_mutexattr_destroy - * - * Description: - * Releases any resources held by the mutex attribute. - * Currently there are no resources associated with the attribute - * Parameters: - * ithread_mutexattr_t * attr (must be valid non NULL pointer to - * pthread_mutexattr_t) - * Returns: - * 0 on success, Nonzero on failure. - * Always returns 0. - * See man page for pthread_mutexattr_destroy - ***************************************************************************/ + + +/**************************************************************************** + * Function: ithread_mutexattr_destroy + * + * Description: + * Releases any resources held by the mutex attribute. + * Currently there are no resources associated with the attribute + * Parameters: + * ithread_mutexattr_t * attr (must be valid non NULL pointer to + * pthread_mutexattr_t) + * Returns: + * 0 on success, Nonzero on failure. + * Always returns 0. + * See man page for pthread_mutexattr_destroy + ***************************************************************************/ #define ithread_mutexattr_destroy pthread_mutexattr_destroy @@ -180,7 +207,7 @@ extern "C" { * ITHREAD_MUTEX_ERRORCHECK_NP * * Parameters: - * ithread_mutexattr_t * mutex (must be valid non NULL pointer to + * ithread_mutexattr_t * attr (must be valid non NULL pointer to * ithread_mutexattr_t) * int kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP * or ITHREAD_MUTEX_ERRORCHECK_NP) @@ -190,9 +217,9 @@ extern "C" { * See man page for pthread_mutexattr_setkind_np *****************************************************************************/ #ifdef PTHREAD_MUTEX_RECURSIVE -#define ithread_mutexattr_setkind_np pthread_mutexattr_settype + #define ithread_mutexattr_setkind_np pthread_mutexattr_settype #else -#define ithread_mutexattr_setkind_np pthread_mutexattr_setkind_np + #define ithread_mutexattr_setkind_np pthread_mutexattr_setkind_np #endif /**************************************************************************** @@ -205,7 +232,7 @@ extern "C" { * ITHREAD_MUTEX_ERRORCHECK_NP * * Parameters: - * ithread_mutexattr_t * mutex (must be valid non NULL pointer to + * ithread_mutexattr_t * attr (must be valid non NULL pointer to * pthread_mutexattr_t) * int *kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP * or ITHREAD_MUTEX_ERRORCHECK_NP) @@ -215,9 +242,9 @@ extern "C" { * See man page for pthread_mutexattr_getkind_np *****************************************************************************/ #ifdef PTHREAD_MUTEX_RECURSIVE -#define ithread_mutexattr_getkind_np pthread_mutexattr_gettype + #define ithread_mutexattr_getkind_np pthread_mutexattr_gettype #else -#define ithread_mutexattr_getkind_np pthread_mutexattr_getkind_np + #define ithread_mutexattr_getkind_np pthread_mutexattr_getkind_np #endif @@ -237,7 +264,8 @@ extern "C" { * See man page for pthread_mutex_init *****************************************************************************/ #define ithread_mutex_init pthread_mutex_init - + + /**************************************************************************** * Function: ithread_mutex_lock * @@ -291,7 +319,170 @@ extern "C" { *****************************************************************************/ #define ithread_mutex_destroy pthread_mutex_destroy + +/**************************************************************************** + * Function: ithread_rwlockattr_init + * + * Description: + * Initializes a rwlock attribute variable to default values. + * Parameters: + * const ithread_rwlockattr_init *attr (must be valid non NULL pointer to + * pthread_rwlockattr_t) + * Returns: + * 0 on success, Nonzero on failure. + * Always returns 0. + * See man page for pthread_rwlockattr_init + ***************************************************************************/ +#define ithread_rwlockattr_init pthread_rwlockattr_init + + +/**************************************************************************** + * Function: ithread_rwlockattr_destroy + * + * Description: + * Releases any resources held by the rwlock attribute. + * Parameters: + * ithread_rwlockattr_t *attr (must be valid non NULL pointer to + * pthread_rwlockattr_t) + * Returns: + * 0 on success, Nonzero on failure. + * Always returns 0. + * See man page for pthread_rwlockattr_destroy + ***************************************************************************/ +#define ithread_rwlockattr_destroy pthread_rwlockattr_destroy + +/**************************************************************************** + * Function: ithread_rwlockatttr_setpshared + * + * Description: + * Sets the rwlock type in the attribute. + * Valid types are: ITHREAD_PROCESS_PRIVATE + * ITHREAD_PROCESS_SHARED + * + * Parameters: + * ithread_rwlockattr_t * attr (must be valid non NULL pointer to + * ithread_rwlockattr_t) + * int kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED) + * + * Returns: + * 0 on success. Nonzero on failure. + * Returns EINVAL if the kind is not supported. + * See man page for pthread_rwlockattr_setkind_np + *****************************************************************************/ +#define ithread_rwlockatttr_setpshared pthread_rwlockatttr_setpshared + + +/**************************************************************************** + * Function: ithread_rwlockatttr_getpshared + * + * Description: + * Gets the rwlock type in the attribute. + * Valid types are: ITHREAD_PROCESS_PRIVATE + * ITHREAD_PROCESS_SHARED + * + * Parameters: + * ithread_rwlockattr_t * attr (must be valid non NULL pointer to + * pthread_rwlockattr_t) + * int *kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED) + * + * Returns: + * 0 on success. Nonzero on failure. + * Always returns 0. + * See man page for pthread_rwlockatttr_getpshared + *****************************************************************************/ +#define ithread_rwlockatttr_getpshared pthread_rwlockatttr_getpshared + + +/**************************************************************************** + * Function: ithread_rwlock_init + * + * Description: + * Initializes rwlock. + * Must be called before use. + * + * Parameters: + * ithread_rwlock_t * rwlock (must be valid non NULL pointer to pthread_rwlock_t) + * const ithread_rwlockattr_t * rwlock_attr + * Returns: + * 0 on success, Nonzero on failure. + * Always returns 0. + * See man page for pthread_rwlock_init + *****************************************************************************/ +#define ithread_rwlock_init pthread_rwlock_init + + +/**************************************************************************** + * Function: ithread_rwlock_rdlock + * + * Description: + * Locks rwlock for reading. + * Parameters: + * ithread_rwlock_t * rwlock (must be valid non NULL pointer to pthread_rwlock_t) + * rwlock must be initialized. + * + * Returns: + * 0 on success, Nonzero on failure. + * Always returns 0. + * See man page for pthread_rwlock_rdlock + *****************************************************************************/ +#define ithread_rwlock_rdlock pthread_rwlock_rdlock + + +/**************************************************************************** + * Function: ithread_rwlock_wrlock + * + * Description: + * Locks rwlock for writting. + * Parameters: + * ithread_rwlock_t * rwlock (must be valid non NULL pointer to pthread_rwlock_t) + * rwlock must be initialized. + * + * Returns: + * 0 on success, Nonzero on failure. + * Always returns 0. + * See man page for pthread_rwlock_wrlock + *****************************************************************************/ +#define ithread_rwlock_wrlock pthread_rwlock_wrlock + + +/**************************************************************************** + * Function: ithread_rwlock_unlock + * + * Description: + * Unlocks rwlock. + * + * Parameters: + * ithread_rwlock_t * rwlock (must be valid non NULL pointer to pthread_rwlock_t) + * rwlock must be initialized. + * + * Returns: + * 0 on success, Nonzero on failure. + * Always returns 0. + * See man page for pthread_rwlock_unlock + *****************************************************************************/ +#define ithread_rwlock_unlock pthread_rwlock_unlock + + +/**************************************************************************** + * Function: ithread_rwlock_destroy + * + * Description: + * Releases any resources held by the rwlock. + * rwlock can no longer be used after this call. + * rwlock is only destroyed when there are no longer any threads waiting on it. + * rwlock cannot be destroyed if it is locked. + * Parameters: + * ithread_rwlock_t * rwlock (must be valid non NULL pointer to pthread_rwlock_t) + * rwlock must be initialized. + * Returns: + * 0 on success. Nonzero on failure. + * Always returns 0. + * See man page for pthread_rwlock_destroy + *****************************************************************************/ +#define ithread_rwlock_destroy pthread_rwlock_destroy + + /**************************************************************************** * Function: ithread_cond_init *