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 if the attributes specified by 'attr' are modified later, the thread's 9 * attributes shall not be affected. 10 * The attribute that will be tested is the detached state. 11 * 12 * 13 * Steps: 14 * 1. Set a pthread_attr_t object to be PTHREAD_CREATE_JOINABLE. 15 * 2. Create a new thread using pthread_create() and passing this attribute 16 * object. 17 * 3. Change the attribute object to be in a detached state rather than 18 * joinable. 19 * 4. Doing this should not effect the fact that the thread that was created 20 * is joinable, and so calling the functions pthread_detach() should not fail. 21 */ 22 23#include <pthread.h> 24#include <stdio.h> 25#include <unistd.h> 26#include <errno.h> 27#include "posixtest.h" 28 29# define TIMEOUT 10 /* Timeout value of 10 seconds. */ 30# define INTHREAD 0 /* Control going to or is already for Thread */ 31# define INMAIN 1 /* Control going to or is already for Main */ 32 33int sem1; /* Manual semaphore */ 34 35void *a_thread_func() 36{ 37 /* Indicate to main() that the thread was created. */ 38 sem1=INTHREAD; 39 40 /* Wait for main to detach change the attribute object and try and detach this thread. 41 * Wait for a timeout value of 10 seconds before timing out if the thread was not able 42 * to be detached. */ 43 sleep(TIMEOUT); 44 45 printf("Test FAILED: Did not detach the thread, main still waiting for it to end execution.\n"); 46 pthread_exit((void*)PTS_FAIL); 47 return NULL; 48} 49 50int main() 51{ 52 pthread_t new_th; 53 pthread_attr_t new_attr; 54 int ret; 55 56 /* Initializing */ 57 sem1 = INMAIN; 58 if(pthread_attr_init(&new_attr) != 0) 59 { 60 perror("Error intializing attribute object\n"); 61 return PTS_UNRESOLVED; 62 63 } 64 65 /* Make the new attribute object joinable */ 66 if(pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_JOINABLE) != 0) 67 { 68 perror("Error setting the detached state of the attribute\n"); 69 return PTS_UNRESOLVED; 70 } 71 72 /* Create a new thread and pass it the attribute object that will 73 * make it joinable. */ 74 if(pthread_create(&new_th, &new_attr, a_thread_func, NULL) != 0) 75 { 76 perror("Error creating thread\n"); 77 return PTS_UNRESOLVED; 78 } 79 80 while(sem1==INMAIN) 81 sleep(1); 82 83 /* Now change the attribute object to be in a detached state */ 84 if(pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_DETACHED) != 0) 85 { 86 perror("Error setting the detached state of the attribute\n"); 87 return PTS_UNRESOLVED; 88 } 89 90 /* The new thread should still be able to be detached. */ 91 if((ret=pthread_detach(new_th)) == EINVAL) 92 { 93 printf("Test FAILED: pthread_detach failed on joinable thread. Return value is %d\n", ret); 94 return PTS_FAIL; 95 } 96 97 printf("Test PASSED\n"); 98 return PTS_PASS; 99} 100 101 102