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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/* 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/. */
/*
* test_mutex2.c
*
* Tests multi-threaded functionality of Mutex
*
*/
#include "testutil.h"
#include "testutil_nss.h"
static int box1 = 0, box2 = 0, box3 = 0;
static PKIX_PL_Mutex *mutex;
static PRCondVar *cv;
static void *plContext = NULL;
static void
consumer(/* ARGSUSED */ void *arg)
{
PRStatus status = PR_SUCCESS;
PKIX_Error *errorResult;
int i = 0;
for (i = 0; i < 5; i++) {
(void)PKIX_PL_Mutex_Lock(mutex, plContext);
while (((box1 == 0) ||
(box2 == 0) ||
(box3 == 0)) &&
(status == PR_SUCCESS))
status = PR_WaitCondVar(cv, PR_INTERVAL_NO_TIMEOUT);
(void)printf("\tConsumer got Box1 = %d ", box1);
box1 = 0;
(void)printf("Box2 = %d ", box2);
box2 = 0;
(void)printf("Box3 = %d\n", box3);
box3 = 0;
status = PR_NotifyAllCondVar(cv);
if (status == PR_FAILURE)
(void)printf("Consumer error while notifying condvar\n");
errorResult = PKIX_PL_Mutex_Unlock(mutex, plContext);
if (errorResult)
testError("PKIX_PL_Mutex_Unlock failed");
}
(void)printf("Consumer exiting...\n");
}
static void
producer(void *arg)
{
PRStatus status = PR_SUCCESS;
int value = *(int *)arg;
int i = 0;
int *box;
PKIX_Error *errorResult;
if (value == 10)
box = &box1;
else if (value == 20)
box = &box2;
else if (value == 30)
box = &box3;
for (i = 0; i < 5; i++) {
(void)PKIX_PL_Mutex_Lock(mutex, plContext);
while ((*box != 0) && (status == PR_SUCCESS))
status = PR_WaitCondVar(cv, PR_INTERVAL_NO_TIMEOUT);
*box = i + 1;
(void)printf("\tProducer %d put value: %d\n", value, *box);
status = PR_NotifyAllCondVar(cv);
if (status == PR_FAILURE)
(void)printf("Producer %d error while notifying condvar\n",
value);
errorResult = PKIX_PL_Mutex_Unlock(mutex, plContext);
if (errorResult)
testError("PKIX_PL_Mutex_Unlock failed");
}
}
int
test_mutex2(int argc, char *argv[])
{
PRThread *consThread, *prodThread, *prodThread2, *prodThread3;
int x = 10, y = 20, z = 30;
PKIX_UInt32 actualMinorVersion;
PKIX_UInt32 j = 0;
PKIX_TEST_STD_VARS();
startTests("Mutex and Threads");
PKIX_TEST_EXPECT_NO_ERROR(
PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
(void)printf("Attempting to create new mutex...\n");
subTest("Mutex Creation");
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Mutex_Create(&mutex, plContext));
cv = PR_NewCondVar(*(PRLock **)mutex);
subTest("Starting consumer thread");
consThread = PR_CreateThread(PR_USER_THREAD,
consumer,
NULL,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_JOINABLE_THREAD,
0);
subTest("Starting producer thread 1");
prodThread = PR_CreateThread(PR_USER_THREAD,
producer,
&x,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_JOINABLE_THREAD,
0);
subTest("Starting producer thread 2");
prodThread2 = PR_CreateThread(PR_USER_THREAD,
producer,
&y,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_JOINABLE_THREAD,
0);
subTest("Starting producer thread 3");
prodThread3 = PR_CreateThread(PR_USER_THREAD,
producer,
&z,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_JOINABLE_THREAD,
0);
PR_JoinThread(consThread);
(void)PR_DestroyCondVar(cv);
PKIX_TEST_DECREF_BC(mutex);
/*
* Note: we should also be freeing each thread's stack, but we
* don't have access to the prodThread->stack variable (since
* it is not exported). As a result, we have 120 bytes of memory
* leakage.
*/
PR_Free(prodThread);
PR_Free(prodThread2);
PR_Free(prodThread3);
cleanup:
PKIX_Shutdown(plContext);
PKIX_TEST_RETURN();
endTests("Mutex and Threads");
return (0);
}
|