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_setspecific()
9 *
10 * shall acssociate a thread-specific value with a key obtained via a previouse call to
11 * pthread_key_create.  Different threads may bind different values to the same key.
12 * Calling pthread_setspecific with a key value not obtiained from pthread_key_create of after
13 * the key has been deleted with pthread_key_delete is undefined.
14 *
15 * Steps:
16 * 1.  Create a key
17 * 2.  Bind a value from the main thread to this key
18 * 3.  Create a thread and bind another value to this key
19 * 4.  Compare the values bound to the key between the main thread and the newly created thread,
20 *     they should be different and pertaining to what each thread set as the value.
21 *
22 */
23
24#include <pthread.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include "posixtest.h"
29
30#define KEY_VALUE_1 100
31#define KEY_VALUE_2 200
32
33pthread_key_t key;
34void* rc1;
35void* rc2;
36
37void *a_thread_func()
38{
39	/* Bind a value to key for this thread (this will be different from the value
40	 * that we bind for the main thread) */
41	if(pthread_setspecific(key, (void *)(KEY_VALUE_2)) != 0)
42	{
43		printf("Test FAILED: Could not set the value of the key to %d\n", (KEY_VALUE_2));
44		pthread_exit((void*)PTS_FAIL);
45		return NULL;
46	}
47
48	/* Get the bound value of the key that we just set. */
49	rc2 = pthread_getspecific(key);
50
51	pthread_exit(0);
52	return NULL;
53
54}
55
56int main()
57{
58	pthread_t new_th;
59
60	/* Create the key */
61	if(pthread_key_create(&key, NULL) != 0)
62	{
63		printf("Error: pthread_key_create() failed\n");
64		return PTS_UNRESOLVED;
65	}
66
67	/* Bind a value for this main thread */
68	if(pthread_setspecific(key, (void *)(KEY_VALUE_1)) != 0)
69	{
70		printf("Test FAILED: Could not set the value of the key to %d\n", (KEY_VALUE_1));
71		return PTS_FAIL;
72	}
73
74	/* Create another thread.  This thread will also bind a value to the key */
75	if(pthread_create(&new_th, NULL, a_thread_func, NULL) != 0)
76	{
77		printf("Error: in pthread_create()\n");
78		return PTS_UNRESOLVED;
79	}
80
81	/* Wait for thread to end execution */
82	pthread_join(new_th, NULL);
83
84	/* Get the value associated for the key in this main thread */
85	rc1 = pthread_getspecific(key);
86
87	/* Compare this value with the value associated for the key in the newly created
88	 * thread, they should be different. */
89	if(rc1 != (void *)(KEY_VALUE_1))
90	{
91		printf("Test FAILED: Incorrect value bound to key, expected %d, got %ld\n", KEY_VALUE_1, (long)rc1);
92		return PTS_FAIL;
93	}
94
95	if(rc2 != (void *)(KEY_VALUE_2))
96	{
97		printf("Test FAILED: Incorrect value bound to key, expected %d, got %ld\n", KEY_VALUE_2, (long)rc2);
98		return PTS_FAIL;
99	}
100
101	printf("Test PASSED\n");
102	return PTS_PASS;
103}
104