[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.
This commit is contained in:
Mike McGreevy 2010-09-10 11:15:34 -05:00
parent 47c792faa0
commit b7c2d18029
13 changed files with 251 additions and 83 deletions

View File

@ -517,6 +517,18 @@ IF (WIN32)
ENDIF (BUILD_SHARED_LIBS)
ENDIF (WIN32)
#-----------------------------------------------------------------------------
# Option to use threadsafe
# Note: Currently CMake only allows configuring of threadsafe on WINDOWS.
#-----------------------------------------------------------------------------
IF (WIN32)
OPTION (HDF5_ENABLE_THREADSAFE "Enable Threadsafety" OFF)
IF (HDF5_ENABLE_THREADSAFE)
SET (H5_HAVE_WIN_THREADS 1)
SET (H5_HAVE_THREADSAFE 1)
ENDIF (HDF5_ENABLE_THREADSAFE)
ENDIF (WIN32)
#-----------------------------------------------------------------------------
# Option to use PACKED BITS SUPPORT
#-----------------------------------------------------------------------------

View File

@ -76,6 +76,10 @@ New Features
Library:
--------
- Added support for threadsafety on windows using the windows threads
library. Use the HDF5_ENABLE_THREADSAFE option in CMake while on a
windows platform to enable this functionality. This is supported on
Windows Vista and newer Windows operating systems. (MAM - 2010/09/10)
- When a mandatory filter failed to write data chunks, the dataset
couldn't close (bug 1260). The fix releases all resources and closes
the dataset but returns a failure. (SLU - 2010/9/8)

View File

@ -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);

View File

@ -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 */

View File

@ -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 */

View File

@ -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;
/* 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;
/* 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)
#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
}

View File

@ -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 \

View File

@ -89,11 +89,14 @@ int main(int argc, char *argv[])
/* Initialize testing framework */
TestInit(argv[0], NULL, NULL);
/* Tests are generally arranged from least to most complexity... */
AddTest("dcreate", tts_dcreate, cleanup_dcreate, "multi-dataset creation", NULL);
AddTest("error", tts_error, cleanup_error, "per-thread error stacks", NULL);
AddTest("cancel", tts_cancel, cleanup_cancel, "thread cancellation safety test", NULL);
AddTest("acreate", tts_acreate, cleanup_acreate, "multi-attribute creation", NULL);
/* Tests are generally arranged from least to most complexity... */
AddTest("dcreate", tts_dcreate, cleanup_dcreate, "multi-dataset creation", NULL);
AddTest("error", tts_error, cleanup_error, "per-thread error stacks", NULL);
#ifdef H5_HAVE_PTHREAD_H
/* Thread cancellability only supported with pthreads ... */
AddTest("cancel", tts_cancel, cleanup_cancel, "thread cancellation safety test", NULL);
#endif /* H5_HAVE_PTHREAD_H */
AddTest("acreate", tts_acreate, cleanup_acreate, "multi-attribute creation", NULL);
/* Display testing information */
TestInfo(argv[0]);

View File

@ -33,7 +33,9 @@
#ifdef H5_HAVE_THREADSAFE
/* Include pthread library for threadsafe tests */
#ifdef H5_HAVE_PTHREAD_H
#include <pthread.h>
#endif /* H5_HAVE_PTHREAD_H */
/* Prototypes for the support routines */
extern char* gen_name(int);

View File

@ -68,8 +68,8 @@ typedef struct acreate_data_struct {
void tts_acreate(void)
{
/* Pthread declarations */
pthread_t threads[NUM_THREADS];
/* Thread declarations */
H5TS_thread_t threads[NUM_THREADS];
/* HDF5 data declarations */
hid_t file, dataset;
@ -118,13 +118,11 @@ void tts_acreate(void)
attrib_data->datatype = datatype;
attrib_data->dataspace = dataspace;
attrib_data->current_index = i;
ret = pthread_create(&threads[i], NULL, tts_acreate_thread, attrib_data);
assert(ret == 0);
threads[i] = H5TS_create_thread(tts_acreate_thread, NULL, attrib_data);
} /* end for */
for(i = 0; i < NUM_THREADS; i++) {
ret = pthread_join(threads[i], NULL);
assert(ret == 0);
H5TS_wait_for_thread(threads[i]);
} /* end for */

View File

@ -47,6 +47,7 @@
#include "ttsafe.h"
#ifdef H5_HAVE_THREADSAFE
#ifndef H5_HAVE_WIN_THREADS
#define FILENAME "ttsafe_cancel.h5"
#define DATASETNAME "commonname"
@ -253,4 +254,5 @@ void cleanup_cancel(void)
HDunlink(FILENAME);
}
#endif /*H5_HAVE_WIN_THREADS*/
#endif /*H5_HAVE_THREADSAFE*/

