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 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
main()44 int main()
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 {
53 perror("Cannot initialize attribute object\n");
54 return PTS_UNRESOLVED;
55 }
56
57 /* Set the attribute object to be detached */
58 if(pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_DETACHED) != 0)
59 {
60 perror("Error in pthread_attr_setdetachstate()\n");
61 return PTS_UNRESOLVED;
62 }
63
64 /* Create the thread */
65 if(pthread_create(&new_th, &new_attr, a_thread_func, NULL) != 0)
66 {
67 perror("Error creating thread\n");
68 return PTS_UNRESOLVED;
69 }
70
71 /* Detach the thread. */
72 ret=pthread_detach(new_th);
73
74 /* Cleanup and cancel the thread */
75 pthread_cancel(new_th);
76
77 /* Check return value of pthread_detach() */
78 if(ret != EINVAL)
79 {
80 if(ret == ESRCH)
81 {
82 perror("Error detaching thread\n");
83 return PTS_UNRESOLVED;
84 }
85
86 printf("Test FAILED: Incorrect return code\n");
87 return PTS_FAIL;
88
89 }
90
91 printf("Test PASSED\n");
92 return PTS_PASS;
93 }
94
95
96