1/* 2 * Copyright (c) 2004, Bull S.A.. All rights reserved. 3 * Created by: Sebastien Decugis 4 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of version 2 of the GNU General Public License as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it would be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 * 13 * You should have received a copy of the GNU General Public License along 14 * with this program; if not, write the Free Software Foundation, Inc., 59 15 * Temple Place - Suite 330, Boston MA 02111-1307, USA. 16 17 18 * This sample test aims to check the following assertion: 19 * 20 * Pending cancelation cleanup handlers are executed in FILO order. 21 22 * The steps are: 23 * 24 * -> Create threads with different attributes (but all must be joinable) 25 * -> inside the thread, 26 * -> register three cancelation handlers 27 * -> call pthread_exit. 28 * -> In the main thread, we join the thread and check the cleanups were executed in order. 29 30 */ 31 32 33 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */ 34 #define _POSIX_C_SOURCE 200112L 35 36 /* Some routines are part of the XSI Extensions */ 37#ifndef WITHOUT_XOPEN 38 #define _XOPEN_SOURCE 600 39#endif 40 41/********************************************************************************************/ 42/****************************** standard includes *****************************************/ 43/********************************************************************************************/ 44 #include <pthread.h> 45 #include <stdarg.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #include <sched.h> 52 #include <semaphore.h> 53 #include <errno.h> 54 #include <assert.h> 55/********************************************************************************************/ 56/****************************** Test framework *****************************************/ 57/********************************************************************************************/ 58 #include "testfrmw.h" 59 #include "testfrmw.c" 60 /* This header is responsible for defining the following macros: 61 * UNRESOLVED(ret, descr); 62 * where descr is a description of the error and ret is an int (error code for example) 63 * FAILED(descr); 64 * where descr is a short text saying why the test has failed. 65 * PASSED(); 66 * No parameter. 67 * 68 * Both three macros shall terminate the calling process. 69 * The testcase shall not terminate in any other maneer. 70 * 71 * The other file defines the functions 72 * void output_init() 73 * void output(char * string, ...) 74 * 75 * Those may be used to output information. 76 */ 77 78/********************************************************************************************/ 79/********************************** Configuration ******************************************/ 80/********************************************************************************************/ 81#ifndef VERBOSE 82#define VERBOSE 1 83#endif 84 85/********************************************************************************************/ 86/*********************************** Test cases *****************************************/ 87/********************************************************************************************/ 88 89#include "threads_scenarii.c" 90 91/* This file will define the following objects: 92 * scenarii: array of struct __scenario type. 93 * NSCENAR : macro giving the total # of scenarii 94 * scenar_init(): function to call before use the scenarii array. 95 * scenar_fini(): function to call after end of use of the scenarii array. 96 */ 97 98/********************************************************************************************/ 99/*********************************** Real Test *****************************************/ 100/********************************************************************************************/ 101 102int global=0; 103int tab[3]; 104 105#define CLEANUP(n) void clnp##n(void * arg)\ 106{\ 107 tab[global]=n; \ 108 global++; \ 109} 110 111CLEANUP(1) 112CLEANUP(2) 113CLEANUP(3) 114 115 116void * threaded (void * arg) 117{ 118 pthread_cleanup_push(clnp3, NULL); 119 pthread_cleanup_push(clnp2, NULL); 120 pthread_cleanup_push(clnp1, NULL); 121 122 pthread_exit(NULL + 1); 123 124 pthread_cleanup_pop(0); 125 pthread_cleanup_pop(0); 126 pthread_cleanup_pop(0); 127 128 FAILED("pthread_exit() did not terminate the thread"); 129 return NULL; 130} 131 132 133int main (int argc, char *argv[]) 134{ 135 int ret=0; 136 void * rval; 137 pthread_t child; 138 int i,j; 139 140 output_init(); 141 142 scenar_init(); 143 144 for (i=0; i < NSCENAR; i++) 145 { 146 if (scenarii[i].detached == 0) 147 { 148 #if VERBOSE > 0 149 output("-----\n"); 150 output("Starting test with scenario (%i): %s\n", i, scenarii[i].descr); 151 #endif 152 153 for (j=0; j<3; j++) 154 tab[j]=0; 155 global=0; 156 157 ret = pthread_create(&child, &scenarii[i].ta, threaded, NULL); 158 switch (scenarii[i].result) 159 { 160 case 0: /* Operation was expected to succeed */ 161 if (ret != 0) { UNRESOLVED(ret, "Failed to create this thread"); } 162 break; 163 164 case 1: /* Operation was expected to fail */ 165 if (ret == 0) { UNRESOLVED(-1, "An error was expected but the thread creation succeeded"); } 166 break; 167 168 case 2: /* We did not know the expected result */ 169 default: 170 #if VERBOSE > 0 171 if (ret == 0) 172 { output("Thread has been created successfully for this scenario\n"); } 173 else 174 { output("Thread creation failed with the error: %s\n", strerror(ret)); } 175 #endif 176 } 177 if (ret == 0) /* The new thread is running */ 178 { 179 ret = pthread_join(child, &rval); 180 if (ret != 0) { UNRESOLVED(ret, "Unable to join a thread"); } 181 182 if (rval != (NULL+1)) 183 { 184 FAILED("pthread_join() did not retrieve the pthread_exit() param"); 185 } 186 187 for (j=0; j<3; j++) 188 if (tab[j] != j+1) 189 { 190 output("dump:\ntab[0]=%i\ntab[1]=%i\ntab[2]=%i\n", tab[0], tab[1], tab[2]); 191 FAILED("The cleanup handlers were not called as expected"); 192 } 193 } 194 } 195 } 196 197 scenar_fini(); 198 #if VERBOSE > 0 199 output("-----\n"); 200 output("All test data destroyed\n"); 201 output("Test PASSED\n"); 202 #endif 203 204 PASSED; 205} 206 207