From 1cc00ac662af4c1f65c8426d7823a72d4c1e07d3 Mon Sep 17 00:00:00 2001 From: Fang Guo Date: Fri, 19 Aug 2005 15:06:26 -0500 Subject: [svn-r11269] Purpose: Improvement Description: ttsafe cancel failed on Windows because pthread mutex and cond are not initialized Add some assertion statements to locate the errors when they happen Solution: Initialize pthread mutex and cond to the default attributes. Platforms tested: MSVS 6.0 on windows XP and heping(with pthread) Misc. update: --- test/ttsafe_cancel.c | 84 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 22 deletions(-) diff --git a/test/ttsafe_cancel.c b/test/ttsafe_cancel.c index e008fe2..71b0529 100644 --- a/test/ttsafe_cancel.c +++ b/test/ttsafe_cancel.c @@ -66,6 +66,9 @@ pthread_t childthread; pthread_mutex_t mutex; pthread_cond_t cond; +//Global variables +int ret; + void tts_cancel(void) { pthread_attr_t attribute; @@ -73,31 +76,47 @@ void tts_cancel(void) int buffer; /* make thread scheduling global */ - pthread_attr_init(&attribute); + ret=pthread_attr_init(&attribute); + assert(ret==0); #ifdef H5_HAVE_SYSTEM_SCOPE_THREADS - pthread_attr_setscope(&attribute, PTHREAD_SCOPE_SYSTEM); + ret=pthread_attr_setscope(&attribute, PTHREAD_SCOPE_SYSTEM); + assert(ret==0); #endif /* H5_HAVE_SYSTEM_SCOPE_THREADS */ + + ret=pthread_mutex_init(&mutex,NULL); + assert(ret==0); + ret=pthread_cond_init(&cond,NULL); + assert(ret==0); + /* * 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); - pthread_create(&childthread, &attribute, tts_cancel_thread, NULL); + assert(cancel_file>=0); + ret=pthread_create(&childthread, &attribute, tts_cancel_thread, NULL); + assert(ret==0); tts_cancel_barrier(); - pthread_cancel(childthread); + ret=pthread_cancel(childthread); + assert(ret==0); dataset = H5Dopen(cancel_file, DATASETNAME); - H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buffer); + 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); - H5Dclose(dataset); - H5Fclose(cancel_file); + ret=H5Dclose(dataset); + assert(ret>=0); + ret=H5Fclose(cancel_file); + assert(ret>=0); /* Destroy the thread attribute */ - pthread_attr_destroy(&attribute); + ret=pthread_attr_destroy(&attribute); + assert(ret==0); } void *tts_cancel_thread(void UNUSED *arg) @@ -111,13 +130,17 @@ void *tts_cancel_thread(void UNUSED *arg) /* define dataspace for dataset */ dimsf[0] = 1; dataspace = H5Screate_simple(1,dimsf,NULL); + assert(dataspace>=0); /* define datatype for the data using native little endian integers */ datatype = H5Tcopy(H5T_NATIVE_INT); - H5Tset_order(datatype, H5T_ORDER_LE); + assert(datatype>=0); + ret=H5Tset_order(datatype, H5T_ORDER_LE); + assert(ret>=0); /* create a new dataset within the file */ dataset = H5Dcreate(cancel_file, DATASETNAME, datatype, dataspace, H5P_DEFAULT); + assert(dataset>=0); /* If thread is cancelled, make cleanup call */ cleanup_structure = (cancel_cleanup_t*)malloc(sizeof(cancel_cleanup_t)); @@ -125,27 +148,36 @@ void *tts_cancel_thread(void UNUSED *arg) cleanup_structure->datatype = datatype; cleanup_structure->dataspace = dataspace; pthread_cleanup_push(cancellation_cleanup, cleanup_structure); + datavalue = 1; - H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &datavalue); + ret=H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &datavalue); + assert(ret>=0); buffer = malloc(sizeof(int)); - H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buffer); - H5Diterate(buffer, H5T_NATIVE_INT, dataspace, tts_cancel_callback, &dataset); + ret=H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buffer); + assert(ret>=0); + ret=H5Diterate(buffer, H5T_NATIVE_INT, dataspace, tts_cancel_callback, &dataset); + assert(ret>=0); sleep(3); datavalue = 100; - H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &datavalue); - H5Dclose(dataset); - H5Tclose(datatype); - H5Sclose(dataspace); + ret=H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &datavalue); + assert(ret>=0); + ret=H5Dclose(dataset); + assert(ret>=0); + ret=H5Tclose(datatype); + assert(ret>=0); + ret=H5Sclose(dataspace); + assert(ret>=0); /* * Required by pthreads. The argument 0 pops the stack but does not * execute the cleanup routine. */ pthread_cleanup_pop(0); + return NULL; } @@ -164,7 +196,8 @@ herr_t tts_cancel_callback(void *elem, hid_t UNUSED type_id, unsigned UNUSED ndi } value += 10; - H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &value); + ret=H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &value); + assert(ret>=0); return 0; } @@ -175,9 +208,12 @@ herr_t tts_cancel_callback(void *elem, hid_t UNUSED type_id, unsigned UNUSED ndi void cancellation_cleanup(void *arg) { cancel_cleanup_t *cleanup_structure = (cancel_cleanup_t *)arg; - H5Dclose(cleanup_structure->dataset); - H5Tclose(cleanup_structure->datatype); - H5Sclose(cleanup_structure->dataspace); + ret=H5Dclose(cleanup_structure->dataset); + assert(ret>=0); + ret=H5Tclose(cleanup_structure->datatype); + assert(ret>=0); + ret=H5Sclose(cleanup_structure->dataspace); + assert(ret>=0); /* retained for debugging */ /* print_func("cancellation noted, cleaning up ... \n"); */ @@ -191,16 +227,20 @@ void tts_cancel_barrier(void) { static int count = 2; - pthread_mutex_lock(&mutex); + ret=pthread_mutex_lock(&mutex); + assert(ret==0); if (count != 1) { count--; - pthread_cond_wait(&cond, &mutex); + ret=pthread_cond_wait(&cond, &mutex); + assert(ret==0); } else { pthread_cond_signal(&cond); + assert(ret==0); } pthread_mutex_unlock(&mutex); + assert(ret==0); } void cleanup_cancel(void) -- cgit v0.12