2003-04-01 01:59:04 +08:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
2007-02-07 22:56:24 +08:00
|
|
|
* Copyright by The HDF Group. *
|
2003-04-01 01:59:04 +08:00
|
|
|
* Copyright by the Board of Trustees of the University of Illinois. *
|
|
|
|
* 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 files COPYING and Copyright.html. COPYING can be found at the root *
|
|
|
|
* of the source code distribution tree; Copyright.html can be found at the *
|
|
|
|
* root level of an installed copy of the electronic HDF5 document set and *
|
|
|
|
* is linked from the top-level documents page. It can also be found at *
|
2007-02-07 22:56:24 +08:00
|
|
|
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
|
|
|
|
* access to either file, you may request a copy from help@hdfgroup.org. *
|
2003-04-01 01:59:04 +08:00
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2000-05-19 03:13:33 +08:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* 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 afterwhich the
|
|
|
|
* main thread attempts to cancel the child thread.
|
|
|
|
*
|
|
|
|
* The cancellation should only work after the child thread has safely
|
|
|
|
* left the H5Diterate call.
|
2005-08-14 04:53:35 +08:00
|
|
|
*
|
2000-05-19 03:13:33 +08:00
|
|
|
* Temporary files generated:
|
2000-05-20 07:00:03 +08:00
|
|
|
* ttsafe_cancel.h5
|
2000-05-19 03:13:33 +08:00
|
|
|
*
|
|
|
|
* HDF5 APIs exercised in thread:
|
[svn-r14199] Description:
Add H5Dcreate to API versioned routines, replacing internal usage with
H5Dcreate2
Fix thread-safe error stack initialization for API versioned error
stack printing routines.
Tested on:
FreeBSD/32 6.2 (duty) in debug mode
FreeBSD/64 6.2 (liberty) w/C++ & FORTRAN, in debug mode
Linux/32 2.6 (kagiso) w/PGI compilers, w/C++ & FORTRAN, w/threadsafe,
in debug mode
Linux/64-amd64 2.6 (smirom) w/default API=1.6.x, w/C++ & FORTRAN,
in production mode
Linux/64-ia64 2.6 (cobalt) w/Intel compilers, w/C++ & FORTRAN,
in production mode
Solaris/32 2.10 (linew) w/deprecated symbols disabled, w/C++ & FORTRAN,
w/szip filter, in production mode
Mac OS X/32 10.4.10 (amazon) in debug mode
2007-10-12 00:24:11 +08:00
|
|
|
* H5Screate_simple, H5Tcopy, H5Tset_order, H5Dcreate2, H5Dclose,
|
2000-05-19 03:13:33 +08:00
|
|
|
* H5Dwrite, H5Dread, H5Diterate, H5Tclose, H5Sclose.
|
|
|
|
*
|
|
|
|
* Created: May 15 2000
|
|
|
|
* Programmer: Chee Wai LEE
|
|
|
|
*
|
|
|
|
* Modification History
|
|
|
|
* --------------------
|
|
|
|
*
|
2000-05-20 07:00:03 +08:00
|
|
|
* 19 May 2000, Bill Wendling
|
|
|
|
* Changed so that it creates its own HDF5 file and removes it at cleanup
|
2004-01-10 09:41:13 +08:00
|
|
|
* time.
|
2000-05-20 07:00:03 +08:00
|
|
|
*
|
2000-05-19 03:13:33 +08:00
|
|
|
********************************************************************/
|
|
|
|
#include "ttsafe.h"
|
|
|
|
|
2003-06-26 10:10:33 +08:00
|
|
|
#ifdef H5_HAVE_THREADSAFE
|
2000-05-19 03:13:33 +08:00
|
|
|
|
2000-05-20 07:00:03 +08:00
|
|
|
#define FILENAME "ttsafe_cancel.h5"
|
|
|
|
#define DATASETNAME "commonname"
|
|
|
|
|
2000-05-19 03:13:33 +08:00
|
|
|
void *tts_cancel_thread(void *);
|
|
|
|
void tts_cancel_barrier(void);
|
2004-12-29 22:26:20 +08:00
|
|
|
herr_t tts_cancel_callback(void *, hid_t, unsigned , const hsize_t *, void *);
|
2000-05-19 03:13:33 +08:00
|
|
|
void cancellation_cleanup(void *);
|
|
|
|
|
|
|
|
hid_t cancel_file;
|
|
|
|
typedef struct cleanup_struct {
|
2000-05-20 07:00:03 +08:00
|
|
|
hid_t dataset;
|
|
|
|
hid_t datatype;
|
|
|
|
hid_t dataspace;
|
2000-05-19 03:13:33 +08:00
|
|
|
} cancel_cleanup_t;
|
|
|
|
|
|
|
|
pthread_t childthread;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t cond;
|
|
|
|
|
2000-05-20 07:00:03 +08:00
|
|
|
void tts_cancel(void)
|
|
|
|
{
|
2004-01-10 09:41:13 +08:00
|
|
|
pthread_attr_t attribute;
|
|
|
|
hid_t dataset;
|
|
|
|
int buffer;
|
2005-08-20 23:45:13 +08:00
|
|
|
int ret;
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2004-01-10 09:41:13 +08:00
|
|
|
/* make thread scheduling global */
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=pthread_attr_init(&attribute);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2003-02-10 23:38:52 +08:00
|
|
|
#ifdef H5_HAVE_SYSTEM_SCOPE_THREADS
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=pthread_attr_setscope(&attribute, PTHREAD_SCOPE_SYSTEM);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2003-02-10 23:38:52 +08:00
|
|
|
#endif /* H5_HAVE_SYSTEM_SCOPE_THREADS */
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2005-08-20 04:06:26 +08:00
|
|
|
|
2005-08-20 23:45:13 +08:00
|
|
|
/* Initialize mutex & condition variables */
|
|
|
|
ret=pthread_mutex_init(&mutex,NULL);
|
|
|
|
assert(ret==0);
|
|
|
|
ret=pthread_cond_init(&cond,NULL);
|
|
|
|
assert(ret==0);
|
2005-08-20 04:06:26 +08:00
|
|
|
|
2004-01-10 09:41:13 +08:00
|
|
|
/*
|
|
|
|
* 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);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(cancel_file>=0);
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=pthread_create(&childthread, &attribute, tts_cancel_thread, NULL);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2004-01-10 09:41:13 +08:00
|
|
|
tts_cancel_barrier();
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=pthread_cancel(childthread);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2004-01-10 09:41:13 +08:00
|
|
|
|
[svn-r14193] Description:
Make H5Dopen versioned and change all internal usage to use H5Dopen2
Add simple regression test for H5Dopen1
Tested on:
FreeBSD/32 6.2 (duty) in debug mode
FreeBSD/64 6.2 (liberty) w/C++ & FORTRAN, in debug mode
Linux/32 2.6 (kagiso) w/PGI compilers, w/C++ & FORTRAN, w/threadsafe,
in debug mode
Linux/64-amd64 2.6 (smirom) w/default API=1.6.x, w/C++ & FORTRAN,
in production mode
Linux/64-ia64 2.6 (cobalt) w/Intel compilers, w/C++ & FORTRAN,
in production mode
Solaris/32 2.10 (linew) w/deprecated symbols disabled, w/C++ & FORTRAN,
w/szip filter, in production mode
Mac OS X/32 10.4.10 (amazon) in debug mode
2007-10-09 03:59:36 +08:00
|
|
|
dataset = H5Dopen2(cancel_file, DATASETNAME, H5P_DEFAULT);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(dataset>=0);
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buffer);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
if (buffer != 11)
|
|
|
|
TestErrPrintf("operation unsuccessful with value at %d instead of 11\n", buffer);
|
|
|
|
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Dclose(dataset);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Fclose(cancel_file);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
/* Destroy the thread attribute */
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=pthread_attr_destroy(&attribute);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2000-05-19 03:13:33 +08:00
|
|
|
}
|
|
|
|
|
2003-06-26 10:10:33 +08:00
|
|
|
void *tts_cancel_thread(void UNUSED *arg)
|
2000-05-20 07:00:03 +08:00
|
|
|
{
|
2004-01-10 09:41:13 +08:00
|
|
|
int datavalue;
|
|
|
|
int *buffer;
|
|
|
|
hid_t dataspace, datatype, dataset;
|
|
|
|
hsize_t dimsf[1]; /* dataset dimensions */
|
|
|
|
cancel_cleanup_t *cleanup_structure;
|
2005-08-20 23:45:13 +08:00
|
|
|
int ret;
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
/* define dataspace for dataset */
|
|
|
|
dimsf[0] = 1;
|
[svn-r14199] Description:
Add H5Dcreate to API versioned routines, replacing internal usage with
H5Dcreate2
Fix thread-safe error stack initialization for API versioned error
stack printing routines.
Tested on:
FreeBSD/32 6.2 (duty) in debug mode
FreeBSD/64 6.2 (liberty) w/C++ & FORTRAN, in debug mode
Linux/32 2.6 (kagiso) w/PGI compilers, w/C++ & FORTRAN, w/threadsafe,
in debug mode
Linux/64-amd64 2.6 (smirom) w/default API=1.6.x, w/C++ & FORTRAN,
in production mode
Linux/64-ia64 2.6 (cobalt) w/Intel compilers, w/C++ & FORTRAN,
in production mode
Solaris/32 2.10 (linew) w/deprecated symbols disabled, w/C++ & FORTRAN,
w/szip filter, in production mode
Mac OS X/32 10.4.10 (amazon) in debug mode
2007-10-12 00:24:11 +08:00
|
|
|
dataspace = H5Screate_simple(1, dimsf, NULL);
|
|
|
|
assert(dataspace >= 0);
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
/* define datatype for the data using native little endian integers */
|
|
|
|
datatype = H5Tcopy(H5T_NATIVE_INT);
|
[svn-r14199] Description:
Add H5Dcreate to API versioned routines, replacing internal usage with
H5Dcreate2
Fix thread-safe error stack initialization for API versioned error
stack printing routines.
Tested on:
FreeBSD/32 6.2 (duty) in debug mode
FreeBSD/64 6.2 (liberty) w/C++ & FORTRAN, in debug mode
Linux/32 2.6 (kagiso) w/PGI compilers, w/C++ & FORTRAN, w/threadsafe,
in debug mode
Linux/64-amd64 2.6 (smirom) w/default API=1.6.x, w/C++ & FORTRAN,
in production mode
Linux/64-ia64 2.6 (cobalt) w/Intel compilers, w/C++ & FORTRAN,
in production mode
Solaris/32 2.10 (linew) w/deprecated symbols disabled, w/C++ & FORTRAN,
w/szip filter, in production mode
Mac OS X/32 10.4.10 (amazon) in debug mode
2007-10-12 00:24:11 +08:00
|
|
|
assert(datatype >= 0);
|
|
|
|
ret = H5Tset_order(datatype, H5T_ORDER_LE);
|
|
|
|
assert(ret >= 0);
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
/* create a new dataset within the file */
|
[svn-r14199] Description:
Add H5Dcreate to API versioned routines, replacing internal usage with
H5Dcreate2
Fix thread-safe error stack initialization for API versioned error
stack printing routines.
Tested on:
FreeBSD/32 6.2 (duty) in debug mode
FreeBSD/64 6.2 (liberty) w/C++ & FORTRAN, in debug mode
Linux/32 2.6 (kagiso) w/PGI compilers, w/C++ & FORTRAN, w/threadsafe,
in debug mode
Linux/64-amd64 2.6 (smirom) w/default API=1.6.x, w/C++ & FORTRAN,
in production mode
Linux/64-ia64 2.6 (cobalt) w/Intel compilers, w/C++ & FORTRAN,
in production mode
Solaris/32 2.10 (linew) w/deprecated symbols disabled, w/C++ & FORTRAN,
w/szip filter, in production mode
Mac OS X/32 10.4.10 (amazon) in debug mode
2007-10-12 00:24:11 +08:00
|
|
|
dataset = H5Dcreate2(cancel_file, DATASETNAME, datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
|
|
|
|
assert(dataset >= 0);
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
/* 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);
|
2006-06-27 22:45:06 +08:00
|
|
|
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
datavalue = 1;
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &datavalue);
|
2005-09-08 05:44:16 +08:00
|
|
|
assert(ret>=0);
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
buffer = malloc(sizeof(int));
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buffer);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Diterate(buffer, H5T_NATIVE_INT, dataspace, tts_cancel_callback, &dataset);
|
|
|
|
assert(ret>=0);
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
|
|
datavalue = 100;
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &datavalue);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Dclose(dataset);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Tclose(datatype);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Sclose(dataspace);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Required by pthreads. The argument 0 pops the stack but does not
|
|
|
|
* execute the cleanup routine.
|
|
|
|
*/
|
|
|
|
pthread_cleanup_pop(0);
|
2006-06-27 22:45:06 +08:00
|
|
|
|
2004-01-10 09:41:13 +08:00
|
|
|
return NULL;
|
2000-05-19 03:13:33 +08:00
|
|
|
}
|
|
|
|
|
2004-12-29 22:26:20 +08:00
|
|
|
herr_t tts_cancel_callback(void *elem, hid_t UNUSED type_id, unsigned UNUSED ndim,
|
|
|
|
const hsize_t UNUSED *point, void *operator_data)
|
2000-05-20 07:00:03 +08:00
|
|
|
{
|
2004-01-10 09:41:13 +08:00
|
|
|
int value = *(int *)elem;
|
|
|
|
hid_t dataset = *(hid_t *)operator_data;
|
2005-08-20 23:45:13 +08:00
|
|
|
int ret;
|
2004-01-10 09:41:13 +08:00
|
|
|
|
|
|
|
tts_cancel_barrier();
|
|
|
|
sleep(3);
|
|
|
|
|
|
|
|
if (value != 1) {
|
|
|
|
TestErrPrintf("Error! Element value should be 1 and not %d\n", value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value += 10;
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &value);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2004-01-10 09:41:13 +08:00
|
|
|
return 0;
|
2000-05-19 03:13:33 +08:00
|
|
|
}
|
|
|
|
|
2000-05-20 07:00:03 +08:00
|
|
|
/*
|
|
|
|
* Need to perform the dataset, datatype and dataspace close that was never
|
|
|
|
* performed because of thread cancellation
|
|
|
|
*/
|
|
|
|
void cancellation_cleanup(void *arg)
|
|
|
|
{
|
2004-01-10 09:41:13 +08:00
|
|
|
cancel_cleanup_t *cleanup_structure = (cancel_cleanup_t *)arg;
|
2005-08-20 23:45:13 +08:00
|
|
|
int ret;
|
|
|
|
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Dclose(cleanup_structure->dataset);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Tclose(cleanup_structure->datatype);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=H5Sclose(cleanup_structure->dataspace);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2004-01-10 09:41:13 +08:00
|
|
|
/* retained for debugging */
|
|
|
|
/* print_func("cancellation noted, cleaning up ... \n"); */
|
2000-05-19 03:13:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-05-20 07:00:03 +08:00
|
|
|
* Artificial (and specific to this test) barrier to keep track of whether
|
|
|
|
* both the main and child threads have reached a point in the program.
|
|
|
|
*/
|
|
|
|
void tts_cancel_barrier(void)
|
|
|
|
{
|
2004-01-10 09:41:13 +08:00
|
|
|
static int count = 2;
|
2005-08-20 23:45:13 +08:00
|
|
|
int ret;
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=pthread_mutex_lock(&mutex);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2004-01-10 09:41:13 +08:00
|
|
|
if (count != 1) {
|
|
|
|
count--;
|
2005-08-20 04:06:26 +08:00
|
|
|
ret=pthread_cond_wait(&cond, &mutex);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2004-01-10 09:41:13 +08:00
|
|
|
} else {
|
2005-08-20 23:45:13 +08:00
|
|
|
ret=pthread_cond_signal(&cond);
|
|
|
|
assert(ret==0);
|
2004-01-10 09:41:13 +08:00
|
|
|
}
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2005-08-20 23:45:13 +08:00
|
|
|
ret=pthread_mutex_unlock(&mutex);
|
|
|
|
assert(ret==0);
|
2000-05-19 03:13:33 +08:00
|
|
|
}
|
|
|
|
|
2000-05-20 07:00:03 +08:00
|
|
|
void cleanup_cancel(void)
|
|
|
|
{
|
2005-08-20 23:45:13 +08:00
|
|
|
HDunlink(FILENAME);
|
2000-05-19 03:13:33 +08:00
|
|
|
}
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2000-05-19 03:13:33 +08:00
|
|
|
#endif /*H5_HAVE_THREADSAFE*/
|