author | stefank |
Mon, 25 Nov 2019 12:22:13 +0100 | |
changeset 59247 | 56bf71d64d51 |
parent 52332 | d2a3503c72f7 |
permissions | -rw-r--r-- |
49800 | 1 |
/* |
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
#include "precompiled.hpp" |
|
25 |
#include "runtime/atomic.hpp" |
|
26 |
#include "runtime/os.hpp" |
|
27 |
#include "utilities/globalCounter.hpp" |
|
28 |
#include "utilities/globalCounter.inline.hpp" |
|
51179
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
50429
diff
changeset
|
29 |
#include "threadHelper.inline.hpp" |
49800 | 30 |
|
49831 | 31 |
#define GOOD_VALUE 1337 |
32 |
#define BAD_VALUE 4711 |
|
49800 | 33 |
|
34 |
struct TestData { |
|
35 |
long test_value; |
|
36 |
}; |
|
37 |
||
38 |
class RCUReaderThread : public JavaTestThread { |
|
39 |
public: |
|
40 |
static volatile bool _exit; |
|
41 |
volatile TestData** _test; |
|
42 |
Semaphore* _wrt_start; |
|
43 |
RCUReaderThread(Semaphore* post, volatile TestData** test, Semaphore* wrt_start) |
|
44 |
: JavaTestThread(post), _test(test), _wrt_start(wrt_start) {}; |
|
45 |
virtual ~RCUReaderThread(){} |
|
46 |
void main_run() { |
|
47 |
_wrt_start->signal(); |
|
48 |
while (!_exit) { |
|
52332
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
51179
diff
changeset
|
49 |
GlobalCounter::CSContext cs_context = GlobalCounter::critical_section_begin(this); |
59247
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
52332
diff
changeset
|
50 |
volatile TestData* test = Atomic::load_acquire(_test); |
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
52332
diff
changeset
|
51 |
long value = Atomic::load_acquire(&test->test_value); |
49831 | 52 |
ASSERT_EQ(value, GOOD_VALUE); |
52332
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
51179
diff
changeset
|
53 |
GlobalCounter::critical_section_end(this, cs_context); |
49800 | 54 |
{ |
55 |
GlobalCounter::CriticalSection cs(this); |
|
59247
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
52332
diff
changeset
|
56 |
volatile TestData* test = Atomic::load_acquire(_test); |
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
52332
diff
changeset
|
57 |
long value = Atomic::load_acquire(&test->test_value); |
49831 | 58 |
ASSERT_EQ(value, GOOD_VALUE); |
49800 | 59 |
} |
60 |
} |
|
61 |
} |
|
62 |
}; |
|
63 |
||
64 |
volatile bool RCUReaderThread::_exit = false; |
|
65 |
||
66 |
class RCUWriterThread : public JavaTestThread { |
|
67 |
public: |
|
68 |
RCUWriterThread(Semaphore* post) : JavaTestThread(post) { |
|
69 |
}; |
|
70 |
virtual ~RCUWriterThread(){} |
|
71 |
void main_run() { |
|
72 |
static const int NUMBER_OF_READERS = 4; |
|
73 |
Semaphore post; |
|
74 |
Semaphore wrt_start; |
|
75 |
volatile TestData* test = NULL; |
|
76 |
||
77 |
RCUReaderThread* reader1 = new RCUReaderThread(&post, &test, &wrt_start); |
|
78 |
RCUReaderThread* reader2 = new RCUReaderThread(&post, &test, &wrt_start); |
|
79 |
RCUReaderThread* reader3 = new RCUReaderThread(&post, &test, &wrt_start); |
|
80 |
RCUReaderThread* reader4 = new RCUReaderThread(&post, &test, &wrt_start); |
|
81 |
||
82 |
TestData* tmp = new TestData(); |
|
49831 | 83 |
tmp->test_value = GOOD_VALUE; |
59247
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
52332
diff
changeset
|
84 |
Atomic::release_store_fence(&test, tmp); |
49800 | 85 |
|
86 |
reader1->doit(); |
|
87 |
reader2->doit(); |
|
88 |
reader3->doit(); |
|
89 |
reader4->doit(); |
|
90 |
||
91 |
int nw = NUMBER_OF_READERS; |
|
92 |
while (nw > 0) { |
|
93 |
wrt_start.wait(); |
|
94 |
--nw; |
|
95 |
} |
|
96 |
jlong stop_ms = os::javaTimeMillis() + 1000; // 1 seconds max test time |
|
97 |
for (int i = 0; i < 100000 && stop_ms > os::javaTimeMillis(); i++) { |
|
98 |
volatile TestData* free_tmp = test; |
|
99 |
tmp = new TestData(); |
|
49831 | 100 |
tmp->test_value = GOOD_VALUE; |
59247
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
52332
diff
changeset
|
101 |
Atomic::release_store(&test, tmp); |
49800 | 102 |
GlobalCounter::write_synchronize(); |
49831 | 103 |
free_tmp->test_value = BAD_VALUE; |
49800 | 104 |
delete free_tmp; |
105 |
} |
|
106 |
RCUReaderThread::_exit = true; |
|
107 |
for (int i = 0; i < NUMBER_OF_READERS; i++) { |
|
108 |
post.wait(); |
|
109 |
} |
|
110 |
} |
|
111 |
}; |
|
112 |
||
113 |
TEST_VM(GlobalCounter, critical_section) { |
|
114 |
RCUReaderThread::_exit = false; |
|
115 |
mt_test_doer<RCUWriterThread>(); |
|
116 |
} |