Home | History | Annotate | Download | only in pthread_exit
      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_exit()
      9  *
     10  * terminates the calling thread and makes the value 'value_ptr' available
     11  * to any successful join with the terminating thread.
     12  *
     13  * Steps:
     14  * 1.  Create a new thread.  Have it return a return code on pthread_exit();
     15  * 2.  Call pthread_join() in main(), and pass to it 'value_ptr'.
     16  * 3.  Check to see of the value_ptr and the value returned by pthread_exit() are the same;
     17  *
     18  */
     19 
     20 #include <pthread.h>
     21 #include <stdio.h>
     22 #include <unistd.h>
     23 #include "posixtest.h"
     24 
     25 #define RETURN_CODE 100		/* Set a random return code number. This shall be the return code of the
     26 				   thread when using pthread_exit(). */
     27 
     28 #define INTHREAD 0		/* Control going to or is already for Thread */
     29 #define INMAIN 1		/* Control going to or is already for Main */
     30 
     31 int sem;			/* Manual semaphore used to indicate when the thread has been created. */
     32 
     33 /* Thread's function. */
     34 void *a_thread_func()
     35 {
     36 	sem = INMAIN;
     37 	pthread_exit((void *)RETURN_CODE);
     38 	return NULL;
     39 }
     40 
     41 int main(void)
     42 {
     43 	pthread_t new_th;
     44 	int *value_ptr;
     45 
     46 	/* Initializing variables. */
     47 	value_ptr = 0;
     48 	sem = INTHREAD;
     49 
     50 	/* Create a new thread. */
     51 	if (pthread_create(&new_th, NULL, a_thread_func, NULL) != 0) {
     52 		perror("Error creating thread\n");
     53 		return PTS_UNRESOLVED;
     54 	}
     55 
     56 	/* Make sure the thread was created before we join it. */
     57 	while (sem == INTHREAD)
     58 		sleep(1);
     59 
     60 	/* Wait for thread to return */
     61 	if (pthread_join(new_th, (void *)&value_ptr) != 0) {
     62 		perror("Error in pthread_join()\n");
     63 		return PTS_UNRESOLVED;
     64 	}
     65 
     66 	/* Check to make sure that 'value_ptr' that was passed to pthread_join() and the
     67 	 * pthread_exit() return code that was used in the thread function are the same. */
     68 	if ((long)value_ptr != RETURN_CODE) {
     69 		printf
     70 		    ("Test FAILED: pthread_exit() could not pass the return value of the thread in 'value_ptr' to pthread_join().\n");
     71 		return PTS_FAIL;
     72 	}
     73 
     74 	printf("Test PASSED\n");
     75 	return PTS_PASS;
     76 }
     77