Home | History | Annotate | Download | only in pthread_detach
      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 failure, it shall return an error number:
     11  * -[EINVAL] The implemenation has detected that the value specified by
     12  * 'thread' does not refer to a joinable thread.
     13  * -[ESRCH] No thread could be found corresponding to that thread
     14 
     15  * It shall not return an error code of [EINTR]
     16  *
     17  * STEPS:
     18  * 1. Create a thread.
     19  * 2.Wait 'till the thread exits.
     20  * 3.Try and detach this thread.
     21  * 4.Check the return value and make sure it is ESRCH
     22  *
     23  */
     24 
     25 #include <pthread.h>
     26 #include <stdio.h>
     27 #include <errno.h>
     28 #include <unistd.h>
     29 #include "posixtest.h"
     30 
     31 /* Thread function */
     32 void *a_thread_func()
     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 	/* Create the thread */
     44 	if (pthread_create(&new_th, NULL, a_thread_func, NULL) != 0) {
     45 		perror("Error creating thread\n");
     46 		return PTS_UNRESOLVED;
     47 	}
     48 
     49 	/* Wait 'till the thread returns.
     50 	 * The thread could have ended by the time we try to join, so
     51 	 * don't worry about it, just so long as other errors don't
     52 	 * occur. The point is to make sure the thread has ended execution. */
     53 	if (pthread_join(new_th, NULL) == EDEADLK) {
     54 		perror("Error joining thread\n");
     55 		return PTS_UNRESOLVED;
     56 	}
     57 
     58 	/* Detach the non-existant thread. */
     59 	ret = pthread_detach(new_th);
     60 
     61 	/* Check return value of pthread_detach() */
     62 	if (ret != ESRCH) {
     63 		printf
     64 		    ("Test FAILED: Incorrect return code: %d instead of ESRCH\n",
     65 		     ret);
     66 		return PTS_FAIL;
     67 
     68 	}
     69 
     70 	printf("Test PASSED\n");
     71 	return PTS_PASS;
     72 }
     73