HDF5 2.0.0.258fa78
API Reference
|
The following code is placed at the beginning of H5private.h:
H5_HAVE_THREADSAFE
is defined when the HDF5 library is compiled with the –enable-threadsafe configuration option using autotools or HDF5_ENABLE_THREADSAFE=ON using CMake. In general, code for the non-threadsafe version of HDF5 library are placed within the # else
part of the conditional compilation. The exception to this rule are the changes to the FUNC_ENTER
(in H5private.h), HRETURN
and HRETURN_ERROR
(in H5Eprivate.h) macros (see section Changes to HRETURN and HRETURN_ERROR).
In the threadsafe implementation, the global library initialization variable H5_libinit_g
is changed to a global structure consisting of the variable with its associated lock (locks are explained in section Recursive Locks):
becomes
where H5_api_t
is
All former references to H5_libinit_g
in the library are now made using the macro H5_INIT_GLOBAL
. If the threadsafe library is to be used, the macro is set to H5_g.H5_libinit_g
instead.
A new global boolean variable H5_allow_concurrent_g
is used to determine if multiple threads are allowed to an API call simultaneously. This is set to FALSE
.
All APIs that are allowed to do so have their own local variable that shadows the global variable and is set to TRUE
. In phase 1, no such APIs exist.
It is defined in H5.c
as follows:
The global variable H5_first_init_g
of type pthread_once_t
is used to allow only the first thread in the application process to call an initialization function using pthread_once
. All subsequent calls to pthread_once
by any thread are disregarded.
The call sets up the mutex in the global structure H5_g
(see section Global library initialization variable) via an initialization function H5_first_thread_init
. The first thread initialization function is described in section First thread initialization.
H5_first_init_g
is defined in H5.c
as follows:
A global pthread-managed key H5_errstk_key_g
is used to allow pthreads to maintain a separate error stack (of type H5E_t
) for each thread. This is defined in H5.c
as:
Error stack management is described in section Per-thread error stack management.
We need to preserve the thread cancellation status of each thread individually by using a key H5_cancel_key_g
. The status is preserved using a structure (of type H5_cancel_t
) which maintains the cancellability state of the thread before it entered the library and a count (which works very much like the recursive lock counter) which keeps track of the number of API calls the thread makes within the library.
The structure is defined in H5private.h
as:
Thread cancellation is described in section Thread Cancellation safety.
The FUNC_ENTER
macro is now extended to include macro calls to initialize first threads, disable cancellability and wraps a lock operation around the checking of the global initialization flag. It should be noted that the cancellability should be disabled before acquiring the lock on the library. Doing so otherwise would allow the possibility that the thread be cancelled just after it has acquired the lock on the library and in that scenario, if the cleanup routines are not properly set, the library would be permanently locked out.
The additional macro code and new macro definitions can be found in Appendix Macro expansion codes. The changes are made in H5private.h
.
The HRETURN
and HRETURN_ERROR
macros are the counterparts to the FUNC_ENTER
macro described in section Changes to FUNC_ENTER. FUNC_LEAVE
makes a macro call to HRETURN
, so it is also covered here.
The basic changes to these two macros involve adding macro calls to call an unlock operation and re-enable cancellability if necessary. It should be noted that the cancellability should be re-enabled only after the thread has released the lock to the library. The consequence of doing otherwise would be similar to that described in section Changes to FUNC_ENTER.
The additional macro code and new macro definitions can be found in Appendix Macro expansion codes. The changes are made in H5Eprivate.h
.
A recursive mutex lock m allows a thread t1 to successfully lock m more than once without blocking t1. Another thread t2 will block if t2 tries to lock m while t1 holds the lock to m. If t1 makes k lock calls on m, then it also needs to make k unlock calls on m before it releases the lock.
Our implementation of recursive locks is built on top of a pthread mutex lock (which is not recursive). It makes use of a pthread condition variable to have unsuccessful threads wait on the mutex. Waiting threads are awaken by a signal from the final unlock call made by the thread holding the lock.
Recursive locks are defined to be the following type (H5private.h
):
Detailed implementation code can be found in Appendix Recursive Lock implementation code. The implementation changes are made in H5TS.c
.
Because the mutex lock associated with a recursive lock cannot be statically initialized, a mechanism is required to initialize the recursive lock associated with H5_g
so that it can be used for the first time.
The pthreads library allows this through the pthread_once call which as described in section Global thread initialization variable allows only the first thread accessing the library in an application to initialize H5_g
.
In addition to initializing H5_g
, it also initializes the key (see section Global key for per-thread error stacks) for use with per-thread error stacks (see section Per-thread error stack management).
The first thread initialization mechanism is implemented as the function call H5_first_thread_init()
in H5TS.c
. This is described in appendix B.
Pthreads allows individual threads to access dynamic and persistent per-thread data through the use of keys. Each key is associated with a table that maps threads to data items. Keys can be initialized by pthread_key_create()
in pthreads (see sections Global key for per-thread error stacks and First thread initialization). Per-thread data items are accessed using a key through the pthread_getspecific()
and pthread_setspecific()
calls to read and write to the association table respectively.
Per-thread error stacks are accessed through the key H5_errstk_key_g
which is initialized by the first thread initialization call (see section First thread initialization).
In the non-threadsafe version of the library, there is a global stack variable H5E_stack_g[1]
which is no longer defined in the threadsafe version. At the same time, the macro call to gain access to the error stack H5E_get_my_stack
is changed from:
to:
where H5E_get_stack()
is a surrogate function that does the following operations:
if a thread is attempting to get an error stack for the first time, the error stack is dynamically allocated for the thread and associated with H5_errstk_key_g
using pthread_setspecific()
. The way we detect if it is the first time is through pthread_getspecific()
which returns NULL
if no previous value is associated with the thread using the key.
pthread_getspecific()
returns a non-null value, then that is the pointer to the error stack associated with the thread and the stack can be used as usual. A final change to the error reporting routines is as follows; the current implementation reports errors to always be detected at thread 0. In the threadsafe implementation, this is changed to report the number returned by a call to pthread_self()
.
The change in code (reflected in H5Eprint
of file H5E.c
) is as follows:
Code for H5E_get_stack()
can be found in Appendix Per-thread error stack acquisition. All the above changes were made in H5E.c
.
To prevent thread cancellations from killing a thread while it is in the library, we maintain per-thread information about the cancellability status of the thread before it entered the library so that we can restore that same status when the thread leaves the library.
By enter and leave the library, we mean the points when a thread makes an API call from a user application and the time that API call returns. Other API or callback function calls made from within that API call are considered within the library.
Because other API calls may be made from within the first API call, we need to maintain a counter to determine which was the first and correspondingly the last return.
When a thread makes an API call, the macro H5_API_SET_CANCEL
calls the worker function H5_cancel_count_inc()
which does the following:
PTHREAD_CANCEL_DISABLE
while storing the previous state into the cancellability structure. cancel_count
is also incremented in this case. When a thread leaves an API call, the macro H5_API_UNSET_CANCEL
calls the worker function H5_cancel_count_dec()
which does the following:
cancel_count
is greater than 1, indicating that the thread is not yet about to leave the library, then cancel_count
is simply decremented. H5_cancel_count_inc
and H5_cancel_count_dec
are described in Appendix Thread cancellation mechanisms and may be found in H5TS.c
.
Except where stated, all tests involve 16 simultaneous threads that make use of HDF5 API calls without any explicit synchronization typically required in a non-threadsafe environment.
The test program sets up 16 threads to simultaneously create 16 different datasets named from zero to fifteen for a single file and then writing an integer value into that dataset equal to the dataset's named value.
The main thread would join with all 16 threads and attempt to match the resulting HDF5 file with expected results - that each dataset contains the correct value (0 for zero, 1 for one etc ...) and all datasets were correctly created.
The test is implemented in the file ttsafe_dcreate.c
.
The error stack test is one in which 16 threads simultaneously try to create datasets with the same name. The result, when properly serialized, should be equivalent to 16 attempts to create the dataset with the same name.
The error stack implementation runs correctly if it reports 15 instances of the dataset name conflict error and finally generates a correct HDF5 containing that single dataset. Each thread should report its own stack of errors with a thread number associated with it.
The test is implemented in the file ttsafe_error.c
.
The main idea in thread cancellation safety is as follows; a child thread is spawned to create and write to a dataset. Following that, it makes a H5Diterate
call on that dataset which activates a callback function.
A deliberate barrier is invoked at the callback function which waits for both the main and child thread to arrive at that point. After that happens, the main thread proceeds to make a thread cancel call on the child thread while the latter sleeps for 3 seconds before proceeding to write a new value to the dataset.
After the iterate call, the child thread logically proceeds to wait another 3 seconds before writing another newer value to the dataset.
The test is correct if the main thread manages to read the second value at the end of the test. This means that cancellation did not take place until the end of the iteration call despite of the 3 second wait within the iteration callback and the extra dataset write operation. Furthermore, the cancellation should occur before the child can proceed to write the last value into the dataset.
A main thread makes 16 threaded calls to H5Acreate
with a generated name for each attribute. Sixteen attributes should be created for the single dataset in random (chronological) order and receive values depending on its generated attribute name (e.g. attrib010 would receive the value 10).
After joining with all child threads, the main thread proceeds to read each attribute by generated name to see if the value tallies. Failure is detected if the attribute name does not exist (meaning they were never created) or if the wrong values were read back.