12c28215423293e443469a07ae7011135d058b671Garrett Cooper/*
20dc076565f772bb1953209fb69ea150b494aaa40robbiew * Copyright (c) 2002, Intel Corporation. All rights reserved.
30dc076565f772bb1953209fb69ea150b494aaa40robbiew * This file is licensed under the GPL license.  For the full content
42c28215423293e443469a07ae7011135d058b671Garrett Cooper * of this license, see the COPYING file at the top level of this
50dc076565f772bb1953209fb69ea150b494aaa40robbiew * source tree.
62c28215423293e443469a07ae7011135d058b671Garrett Cooper *
70dc076565f772bb1953209fb69ea150b494aaa40robbiew * Test pthread_rwlock_init().
80dc076565f772bb1953209fb69ea150b494aaa40robbiew *
90dc076565f772bb1953209fb69ea150b494aaa40robbiew * 	May fail if:
100dc076565f772bb1953209fb69ea150b494aaa40robbiew *	[EBUSY] The implementation has detected an attempt to reinitialize the object
110dc076565f772bb1953209fb69ea150b494aaa40robbiew *	referenced by rwlock, a previously initialized but not yet destroyed read-write
120dc076565f772bb1953209fb69ea150b494aaa40robbiew *	lock.
130dc076565f772bb1953209fb69ea150b494aaa40robbiew *
140dc076565f772bb1953209fb69ea150b494aaa40robbiew * Steps:
150dc076565f772bb1953209fb69ea150b494aaa40robbiew * 1.  Initialize a pthread_rwlock_t object 'rwlock' with pthread_rwlock_init().
160dc076565f772bb1953209fb69ea150b494aaa40robbiew * 2.  Re-initialize it again without destroying it first.
170dc076565f772bb1953209fb69ea150b494aaa40robbiew *
180dc076565f772bb1953209fb69ea150b494aaa40robbiew */
190dc076565f772bb1953209fb69ea150b494aaa40robbiew#define _XOPEN_SOURCE 600
200dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <pthread.h>
210dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdio.h>
220dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdlib.h>
230dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <unistd.h>
240dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <errno.h>
250dc076565f772bb1953209fb69ea150b494aaa40robbiew#include "posixtest.h"
260dc076565f772bb1953209fb69ea150b494aaa40robbiew
274ca2bbdcd3003f3c8df4e6129e9c7b2bd1514f87Cyril Hrubisint main(void)
280dc076565f772bb1953209fb69ea150b494aaa40robbiew{
290dc076565f772bb1953209fb69ea150b494aaa40robbiew
300dc076565f772bb1953209fb69ea150b494aaa40robbiew	static pthread_rwlock_t rwlock;
310dc076565f772bb1953209fb69ea150b494aaa40robbiew	int rc;
320dc076565f772bb1953209fb69ea150b494aaa40robbiew
330dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Initialize the rwlock */
340dc076565f772bb1953209fb69ea150b494aaa40robbiew	rc = pthread_rwlock_init(&rwlock, NULL);
35354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (rc != 0) {
36354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		printf
37354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		    ("Test FAILED: Error at pthread_rwlock_init(), returns %d\n",
38354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		     rc);
390dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_FAIL;
400dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
412c28215423293e443469a07ae7011135d058b671Garrett Cooper
420dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Re-intialize without destroying it first */
430dc076565f772bb1953209fb69ea150b494aaa40robbiew	rc = pthread_rwlock_init(&rwlock, NULL);
442c28215423293e443469a07ae7011135d058b671Garrett Cooper
452c28215423293e443469a07ae7011135d058b671Garrett Cooper	/* Cleanup */
46354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (pthread_rwlock_destroy(&rwlock) != 0) {
470dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf("Error at pthread_rwlock_destroy()\n");
480dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
492c28215423293e443469a07ae7011135d058b671Garrett Cooper	}
500dc076565f772bb1953209fb69ea150b494aaa40robbiew
51354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (rc == EBUSY) {
520dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf("Test PASSED\n");
530dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_PASS;
54354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	} else if (rc == 0) {
55354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		printf
56354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		    ("Test PASSED: Note*: pthread_rwlock_init() returned 0 instead of EBUSY, but standard specifies _may_ fail\n");
570dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_PASS;
58354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	} else {
59354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		printf
60354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		    ("Test FAILED: Error at pthread_rwlock_init(), should return 0 or EBUSY, but returns %d\n",
61354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		     rc);
620dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_FAIL;
630dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
642c28215423293e443469a07ae7011135d058b671Garrett Cooper
65ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
66