-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathforkResetRWMutexTest.cpp
450 lines (399 loc) · 14.2 KB
/
forkResetRWMutexTest.cpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*******************************************************************************
* Copyright IBM Corp. and others 2014
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include "omrTest.h"
#include "thread_api.h"
#include "threadTestHelp.h"
#include "testHelper.hpp"
extern ThreadTestEnvironment *omrTestEnv;
#define START_DELAY (1 * 1000)
#define TIMEOUT (60 * 1000)
typedef struct testdata_rw_t {
omrthread_monitor_t monitor;
omrthread_rwmutex_t rwmutex;
volatile intptr_t rc;
} testdata_rw_t;
static intptr_t createJoinableThreadNoMacros(omrthread_t *newThread, omrthread_entrypoint_t entryProc, void *entryArg);
static void childForkHelper(int forkCount);
static void siblingWriteStart(int forkCount);
static void siblingWriteChild(testdata_rw_t *, int[2]);
static int siblingWriteMain(void *arg);
static void parentWriteStart(int forkCount);
static void parentWriteChild(testdata_rw_t *, int[2]);
static void childSiblingWriteStart(int forkCount);
static void childSiblingWriteChild(testdata_rw_t *, int[2]);
static int childSiblingWriteMain(void *arg);
static void parentWriteSiblingReadStart(int forkCount);
static void parentWriteSiblingReadChild(testdata_rw_t *, int[2]);
static int parentWriteSiblingReadMain(void *arg);
static void
childForkHelper(int forkCount)
{
while (forkCount > 0) {
omrthread_lib_postForkChild();
omrthread_lib_preFork();
if (0 == fork()) {
forkCount = forkCount - 1;
} else {
exit(0);
}
}
}
TEST(ThreadForkResetTest, SiblingWriteLock)
{
siblingWriteStart(1);
}
static void
siblingWriteStart(int forkCount)
{
omrthread_t t;
testdata_rw_t testdata;
int pipedata[2];
/* Pre-fork */
int pipeRC = pipe(pipedata);
EXPECT_TRUE(0 == pipeRC) << "Failure occurred calling pipe";
omrthread_monitor_init(&testdata.monitor, 0);
omrthread_rwmutex_init(&testdata.rwmutex, 0, "OMR_TEST SiblingWriteLock");
omrthread_monitor_enter(testdata.monitor);
ASSERT_NO_FATAL_FAILURE(createJoinableThread(&t, siblingWriteMain, &testdata));
EXPECT_FALSE(J9THREAD_TIMED_OUT == omrthread_monitor_wait_timed(testdata.monitor, TIMEOUT, 0))
<< "Timed out waiting for sibling thread to enter monitor.";
omrthread_lib_preFork();
if (0 >= forkCount || 0 == fork()) {
childForkHelper(forkCount - 1);
/* Child process post-fork */
siblingWriteChild(&testdata, pipedata);
}
omrthread_lib_postForkParent();
/* Parent process post-fork */
ssize_t readBytes = read(pipedata[0], (int *)&testdata.rc, sizeof(testdata.rc));
EXPECT_TRUE(readBytes == sizeof(testdata.rc)) << "Didn't read back enough from pipe";
EXPECT_TRUE(0 == testdata.rc) << "Failure occurred in child process.";
close(pipedata[0]);
close(pipedata[1]);
EXPECT_TRUE(0 == omrthread_monitor_notify(testdata.monitor))
<< "Notifying monitor failed after fork: parent thread does not own monitor.";
EXPECT_TRUE(0 == omrthread_monitor_exit(testdata.monitor))
<< "Exiting monitor after fork failed: parent thread does not own monitor.";
VERBOSE_JOIN(t, J9THREAD_SUCCESS);
EXPECT_TRUE(0 == omrthread_monitor_destroy(testdata.monitor))
<< "Destroying monitor failed: monitor still in use.";
EXPECT_TRUE(0 == omrthread_rwmutex_destroy(testdata.rwmutex))
<< "Destroying rwmutex failed: monitor still in use.";
EXPECT_TRUE(0 == testdata.rc) << "Failure occurred in sibling thread.";
}
static void
siblingWriteChild(testdata_rw_t *testdata, int pipedata[2])
{
omrthread_lib_postForkChild();
testdata->rc = omrthread_rwmutex_try_enter_write(testdata->rwmutex);
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_exit_write(testdata->rwmutex);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_destroy(testdata->rwmutex);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_monitor_exit(testdata->monitor);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_monitor_destroy(testdata->monitor);
}
J9_IGNORE_RETURNVAL(write(pipedata[1], (int *)&testdata->rc, sizeof(testdata->rc)));
close(pipedata[0]);
close(pipedata[1]);
exit(0);
}
static int
siblingWriteMain(void *arg)
{
testdata_rw_t *testdata = (testdata_rw_t *)arg;
testdata->rc = omrthread_monitor_enter(testdata->monitor);
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_try_enter_write(testdata->rwmutex);
}
/* Notify write lock is complete. */
if (0 == testdata->rc) {
testdata->rc = omrthread_monitor_notify(testdata->monitor);
}
/* Wait until after fork. */
if (0 == testdata->rc) {
testdata->rc = omrthread_monitor_wait_timed(testdata->monitor, TIMEOUT, 0);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_exit_write(testdata->rwmutex);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_monitor_exit(testdata->monitor);
}
return 0;
}
TEST(ThreadForkResetTest, ParentWriteLock)
{
parentWriteStart(1);
}
static void
parentWriteStart(int forkCount)
{
testdata_rw_t testdata;
int pipedata[2];
/* Pre-fork */
int pipeRC = pipe(pipedata);
EXPECT_TRUE(0 == pipeRC) << "Failure occurred calling pipe";
testdata.rc = omrthread_rwmutex_init(&testdata.rwmutex, 0, "OMR_TEST ParentWriteLock");
EXPECT_TRUE(0 == omrthread_rwmutex_try_enter_write(testdata.rwmutex))
<< "Enter write rwmutex failed before fork.";
omrthread_lib_preFork();
if (0 >= forkCount || 0 == fork()) {
childForkHelper(forkCount - 1);
/* Child process post-fork */
parentWriteChild(&testdata, pipedata);
}
omrthread_lib_postForkParent();
/* Parent process post-fork */
ssize_t readBytes = read(pipedata[0], (int *)&testdata.rc, sizeof(testdata.rc));
EXPECT_TRUE(readBytes == sizeof(testdata.rc)) << "Didn't read back enough from pipe";
EXPECT_TRUE(0 == testdata.rc) << "Failure occurred in child process.";
close(pipedata[0]);
close(pipedata[1]);
EXPECT_TRUE(0 == omrthread_rwmutex_exit_write(testdata.rwmutex))
<< "Exit rwmutex failed after fork: monitor still in use.";
EXPECT_TRUE(0 == omrthread_rwmutex_destroy(testdata.rwmutex))
<< "Destroying monitor failed: monitor still in use.";
}
static void
parentWriteChild(testdata_rw_t *testdata, int pipedata[2])
{
omrthread_lib_postForkChild();
testdata->rc = omrthread_rwmutex_try_enter_write(testdata->rwmutex);
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_exit_write(testdata->rwmutex);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_exit_write(testdata->rwmutex);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_destroy(testdata->rwmutex);
}
J9_IGNORE_RETURNVAL(write(pipedata[1], (int *)&testdata->rc, sizeof(testdata->rc)));
close(pipedata[0]);
close(pipedata[1]);
exit(0);
}
TEST(ThreadForkResetTest, ChildSiblingWriteLock)
{
childSiblingWriteStart(1);
}
static void
childSiblingWriteStart(int forkCount)
{
testdata_rw_t testdata;
int pipedata[2];
/* Pre-fork */
int pipeRC = pipe(pipedata);
EXPECT_TRUE(0 == pipeRC) << "Failure occurred calling pipe";
testdata.rc = omrthread_rwmutex_init(&testdata.rwmutex, 0, "OMR_TEST ParentWriteLock");
EXPECT_TRUE(0 == omrthread_rwmutex_try_enter_write(testdata.rwmutex))
<< "Enter write rwmutex failed before fork.";
omrthread_lib_preFork();
if (0 >= forkCount || 0 == fork()) {
childForkHelper(forkCount - 1);
/* Child process post-fork */
childSiblingWriteChild(&testdata, pipedata);
}
omrthread_lib_postForkParent();
/* Parent process post-fork */
ssize_t readBytes = read(pipedata[0], (int *)&testdata.rc, sizeof(testdata.rc));
EXPECT_TRUE(readBytes == sizeof(testdata.rc)) << "Didn't read back enough from pipe";
EXPECT_TRUE(0 == testdata.rc) << "Failure occurred in child process.";
close(pipedata[0]);
close(pipedata[1]);
EXPECT_TRUE(0 == omrthread_rwmutex_exit_write(testdata.rwmutex))
<< "Exit rwmutex failed after fork: monitor still in use.";
EXPECT_TRUE(0 == omrthread_rwmutex_destroy(testdata.rwmutex))
<< "Destroying monitor failed: monitor still in use.";
}
static void
childSiblingWriteChild(testdata_rw_t *testdata, int pipedata[2])
{
omrthread_t t = NULL;
omrthread_lib_postForkChild();
testdata->rc = createJoinableThreadNoMacros(&t, childSiblingWriteMain, testdata);
if (NULL != t) {
testdata->rc = omrthread_join(t);
if (testdata->rc & J9THREAD_ERR_OS_ERRNO_SET) {
omrTestEnv->log(LEVEL_ERROR, "omrthread_join() returned 0x%08x and os_errno=%d\n", (int)testdata->rc, (int)omrthread_get_os_errno());
}
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_exit_write(testdata->rwmutex);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_destroy(testdata->rwmutex);
}
J9_IGNORE_RETURNVAL(write(pipedata[1], (int *)&testdata->rc, sizeof(testdata->rc)));
close(pipedata[0]);
close(pipedata[1]);
exit(0);
}
static int
childSiblingWriteMain(void *arg)
{
testdata_rw_t *testdata = (testdata_rw_t *)arg;
testdata->rc = omrthread_rwmutex_try_enter_write(testdata->rwmutex);
if (J9THREAD_RWMUTEX_WOULDBLOCK == testdata->rc) {
testdata->rc = 0;
} else {
omrthread_rwmutex_exit_write(testdata->rwmutex);
testdata->rc = 1;
}
return 0;
}
/*
* This test seems less than ideal with the sleeps involved.
* Perhaps there is a better way..
*/
TEST(ThreadForkResetTest, ParentWriteSiblingReadLock)
{
parentWriteSiblingReadStart(1);
}
static void
parentWriteSiblingReadStart(int forkCount)
{
omrthread_t t;
testdata_rw_t testdata;
int pipedata[2];
/* Pre-fork */
int pipeRC = pipe(pipedata);
EXPECT_TRUE(0 == pipeRC) << "Failure occurred calling pipe";
omrthread_monitor_init(&testdata.monitor, 0);
omrthread_rwmutex_init(&testdata.rwmutex, 0, "OMR_TEST ParentWriteSiblingReadLock");
omrthread_monitor_enter(testdata.monitor);
omrthread_rwmutex_enter_write(testdata.rwmutex);
ASSERT_NO_FATAL_FAILURE(createJoinableThread(&t, parentWriteSiblingReadMain, &testdata));
EXPECT_FALSE(J9THREAD_TIMED_OUT == omrthread_monitor_wait_timed(testdata.monitor, TIMEOUT, 0))
<< "Timed out waiting for sibling thread to enter monitor.";
EXPECT_TRUE(0 == omrthread_monitor_exit(testdata.monitor))
<< "Exiting monitor after fork failed: parent thread does not own monitor.";
EXPECT_TRUE(0 == omrthread_monitor_destroy(testdata.monitor))
<< "Destroying monitor failed: monitor still in use.";
omrthread_sleep(START_DELAY);
omrthread_lib_preFork();
if (0 >= forkCount || 0 == fork()) {
childForkHelper(forkCount - 1);
/* Child process post-fork */
parentWriteSiblingReadChild(&testdata, pipedata);
}
omrthread_lib_postForkParent();
/* Parent process post-fork */
ssize_t readBytes = read(pipedata[0], (int *)&testdata.rc, sizeof(testdata.rc));
EXPECT_TRUE(readBytes == sizeof(testdata.rc)) << "Didn't read back enough from pipe";
EXPECT_TRUE(0 == testdata.rc) << "Failure occurred in child process.";
close(pipedata[0]);
close(pipedata[1]);
EXPECT_TRUE(0 == omrthread_rwmutex_exit_write(testdata.rwmutex))
<< "Exit write failed after fork.";
VERBOSE_JOIN(t, J9THREAD_SUCCESS);
EXPECT_TRUE(0 == omrthread_rwmutex_destroy(testdata.rwmutex))
<< "Destroying rwmutex failed: monitor still in use.";
EXPECT_TRUE(0 == testdata.rc) << "Failure occurred in sibling thread.";
}
static void
parentWriteSiblingReadChild(testdata_rw_t *testdata, int pipedata[2])
{
omrthread_lib_postForkChild();
testdata->rc = omrthread_rwmutex_exit_write(testdata->rwmutex);
omrthread_sleep(START_DELAY);
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_try_enter_write(testdata->rwmutex);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_exit_write(testdata->rwmutex);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_enter_read(testdata->rwmutex);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_exit_read(testdata->rwmutex);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_destroy(testdata->rwmutex);
}
J9_IGNORE_RETURNVAL(write(pipedata[1], (int *)&testdata->rc, sizeof(testdata->rc)));
close(pipedata[0]);
close(pipedata[1]);
exit(0);
}
static int
parentWriteSiblingReadMain(void *arg)
{
testdata_rw_t *testdata = (testdata_rw_t *)arg;
testdata->rc = omrthread_monitor_enter(testdata->monitor);
/* Notify write lock is complete. */
if (0 == testdata->rc) {
testdata->rc = omrthread_monitor_notify(testdata->monitor);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_monitor_exit(testdata->monitor);
}
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_enter_read(testdata->rwmutex);
}
/* Wait until after fork. */
if (0 == testdata->rc) {
testdata->rc = omrthread_rwmutex_exit_read(testdata->rwmutex);
}
return 0;
}
TEST(ThreadForkResetTest, GrandchildRWLock)
{
siblingWriteStart(2);
//siblingReadStart(2);
parentWriteStart(2);
childSiblingWriteStart(2);
parentWriteSiblingReadStart(2);
}
static intptr_t
createJoinableThreadNoMacros(omrthread_t *newThread, omrthread_entrypoint_t entryProc, void *entryArg)
{
omrthread_attr_t attr = NULL;
intptr_t rc = 0;
rc = omrthread_attr_init(&attr);
if (J9THREAD_SUCCESS == rc) {
rc = omrthread_attr_set_detachstate(&attr, J9THREAD_CREATE_JOINABLE);
}
if (J9THREAD_SUCCESS == rc) {
rc = omrthread_create_ex(newThread, &attr, 0, entryProc, entryArg);
if (J9THREAD_SUCCESS != rc) {
if (rc & J9THREAD_ERR_OS_ERRNO_SET) {
omrTestEnv->log(LEVEL_ERROR, "omrthread_create_ex() returned 0x%08x and os_errno=%d\n", (int)rc, (int)omrthread_get_os_errno());
}
}
}
if (NULL != attr) {
rc = omrthread_attr_destroy(&attr);
}
return rc;
}