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 (a)
19 *
20 * STEPS:
21 * 1. Create a thread
22 * 2. The thread will push a cleanup handler routine and then exit befor the cleanup_pop
23 *    routine is reached.
24 * 3. Verify that the cleanup handler was called.
25 *
26 */
27
28#include <pthread.h>
29#include <stdio.h>
30#include <errno.h>
31#include <unistd.h>
32#include "posixtest.h"
33
34# define CLEANUP_NOTCALLED 0
35# define CLEANUP_CALLED 1
36
37int cleanup_flag;
38
39/* Cleanup handler */
40void a_cleanup_func(void *flag_val)
41{
42	cleanup_flag = (long)flag_val;
43	return;
44}
45
46/* Function that the thread executes upon its creation */
47void *a_thread_func()
48{
49	pthread_cleanup_push(a_cleanup_func, (void*) CLEANUP_CALLED);
50
51	pthread_exit(0);
52        pthread_cleanup_pop(0);
53	return NULL;
54}
55
56int main()
57{
58	pthread_t new_th;
59
60	/* Initializing values */
61	cleanup_flag = CLEANUP_NOTCALLED;
62
63	/* Create a new thread. */
64	if(pthread_create(&new_th, NULL, a_thread_func, NULL) != 0)
65	{
66		perror("Error creating thread\n");
67		return PTS_UNRESOLVED;
68	}
69
70	/* Wait for thread to end execution */
71	if(pthread_join(new_th, NULL) != 0)
72	{
73		perror("Error in pthread_join()\n");
74		return PTS_UNRESOLVED;
75	}
76
77	/* Check to verify that the cleanup handler was called */
78	if(cleanup_flag != CLEANUP_CALLED)
79	{
80		printf("Test FAILED: Cleanup handler not called upon exit\n");
81		return PTS_FAIL;
82	}
83
84	printf("Test PASSED\n");
85	return PTS_PASS;
86}
87
88
89