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_create() creates a new thread with attributes specified
9 * by 'attr', within a process.
10 *
11 * Steps:
12 * 1.  Create a new thread that will go into a never-ending while loop.
13 * 2.  If the thread is truly asynchronise, then the main function will
14 *     continue instead of waiting for the thread to return (which in never
15 *     does in this test case).
16 * 3.  An alarm is set to go off (i.e. send the SIGARLM signal) after 3
17 *     seconds. This is done for 'timeing-out' reasons, in case main DOES
18 *     wait for the thread to return.  This would also mean that the test
19 *     failed.
20 */
21
22#include <pthread.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <unistd.h>
26#include <signal.h>
27#include <string.h>
28#include "posixtest.h"
29
30void *a_thread_function();
31void alarm_handler();
32
33pthread_t a;
34
35int main(void)
36{
37	int ret;
38
39	/* Set the action for SIGALRM to generate an error if it is
40	 * reached. This is because if SIGALRM was sent, then the
41	 * test timed out. */
42	if (signal(SIGALRM, alarm_handler) == SIG_ERR) {
43		printf("Error in signal()\n");
44		return PTS_UNRESOLVED;
45	}
46
47	/* SIGALRM will be sent in 5 seconds. */
48	alarm(5);
49
50	ret = pthread_create(&a, NULL, a_thread_function, NULL);
51	if (ret) {
52		fprintf(stderr, "pthread_create(): %s\n", strerror(ret));
53		return PTS_UNRESOLVED;
54	}
55
56	pthread_cancel(a);
57
58	pthread_join(a, NULL);
59
60	printf("Test PASSED\n");
61	return PTS_PASS;
62}
63
64/* A never-ending thread function */
65void *a_thread_function()
66{
67	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
68
69	while (1)
70		sleep(1);
71
72	return NULL;
73}
74
75#define WRITE(str) write(STDOUT_FILENO, str, sizeof(str) - 1)
76
77/* If this handler is called, that means that the test has failed. */
78void alarm_handler()
79{
80	WRITE("Test FAILED: Alarm fired while waiting for cancelation\n");
81	_exit(PTS_FAIL);
82}
83