author | stefank |
Mon, 25 Nov 2019 12:31:39 +0100 | |
changeset 59249 | 29b0d0b61615 |
parent 59247 | 56bf71d64d51 |
child 59252 | 623722a6aeb9 |
permissions | -rw-r--r-- |
51511
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
1 |
/* |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
4 |
* |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
8 |
* |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
13 |
* accompanied this code). |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
14 |
* |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
18 |
* |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
21 |
* questions. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
22 |
* |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
23 |
*/ |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
24 |
|
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
25 |
#include "precompiled.hpp" |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
26 |
#include "runtime/atomic.hpp" |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
27 |
#include "runtime/orderAccess.hpp" |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
28 |
#include "runtime/os.hpp" |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
29 |
#include "utilities/debug.hpp" |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
30 |
#include "utilities/singleWriterSynchronizer.hpp" |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
31 |
#include "utilities/macros.hpp" |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
32 |
|
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
33 |
SingleWriterSynchronizer::SingleWriterSynchronizer() : |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
34 |
_enter(0), |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
35 |
_exit(), |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
36 |
// The initial value of 1 for _waiting_for puts it on the inactive |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
37 |
// track, so no thread exiting a critical section will match it. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
38 |
_waiting_for(1), |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
39 |
_wakeup() |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
40 |
DEBUG_ONLY(COMMA _writers(0)) |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
41 |
{} |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
42 |
|
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
43 |
// Wait until all threads that entered a critical section before |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
44 |
// synchronization have exited that critical section. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
45 |
void SingleWriterSynchronizer::synchronize() { |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
46 |
// Side-effect in assert balanced by debug-only dec at end. |
59249
29b0d0b61615
8234737: Harmonize parameter order in Atomic - add
stefank
parents:
59247
diff
changeset
|
47 |
assert(Atomic::add(&_writers, 1u) == 1u, "multiple writers"); |
51511
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
48 |
// We don't know anything about the muxing between this invocation |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
49 |
// and invocations in other threads. We must start with the latest |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
50 |
// _enter polarity, else we could clobber the wrong _exit value on |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
51 |
// the first iteration. So fence to ensure everything here follows |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
52 |
// whatever muxing was used. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
53 |
OrderAccess::fence(); |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
54 |
uint value = _enter; |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
55 |
// (1) Determine the old and new exit counters, based on the |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
56 |
// polarity (bit0 value) of the on-entry enter counter. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
57 |
volatile uint* new_ptr = &_exit[(value + 1) & 1]; |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
58 |
// (2) Change the in-use exit counter to the new counter, by adding |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
59 |
// 1 to the enter counter (flipping the polarity), meanwhile |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
60 |
// "simultaneously" initializing the new exit counter to that enter |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
61 |
// value. Note: The new exit counter is not being used by read |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
62 |
// operations until this change of _enter succeeds. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
63 |
uint old; |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
64 |
do { |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
65 |
old = value; |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
66 |
*new_ptr = ++value; |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
67 |
value = Atomic::cmpxchg(value, &_enter, old); |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
68 |
} while (old != value); |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
69 |
// Critical sections entered before we changed the polarity will use |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
70 |
// the old exit counter. Critical sections entered after the change |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
71 |
// will use the new exit counter. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
72 |
volatile uint* old_ptr = &_exit[old & 1]; |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
73 |
assert(old_ptr != new_ptr, "invariant"); |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
74 |
// (3) Inform threads in in-progress critical sections that there is |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
75 |
// a pending synchronize waiting. The thread that completes the |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
76 |
// request (_exit value == old) will signal the _wakeup semaphore to |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
77 |
// allow us to proceed. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
78 |
_waiting_for = old; |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
79 |
// Write of _waiting_for must precede read of _exit and associated |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
80 |
// conditional semaphore wait. If they were re-ordered then a |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
81 |
// critical section exit could miss the wakeup request, failing to |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
82 |
// signal us while we're waiting. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
83 |
OrderAccess::fence(); |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
84 |
// (4) Wait for all the critical sections started before the change |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
85 |
// to complete, e.g. for the value of old_ptr to catch up with old. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
86 |
// Loop because there could be pending wakeups unrelated to this |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
87 |
// synchronize request. |
59247
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
51511
diff
changeset
|
88 |
while (old != Atomic::load_acquire(old_ptr)) { |
51511
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
89 |
_wakeup.wait(); |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
90 |
} |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
91 |
// (5) Drain any pending wakeups. A critical section exit may have |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
92 |
// completed our request and seen our _waiting_for before we checked |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
93 |
// for completion. There are also possible (though rare) spurious |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
94 |
// wakeup signals in the timing gap between changing the _enter |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
95 |
// polarity and setting _waiting_for. Enough of any of those could |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
96 |
// lead to semaphore overflow. This doesn't guarantee no unrelated |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
97 |
// wakeups for the next wait, but prevents unbounded accumulation. |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
98 |
while (_wakeup.trywait()) {} |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
99 |
DEBUG_ONLY(Atomic::dec(&_writers);) |
eb8d5aeabab3
8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff
changeset
|
100 |
} |