From 20c4e016fa948fc61ffb437d889a5eabc680c8af Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Thu, 25 Oct 2007 06:42:40 +0000 Subject: [PATCH] Added native thread-specific data support --- include/ldap_int_thread.h | 6 ++++++ include/ldap_pvt_thread.h | 13 ++++++++++++ libraries/libldap_r/thr_cthreads.c | 24 ++++++++++++++++++++++ libraries/libldap_r/thr_nt.c | 33 ++++++++++++++++++++++++++++++ libraries/libldap_r/thr_posix.c | 25 ++++++++++++++++++++++ libraries/libldap_r/thr_pth.c | 25 ++++++++++++++++++++++ libraries/libldap_r/thr_stub.c | 24 ++++++++++++++++++++++ libraries/libldap_r/thr_thr.c | 24 ++++++++++++++++++++++ libraries/libldap_r/tpool.c | 6 +++--- 9 files changed, 177 insertions(+), 3 deletions(-) diff --git a/include/ldap_int_thread.h b/include/ldap_int_thread.h index d6a6d77957..db345df06d 100644 --- a/include/ldap_int_thread.h +++ b/include/ldap_int_thread.h @@ -44,6 +44,7 @@ LDAP_BEGIN_DECL typedef pthread_t ldap_int_thread_t; typedef pthread_mutex_t ldap_int_thread_mutex_t; typedef pthread_cond_t ldap_int_thread_cond_t; +typedef pthread_key_t ldap_int_thread_key_t; #define ldap_int_thread_equal(a, b) pthread_equal((a), (b)) @@ -88,6 +89,7 @@ LDAP_BEGIN_DECL typedef cthread_t ldap_int_thread_t; typedef struct mutex ldap_int_thread_mutex_t; typedef struct condition ldap_int_thread_cond_t; +typedef cthread_key_t ldap_int_thread_key_t; LDAP_END_DECL @@ -106,6 +108,7 @@ LDAP_BEGIN_DECL typedef pth_t ldap_int_thread_t; typedef pth_mutex_t ldap_int_thread_mutex_t; typedef pth_cond_t ldap_int_thread_cond_t; +typedef pth_key_t ldap_int_thread_key_t; #if 0 #define LDAP_THREAD_HAVE_RDWR 1 @@ -129,6 +132,7 @@ LDAP_BEGIN_DECL typedef thread_t ldap_int_thread_t; typedef mutex_t ldap_int_thread_mutex_t; typedef cond_t ldap_int_thread_cond_t; +typedef thread_key_t ldap_int_thread_key_t; #define HAVE_REENTRANT_FUNCTIONS 1 @@ -181,6 +185,7 @@ LDAP_BEGIN_DECL typedef unsigned long ldap_int_thread_t; typedef HANDLE ldap_int_thread_mutex_t; typedef HANDLE ldap_int_thread_cond_t; +typedef DWORD ldap_int_thread_key_t; LDAP_END_DECL @@ -201,6 +206,7 @@ LDAP_BEGIN_DECL typedef int ldap_int_thread_t; typedef int ldap_int_thread_mutex_t; typedef int ldap_int_thread_cond_t; +typedef int ldap_int_thread_key_t; #define LDAP_THREAD_HAVE_TPOOL 1 typedef int ldap_int_thread_pool_t; diff --git a/include/ldap_pvt_thread.h b/include/ldap_pvt_thread.h index 151756d692..21b2dc69cd 100644 --- a/include/ldap_pvt_thread.h +++ b/include/ldap_pvt_thread.h @@ -34,6 +34,7 @@ typedef ldap_int_thread_cond_t ldap_pvt_thread_cond_t; typedef ldap_int_thread_rdwr_t ldap_pvt_thread_rdwr_t; #endif typedef ldap_int_thread_rmutex_t ldap_pvt_thread_rmutex_t; +typedef ldap_int_thread_key_t ldap_pvt_thread_key_t; #endif /* !LDAP_PVT_THREAD_H_DONE */ #define ldap_pvt_thread_equal ldap_int_thread_equal @@ -169,6 +170,18 @@ ldap_pvt_thread_rdwr_wtrylock LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp)); LDAP_F( int ) ldap_pvt_thread_rdwr_wunlock LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp)); +LDAP_F( int ) +ldap_pvt_thread_key_create LDAP_P((ldap_pvt_thread_key_t *keyp)); + +LDAP_F( int ) +ldap_pvt_thread_key_destroy LDAP_P((ldap_pvt_thread_key_t key)); + +LDAP_F( int ) +ldap_pvt_thread_key_setdata LDAP_P((ldap_pvt_thread_key_t key, void *data)); + +LDAP_F( int ) +ldap_pvt_thread_key_getdata LDAP_P((ldap_pvt_thread_key_t key, void **data)); + #ifdef LDAP_DEBUG LDAP_F( int ) ldap_pvt_thread_rdwr_readers LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp)); diff --git a/libraries/libldap_r/thr_cthreads.c b/libraries/libldap_r/thr_cthreads.c index b98a47afa2..ab19880bb7 100644 --- a/libraries/libldap_r/thr_cthreads.c +++ b/libraries/libldap_r/thr_cthreads.c @@ -153,4 +153,28 @@ ldap_pvt_thread_self( void ) return cthread_self(); } +int +ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *key ) +{ + return cthread_keycreate( key ); +} + +int +ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key ) +{ + return( 0 ); +} + +int +ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data ) +{ + return cthread_setspecific( key, data ); +} + +int +ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data ) +{ + return cthread_getspecific( key, data ); +} + #endif /* HAVE_MACH_CTHREADS */ diff --git a/libraries/libldap_r/thr_nt.c b/libraries/libldap_r/thr_nt.c index f3d0755de9..29416fa85e 100644 --- a/libraries/libldap_r/thr_nt.c +++ b/libraries/libldap_r/thr_nt.c @@ -198,4 +198,37 @@ ldap_pvt_thread_self( void ) return GetCurrentThreadId(); } +int +ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *keyp ) +{ + DWORD key = TlsAlloc(); + if ( key != TLS_OUT_OF_INDEXES ) { + *keyp = key; + return 0; + } else { + return -1; + } +} + +int +ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key ) +{ + /* TlsFree returns 0 on failure */ + return( TlsFree( key ) == 0 ); +} + +int +ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data ) +{ + return ( TlsSetValue( key, data ) == 0 ); +} + +int +ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data ) +{ + void *ptr = TlsGetValue( key ); + *data = ptr; + return( ptr ? GetLastError() : 0 ); +} + #endif diff --git a/libraries/libldap_r/thr_posix.c b/libraries/libldap_r/thr_posix.c index 4e0a45b432..34914b626f 100644 --- a/libraries/libldap_r/thr_posix.c +++ b/libraries/libldap_r/thr_posix.c @@ -312,6 +312,31 @@ ldap_pvt_thread_t ldap_pvt_thread_self( void ) return pthread_self(); } +int +ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *key ) +{ + return pthread_key_create( key, NULL ); +} + +int +ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key ) +{ + return pthread_key_delete( key ); +} + +int +ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data ) +{ + return pthread_setspecific( key, data ); +} + +int +ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data ) +{ + *data = pthread_getspecific( key ); + return 0; +} + #ifdef LDAP_THREAD_HAVE_RDWR #ifdef HAVE_PTHREAD_RWLOCK_DESTROY int diff --git a/libraries/libldap_r/thr_pth.c b/libraries/libldap_r/thr_pth.c index c5844533c1..f06efe0d13 100644 --- a/libraries/libldap_r/thr_pth.c +++ b/libraries/libldap_r/thr_pth.c @@ -159,6 +159,31 @@ ldap_pvt_thread_self( void ) return pth_self(); } +int +ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *key ) +{ + return pth_key_create( key, NULL ); +} + +int +ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key ) +{ + return pth_key_delete( key ); +} + +int +ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data ) +{ + return pth_key_setdata( key, data ); +} + +int +ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data ) +{ + *data = pth_key_getdata( key ); + return 0; +} + #ifdef LDAP_THREAD_HAVE_RDWR int ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw ) diff --git a/libraries/libldap_r/thr_stub.c b/libraries/libldap_r/thr_stub.c index 1dce9d160b..65a18bd0ac 100644 --- a/libraries/libldap_r/thr_stub.c +++ b/libraries/libldap_r/thr_stub.c @@ -237,6 +237,30 @@ ldap_pvt_thread_self( void ) return(0); } +int +ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *key ) +{ + return(0); +} + +int +ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key ) +{ + return(0); +} + +int +ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data ) +{ + return(0); +} + +int +ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data ) +{ + return(0); +} + ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx ) { diff --git a/libraries/libldap_r/thr_thr.c b/libraries/libldap_r/thr_thr.c index 2acbe083a6..2a9abae545 100644 --- a/libraries/libldap_r/thr_thr.c +++ b/libraries/libldap_r/thr_thr.c @@ -159,4 +159,28 @@ ldap_pvt_thread_self( void ) return thr_self(); } +int +ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *key ) +{ + return thr_keycreate( key, NULL ); +} + +int +ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key ) +{ + return( 0 ); +} + +int +ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data ) +{ + return thr_setspecific( key, data ); +} + +int +ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data ) +{ + return thr_getspecific( key, data ); +} + #endif /* HAVE_THR */ diff --git a/libraries/libldap_r/tpool.c b/libraries/libldap_r/tpool.c index 6e3d81b3b2..8309089cc7 100644 --- a/libraries/libldap_r/tpool.c +++ b/libraries/libldap_r/tpool.c @@ -38,11 +38,11 @@ typedef enum ldap_int_thread_pool_state_e { } ldap_int_thread_pool_state_t; /* Thread-specific key with data and optional free function */ -typedef struct ldap_int_thread_key_s { +typedef struct ldap_int_tpool_key_s { void *ltk_key; void *ltk_data; ldap_pvt_thread_pool_keyfree_t *ltk_free; -} ldap_int_thread_key_t; +} ldap_int_tpool_key_t; /* Max number of thread-specific keys we store per thread. * We don't expect to use many... @@ -55,7 +55,7 @@ typedef struct ldap_int_thread_key_s { /* Context: thread ID and thread-specific key/data pairs */ typedef struct ldap_int_thread_userctx_s { ldap_pvt_thread_t ltu_id; - ldap_int_thread_key_t ltu_key[MAXKEYS]; + ldap_int_tpool_key_t ltu_key[MAXKEYS]; } ldap_int_thread_userctx_t; -- 2.39.5