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_attr_setdetachstate()
|
---|
9 | *
|
---|
10 | * If the thread is created detached, then use of the ID of the newly created
|
---|
11 | * thread by the pthread_detach() or pthread_join() function is an error.
|
---|
12 | *
|
---|
13 | * Steps:
|
---|
14 | * 1. Initialize a pthread_attr_t object using pthread_attr_init().
|
---|
15 | * 2. Using pthread_attr_setdetachstate(), set the detachstate to
|
---|
16 | * PTHREAD_CREATE_DETACHED.
|
---|
17 | * 3. Create a thread with this attribute.
|
---|
18 | * 4. Call pthread_detach() and pthread_join() on this thread, it should give
|
---|
19 | * an error.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include <pthread.h>
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <errno.h>
|
---|
26 | #include "posixtest.h"
|
---|
27 |
|
---|
28 | void *a_thread_func()
|
---|
29 | {
|
---|
30 |
|
---|
31 | pthread_exit(0);
|
---|
32 | return NULL;
|
---|
33 | }
|
---|
34 |
|
---|
35 | int main()
|
---|
36 | {
|
---|
37 | pthread_t new_th;
|
---|
38 | pthread_attr_t new_attr;
|
---|
39 | int ret_val;
|
---|
40 |
|
---|
41 | /* Initialize attribute */
|
---|
42 | if(pthread_attr_init(&new_attr) != 0)
|
---|
43 | {
|
---|
44 | perror("Cannot initialize attribute object\n");
|
---|
45 | return PTS_UNRESOLVED;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /* Set the attribute object to PTHREAD_CREATE_DETACHED. */
|
---|
49 | if(pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_DETACHED) != 0)
|
---|
50 | {
|
---|
51 | perror("Error in pthread_attr_setdetachstate()\n");
|
---|
52 | return PTS_UNRESOLVED;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /* Create a new thread passing it the new attribute object */
|
---|
56 | if(pthread_create(&new_th, &new_attr, a_thread_func, NULL) != 0)
|
---|
57 | {
|
---|
58 | perror("Error creating thread\n");
|
---|
59 | return PTS_UNRESOLVED;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /* If pthread_join() or pthread_detach fail, that means that the
|
---|
63 | * test fails as well. */
|
---|
64 | ret_val=pthread_join(new_th, NULL);
|
---|
65 |
|
---|
66 | if(ret_val != EINVAL)
|
---|
67 | {
|
---|
68 | printf("Test FAILED\n");
|
---|
69 | return PTS_FAIL;
|
---|
70 | }
|
---|
71 |
|
---|
72 | ret_val=pthread_detach(new_th);
|
---|
73 |
|
---|
74 | if(ret_val != EINVAL)
|
---|
75 | {
|
---|
76 | printf("Test FAILED\n");
|
---|
77 | return PTS_FAIL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | printf("Test PASSED\n");
|
---|
81 | return PTS_PASS;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|