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/H5TS.c | |
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/H5TS.c')
-rw-r--r-- | src/H5TS.c | 139 |
1 files changed, 112 insertions, 27 deletions
@@ -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 */ |