diff options
author | Mike McGreevy <mamcgree@hdfgroup.org> | 2010-09-10 16:15:34 (GMT) |
---|---|---|
committer | Mike McGreevy <mamcgree@hdfgroup.org> | 2010-09-10 16:15:34 (GMT) |
commit | b7c2d18029df6d724e44b74c8f49bb7aa1e2f02b (patch) | |
tree | c3dd326b9b8ed4746b57165165713ae9b046c62b /src | |
parent | 47c792faa033c4c149b2c5d4dd0e70e4f2b0e3d2 (diff) | |
download | hdf5-b7c2d18029df6d724e44b74c8f49bb7aa1e2f02b.zip hdf5-b7c2d18029df6d724e44b74c8f49bb7aa1e2f02b.tar.gz hdf5-b7c2d18029df6d724e44b74c8f49bb7aa1e2f02b.tar.bz2 |
[svn-r19367] Purpose:
Add windows threads support to HDF5.
Description:
Added calls to the windows threads library to the H5TS layer, and wrapped
most calls to either pthreads or windows threads library with portable
H5TS-style defines. Modified tests to use portable function definitions
as well.
This can be configured via CMake with the HDF5_ENABLE_THREADSAFE
option, and should work on windows vista and later operating systems.
Tested:
h5committest, plus threadsafe with pthreads on jam and amani,
and tested on a Windows Vista VM with threadsafe using windows threads.
Diffstat (limited to 'src')
-rw-r--r-- | src/H5CS.c | 4 | ||||
-rw-r--r-- | src/H5E.c | 4 | ||||
-rw-r--r-- | src/H5TS.c | 139 | ||||
-rw-r--r-- | src/H5TSprivate.h | 71 | ||||
-rw-r--r-- | src/H5private.h | 17 |
5 files changed, 196 insertions, 39 deletions
@@ -81,7 +81,7 @@ H5CS_get_stack(void) FUNC_ENTER_NOAPI_NOFUNC_NOFS(H5CS_get_stack); - fstack = pthread_getspecific(H5TS_funcstk_key_g); + fstack = H5TS_get_thread_local_value(H5TS_funcstk_key_g); if (!fstack) { /* no associated value with current thread - create one */ fstack = (H5CS_t *)HDmalloc(sizeof(H5CS_t)); /* Don't use H5MM_malloc() here, it causes infinite recursion */ @@ -94,7 +94,7 @@ H5CS_get_stack(void) * released by the "key destructor" set up in the H5TS * routines. See calls to pthread_key_create() in H5TS.c -QAK) */ - pthread_setspecific(H5TS_funcstk_key_g, (void *)fstack); + H5TS_set_thread_local_value(H5TS_funcstk_key_g, (void *)fstack); } FUNC_LEAVE_NOAPI_NOFS(fstack); @@ -336,7 +336,7 @@ H5E_get_stack(void) FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5E_get_stack) - estack = (H5E_t *)pthread_getspecific(H5TS_errstk_key_g); + estack = (H5E_t *)H5TS_get_thread_local_value(H5TS_errstk_key_g); if(!estack) { /* no associated value with current thread - create one */ @@ -351,7 +351,7 @@ H5E_get_stack(void) * released by the "key destructor" set up in the H5TS * routines. See calls to pthread_key_create() in H5TS.c -QAK) */ - pthread_setspecific(H5TS_errstk_key_g, (void *)estack); + H5TS_set_thread_local_value(H5TS_errstk_key_g, (void *)estack); } /* end if */ /* Set return value */ @@ -29,16 +29,10 @@ typedef struct H5TS_cancel_struct { } H5TS_cancel_t; /* Global variable definitions */ -pthread_once_t H5TS_first_init_g = PTHREAD_ONCE_INIT; -pthread_key_t H5TS_errstk_key_g; -pthread_key_t H5TS_funcstk_key_g; -pthread_key_t H5TS_cancel_key_g; -hbool_t H5TS_allow_concurrent_g = FALSE; /* concurrent APIs override this */ - -/* Local function definitions */ -#ifdef NOT_USED -static void H5TS_mutex_init(H5TS_mutex_t *mutex); -#endif /* NOT_USED */ +H5TS_once_t H5TS_first_init_g; +H5TS_key_t H5TS_errstk_key_g; +H5TS_key_t H5TS_funcstk_key_g; +H5TS_key_t H5TS_cancel_key_g; /*-------------------------------------------------------------------------- @@ -94,8 +88,13 @@ H5TS_key_destructor(void *key_val) void H5TS_first_thread_init(void) { +#ifdef H5_HAVE_WIN_THREADS + InitializeCriticalSection ( &H5_g.init_lock.CriticalSection ); + H5TS_errstk_key_g = TlsAlloc(); + H5TS_funcstk_key_g = TlsAlloc(); + H5TS_cancel_key_g = TlsAlloc(); +#else /* H5_HAVE_WIN_THREADS */ H5_g.H5_libinit_g = FALSE; - /* initialize global API mutex lock */ pthread_mutex_init(&H5_g.init_lock.atomic_lock, NULL); pthread_cond_init(&H5_g.init_lock.cond_var, NULL); @@ -109,8 +108,39 @@ H5TS_first_thread_init(void) /* initialize key for thread cancellability mechanism */ pthread_key_create(&H5TS_cancel_key_g, H5TS_key_destructor); +#endif /* H5_HAVE_WIN_THREADS */ } + +/*-------------------------------------------------------------------------- + * NAME + * H5TS_win32_first_thread_init + * + * USAGE + * H5TS_win32_first_thread_init() + * + * RETURNS + * + * DESCRIPTION + * Special function on windows needed to call the H5TS_first_thread_init + * function. + * + * PROGRAMMER: Mike McGreevy + * September 1, 2010 + * + * MODIFICATIONS: + * + *-------------------------------------------------------------------------- + */ +#ifdef H5_HAVE_WIN_THREADS +BOOL CALLBACK +H5TS_win32_first_thread_init(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContex) +{ + H5TS_first_thread_init(); + return TRUE; +} /* H5TS_win32_first_thread_init() */ +#endif + /*-------------------------------------------------------------------------- * NAME * H5TS_mutex_lock @@ -139,9 +169,11 @@ H5TS_first_thread_init(void) herr_t H5TS_mutex_lock(H5TS_mutex_t *mutex) { - herr_t ret_value; - - ret_value = pthread_mutex_lock(&mutex->atomic_lock); +#ifdef H5_HAVE_WIN_THREADS + EnterCriticalSection( &mutex->CriticalSection); + return 0; +#else /* H5_HAVE_WIN_THREADS */ + herr_t ret_value = pthread_mutex_lock(&mutex->atomic_lock); if (ret_value) return ret_value; @@ -159,7 +191,8 @@ H5TS_mutex_lock(H5TS_mutex_t *mutex) mutex->lock_count = 1; } - return pthread_mutex_unlock(&mutex->atomic_lock); + return pthread_mutex_unlock(&mutex->atomic_lock); +#endif /* H5_HAVE_WIN_THREADS */ } /*-------------------------------------------------------------------------- @@ -191,9 +224,12 @@ H5TS_mutex_lock(H5TS_mutex_t *mutex) herr_t H5TS_mutex_unlock(H5TS_mutex_t *mutex) { - herr_t ret_value; /* Return value */ - - ret_value = pthread_mutex_lock(&mutex->atomic_lock); +#ifdef H5_HAVE_WIN_THREADS + /* Releases ownership of the specified critical section object. */ + LeaveCriticalSection(&mutex->CriticalSection); + return 0; +#else /* H5_HAVE_WIN_THREADS */ + herr_t ret_value = pthread_mutex_lock(&mutex->atomic_lock); if(ret_value) return ret_value; @@ -210,8 +246,9 @@ H5TS_mutex_unlock(H5TS_mutex_t *mutex) ret_value = err; } /* end if */ - return ret_value; -} + return ret_value; +#endif /* H5_HAVE_WIN_THREADS */ +} /* H5TS_mutex_unlock */ /*-------------------------------------------------------------------------- * NAME @@ -246,10 +283,14 @@ H5TS_mutex_unlock(H5TS_mutex_t *mutex) herr_t H5TS_cancel_count_inc(void) { +#ifdef H5_HAVE_WIN_THREADS + /* unsupported; just return 0 */ + return SUCCEED; +#else /* H5_HAVE_WIN_THREADS */ H5TS_cancel_t *cancel_counter; - herr_t ret_value = 0; + herr_t ret_value = SUCCEED; - cancel_counter = pthread_getspecific(H5TS_cancel_key_g); + cancel_counter = H5TS_get_thread_local_value(H5TS_cancel_key_g); if (!cancel_counter) { /* @@ -274,7 +315,9 @@ H5TS_cancel_count_inc(void) &cancel_counter->previous_state); ++cancel_counter->cancel_count; - return ret_value; + + return ret_value; +#endif /* H5_HAVE_WIN_THREADS */ } /*-------------------------------------------------------------------------- @@ -307,16 +350,58 @@ H5TS_cancel_count_inc(void) herr_t H5TS_cancel_count_dec(void) { - herr_t ret_value = 0; - register H5TS_cancel_t *cancel_counter; +#ifdef H5_HAVE_WIN_THREADS + /* unsupported; will just return 0 */ + return SUCCEED; +#else /* H5_HAVE_WIN_THREADS */ + H5E_t *estack = NULL; + herr_t ret_value = SUCCEED; - cancel_counter = pthread_getspecific(H5TS_cancel_key_g); + register H5TS_cancel_t *cancel_counter; + cancel_counter = H5TS_get_thread_local_value(H5TS_cancel_key_g); if (cancel_counter->cancel_count == 1) ret_value = pthread_setcancelstate(cancel_counter->previous_state, NULL); - --cancel_counter->cancel_count; + --cancel_counter->cancel_count; + return ret_value; +#endif /* H5_HAVE_WIN_THREADS */ } + +/*-------------------------------------------------------------------------- + * NAME + * H5TS_create_thread + * + * RETURNS + * Thread identifier. + * + * DESCRIPTION + * Spawn off a new thread calling function 'func' with input 'udata'. + * + * PROGRAMMER: Mike McGreevy + * August 31, 2010 + * + *-------------------------------------------------------------------------- + */ +H5TS_thread_t +H5TS_create_thread(void * func, H5TS_attr_t * attr, void*udata) +{ + H5TS_thread_t ret_value; + +#ifdef H5_HAVE_WIN_THREADS + + ret_value = CreateThread(NULL, 0, func, udata, 0, NULL); + +#else /* H5_HAVE_WIN_THREADS */ + + pthread_create(&ret_value, attr, func, udata); + +#endif /* H5_HAVE_WIN_THREADS */ + + return ret_value; + +} /* H5TS_create_thread */ + #endif /* H5_HAVE_THREADSAFE */ diff --git a/src/H5TSprivate.h b/src/H5TSprivate.h index 0d0b620..964dcf3 100644 --- a/src/H5TSprivate.h +++ b/src/H5TSprivate.h @@ -33,30 +33,91 @@ #include "H5TSpublic.h" /*Public API prototypes */ #endif /* LATER */ +#ifdef H5_HAVE_WIN_THREADS + /* Library level data structures */ +/* Mutexes, Threads, and Attributes */ +typedef struct H5TS_mutex_struct { + CRITICAL_SECTION CriticalSection; +} H5TS_mutex_t; +typedef CRITICAL_SECTION H5TS_mutex_simple_t; +typedef HANDLE H5TS_thread_t; +typedef HANDLE H5TS_attr_t; +typedef DWORD H5TS_key_t; +typedef INIT_ONCE H5TS_once_t; + +/* Defines */ +/* not used on windows side, but need to be defined to something */ +#define H5TS_SCOPE_SYSTEM 0 +#define H5TS_SCOPE_PROCESS 0 + +/* Functions */ +#define H5TS_get_thread_local_value(key) TlsGetValue( key ) +#define H5TS_set_thread_local_value(key, value) TlsSetValue( key, value ) +#define H5TS_attr_init(attr_ptr) 0 +#define H5TS_attr_setscope(attr_ptr, scope) 0 +#define H5TS_attr_destroy(attr_ptr) 0 +#define H5TS_wait_for_thread(thread) WaitForSingleObject(thread, INFINITE) +#define H5TS_mutex_init(mutex) InitializeCriticalSection(mutex) +#define H5TS_mutex_lock_simple(mutex) EnterCriticalSection(mutex) +#define H5TS_mutex_unlock_simple(mutex) LeaveCriticalSection(mutex) + +H5_DLL BOOL CALLBACK +H5TS_win32_first_thread_init(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContex); + +#else /* H5_HAVE_WIN_THREADS */ + +/* Library level data structures */ + +/* Mutexes, Threads, and Attributes */ typedef struct H5TS_mutex_struct { pthread_t owner_thread; /* current lock owner */ pthread_mutex_t atomic_lock; /* lock for atomicity of new mechanism */ pthread_cond_t cond_var; /* condition variable */ unsigned int lock_count; } H5TS_mutex_t; +typedef pthread_t H5TS_thread_t; +typedef pthread_attr_t H5TS_attr_t; +typedef pthread_mutex_t H5TS_mutex_simple_t; +typedef pthread_key_t H5TS_key_t; +typedef pthread_once_t H5TS_once_t; + +/* Scope Definitions */ +#define H5TS_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM +#define H5TS_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS + +/* Functions */ +#define H5TS_get_thread_local_value(key) pthread_getspecific( key ) +#define H5TS_set_thread_local_value(key, value) pthread_setspecific( key, value ) +#define H5TS_attr_init(attr_ptr) pthread_attr_init((attr_ptr)) +#define H5TS_attr_setscope(attr_ptr, scope) pthread_attr_setscope(attr_ptr, scope) +#define H5TS_attr_destroy(attr_ptr) pthread_attr_destroy(attr_ptr) +#define H5TS_wait_for_thread(thread) pthread_join(thread, NULL) +#define H5TS_mutex_init(mutex) pthread_mutex_init(mutex, NULL) +#define H5TS_mutex_lock_simple(mutex) pthread_mutex_lock(mutex) +#define H5TS_mutex_unlock_simple(mutex) pthread_mutex_unlock(mutex) -/* Extern global variables */ -extern pthread_once_t H5TS_first_init_g; -extern pthread_key_t H5TS_errstk_key_g; -extern pthread_key_t H5TS_funcstk_key_g; +#endif /* H5_HAVE_WIN_THREADS */ + +/* External global variables */ +extern H5TS_once_t H5TS_first_init_g; +extern H5TS_key_t H5TS_errstk_key_g; +extern H5TS_key_t H5TS_funcstk_key_g; #if defined c_plusplus || defined __cplusplus extern "C" { #endif /* c_plusplus || __cplusplus */ -H5_DLL void H5TS_first_thread_init(void); +H5_DLL void H5TS_first_thread_init(void); H5_DLL herr_t H5TS_mutex_lock(H5TS_mutex_t *mutex); H5_DLL herr_t H5TS_mutex_unlock(H5TS_mutex_t *mutex); H5_DLL herr_t H5TS_cancel_count_inc(void); H5_DLL herr_t H5TS_cancel_count_dec(void); +H5_DLL H5TS_thread_t H5TS_create_thread(void * func, H5TS_attr_t * attr, void *udata); + + #if defined c_plusplus || defined __cplusplus } diff --git a/src/H5private.h b/src/H5private.h index 3875fb3..cc36d16 100644 --- a/src/H5private.h +++ b/src/H5private.h @@ -30,8 +30,12 @@ /* include the pthread header */ #ifdef H5_HAVE_THREADSAFE +#ifdef H5_HAVE_PTHREAD_H #include <pthread.h> -#endif +#else /* H5_HAVE_PTHREAD_H */ +#define H5_HAVE_WIN_THREADS +#endif /* H5_HAVE_PTHREAD_H */ +#endif /* H5_HAVE_THREADSAFE */ /* * Include ANSI-C header files. @@ -1666,8 +1670,15 @@ typedef struct H5_api_struct { #define H5_INIT_GLOBAL H5_g.H5_libinit_g /* Macro for first thread initialization */ -#define H5_FIRST_THREAD_INIT \ - pthread_once(&H5TS_first_init_g, H5TS_first_thread_init); +#ifdef H5_HAVE_WIN_THREADS +#define H5_FIRST_THREAD_INIT \ + if (!H5_INIT_GLOBAL) \ + InitOnceExecuteOnce(&H5TS_first_init_g, H5TS_win32_first_thread_init, NULL, NULL); +#else +#define H5_FIRST_THREAD_INIT \ + if (!H5_INIT_GLOBAL) \ + pthread_once(&H5TS_first_init_g, H5TS_first_thread_init); +#endif /* Macros for threadsafe HDF-5 Phase I locks */ #define H5_API_LOCK \ |