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 33 pthread_key_t key; 34 void *rc1; 35 void *rc2; 36 37 void *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 printf 43 ("Test FAILED: Could not set the value of the key to %d\n", 44 (KEY_VALUE_2)); 45 pthread_exit((void *)PTS_FAIL); 46 return NULL; 47 } 48 49 /* Get the bound value of the key that we just set. */ 50 rc2 = pthread_getspecific(key); 51 52 pthread_exit(0); 53 return NULL; 54 55 } 56 57 int main(void) 58 { 59 pthread_t new_th; 60 61 /* Create the key */ 62 if (pthread_key_create(&key, NULL) != 0) { 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 printf 70 ("Test FAILED: Could not set the value of the key to %d\n", 71 (KEY_VALUE_1)); 72 return PTS_FAIL; 73 } 74 75 /* Create another thread. This thread will also bind a value to the key */ 76 if (pthread_create(&new_th, NULL, a_thread_func, NULL) != 0) { 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 printf 91 ("Test FAILED: Incorrect value bound to key, expected %d, got %ld\n", 92 KEY_VALUE_1, (long)rc1); 93 return PTS_FAIL; 94 } 95 96 if (rc2 != (void *)(KEY_VALUE_2)) { 97 printf 98 ("Test FAILED: Incorrect value bound to key, expected %d, got %ld\n", 99 KEY_VALUE_2, (long)rc2); 100 return PTS_FAIL; 101 } 102 103 printf("Test PASSED\n"); 104 return PTS_PASS; 105 } 106