2-1.c revision ec6edca7aa42b6affd989ef91b5897f96795e40f
12c28215423293e443469a07ae7011135d058b671Garrett Cooper/*
20dc076565f772bb1953209fb69ea150b494aaa40robbiew * Copyright (c) 2002, Intel Corporation. All rights reserved.
30dc076565f772bb1953209fb69ea150b494aaa40robbiew * Created by:  bing.wei.liu REMOVE-THIS AT intel DOT com
40dc076565f772bb1953209fb69ea150b494aaa40robbiew * This file is licensed under the GPL license.  For the full content
52c28215423293e443469a07ae7011135d058b671Garrett Cooper * of this license, see the COPYING file at the top level of this
60dc076565f772bb1953209fb69ea150b494aaa40robbiew * source tree.
70dc076565f772bb1953209fb69ea150b494aaa40robbiew
80dc076565f772bb1953209fb69ea150b494aaa40robbiew * Test that pthread_key_create()
90dc076565f772bb1953209fb69ea150b494aaa40robbiew *
100dc076565f772bb1953209fb69ea150b494aaa40robbiew * Upon key creation, the value NULL shall be associated with the new key in all active threads.
110dc076565f772bb1953209fb69ea150b494aaa40robbiew * Upon thread creation, the value NULL shall be associated with all defined keys in the new
120dc076565f772bb1953209fb69ea150b494aaa40robbiew * thread.
130dc076565f772bb1953209fb69ea150b494aaa40robbiew *
140dc076565f772bb1953209fb69ea150b494aaa40robbiew * Steps:
150dc076565f772bb1953209fb69ea150b494aaa40robbiew * 1. Create a key
160dc076565f772bb1953209fb69ea150b494aaa40robbiew * 2. Verify that the default value is NULL
172c28215423293e443469a07ae7011135d058b671Garrett Cooper *
180dc076565f772bb1953209fb69ea150b494aaa40robbiew */
190dc076565f772bb1953209fb69ea150b494aaa40robbiew
200dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <pthread.h>
210dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdio.h>
220dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdlib.h>
230dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <unistd.h>
240dc076565f772bb1953209fb69ea150b494aaa40robbiew#include "posixtest.h"
250dc076565f772bb1953209fb69ea150b494aaa40robbiew
260dc076565f772bb1953209fb69ea150b494aaa40robbiewint main()
270dc076565f772bb1953209fb69ea150b494aaa40robbiew{
280dc076565f772bb1953209fb69ea150b494aaa40robbiew	pthread_key_t key;
290dc076565f772bb1953209fb69ea150b494aaa40robbiew	void* rc;
300dc076565f772bb1953209fb69ea150b494aaa40robbiew
310dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Verify that the value associated with "key" in a new thread is NULL */
320dc076565f772bb1953209fb69ea150b494aaa40robbiew	rc = pthread_getspecific(key);
33df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	if (rc != NULL)
340dc076565f772bb1953209fb69ea150b494aaa40robbiew	{
350dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf("Test FAILED\n");
360dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_FAIL;
370dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
380dc076565f772bb1953209fb69ea150b494aaa40robbiew
39df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	if (pthread_key_create(&key, NULL) != 0)
400dc076565f772bb1953209fb69ea150b494aaa40robbiew	{
410dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf("Error: pthread_key_create() failed\n");
420dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
430dc076565f772bb1953209fb69ea150b494aaa40robbiew	} else
440dc076565f772bb1953209fb69ea150b494aaa40robbiew	{
450dc076565f772bb1953209fb69ea150b494aaa40robbiew		/* Verify that the value associated with "key" after it is newly created is
460dc076565f772bb1953209fb69ea150b494aaa40robbiew		 * NULL */
470dc076565f772bb1953209fb69ea150b494aaa40robbiew		rc = pthread_getspecific(key);
48df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper		if (rc != NULL)
490dc076565f772bb1953209fb69ea150b494aaa40robbiew		{
500dc076565f772bb1953209fb69ea150b494aaa40robbiew			printf("Test FAILED\n");
510dc076565f772bb1953209fb69ea150b494aaa40robbiew			return PTS_FAIL;
520dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
532c28215423293e443469a07ae7011135d058b671Garrett Cooper
540dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
550dc076565f772bb1953209fb69ea150b494aaa40robbiew
560dc076565f772bb1953209fb69ea150b494aaa40robbiew	printf("Test PASSED\n");
570dc076565f772bb1953209fb69ea150b494aaa40robbiew	return PTS_PASS;
58ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
59