Files
hdf5/test/ttsafe_cancel.c
T
Quincey Koziol 9fd88560d5 Refactor threading and other concurrency support (#4469)
Complete overhaul of the concurrency-related aspects of the library (threading, atomics, locking, etc.), adding private routines in the H5TS package to allow internal algorithms to use all of these capabilities.

Adds many new features & components in the H5TS package that are equivalent to common concurrency data structures and capabilities: "regular" and recursive mutices, condition variables, semaphores, thread barriers, 'once' support, thread pools, atomic variables, thread-local keys, and spawning & joining internal threads.

Now supports C11, pthreads, and Windows threading for all H5TS capabilities, except the recursive readers/writers lock, which is not supported on Windows (because Windows threads don't provide a callback on thread-local variable deletion).

The "global" API lock is switched to use a recursive mutex from the H5TS package, instead of its own variant.

API context code (H5CX package) and error stacks (H5E package) now use the common thread-local info, instead of their own variants.

Subfiling code is switched from using Mercury threading features to the new internal H5TS features.

Removes the mercury threading code.

Adds a configure option (--enable-threads / HDF5_ENABLE_THREADS), enabled by default, to control whether threading is enabled within the library.
2024-07-31 12:34:43 -05:00

211 lines
6.9 KiB
C

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://www.hdfgroup.org/licenses. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/********************************************************************
*
* Testing thread safety. Thread Cancellation safety
* -------------------------------------------------
*
* The main thread spawns a child to perform a series of dataset writes
* to a hdf5 file. The main thread and child thread synchronizes within
* a callback function called during a H5Diterate call after which the
* main thread attempts to cancel the child thread.
*
* The cancellation should only work after the child thread has safely
* left the H5Diterate call.
*
* Temporary files generated:
* ttsafe_cancel.h5
*
********************************************************************/
#include "ttsafe.h"
#ifdef H5_HAVE_THREADSAFE
#ifdef H5_HAVE_PTHREAD_H
#define FILENAME "ttsafe_cancel.h5"
#define DATASETNAME "commonname"
void *tts_cancel_thread(void *);
void tts_cancel_barrier(void);
herr_t tts_cancel_callback(void *, hid_t, unsigned, const hsize_t *, void *);
void cancellation_cleanup(void *);
hid_t cancel_file;
typedef struct cleanup_struct {
hid_t dataset;
hid_t datatype;
hid_t dataspace;
} cancel_cleanup_t;
pthread_t childthread;
static H5TS_barrier_t barrier;
void
tts_cancel(void)
{
hid_t dataset;
int buffer;
int ret;
/* Initialize barrier */
ret = H5TS_barrier_init(&barrier, 2);
CHECK_I(ret, "H5TS_barrier_init");
/*
* Create a hdf5 file using H5F_ACC_TRUNC access, default file
* creation plist and default file access plist
*/
cancel_file = H5Fcreate(FILENAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
assert(cancel_file >= 0);
ret = pthread_create(&childthread, NULL, tts_cancel_thread, NULL);
assert(ret == 0);
ret = H5TS_barrier_wait(&barrier);
assert(ret == 0);
ret = pthread_cancel(childthread);
assert(ret == 0);
dataset = H5Dopen2(cancel_file, DATASETNAME, H5P_DEFAULT);
assert(dataset >= 0);
ret = H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buffer);
assert(ret >= 0);
if (buffer != 11)
TestErrPrintf("operation unsuccessful with value at %d instead of 11\n", buffer);
ret = H5Dclose(dataset);
assert(ret >= 0);
ret = H5Fclose(cancel_file);
assert(ret >= 0);
ret = H5TS_barrier_destroy(&barrier);
CHECK_I(ret, "H5TS_barrier_destroy");
} /* end tts_cancel() */
void *
tts_cancel_thread(void H5_ATTR_UNUSED *arg)
{
hid_t dataspace = H5I_INVALID_HID;
hid_t datatype = H5I_INVALID_HID;
hid_t dataset = H5I_INVALID_HID;
int datavalue;
int buffer;
hsize_t dimsf[1]; /* dataset dimensions */
cancel_cleanup_t *cleanup_structure;
herr_t status;
/* define dataspace for dataset */
dimsf[0] = 1;
dataspace = H5Screate_simple(1, dimsf, NULL);
CHECK(dataspace, H5I_INVALID_HID, "H5Screate_simple");
/* define datatype for the data using native little endian integers */
datatype = H5Tcopy(H5T_NATIVE_INT);
CHECK(datatype, H5I_INVALID_HID, "H5Tcopy");
status = H5Tset_order(datatype, H5T_ORDER_LE);
CHECK(status, FAIL, "H5Tset_order");
/* create a new dataset within the file */
dataset =
H5Dcreate2(cancel_file, DATASETNAME, datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
CHECK(dataset, H5I_INVALID_HID, "H5Dcreate2");
/* If thread is cancelled, make cleanup call */
cleanup_structure = (cancel_cleanup_t *)malloc(sizeof(cancel_cleanup_t));
cleanup_structure->dataset = dataset;
cleanup_structure->datatype = datatype;
cleanup_structure->dataspace = dataspace;
pthread_cleanup_push(cancellation_cleanup, cleanup_structure);
datavalue = 1;
status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &datavalue);
CHECK(status, FAIL, "H5Dwrite");
status = H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buffer);
CHECK(status, FAIL, "H5Dread");
status = H5Diterate(&buffer, H5T_NATIVE_INT, dataspace, tts_cancel_callback, &dataset);
CHECK(status, FAIL, "H5Diterate");
HDsleep(3);
datavalue = 100;
status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &datavalue);
CHECK(status, FAIL, "H5Dwrite");
status = H5Dclose(dataset);
CHECK(status, FAIL, "H5Dclose");
status = H5Tclose(datatype);
CHECK(status, FAIL, "H5Tclose");
status = H5Sclose(dataspace);
CHECK(status, FAIL, "H5Sclose");
/*
* Required by pthreads. The argument 0 pops the stack but does not
* execute the cleanup routine.
*/
pthread_cleanup_pop(0);
return NULL;
} /* end tts_cancel_thread() */
herr_t
tts_cancel_callback(void *elem, hid_t H5_ATTR_UNUSED type_id, unsigned H5_ATTR_UNUSED ndim,
const hsize_t H5_ATTR_UNUSED *point, void *operator_data)
{
hid_t dataset = *(hid_t *)operator_data;
int value = *(int *)elem;
herr_t status;
status = H5TS_barrier_wait(&barrier);
CHECK_I(status, "H5TS_barrier_wait");
HDsleep(3);
if (value != 1) {
TestErrPrintf("Error! Element value should be 1 and not %d\n", value);
return FAIL;
}
value += 10;
status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &value);
CHECK(status, FAIL, "H5Dwrite");
return SUCCEED;
} /* end tts_cancel_callback() */
/*
* Need to perform the dataset, datatype and dataspace close that was never
* performed because of thread cancellation
*/
void
cancellation_cleanup(void *arg)
{
cancel_cleanup_t *cleanup_structure = (cancel_cleanup_t *)arg;
herr_t status;
status = H5Dclose(cleanup_structure->dataset);
CHECK(status, FAIL, "H5Dclose");
status = H5Tclose(cleanup_structure->datatype);
CHECK(status, FAIL, "H5Tclose");
status = H5Sclose(cleanup_structure->dataspace);
CHECK(status, FAIL, "H5Sclose");
} /* end cancellation_cleanup() */
void
cleanup_cancel(void)
{
HDunlink(FILENAME);
}
#endif /*H5_HAVE_PTHREAD_H*/
#endif /*H5_HAVE_THREADSAFE*/