1 /* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * Created by: rolla.n.selbak REMOVE-THIS AT intel DOT com 4 * This file is licensed under the GPL license. For the full content 5 * of this license, see the COPYING file at the top level of this 6 * source tree. 7 8 * Test pthread_cancelstate 9 * Atomically sets the cancelability state to 'state' and returns the old 10 * cancelability state in the location referenced by 'oldstate'. 11 * 'state' can either be PTHREAD_CANCEL_ENABLE, or PTHREAD_CANCEL_DISABLE. 12 * 13 * Test when a thread is PTHREAD_CANCEL_ENABLE 14 * 15 * STEPS: 16 * 1. Create a thread. 17 * 2. In the thread function, set the state to 18 * PTHREAD_CANCEL_ENABLE and the cancel_flag to 1. 19 * 3. Send out a thread cancel request to the new thread 20 * 4. If the cancel request was honored, the cancel_flag will remain 1. 21 * 5. If not, the thread will continue until the end of execution, cancel_flag will be set to -1 22 * and,therefore failing the test. 23 */ 24 25 #include <pthread.h> 26 #include <stdio.h> 27 #include <errno.h> 28 #include <unistd.h> 29 #include "posixtest.h" 30 31 #define INTHREAD 0 /* Control going to or is already for Thread */ 32 #define INMAIN 1 /* Control going to or is already for Main */ 33 34 int sem1; /* Manual semaphore */ 35 int cancel_flag; 36 37 /* Function that the thread executes upon its creation */ 38 void *a_thread_func() 39 { 40 /* Set cancel state to ENABLE, meaning it should honor all cancel requests. */ 41 /* Set cancel type to ASYNCHRONOUS so that it honors cancel requests immediately. */ 42 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); 43 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); 44 45 cancel_flag = 1; 46 47 /* Indicate to main() that the thread has been created. */ 48 sem1 = INMAIN; 49 50 /* Wait until main() has sent out a cancel request, meaning until it 51 * sets sem1==INTHREAD. */ 52 while (sem1 == INMAIN) 53 sleep(1); 54 55 /* If the thread correctly honors the cancel request, then the cancel_flag will 56 * remain 1. If it contiues on with the thread execution, then the cancel_flag 57 * will be -1, and therefore failing this test. */ 58 pthread_testcancel(); 59 60 /* Should not reach here if the thread correctly honors the cancel request. */ 61 cancel_flag = -1; 62 pthread_exit(0); 63 return NULL; 64 } 65 66 int main(void) 67 { 68 pthread_t new_th; 69 70 /* Initializing values */ 71 sem1 = INTHREAD; 72 cancel_flag = 0; 73 74 /* Create a new thread. */ 75 if (pthread_create(&new_th, NULL, a_thread_func, NULL) != 0) { 76 perror("Error creating thread\n"); 77 return PTS_UNRESOLVED; 78 } 79 80 /* Make sure thread is created before we cancel it. (wait for 81 * a_thread_func() to set sem1=INMAIN.) */ 82 while (sem1 == INTHREAD) 83 sleep(1); 84 85 if (pthread_cancel(new_th) != 0) { 86 perror("Error sending cancel request\n"); 87 return PTS_UNRESOLVED; 88 } 89 90 /* Indicate to the thread function that the thread cancel request 91 * has been sent to it. */ 92 sem1 = INTHREAD; 93 94 /* Wait for thread to end execution. */ 95 if (pthread_join(new_th, NULL) != 0) { 96 perror("Error in pthread_join()\n"); 97 return PTS_UNRESOLVED; 98 } 99 100 /* This means that the cancel request was ignored rather than honored, and 101 * the test fails. */ 102 if (cancel_flag < 0) { 103 printf 104 ("Test FAILED: Thread of cancel type PTHREAD_CANCEL_ENABLE did not honor cancel request\n"); 105 return PTS_FAIL; 106 } 107 108 printf("Test PASSED\n"); 109 return PTS_PASS; 110 } 111