diff options
Diffstat (limited to 'src/H5TS.c')
-rw-r--r-- | src/H5TS.c | 146 |
1 files changed, 116 insertions, 30 deletions
@@ -29,16 +29,14 @@ 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 */ +#ifdef H5_HAVE_WIN_THREADS +H5TS_once_t H5TS_first_init_g; +#else /* H5_HAVE_WIN_THREADS */ +H5TS_once_t H5TS_first_init_g = PTHREAD_ONCE_INIT; +#endif /* H5_HAVE_WIN_THREADS */ +H5TS_key_t H5TS_errstk_key_g; +H5TS_key_t H5TS_funcstk_key_g; +H5TS_key_t H5TS_cancel_key_g; /*-------------------------------------------------------------------------- @@ -70,12 +68,45 @@ H5TS_key_destructor(void *key_val) HDfree(key_val); } +#ifdef H5_HAVE_WIN_THREADS + /*-------------------------------------------------------------------------- * NAME - * H5TS_first_thread_init + * H5TS_win32_first_thread_init * * USAGE - * H5TS_first_thread_init() + * 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: + * + *-------------------------------------------------------------------------- + */ +BOOL CALLBACK +H5TS_win32_first_thread_init(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContex) +{ + InitializeCriticalSection ( &H5_g.init_lock.CriticalSection ); + H5TS_errstk_key_g = TlsAlloc(); + H5TS_funcstk_key_g = TlsAlloc(); + H5TS_cancel_key_g = TlsAlloc(); + + return TRUE; +} /* H5TS_win32_first_thread_init() */ +#else /* H5_HAVE_WIN_THREADS */ +/*-------------------------------------------------------------------------- + * NAME + * H5TS_pthread_first_thread_init + * + * USAGE + * H5TS_pthread_first_thread_init() * * RETURNS * @@ -92,7 +123,7 @@ H5TS_key_destructor(void *key_val) *-------------------------------------------------------------------------- */ void -H5TS_first_thread_init(void) +H5TS_pthread_first_thread_init(void) { H5_g.H5_libinit_g = FALSE; @@ -110,6 +141,7 @@ 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 @@ -139,9 +171,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 +193,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 +226,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 +248,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,17 +285,21 @@ 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_cancel_t *)H5TS_get_thread_local_value(H5TS_cancel_key_g); if (!cancel_counter) { /* * First time thread calls library - create new counter and associate * with key */ - cancel_counter = H5MM_calloc(sizeof(H5TS_cancel_t)); + cancel_counter = (H5TS_cancel_t *)H5MM_calloc(sizeof(H5TS_cancel_t)); if (!cancel_counter) { H5E_push_stack(NULL, "H5TS_cancel_count_inc", @@ -274,7 +317,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 +352,57 @@ 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 */ + register H5TS_cancel_t *cancel_counter; + herr_t ret_value = SUCCEED; - cancel_counter = pthread_getspecific(H5TS_cancel_key_g); + cancel_counter = (H5TS_cancel_t *)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, (void * (*)(void *))func, udata); + +#endif /* H5_HAVE_WIN_THREADS */ + + return ret_value; + +} /* H5TS_create_thread */ + #endif /* H5_HAVE_THREADSAFE */ |