1/*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * This file is licensed under the GPL license.  For the full content
4 * of this license, see the COPYING file at the top level of this
5 * source tree.
6 *
7 * Test pthread_rwlock_init().
8 *
9 * 	Once initialized, the lock can be used any number of times without being
10 *	reinitialized.
11 *
12 * Steps:
13 * 1.  Initialize a pthread_rwlock_t object 'rwlock' with pthread_rwlock_init()
14 * 2.  Loop for COUNT time: lock for reading, unlock, lock for writing, unlock;
15 */
16
17#define _XOPEN_SOURCE 600
18
19#include <pthread.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
23#include <errno.h>
24#include "posixtest.h"
25
26#define COUNT 1000
27
28static pthread_rwlock_t rwlock;
29
30int main(void)
31{
32	int cnt = 0;
33	pthread_rwlockattr_t rwlockattr;
34
35	if (pthread_rwlockattr_init(&rwlockattr) != 0) {
36		printf("Error at pthread_rwlockattr_init()\n");
37		return PTS_UNRESOLVED;
38	}
39
40	if (pthread_rwlock_init(&rwlock, &rwlockattr) != 0) {
41		printf("Test FAILED: Error in pthread_rwlock_init()\n");
42		return PTS_FAIL;
43	}
44
45	while (cnt++ < COUNT) {
46		if (pthread_rwlock_rdlock(&rwlock) != 0) {
47			printf
48			    ("Test FAILED: cannot get read lock on %dth loop\n",
49			     cnt);
50			return PTS_FAIL;
51		}
52
53		if (pthread_rwlock_unlock(&rwlock) != 0) {
54			printf
55			    ("Test FAILED: cannot release read lock on %dth loop\n",
56			     cnt);
57			return PTS_FAIL;
58		}
59
60		if (pthread_rwlock_wrlock(&rwlock) != 0) {
61			printf
62			    ("Test FAILED: cannot get write lock on %dth loop\n",
63			     cnt);
64			return PTS_FAIL;
65		}
66
67		if (pthread_rwlock_unlock(&rwlock) != 0) {
68			printf
69			    ("Test FAILED: cannot release write lock on %dth loop\n",
70			     cnt);
71			return PTS_FAIL;
72		}
73	}
74
75	if (pthread_rwlock_destroy(&rwlock) != 0) {
76		printf("Error at pthread_rwlockattr_destroy()\n");
77		return PTS_UNRESOLVED;
78	}
79
80	if (pthread_rwlockattr_destroy(&rwlockattr) != 0) {
81		printf("Error at pthread_rwlockattr_destroy()\n");
82		return PTS_UNRESOLVED;
83	}
84
85	printf("Test PASSED\n");
86	return PTS_PASS;
87}
88