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_create() 9 * 10 * shall create a thread-specific data key visible to all threaads in the process. Key values 11 * provided by pthread_key_create() are opaque objects used to locate thread-specific data. 12 * Although the same key value may be used by different threads, the values bound to the key 13 * by pthread_setspecific() are maintained on a per-thread basis and persist for the life of 14 * the calling thread. 15 * 16 * Steps: 17 * 1. Define an array of keys 18 * 2. Use pthread_key_create() and create those keys 19 * 3. Verify that you can set and get specific values for those keys without errors. 20 * 21 */ 22 23 #include <pthread.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <unistd.h> 27 #include "posixtest.h" 28 29 #define NUM_OF_KEYS 10 30 #define KEY_VALUE 0 31 32 pthread_key_t keys[NUM_OF_KEYS]; 33 34 int main(void) 35 { 36 int i; 37 void *rc; 38 39 for (i = 0; i < NUM_OF_KEYS; i++) { 40 if (pthread_key_create(&keys[i], NULL) != 0) { 41 printf("Error: pthread_key_create() failed\n"); 42 return PTS_UNRESOLVED; 43 } else { 44 if (pthread_setspecific 45 (keys[i], (void *)(long)(i + KEY_VALUE)) != 0) { 46 printf("Error: pthread_setspecific() failed\n"); 47 return PTS_UNRESOLVED; 48 } 49 50 } 51 } 52 53 for (i = 0; i < NUM_OF_KEYS; ++i) { 54 rc = pthread_getspecific(keys[i]); 55 if (rc != (void *)(long)(i + KEY_VALUE)) { 56 printf 57 ("Test FAILED: Did not return correct value of thread-specific key, expected %ld, but got %ld\n", 58 (long)(i + KEY_VALUE), (long)rc); 59 return PTS_FAIL; 60 } else { 61 if (pthread_key_delete(keys[i]) != 0) { 62 printf("Error: pthread_key_delete() failed\n"); 63 return PTS_UNRESOLVED; 64 } 65 } 66 } 67 68 printf("Test PASSED\n"); 69 return PTS_PASS; 70 } 71