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 detached state thread
19  * 2.Detach that thread
20  * 3.Check the return value and make sure it is EINVAL
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 /* Thread function */
a_thread_func()31 static void *a_thread_func()
32 {
33 
34     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
35 
36     /* If the thread wasn't canceled in 10 seconds, time out */
37     sleep(10);
38 
39     perror("Thread couldn't be canceled (at cleanup time), timing out\n");
40     pthread_exit(0);
41     return NULL;
42 }
43 
posix_testcase(void)44 static int posix_testcase(void)
45 {
46     pthread_attr_t new_attr;
47     pthread_t new_th;
48     int ret;
49 
50     /* Initialize attribute */
51     if (pthread_attr_init(&new_attr) != 0) {
52         perror("Cannot initialize attribute object\n");
53         return PTS_UNRESOLVED;
54     }
55 
56     /* Set the attribute object to be detached */
57     if (pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_DETACHED) !=
58         0) {
59         perror("Error in pthread_attr_setdetachstate()\n");
60         return PTS_UNRESOLVED;
61     }
62 
63     /* Create the thread */
64     if (pthread_create(&new_th, &new_attr, a_thread_func, NULL) != 0) {
65         perror("Error creating thread\n");
66         return PTS_UNRESOLVED;
67     }
68 
69     /* Detach the thread. */
70     ret = pthread_detach(new_th);
71 
72     /* Cleanup and cancel the thread */
73     pthread_cancel(new_th);
74 
75     /* Check return value of pthread_detach() */
76     if (ret != EINVAL) {
77         if (ret == ESRCH) {
78             perror("Error detaching thread\n");
79             return PTS_UNRESOLVED;
80         }
81 
82         printf("Test FAILED: Incorrect return code\n");
83         return PTS_FAIL;
84 
85     }
86 
87     printf("Test PASSED\n");
88     return PTS_PASS;
89 }
90 #include <rtt_utest_internal.h>
91 UTEST_TC_EXPORT(testcase, "posix.pthread_detach.4-1.c", RT_NULL, RT_NULL, 10);
92 
93