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 *
|
|
|
|
* http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html. If you do not have *
|
|
|
|
* access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2000-05-19 03:13:33 +08:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* Testing thread safety. Deliberate per-thread errors to test error stack
|
|
|
|
* -----------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Create 16 multiple threads to create datasets with the same name. The
|
|
|
|
* library should respond with 15 equivalent error stack printouts (one for
|
|
|
|
* each bad thread). The final hdf5 file should be a valid file with one
|
|
|
|
* entry.
|
|
|
|
*
|
|
|
|
* Temporary files generated:
|
2001-09-05 01:37:10 +08:00
|
|
|
*
|
|
|
|
* ttsafe_error.h5
|
2000-05-19 03:13:33 +08:00
|
|
|
*
|
|
|
|
* HDF5 APIs exercised in thread:
|
2001-09-05 01:37:10 +08:00
|
|
|
*
|
|
|
|
* H5Screate_simple, H5Tcopy, H5Tset_order, H5Dcreate, H5Dclose,
|
|
|
|
* H5Tclose, H5Sclose.
|
2000-05-19 03:13:33 +08:00
|
|
|
*
|
|
|
|
* Created: Apr 28 2000
|
|
|
|
* Programmer: Chee Wai LEE
|
|
|
|
*
|
|
|
|
* Modification History
|
|
|
|
* --------------------
|
|
|
|
*
|
2000-05-20 07:00:03 +08:00
|
|
|
* 19 May 2000, Bill Wendling
|
|
|
|
* Modified so that it creates a unique HDF5 file and removes it on
|
2004-01-10 09:41:13 +08:00
|
|
|
* cleanup.
|
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
|
|
|
|
2006-08-28 05:09:31 +08:00
|
|
|
#define NUM_THREAD 16
|
2001-09-05 01:37:10 +08:00
|
|
|
#define FILENAME "ttsafe_error.h5"
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2000-05-19 03:13:33 +08:00
|
|
|
/* Having a common dataset name is an error */
|
2000-05-20 07:00:03 +08:00
|
|
|
#define DATASETNAME "commonname"
|
2003-01-10 21:17:03 +08:00
|
|
|
#define EXPECTED_ERROR_DEPTH 8
|
2000-05-20 07:00:03 +08:00
|
|
|
#define WRITE_NUMBER 37
|
|
|
|
|
2006-08-01 04:57:44 +08:00
|
|
|
static herr_t error_callback(hid_t , void *);
|
|
|
|
static herr_t walk_error_callback(unsigned, const H5E_error_stack_t *, void *);
|
2001-09-05 01:37:10 +08:00
|
|
|
static void *tts_error_thread(void *);
|
2000-05-19 03:13:33 +08:00
|
|
|
|
2001-09-05 01:37:10 +08:00
|
|
|
/* Global variables */
|
2000-05-19 03:13:33 +08:00
|
|
|
hid_t error_file;
|
|
|
|
|
|
|
|
typedef struct err_num_struct {
|
2003-08-09 03:06:29 +08:00
|
|
|
hid_t maj_num;
|
|
|
|
hid_t min_num;
|
2000-05-19 03:13:33 +08:00
|
|
|
} err_num_t;
|
|
|
|
|
2005-08-14 04:53:35 +08:00
|
|
|
err_num_t expected[8];
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2000-05-19 03:13:33 +08:00
|
|
|
int error_flag = 0;
|
|
|
|
int error_count = 0;
|
|
|
|
pthread_mutex_t error_mutex;
|
|
|
|
|
2000-05-20 07:00:03 +08:00
|
|
|
void tts_error(void)
|
|
|
|
{
|
2003-08-09 03:06:29 +08:00
|
|
|
pthread_t threads[NUM_THREAD];
|
|
|
|
pthread_attr_t attribute;
|
|
|
|
hid_t dataset;
|
|
|
|
int value, i;
|
2005-08-20 23:45:13 +08:00
|
|
|
int ret;
|
2003-08-09 03:06:29 +08:00
|
|
|
|
|
|
|
/* Must initialize these at runtime */
|
2006-08-28 05:09:31 +08:00
|
|
|
expected[0].maj_num = H5E_DATASET;
|
2006-08-10 03:59:35 +08:00
|
|
|
expected[0].min_num = H5E_CANTINIT;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2006-08-28 05:09:31 +08:00
|
|
|
expected[1].maj_num = H5E_SYM;
|
|
|
|
expected[1].min_num = H5E_EXISTS;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2003-08-09 03:06:29 +08:00
|
|
|
expected[2].maj_num = H5E_SYM;
|
2006-08-28 05:09:31 +08:00
|
|
|
expected[2].min_num = H5E_NOTFOUND;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2003-08-09 03:06:29 +08:00
|
|
|
expected[3].maj_num = H5E_SYM;
|
2006-08-28 05:09:31 +08:00
|
|
|
expected[3].min_num = H5E_CALLBACK;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2003-08-09 03:06:29 +08:00
|
|
|
expected[4].maj_num = H5E_SYM;
|
2006-08-28 05:09:31 +08:00
|
|
|
expected[4].min_num = H5E_EXISTS;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-11-15 10:55:39 +08:00
|
|
|
expected[5].maj_num = H5E_SYM;
|
|
|
|
expected[5].min_num = H5E_EXISTS;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2003-08-09 03:06:29 +08:00
|
|
|
/* set up mutex for global count of errors */
|
2005-08-20 04:01:41 +08:00
|
|
|
ret=pthread_mutex_init(&error_mutex, NULL);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2003-08-09 03:06:29 +08:00
|
|
|
|
|
|
|
/* make thread scheduling global */
|
2005-08-20 04:01:41 +08:00
|
|
|
ret=pthread_attr_init(&attribute);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2005-08-20 04:01:41 +08:00
|
|
|
|
2003-08-09 03:06:29 +08:00
|
|
|
#ifdef H5_HAVE_SYSTEM_SCOPE_THREADS
|
2005-08-20 04:01:41 +08:00
|
|
|
ret=pthread_attr_setscope(&attribute, PTHREAD_SCOPE_SYSTEM);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2003-08-09 03:06:29 +08:00
|
|
|
#endif /* H5_HAVE_SYSTEM_SCOPE_THREADS */
|
2003-07-26 10:55:47 +08:00
|
|
|
|
2003-08-09 03:06:29 +08:00
|
|
|
/*
|
|
|
|
* Create a hdf5 file using H5F_ACC_TRUNC access, default file
|
|
|
|
* creation plist and default file access plist
|
|
|
|
*/
|
|
|
|
error_file = H5Fcreate(FILENAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(error_file>=0);
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2005-08-20 04:01:41 +08:00
|
|
|
for (i = 0; i < NUM_THREAD; i++){
|
|
|
|
ret=pthread_create(&threads[i], &attribute, tts_error_thread, NULL);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
|
|
|
}
|
2000-05-20 07:00:03 +08:00
|
|
|
|
2005-08-20 04:01:41 +08:00
|
|
|
for (i = 0; i < NUM_THREAD; i++){
|
|
|
|
ret=pthread_join(threads[i],NULL);
|
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 (error_flag)
|
|
|
|
TestErrPrintf("Threads reporting different error values!\n");
|
2003-08-09 03:06:29 +08:00
|
|
|
|
2004-01-10 09:41:13 +08:00
|
|
|
if (error_count != NUM_THREAD - 1)
|
|
|
|
TestErrPrintf("Error: %d threads failed instead of %d\n", error_count, NUM_THREAD-1);
|
2003-08-09 03:06:29 +08:00
|
|
|
|
|
|
|
dataset = H5Dopen(error_file, DATASETNAME);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(dataset>=0);
|
2005-08-20 04:01:41 +08:00
|
|
|
|
|
|
|
ret=H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &value);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2003-08-09 03:06:29 +08:00
|
|
|
|
2004-01-10 09:41:13 +08:00
|
|
|
if (value != WRITE_NUMBER)
|
|
|
|
TestErrPrintf("Error: Successful thread wrote value %d instead of %d\n", value, WRITE_NUMBER);
|
2003-08-09 03:06:29 +08:00
|
|
|
|
2005-08-20 04:01:41 +08:00
|
|
|
ret=H5Dclose(dataset);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2005-08-20 04:01:41 +08:00
|
|
|
ret=H5Fclose(error_file);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2003-08-09 03:06:29 +08:00
|
|
|
|
|
|
|
/* Destroy the thread attribute */
|
2005-08-20 04:01:41 +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
|
|
|
}
|
|
|
|
|
2001-09-05 01:37:10 +08:00
|
|
|
static
|
2003-08-09 03:06:29 +08:00
|
|
|
void *tts_error_thread(void UNUSED *arg)
|
2000-05-20 07:00:03 +08:00
|
|
|
{
|
2003-08-09 03:06:29 +08:00
|
|
|
hid_t dataspace, datatype, dataset;
|
|
|
|
hsize_t dimsf[1]; /* dataset dimensions */
|
2004-09-09 10:37:02 +08:00
|
|
|
H5E_auto_stack_t old_error_cb;
|
2003-08-09 03:06:29 +08:00
|
|
|
void *old_error_client_data;
|
|
|
|
int value;
|
2005-08-20 23:45:13 +08:00
|
|
|
int ret;
|
2003-08-09 03:06:29 +08:00
|
|
|
|
|
|
|
/* preserve previous error stack handler */
|
2006-08-28 05:09:31 +08:00
|
|
|
H5Eget_auto_stack(H5E_DEFAULT, &old_error_cb, &old_error_client_data);
|
2003-08-09 03:06:29 +08:00
|
|
|
|
|
|
|
/* set each thread's error stack handler */
|
2006-08-28 05:09:31 +08:00
|
|
|
H5Eset_auto_stack(H5E_DEFAULT, error_callback, NULL);
|
2003-08-09 03:06:29 +08:00
|
|
|
|
|
|
|
/* define dataspace for dataset */
|
|
|
|
dimsf[0] = 1;
|
|
|
|
dataspace = H5Screate_simple(1,dimsf,NULL);
|
2005-08-20 04:01:41 +08:00
|
|
|
assert(dataspace>=0);
|
2003-08-09 03:06:29 +08:00
|
|
|
|
|
|
|
/* define datatype for the data using native little endian integers */
|
|
|
|
datatype = H5Tcopy(H5T_NATIVE_INT);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(datatype>=0);
|
2003-08-09 03:06:29 +08:00
|
|
|
H5Tset_order(datatype, H5T_ORDER_LE);
|
2004-12-29 22:26:20 +08:00
|
|
|
|
2003-08-09 03:06:29 +08:00
|
|
|
/* create a new dataset within the file */
|
|
|
|
dataset = H5Dcreate(error_file, DATASETNAME, datatype, dataspace, H5P_DEFAULT);
|
|
|
|
if (dataset >= 0) { /* not an error */
|
|
|
|
value = WRITE_NUMBER;
|
|
|
|
H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &value);
|
|
|
|
H5Dclose(dataset);
|
|
|
|
}
|
|
|
|
|
2005-08-20 04:01:41 +08:00
|
|
|
ret=H5Tclose(datatype);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2005-08-20 04:01:41 +08:00
|
|
|
ret=H5Sclose(dataspace);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret>=0);
|
2003-08-09 03:06:29 +08:00
|
|
|
|
|
|
|
/* turn our error stack handler off */
|
2006-08-10 03:59:35 +08:00
|
|
|
H5Eset_auto_stack(H5E_DEFAULT, old_error_cb, old_error_client_data);
|
2003-08-09 03:06:29 +08:00
|
|
|
|
|
|
|
return NULL;
|
2000-05-19 03:13:33 +08:00
|
|
|
}
|
|
|
|
|
2003-09-30 00:24:56 +08:00
|
|
|
static
|
2006-08-01 04:57:44 +08:00
|
|
|
herr_t error_callback(hid_t estack_id, void *client_data)
|
2003-09-30 00:24:56 +08:00
|
|
|
{
|
2005-08-20 23:45:13 +08:00
|
|
|
int ret;
|
|
|
|
|
2005-08-20 04:01:41 +08:00
|
|
|
ret=pthread_mutex_lock(&error_mutex);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2003-09-30 00:24:56 +08:00
|
|
|
error_count++;
|
2005-08-20 04:01:41 +08:00
|
|
|
ret=pthread_mutex_unlock(&error_mutex);
|
2005-08-20 23:45:13 +08:00
|
|
|
assert(ret==0);
|
2006-08-01 04:57:44 +08:00
|
|
|
return H5Ewalk_stack(H5E_DEFAULT, H5E_WALK_DOWNWARD, walk_error_callback, client_data);
|
2003-09-30 00:24:56 +08:00
|
|
|
}
|
|
|
|
|
2001-09-05 01:37:10 +08:00
|
|
|
static
|
2006-08-01 04:57:44 +08:00
|
|
|
herr_t walk_error_callback(unsigned n, const H5E_error_stack_t *err_desc, void UNUSED *client_data)
|
2000-05-20 07:00:03 +08:00
|
|
|
{
|
2003-08-09 03:06:29 +08:00
|
|
|
hid_t maj_num, min_num;
|
|
|
|
|
|
|
|
if (err_desc) {
|
2003-09-30 00:24:56 +08:00
|
|
|
maj_num = err_desc->maj_num;
|
|
|
|
min_num = err_desc->min_num;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2003-08-09 03:06:29 +08:00
|
|
|
if (n < EXPECTED_ERROR_DEPTH && maj_num == expected[n].maj_num &&
|
|
|
|
min_num == expected[n].min_num)
|
|
|
|
return SUCCEED;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_flag = -1;
|
|
|
|
return SUCCEED;
|
2000-05-20 07:00:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup_error(void)
|
|
|
|
{
|
2003-08-09 03:06:29 +08:00
|
|
|
HDunlink(FILENAME);
|
2000-05-19 03:13:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*H5_HAVE_THREADSAFE*/
|