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_cancel 9 * Upon successful completion will return a 0. 10 * 11 * STEPS: 12 * 1. Create a thread 13 * 2. Cancel that thread 14 * 3. If pthread_cancel does not return [ESRCH] then it should return 0 15 */ 16 17 #include <pthread.h> 18 #include <stdio.h> 19 #include <errno.h> 20 #include <unistd.h> 21 #include "posixtest.h" 22 23 int sem; /* Manual semaphore */ 24 void *a_thread_func() 25 { 26 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); 27 28 /* Indicate to main() that the thread has been created. */ 29 sem = 1; 30 31 while (1) 32 sleep(1); 33 34 pthread_exit(0); 35 return NULL; 36 } 37 38 int main(void) 39 { 40 pthread_t new_th; 41 int ret; 42 43 sem = 0; 44 45 /* Create a new thread. */ 46 if (pthread_create(&new_th, NULL, a_thread_func, NULL) != 0) { 47 perror("Error creating thread\n"); 48 return PTS_UNRESOLVED; 49 } 50 51 /* Make sure thread is created before we cancel it. */ 52 while (sem == 0) 53 sleep(1); 54 55 /* Send cancel request to thread */ 56 ret = pthread_cancel(new_th); 57 58 if (ret != 0) { 59 if (ret == ESRCH) { 60 perror("Could not cancel thread\n"); 61 return PTS_UNRESOLVED; 62 } else { 63 printf("Test FAILED\n"); 64 return PTS_FAIL; 65 } 66 } 67 68 printf("Test PASSED\n"); 69 return PTS_PASS; 70 } 71