1/*
2 * Copyright (c) 2004, QUALCOMM Inc. All rights reserved.
3 * Created by:  abisain REMOVE-THIS AT qualcomm 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_exit()
9 *
10 * Any destructors for thread_specific data will be called when
11 * pthread_exit is called
12 *
13 * Steps:
14 * 1. Create a new thread.
15 * 2. Create thread specific data, with a destructor in the thread
16 * 3. Call pthread_exit in the thread.
17 * 4. Make sure that the destructor was called
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 TEST "3-1"
28#define FUNCTION "pthread_exit"
29#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
30
31/* Flag to indicate that the destructor was called */
32int cleanup_flag = 0;
33
34void destructor(void *tmp)
35{
36	cleanup_flag = 1;
37}
38
39/* Thread's function. */
40void *a_thread_func(void *tmp)
41{
42	pthread_key_t key;
43	int value = 1;
44	int rc = 0;
45
46	rc = pthread_key_create(&key, destructor);
47	if (rc != 0) {
48		printf(ERROR_PREFIX "pthread_key_create\n");
49		exit(PTS_UNRESOLVED);
50	}
51
52	rc = pthread_setspecific(key, &value);
53	if (rc != 0) {
54		printf(ERROR_PREFIX "pthread_setspecific\n");
55		exit(PTS_UNRESOLVED);
56	}
57
58	pthread_exit(0);
59	return NULL;
60}
61
62int main(void)
63{
64	pthread_t new_th;
65	int rc = 0;
66
67	/* Create a new thread. */
68	rc = pthread_create(&new_th, NULL, a_thread_func, NULL);
69	if (rc != 0) {
70		printf(ERROR_PREFIX "pthread_create\n");
71		return PTS_UNRESOLVED;
72	}
73
74	/* Wait for thread to return */
75	rc = pthread_join(new_th, NULL);
76	if (rc != 0) {
77		printf(ERROR_PREFIX "pthread_join\n");
78		return PTS_UNRESOLVED;
79	}
80
81	if (cleanup_flag != 1) {
82		printf("Test FAIL: Destructor was not called.\n");
83		return PTS_FAIL;
84	}
85
86	printf("Test PASSED\n");
87	return PTS_PASS;
88
89}
90