aboutsummaryrefslogtreecommitdiff
path: root/testcases/open_posix_testsuite/conformance/interfaces/pthread_exit/3-1.c
blob: 1f1a07b80d49ea0deb984ec4b2d9a3b22e1dfce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (c) 2004, QUALCOMM Inc. All rights reserved.
 * Created by:  abisain REMOVE-THIS AT qualcomm DOT com
 * This file is licensed under the GPL license.  For the full content
 * of this license, see the COPYING file at the top level of this
 * source tree.

 * Test pthread_exit()
 *
 * Any destructors for thread_specific data will be called when
 * pthread_exit is called
 *
 * Steps:
 * 1. Create a new thread.
 * 2. Create thread specific data, with a destructor in the thread
 * 3. Call pthread_exit in the thread.
 * 4. Make sure that the destructor was called
 *
 */

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "posixtest.h"

#define TEST "3-1"
#define FUNCTION "pthread_exit"
#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "

/* Flag to indicate that the destructor was called */
int cleanup_flag = 0;

void destructor(void *tmp)
{
	cleanup_flag = 1;
}

/* Thread's function. */
void *a_thread_func(void *tmp)
{
	pthread_key_t key;
	int value = 1;
	int rc = 0;

	rc = pthread_key_create(&key, destructor);
	if (rc != 0) {
		printf(ERROR_PREFIX "pthread_key_create\n");
		exit(PTS_UNRESOLVED);
	}

	rc = pthread_setspecific(key, &value);
	if (rc != 0) {
		printf(ERROR_PREFIX "pthread_setspecific\n");
		exit(PTS_UNRESOLVED);
	}

	pthread_exit(0);
	return NULL;
}

int main(void)
{
	pthread_t new_th;
	int rc = 0;

	/* Create a new thread. */
	rc = pthread_create(&new_th, NULL, a_thread_func, NULL);
	if (rc != 0) {
		printf(ERROR_PREFIX "pthread_create\n");
		return PTS_UNRESOLVED;
	}

	/* Wait for thread to return */
	rc = pthread_join(new_th, NULL);
	if (rc != 0) {
		printf(ERROR_PREFIX "pthread_join\n");
		return PTS_UNRESOLVED;
	}

	if (cleanup_flag != 1) {
		printf("Test FAIL: Destructor was not called.\n");
		return PTS_FAIL;
	}

	printf("Test PASSED\n");
	return PTS_PASS;

}