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 pthread_cancel
9 * Shall request that 'thread' shall be canceled.  The target thread's
10 * cancelability state and type determines when the cancelation takes
11 * effect.
12 *
13 * Test when a thread is PTHREAD_CANCEL_DISABLE
14 *
15 * STEPS:
16 * 1. Create a thread.
17 * 2. In the thread function, set the state to
18 *    PTHREAD_CANCEL_DISABLE
19 * 3. Setup a cleanup handler for the thread.
20 * 4. Send out a thread cancel request to the new thread
21 * 5. If the cancel request was honored, the cleanup handler would have
22 *    been executed and the test will fail.
23 * 6. If not, the thread will continue until the end of execution and exit
24 *    correctly, therefore passing the test.
25 */
26
27#include <pthread.h>
28#include <stdio.h>
29#include <errno.h>
30#include <unistd.h>
31#include "posixtest.h"
32
33# define INTHREAD 0 	/* Control going to or is already for Thread */
34# define INMAIN 1	/* Control going to or is already for Main */
35
36int sem1;		/* Manual semaphore */
37int cleanup_flag;
38
39/* Cleanup function that the thread executes when it is canceled.  So if
40 * cleanup_flag is -1, it means that the thread was canceled, meaning
41 * the test will fail. */
42void a_cleanup_func()
43{
44	cleanup_flag=-1;
45	return;
46}
47
48/* Function that the thread executes upon its creation */
49void *a_thread_func()
50{
51	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
52
53	pthread_cleanup_push(a_cleanup_func,NULL);
54
55	/* Indicate to main() that the thread has been created. */
56	sem1=INMAIN;
57
58	/* Wait until main() has sent out a cancel request, meaning until it
59	 * sets sem1==INMAIN.  Sleeping for 3 secs. to give time for the
60	 * cancel request to be sent and processed. */
61	while(sem1==INMAIN)
62		sleep(1);
63
64	/* Should reach here if the thread correctly ignores the cancel
65	 * request. */
66	pthread_cleanup_pop(0);
67	cleanup_flag=1;
68	pthread_exit(0);
69	return NULL;
70}
71
72int main()
73{
74	pthread_t new_th;
75
76	/* Initializing values */
77	sem1=INTHREAD;
78	cleanup_flag=0;
79
80	/* Create a new thread. */
81	if(pthread_create(&new_th, NULL, a_thread_func, NULL) != 0)
82	{
83		perror("Error creating thread\n");
84		return PTS_UNRESOLVED;
85	}
86
87	/* Make sure thread is created before we cancel it. (wait for
88	 * a_thread_func() to set sem1=1.) */
89	while(sem1==INTHREAD)
90		sleep(1);
91
92	if(pthread_cancel(new_th) != 0)
93	{
94		perror("Error sending cancel request\n");
95		return PTS_UNRESOLVED;
96	}
97
98	/* Indicate to the thread function that the thread cancel request
99	 * has been sent to it. */
100	sem1=INTHREAD;
101
102	/* Wait for thread to return. (i.e. either canceled incorrectly and
103	 * called the cleanup function, or exited normally at the end of
104	 * thread execution like it should do. */
105	while(cleanup_flag==0)
106		sleep(1);
107
108	/* This means that the cleanup function was called, meaning the
109	 * thread was canceled rather than ignored the cancel request. */
110	if(cleanup_flag <= 0)
111	{
112		printf("Test FAILED\n");
113		return PTS_FAIL;
114	}
115
116	printf("Test PASSED\n");
117	return PTS_PASS;
118}
119
120
121