2
* The contents of this file are subject to the Mozilla Public
3
* License Version 1.1 (the "License"); you may not use this file
4
* except in compliance with the License. You may obtain a copy of
5
* the License at http://www.mozilla.org/MPL/
7
* Software distributed under the License is distributed on an "AS
8
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9
* implied. See the License for the specific language governing
10
* rights and limitations under the License.
12
* The Original Code is the Netscape Portable Runtime (NSPR).
14
* The Initial Developer of the Original Code is Netscape
15
* Communications Corporation. Portions created by Netscape are
16
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
21
* Alternatively, the contents of this file may be used under the
22
* terms of the GNU General Public License Version 2 or later (the
23
* "GPL"), in which case the provisions of the GPL are applicable
24
* instead of those above. If you wish to allow use of your
25
* version of this file only under the terms of the GPL and not to
26
* allow others to use your version of this file under the MPL,
27
* indicate your decision by deleting the provisions above and
28
* replace them with the notice and other provisions required by
29
* the GPL. If you do not delete the provisions above, a recipient
30
* may use your version of this file under either the MPL or the
39
* Several threads are created to access and modify data arrays using
40
* PRRWLocks for synchronization. Two data arrays, array_A and array_B, are
41
* initialized with random data and a third array, array_C, is initialized
42
* with the sum of the first 2 arrays.
44
* Each one of the threads acquires a read lock to verify that the sum of
45
* the arrays A and B is equal to array C, and acquires a write lock to
46
* consistently update arrays A and B so that their is equal to array C.
55
static void rwtest(void *args);
56
static PRInt32 *array_A,*array_B,*array_C;
57
static void update_array(void);
58
static void check_array(void);
60
typedef struct thread_args {
66
PRFileDesc *errhandle;
68
#define DEFAULT_THREAD_CNT 4
69
#define DEFAULT_LOOP_CNT 100
70
#define TEST_ARRAY_SIZE 100
72
PRIntn main(PRIntn argc, char **argv)
78
PRInt32 thread_cnt = DEFAULT_THREAD_CNT;
79
PRInt32 loop_cnt = DEFAULT_LOOP_CNT;
85
PLOptState *opt = PL_CreateOptState(argc, argv, "dt:c:");
87
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
89
if (PL_OPT_BAD == os) continue;
92
case 'd': /* debug mode */
95
case 't': /* thread count */
96
thread_cnt = atoi(opt->value);
98
case 'c': /* loop count */
99
loop_cnt = atoi(opt->value);
105
PL_DestroyOptState(opt);
107
PR_SetConcurrency(4);
109
output = PR_GetSpecialFD(PR_StandardOutput);
110
errhandle = PR_GetSpecialFD(PR_StandardError);
112
rwlock1 = PR_NewRWLock(0,"Lock 1");
113
if (rwlock1 == NULL) {
114
PR_fprintf(errhandle, "PR_NewRWLock failed - error %d\n",
119
threads = (PRThread**) PR_CALLOC(sizeof(PRThread*) * thread_cnt);
120
params = (thread_args *) PR_CALLOC(sizeof(thread_args) * thread_cnt);
123
* allocate and initialize data arrays
125
array_A =(PRInt32 *) PR_MALLOC(sizeof(PRInt32) * TEST_ARRAY_SIZE);
126
array_B =(PRInt32 *) PR_MALLOC(sizeof(PRInt32) * TEST_ARRAY_SIZE);
127
array_C =(PRInt32 *) PR_MALLOC(sizeof(PRInt32) * TEST_ARRAY_SIZE);
129
for (i=0; i < TEST_ARRAY_SIZE;i++) {
132
array_C[i] = array_A[i] + array_B[i];
136
PR_fprintf(output,"%s: thread_cnt = %d loop_cnt = %d\n", argv[0],
137
thread_cnt, loop_cnt);
138
for(cnt = 0; cnt < thread_cnt; cnt++) {
141
params[cnt].rwlock = rwlock1;
142
params[cnt].loop_cnt = loop_cnt;
145
* create LOCAL and GLOBAL threads alternately
148
scope = PR_LOCAL_THREAD;
150
scope = PR_GLOBAL_THREAD;
152
threads[cnt] = PR_CreateThread(PR_USER_THREAD,
153
rwtest, ¶ms[cnt],
158
if (threads[cnt] == NULL) {
159
PR_fprintf(errhandle, "PR_CreateThread failed - error %d\n",
164
PR_fprintf(output,"%s: created thread = 0x%x\n", argv[0],
168
for(cnt = 0; cnt < thread_cnt; cnt++) {
169
rc = PR_JoinThread(threads[cnt]);
170
PR_ASSERT(rc == PR_SUCCESS);
181
PR_DestroyRWLock(rwlock1);
188
static void rwtest(void *args)
191
thread_args *arg = (thread_args *) args;
194
for (index = 0; index < arg->loop_cnt; index++) {
197
* verify sum, update arrays and verify sum again
200
PR_RWLock_Rlock(arg->rwlock);
202
PR_RWLock_Unlock(arg->rwlock);
204
PR_RWLock_Wlock(arg->rwlock);
206
PR_RWLock_Unlock(arg->rwlock);
208
PR_RWLock_Rlock(arg->rwlock);
210
PR_RWLock_Unlock(arg->rwlock);
214
"Thread[0x%x] lock = 0x%x exiting\n",
215
PR_GetCurrentThread(), arg->rwlock);
219
static void check_array(void)
223
for (i=0; i < TEST_ARRAY_SIZE;i++)
224
if (array_C[i] != (array_A[i] + array_B[i])) {
225
PR_fprintf(output, "Error - data check failed\n");
230
static void update_array(void)
234
for (i=0; i < TEST_ARRAY_SIZE;i++) {