Home | History | Annotate | Download | only in speculative
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  bing.wei.liu REMOVE-THIS AT intel DOT com
      4  * Copyright (c) 2013 Cyril Hrubis <chrubis (at) suse.cz>
      5  *
      6  * This file is licensed under the GPL license.  For the full content
      7  * of this license, see the COPYING file at the top level of this
      8  * source tree.
      9  *
     10  * Test that pthread_key_create()
     11  *
     12  * If successful, the pthread_key_create() function shall store the newly
     13  * created key value at *key and shall return zero.  Otherwise, an error number
     14  * shall be returned to indicate an error:
     15  *
     16  * [EAGAIN] - the system lacked the necessary resources to create another
     17  *            thread_specific data key, or the system imposed limit on the
     18  *            total number of keys per process [PTHREAD_KEYS_MAX] has been
     19  *            exceeded.
     20  *
     21  * [ENOMEM] - insufficient memory exists to create the key.
     22  *
     23  * TESTING [EAGAIN]
     24  *
     25  * Steps:
     26  * 1. Define an array of keys
     27  * 2. Use pthread_key_create() and create those keys
     28  * 3. Verify that you can set and get specific values for those keys without
     29  *    errors.
     30  *
     31  */
     32 
     33 #include <errno.h>
     34 #include <limits.h>
     35 #include <pthread.h>
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <unistd.h>
     39 #include "posixtest.h"
     40 
     41 static pthread_key_t keys[PTHREAD_KEYS_MAX + 1];
     42 
     43 int main(void)
     44 {
     45 	int i, rc;
     46 
     47 	for (i = 0; i <= PTHREAD_KEYS_MAX; i++) {
     48 		rc = pthread_key_create(&keys[i], NULL);
     49 
     50 		if (rc != 0)
     51 			break;
     52 	}
     53 
     54 	if (i == PTHREAD_KEYS_MAX) {
     55 		if (rc == EAGAIN) {
     56 			printf("Test PASSED\n");
     57 			return PTS_PASS;
     58 		} else {
     59 			printf("Expected EAGAIN on exceeding the limit, got: %d\n", rc);
     60 			return PTS_FAIL;
     61 		}
     62 	}
     63 
     64 	if (rc == EAGAIN) {
     65 		printf("EAGAIN returned before the key limit was exceeded\n");
     66 		return PTS_FAIL;
     67 	}
     68 
     69 	printf("Error: pthread_key_create() failed with %d\n", rc);
     70 	return PTS_UNRESOLVED;
     71 }
     72