View File

@ -88,21 +88,21 @@ thread_info thread_out[NUM_THREAD];
*/
void tts_dcreate(void)
{
/* Pthread definitions */
pthread_t threads[NUM_THREAD];
/* thread definitions */
H5TS_thread_t threads[NUM_THREAD];
/* HDF5 data definitions */
hid_t file, dataset;
int datavalue, i;
pthread_attr_t attribute;
H5TS_attr_t attribute;
int ret;
/* set pthread attribute to perform global scheduling */
ret=pthread_attr_init(&attribute);
assert(ret==0);
H5TS_attr_init(&attribute);
/* set thread scope to system */
#ifdef H5_HAVE_SYSTEM_SCOPE_THREADS
ret=pthread_attr_setscope(&attribute, PTHREAD_SCOPE_SYSTEM);
assert(ret==0);
H5TS_attr_setscope(&attribute, H5TS_SCOPE_SYSTEM);
#endif /* H5_HAVE_SYSTEM_SCOPE_THREADS */
/*
@ -117,13 +117,11 @@ void tts_dcreate(void)
thread_out[i].id = i;
thread_out[i].file = file;
thread_out[i].dsetname = dsetname[i];
ret=pthread_create(&threads[i], NULL, tts_dcreate_creator, &thread_out[i]);
assert(ret==0);
threads[i] = H5TS_create_thread(tts_dcreate_creator, NULL, &thread_out[i]);
} /* end for */
for(i = 0;i < NUM_THREAD; i++) {
ret = pthread_join(threads[i], NULL);
assert(ret == 0);
H5TS_wait_for_thread(threads[i]);
} /* end for */
/* compare data to see if it is written correctly */
@ -157,8 +155,7 @@ void tts_dcreate(void)
assert(ret >= 0);
/* Destroy the thread attribute */
ret=pthread_attr_destroy(&attribute);
assert(ret==0);
H5TS_attr_destroy(&attribute);
}
void *tts_dcreate_creator(void *_thread_data)

View File

@ -71,12 +71,12 @@ err_num_t expected[8];
int error_flag = 0;
int error_count = 0;
pthread_mutex_t error_mutex;
H5TS_mutex_simple_t error_mutex;
void tts_error(void)
{
pthread_t threads[NUM_THREAD];
pthread_attr_t attribute;
H5TS_thread_t threads[NUM_THREAD];
H5TS_attr_t attribute;
hid_t dataset;
int value, i;
int ret;
@ -104,16 +104,15 @@ void tts_error(void)
expected[6].min_num = H5E_EXISTS;
/* set up mutex for global count of errors */
ret=pthread_mutex_init(&error_mutex, NULL);
assert(ret==0);
H5TS_mutex_init(&error_mutex);
/* make thread scheduling global */
ret=pthread_attr_init(&attribute);
assert(ret==0);
H5TS_attr_init(&attribute);
/* set thread scope to system */
#ifdef H5_HAVE_SYSTEM_SCOPE_THREADS
ret=pthread_attr_setscope(&attribute, PTHREAD_SCOPE_SYSTEM);
assert(ret==0);
H5TS_attr_setscope(&attribute, H5TS_SCOPE_SYSTEM);
#endif /* H5_HAVE_SYSTEM_SCOPE_THREADS */
/*
@ -124,13 +123,11 @@ void tts_error(void)
assert(error_file>=0);
for (i = 0; i < NUM_THREAD; i++){
ret=pthread_create(&threads[i], &attribute, tts_error_thread, NULL);
assert(ret==0);
threads[i] = H5TS_create_thread(tts_error_thread, &attribute, NULL);
}
for (i = 0; i < NUM_THREAD; i++){
ret=pthread_join(threads[i],NULL);
assert(ret==0);
H5TS_wait_for_thread(threads[i]);
}
if (error_flag)
@ -153,9 +150,7 @@ void tts_error(void)
ret=H5Fclose(error_file);
assert(ret>=0);
/* Destroy the thread attribute */
ret=pthread_attr_destroy(&attribute);
assert(ret==0);
H5TS_attr_destroy(&attribute);
}
static
@ -208,11 +203,9 @@ herr_t error_callback(hid_t estack_id, void *client_data)
{
int ret;
ret=pthread_mutex_lock(&error_mutex);
assert(ret==0);
H5TS_mutex_lock_simple(&error_mutex);
error_count++;
ret=pthread_mutex_unlock(&error_mutex);
assert(ret==0);
H5TS_mutex_unlock_simple(&error_mutex);
return H5Ewalk2(H5E_DEFAULT, H5E_WALK_DOWNWARD, walk_error_callback, client_data);
}