1/*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Created by:  bing.wei.liu 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_key_delete()
9 *
10  pthread_key_delete function shall be callable from within destructor functions.  No
11  destructor functions shall be invoked by pthread_key_delete.  Any destructor function
12  that may have been associated with 'key' shall no longer be called up thread exit.
13 *
14 * Steps:
15 * 1. Create a key with a destructor function associated with it
16 * 2. In the destructor function, call pthread_key_delete
17 * 3. Verify that this can be done with no errors
18 *
19 */
20
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include "posixtest.h"
26
27#define KEY_VALUE 1000
28
29pthread_key_t key;
30int dest_cnt;
31
32/* Destructor funciton */
33void dest_func(void *p)
34{
35	dest_cnt++;
36	/* Delete the key and check if an error has occured */
37	if(pthread_key_delete(key) != 0)
38	{
39		dest_cnt++;
40	}
41}
42
43/* Thread function */
44void *a_thread_func()
45{
46
47	/* Set the value of the key to a value */
48	if(pthread_setspecific(key, (void *)(KEY_VALUE)) != 0)
49	{
50		printf("Error: pthread_setspecific() failed\n");
51		pthread_exit((void*) PTS_UNRESOLVED);
52	}
53
54	/* The thread ends here, the destructor for the key should now be called after this */
55	pthread_exit(0);
56	return NULL;
57}
58
59int main()
60{
61	pthread_t new_th;
62
63	/* Inialize the destructor flag */
64	dest_cnt = 0;
65
66	/* Create a key with a destructor function */
67	if(pthread_key_create(&key, dest_func) != 0)
68	{
69		printf("Error: pthread_key_create() failed\n");
70		pthread_exit((void*) PTS_UNRESOLVED);
71	}
72
73	/* Create a thread */
74	if(pthread_create(&new_th, NULL, a_thread_func, NULL) != 0)
75	{
76		perror("Error creating thread\n");
77		return PTS_UNRESOLVED;
78	}
79
80	/* Wait for the thread's return */
81	if(pthread_join(new_th, NULL) != 0)
82	{
83		perror("Error in pthread_join()\n");
84		return PTS_UNRESOLVED;
85	}
86
87	/* Check if the destructor was called and if the pthread_key_delete function was
88	 * called successfully */
89	if(dest_cnt != 1)
90	{
91		if(dest_cnt == 0)
92		{
93			printf("Error calling the key destructor function\n");
94			return PTS_UNRESOLVED;
95		} else
96		{
97			printf("Test FAILED: pthread_key_delete failed to be called from the destructor function\n");
98			return PTS_FAIL;
99		}
100	}
101
102	printf("Test PASSED\n");
103	return PTS_PASS;
104}
105