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 that pthread_detach() 9 * 10 * Upon succesful completion, it shall return a 0; 11 * 12 * STEPS: 13 * 1.Create a joinable thread 14 * 2.Detach that thread 15 * 3.Check the return value 16 * 17 */ 18 19 #include <pthread.h> 20 #include <stdio.h> 21 #include <errno.h> 22 #include <unistd.h> 23 #include "posixtest.h" 24 25 /* Thread function */ 26 void *a_thread_func() 27 { 28 29 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); 30 31 /* If the thread wasn't canceled in 10 seconds, time out */ 32 sleep(10); 33 34 perror("Thread couldn't be canceled (at cleanup time), timing out\n"); 35 pthread_exit(0); 36 return NULL; 37 } 38 39 int main(void) 40 { 41 pthread_attr_t new_attr; 42 pthread_t new_th; 43 int ret; 44 45 /* Initialize attribute */ 46 if (pthread_attr_init(&new_attr) != 0) { 47 perror("Cannot initialize attribute object\n"); 48 return PTS_UNRESOLVED; 49 } 50 51 /* Set the attribute object to be joinable */ 52 if (pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_JOINABLE) != 53 0) { 54 perror("Error in pthread_attr_setdetachstate()\n"); 55 return PTS_UNRESOLVED; 56 } 57 58 /* Create the thread */ 59 if (pthread_create(&new_th, &new_attr, a_thread_func, NULL) != 0) { 60 perror("Error creating thread\n"); 61 return PTS_UNRESOLVED; 62 } 63 64 /* Detach the thread. */ 65 ret = pthread_detach(new_th); 66 67 /* Cleanup and cancel the thread */ 68 pthread_cancel(new_th); 69 70 /* Check return value of pthread_detach() */ 71 if (ret != 0) { 72 if ((ret != ESRCH) || (ret != EINVAL)) { 73 printf("Test FAILED: Incorrect return code\n"); 74 return PTS_FAIL; 75 } 76 77 perror("Error detaching thread\n"); 78 return PTS_UNRESOLVED; 79 } 80 81 printf("Test PASSED\n"); 82 return PTS_PASS; 83 } 84