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 *  void pthread_cleanup_push(void (*routine) (void*), void *arg);
9 *
10 * Shall push the specified cancelation cleanup handler routine onto the calling thread's
11 * cancelation cleanup stack. The cancelation cleanup handler shall be popped from the
12 * cancelation cleanup stack and invoked with the argument arg when:
13 *
14 * (a)- The thread exits (calls pthread_exit())
15 * (b)- The thread acts upon a cancelation request
16 * (c)- the thread calls pthread_cleanup_pop() with a non-zero execution argument
17 *
18 *  Testing (c)
19 *
20 * STEPS:
21 * 1. Create a thread
22 * 2. The thread will push a cleanup handler routine, then pop the cleanup handler routine.
23 * 3. Verify that the cleanup handler was called.
24 *
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 CLEANUP_NOTCALLED 0
34# define CLEANUP_CALLED 1
35
36int cleanup_flag;
37
38/* Cleanup handler */
39void a_cleanup_func(void *flag_val)
40{
41	cleanup_flag = (long)flag_val;
42	return;
43}
44
45/* Function that the thread executes upon its creation */
46void *a_thread_func()
47{
48	pthread_cleanup_push(a_cleanup_func, (void*) CLEANUP_CALLED);
49	pthread_cleanup_pop(1);
50
51	pthread_exit(0);
52	return NULL;
53}
54
55int main()
56{
57	pthread_t new_th;
58
59	/* Initializing values */
60	cleanup_flag = CLEANUP_NOTCALLED;
61
62	/* Create a new thread. */
63	if(pthread_create(&new_th, NULL, a_thread_func, NULL) != 0)
64	{
65		perror("Error creating thread\n");
66		return PTS_UNRESOLVED;
67	}
68
69	/* Wait for thread to end execution */
70	if(pthread_join(new_th, NULL) != 0)
71	{
72		perror("Error in pthread_join()\n");
73		return PTS_UNRESOLVED;
74	}
75
76	/* Check to verify that the cleanup handler was called */
77	if(cleanup_flag != CLEANUP_CALLED)
78	{
79		printf("Test FAILED: Cleanup handler not called\n");
80		return PTS_FAIL;
81	}
82
83	printf("Test PASSED\n");
84	return PTS_PASS;
85}
86
87
88