/* * Copyright (c) 2004, Bull S.A.. All rights reserved. * Created by: Sebastien Decugis * This program is free software; you can redistribute it and/or modify it * under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it would be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write the Free Software Foundation, Inc., 59 * Temple Place - Suite 330, Boston MA 02111-1307, USA. * This file is a stress test for the function pthread_exit. * * It aims to check that: * -> when the threads are joinable, pthread_join always retrieve the * correct value. * -> pthread_exit() frees all the resources used by the threads. * * The second assertion is implicitly checked by monitoring the system * while the stress test is running. * */ /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */ #define _POSIX_C_SOURCE 200112L /* We need the XSI extention for some routines */ #ifndef WITHOUT_XOPEN #define _XOPEN_SOURCE 600 #endif /********************************************************************************************/ /****************************** standard includes *****************************************/ /********************************************************************************************/ #include #include #include #include #include #include #include #include /********************************************************************************************/ /****************************** Test framework *****************************************/ /********************************************************************************************/ #include "testfrmw.h" #include "testfrmw.c" /* This header is responsible for defining the following macros: * UNRESOLVED(ret, descr); * where descr is a description of the error and ret is an int (error code for example) * FAILED(descr); * where descr is a short text saying why the test has failed. * PASSED(); * No parameter. * * Both three macros shall terminate the calling process. * The testcase shall not terminate in any other maneer. * * The other file defines the functions * void output_init() * void output(char * string, ...) * * Those may be used to output information. */ /********************************************************************************************/ /********************************** Configuration ******************************************/ /********************************************************************************************/ #ifndef SCALABILITY_FACTOR #define SCALABILITY_FACTOR 1 #endif #ifndef VERBOSE #define VERBOSE 1 #endif #define FACTOR 5 /* This testcase needs the XSI features */ #ifndef WITHOUT_XOPEN /********************************************************************************************/ /*********************************** Test case *****************************************/ /********************************************************************************************/ #include "threads_scenarii.c" /* This file will define the following objects: * scenarii: array of struct __scenario type. * NSCENAR : macro giving the total # of scenarii * scenar_init(): function to call before use the scenarii array. * scenar_fini(): function to call after end of use of the scenarii array. */ /********************************************************************************************/ /*********************************** Real Test *****************************************/ /********************************************************************************************/ char do_it=1; long long iterations=0; /* Handler for user request to terminate */ void sighdl(int sig) { /* do_it = 0 */ do { do_it = 0; } while (do_it); } /* Cleanup handler to make sure the thread is exiting */ void cleanup(void * arg) { int ret = 0; sem_t * sem = (sem_t *) arg; /* Signal we're done (especially in case of a detached thread) */ do { ret = sem_post(sem); } while ((ret == -1) && (errno == EINTR)); if (ret == -1) { UNRESOLVED(errno, "Failed to wait for the semaphore"); } } /* Thread routine */ void * threaded(void * arg) { pthread_cleanup_push(cleanup, &scenarii[sc].sem); pthread_exit(arg); FAILED("the pthread_exit routine returned"); pthread_cleanup_pop(1); return NULL; /* For the sake of compiler */ } /* main routine */ int main(int argc, char * argv[]) { int ret, i; void * rval; struct sigaction sa; pthread_t threads[NSCENAR * SCALABILITY_FACTOR * FACTOR]; int rets[NSCENAR * SCALABILITY_FACTOR * FACTOR]; /* Initialize output */ output_init(); /* Initialize scenarii table */ scenar_init(); /* Register the signal handler for SIGUSR1 */ sigemptyset (&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = sighdl; if ((ret = sigaction (SIGUSR1, &sa, NULL))) { UNRESOLVED(ret, "Unable to register signal handler"); } if ((ret = sigaction (SIGALRM, &sa, NULL))) { UNRESOLVED(ret, "Unable to register signal handler"); } #if VERBOSE > 1 output("[parent] Signal handler registered\n"); #endif while (do_it) { /* Create all the threads */ for (i=0; i 5 if (rets[i*NSCENAR + sc] == 0) { output("Thread has been created successfully for this scenario\n"); } else { output("Thread creation failed with the error: %s\n", strerror(rets[i*NSCENAR + sc])); } #endif ; } if (rets[i*NSCENAR + sc] == 0) { /* Just wait for the thread to terminate */ do { ret = sem_wait(&scenarii[sc].sem); } while ((ret == -1) && (errno == EINTR)); if (ret == -1) { UNRESOLVED(errno, "Failed to wait for the semaphore"); } } } } /* Join all the joinable threads and check the value */ for (i=0; i