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 succesful completion, it shall return a 0;
11 *
12 * STEPS:
13 * 1.Create a joinable thread
14 * 2.Detach that thread
15 * 3.Check the return value
16 *
17 */
18
19#include <pthread.h>
20#include <stdio.h>
21#include <errno.h>
22#include <unistd.h>
23#include "posixtest.h"
24
25/* Thread function */
26void *a_thread_func()
27{
28
29	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
30
31	/* If the thread wasn't canceled in 10 seconds, time out */
32	sleep(10);
33
34	perror("Thread couldn't be canceled (at cleanup time), timing out\n");
35	pthread_exit(0);
36	return NULL;
37}
38
39int main()
40{
41	pthread_attr_t new_attr;
42	pthread_t new_th;
43	int ret;
44
45	/* Initialize attribute */
46	if(pthread_attr_init(&new_attr) != 0)
47	{
48		perror("Cannot initialize attribute object\n");
49		return PTS_UNRESOLVED;
50	}
51
52	/* Set the attribute object to be joinable */
53	if(pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_JOINABLE) != 0)
54	{
55		perror("Error in pthread_attr_setdetachstate()\n");
56		return PTS_UNRESOLVED;
57	}
58
59	/* Create the thread */
60	if(pthread_create(&new_th, &new_attr, a_thread_func, NULL) != 0)
61	{
62		perror("Error creating thread\n");
63		return PTS_UNRESOLVED;
64	}
65
66	/* Detach the thread. */
67	ret=pthread_detach(new_th);
68
69	/* Cleanup and cancel the thread */
70	pthread_cancel(new_th);
71
72	/* Check return value of pthread_detach() */
73	if(ret != 0)
74	{
75		if((ret != ESRCH) || (ret != EINVAL))
76		{
77			printf("Test FAILED: Incorrect return code\n");
78			return PTS_FAIL;
79		}
80
81		perror("Error detaching thread\n");
82		return PTS_UNRESOLVED;
83	}
84
85	printf("Test PASSED\n");
86	return PTS_PASS;
87}
88
89
90