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 * pthread_exit() terminates the current thread and 21 * allows a call to pthread_join() on this thread retrieve 22 * the argument (if the thread is not detached). 23 24 * The steps are: 25 * 26 * -> Create threads with different attributes (but all must be joinable) 27 * -> inside the thread, a call to pthread_exit() is made with a certain value. 28 * -> In the main thread, we join the thread and check the value is as expected. 29 30 * The test fails if the values are different. 31 32 */ 33 34 35 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */ 36 #define _POSIX_C_SOURCE 200112L 37 38 /* Some routines are part of the XSI Extensions */ 39#ifndef WITHOUT_XOPEN 40 #define _XOPEN_SOURCE 600 41#endif 42 43/********************************************************************************************/ 44/****************************** standard includes *****************************************/ 45/********************************************************************************************/ 46 #include <pthread.h> 47 #include <stdarg.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 53 #include <sched.h> 54 #include <semaphore.h> 55 #include <errno.h> 56 #include <assert.h> 57/********************************************************************************************/ 58/****************************** Test framework *****************************************/ 59/********************************************************************************************/ 60 #include "testfrmw.h" 61 #include "testfrmw.c" 62 /* This header is responsible for defining the following macros: 63 * UNRESOLVED(ret, descr); 64 * where descr is a description of the error and ret is an int (error code for example) 65 * FAILED(descr); 66 * where descr is a short text saying why the test has failed. 67 * PASSED(); 68 * No parameter. 69 * 70 * Both three macros shall terminate the calling process. 71 * The testcase shall not terminate in any other maneer. 72 * 73 * The other file defines the functions 74 * void output_init() 75 * void output(char * string, ...) 76 * 77 * Those may be used to output information. 78 */ 79 80/********************************************************************************************/ 81/********************************** Configuration ******************************************/ 82/********************************************************************************************/ 83#ifndef VERBOSE 84#define VERBOSE 1 85#endif 86 87/********************************************************************************************/ 88/*********************************** Test cases *****************************************/ 89/********************************************************************************************/ 90 91#include "threads_scenarii.c" 92 93/* This file will define the following objects: 94 * scenarii: array of struct __scenario type. 95 * NSCENAR : macro giving the total # of scenarii 96 * scenar_init(): function to call before use the scenarii array. 97 * scenar_fini(): function to call after end of use of the scenarii array. 98 */ 99 100/********************************************************************************************/ 101/*********************************** Real Test *****************************************/ 102/********************************************************************************************/ 103 104 105void * threaded (void * arg) 106{ 107 pthread_exit(NULL + 1); 108 109 FAILED("pthread_exit() did not terminate the thread"); 110 111 return NULL; 112} 113 114 115int main (int argc, char *argv[]) 116{ 117 int ret=0; 118 void * rval; 119 pthread_t child; 120 int i; 121 122 output_init(); 123 124 scenar_init(); 125 126 for (i=0; i < NSCENAR; i++) 127 { 128 if (scenarii[i].detached == 0) 129 { 130 #if VERBOSE > 0 131 output("-----\n"); 132 output("Starting test with scenario (%i): %s\n", i, scenarii[i].descr); 133 #endif 134 135 ret = pthread_create(&child, &scenarii[i].ta, threaded, NULL); 136 switch (scenarii[i].result) 137 { 138 case 0: /* Operation was expected to succeed */ 139 if (ret != 0) { UNRESOLVED(ret, "Failed to create this thread"); } 140 break; 141 142 case 1: /* Operation was expected to fail */ 143 if (ret == 0) { UNRESOLVED(-1, "An error was expected but the thread creation succeeded"); } 144 break; 145 146 case 2: /* We did not know the expected result */ 147 default: 148 #if VERBOSE > 0 149 if (ret == 0) 150 { output("Thread has been created successfully for this scenario\n"); } 151 else 152 { output("Thread creation failed with the error: %s\n", strerror(ret)); } 153 #endif 154 } 155 if (ret == 0) /* The new thread is running */ 156 { 157 ret = pthread_join(child, &rval); 158 if (ret != 0) { UNRESOLVED(ret, "Unable to join a thread"); } 159 160 if (rval != (NULL+1)) 161 { 162 FAILED("pthread_join() did not retrieve the pthread_exit() param"); 163 } 164 } 165 } 166 } 167 168 scenar_fini(); 169 #if VERBOSE > 0 170 output("-----\n"); 171 output("All test data destroyed\n"); 172 output("Test PASSED\n"); 173 #endif 174 175 PASSED; 176} 177 178