Home | History | Annotate | Download | only in pthread_attr_init
      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 the resulting attributes object (possibly modified by setting individual
      9  * attribute values) when used by pthread_create() defines the attributes of
     10  * the thread created.
     11  *
     12  * Steps:
     13  * 1.  Initialize a pthread_attr_t object using pthread_attr_init()
     14  * 2.  Pass the newly created attribute object to pthread_create()
     15  * 3.  Test that thread is joinable, since using pthread_attr_init() will
     16  *     set the default detachstate to PTHREAD_CREATE_JOINABLE.
     17  * 4.  Pthread_detach() to test this.  It should
     18  *     not return errors since the thread should be joinable (non-detached).  If it
     19  *     returns an error, that means that the thread is not joinable, but rather
     20  *     in a detached state, and the test fails.
     21  *
     22  */
     23 
     24 #include <pthread.h>
     25 #include <stdio.h>
     26 #include <errno.h>
     27 #include <unistd.h>
     28 #include "posixtest.h"
     29 
     30 #define TIMEOUT 5		/* Timeout value of 5 seconds. */
     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 
     36 void *a_thread_func()
     37 {
     38 
     39 	/* Indicate to main() that the thread was created. */
     40 	sem1 = INTHREAD;
     41 
     42 	/* Wait for main to detach change the attribute object and try and detach this thread.
     43 	 * Wait for a timeout value of 10 seconds before timing out if the thread was not able
     44 	 * to be detached. */
     45 	sleep(TIMEOUT);
     46 
     47 	printf
     48 	    ("Test FAILED: Did not detach the thread, main still waiting for it to end execution.\n");
     49 	pthread_exit((void *)PTS_FAIL);
     50 	return NULL;
     51 }
     52 
     53 int main(void)
     54 {
     55 	pthread_t new_th;
     56 	pthread_attr_t new_attr;
     57 	int ret_val;
     58 
     59 	/* Initializing */
     60 	sem1 = INMAIN;
     61 	if (pthread_attr_init(&new_attr) != 0) {
     62 		perror("Cannot initialize attribute object\n");
     63 		return PTS_UNRESOLVED;
     64 	}
     65 
     66 	/* Create a new thread passing it the new attribute object */
     67 	if (pthread_create(&new_th, &new_attr, a_thread_func, NULL) != 0) {
     68 		perror("Error creating thread\n");
     69 		return PTS_UNRESOLVED;
     70 	}
     71 
     72 	/* Wait for thread to indicate that the start routine for the thread has started. */
     73 	while (sem1 == INMAIN)
     74 		sleep(1);
     75 
     76 	/* If pthread_detach fails, that means that the test fails as well. */
     77 	ret_val = pthread_detach(new_th);
     78 
     79 	if (ret_val != 0) {
     80 		/* Thread is already detached. */
     81 		if (ret_val == EINVAL) {
     82 			printf("Test FAILED\n");
     83 			return PTS_FAIL;
     84 		}
     85 		/* pthread_detach() failed for another reason. */
     86 		else {
     87 			printf("Error in pthread_detach(), error: %d\n",
     88 			       ret_val);
     89 			return PTS_UNRESOLVED;
     90 		}
     91 	}
     92 
     93 	printf("Test PASSED\n");
     94 	return PTS_PASS;
     95 
     96 }
     97