1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* RWLock rank tests
*/
#include "nspr.h"
#include "plgetopt.h"
static int _debug_on;
static PRRWLock *rwlock0;
static PRRWLock *rwlock1;
static PRRWLock *rwlock2;
static void rwtest(void *args)
{
PR_RWLock_Rlock(rwlock1);
PR_RWLock_Unlock(rwlock1);
PR_RWLock_Rlock(rwlock1);
PR_RWLock_Unlock(rwlock1);
/* Test correct lock rank. */
PR_RWLock_Rlock(rwlock1);
PR_RWLock_Rlock(rwlock2);
PR_RWLock_Unlock(rwlock2);
PR_RWLock_Unlock(rwlock1);
PR_RWLock_Rlock(rwlock1);
PR_RWLock_Rlock(rwlock2);
PR_RWLock_Unlock(rwlock1);
PR_RWLock_Unlock(rwlock2);
PR_RWLock_Rlock(rwlock1);
PR_RWLock_Rlock(rwlock0);
PR_RWLock_Rlock(rwlock2);
PR_RWLock_Unlock(rwlock2);
PR_RWLock_Unlock(rwlock0);
PR_RWLock_Unlock(rwlock1);
#if 0
/* Test incorrect lock rank. */
PR_RWLock_Rlock(rwlock2);
PR_RWLock_Rlock(rwlock1);
PR_RWLock_Unlock(rwlock1);
PR_RWLock_Unlock(rwlock2);
PR_RWLock_Rlock(rwlock2);
PR_RWLock_Rlock(rwlock0);
PR_RWLock_Rlock(rwlock1);
PR_RWLock_Unlock(rwlock1);
PR_RWLock_Unlock(rwlock0);
PR_RWLock_Unlock(rwlock2);
#endif
}
int main(int argc, char **argv)
{
PRStatus rc;
PRThread *thread;
PLOptStatus os;
PLOptState *opt = PL_CreateOptState(argc, argv, "d");
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
if (PL_OPT_BAD == os) continue;
switch (opt->option) {
case 'd': /* debug mode */
_debug_on = 1;
break;
default:
break;
}
}
PL_DestroyOptState(opt);
rwlock0 = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "Lock 0");
if (rwlock0 == NULL) {
fprintf(stderr, "PR_NewRWLock failed - error %d\n",
(int)PR_GetError());
return 1;
}
rwlock1 = PR_NewRWLock(1, "Lock 1");
if (rwlock1 == NULL) {
fprintf(stderr, "PR_NewRWLock failed - error %d\n",
(int)PR_GetError());
return 1;
}
rwlock2 = PR_NewRWLock(2, "Lock 2");
if (rwlock2 == NULL) {
fprintf(stderr, "PR_NewRWLock failed - error %d\n",
(int)PR_GetError());
return 1;
}
thread = PR_CreateThread(PR_USER_THREAD, rwtest, NULL, PR_PRIORITY_NORMAL,
PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
if (thread == NULL) {
fprintf(stderr, "PR_CreateThread failed - error %d\n",
(int)PR_GetError());
PR_ProcessExit(2);
}
if (_debug_on) {
printf("%s: created thread = %p\n", argv[0], thread);
}
rc = PR_JoinThread(thread);
PR_ASSERT(rc == PR_SUCCESS);
PR_DestroyRWLock(rwlock0);
rwlock0 = NULL;
PR_DestroyRWLock(rwlock1);
rwlock1 = NULL;
PR_DestroyRWLock(rwlock2);
rwlock2 = NULL;
printf("PASS\n");
return 0;
}
|