author | jprovino |
Tue, 02 Aug 2016 16:39:33 -0400 | |
changeset 40328 | a2851f5f1cf6 |
parent 37188 | 390e3a2e7cee |
permissions | -rw-r--r-- |
24094 | 1 |
/* |
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
24094 | 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 |
||
25 |
#include "precompiled.hpp" |
|
30764 | 26 |
#include "gc/g1/suspendibleThreadSet.hpp" |
24094 | 27 |
#include "runtime/mutexLocker.hpp" |
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
28 |
#include "runtime/semaphore.hpp" |
24094 | 29 |
#include "runtime/thread.inline.hpp" |
30 |
||
31 |
uint SuspendibleThreadSet::_nthreads = 0; |
|
32 |
uint SuspendibleThreadSet::_nthreads_stopped = 0; |
|
33 |
bool SuspendibleThreadSet::_suspend_all = false; |
|
34 |
double SuspendibleThreadSet::_suspend_all_start = 0.0; |
|
35 |
||
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
36 |
static Semaphore* _synchronize_wakeup = NULL; |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
37 |
|
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
38 |
void SuspendibleThreadSet_init() { |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
39 |
assert(_synchronize_wakeup == NULL, "STS already initialized"); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
40 |
_synchronize_wakeup = new Semaphore(); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
41 |
} |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
42 |
|
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
43 |
bool SuspendibleThreadSet::is_synchronized() { |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
44 |
assert_lock_strong(STS_lock); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
45 |
assert(_nthreads_stopped <= _nthreads, "invariant"); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
46 |
return _nthreads_stopped == _nthreads; |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
47 |
} |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
48 |
|
24094 | 49 |
void SuspendibleThreadSet::join() { |
30610
d4f41f692503
7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents:
24094
diff
changeset
|
50 |
assert(!Thread::current()->is_suspendible_thread(), "Thread already joined"); |
24094 | 51 |
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); |
52 |
while (_suspend_all) { |
|
53 |
ml.wait(Mutex::_no_safepoint_check_flag); |
|
54 |
} |
|
55 |
_nthreads++; |
|
30610
d4f41f692503
7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents:
24094
diff
changeset
|
56 |
DEBUG_ONLY(Thread::current()->set_suspendible_thread();) |
24094 | 57 |
} |
58 |
||
59 |
void SuspendibleThreadSet::leave() { |
|
30610
d4f41f692503
7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents:
24094
diff
changeset
|
60 |
assert(Thread::current()->is_suspendible_thread(), "Thread not joined"); |
24094 | 61 |
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); |
62 |
assert(_nthreads > 0, "Invalid"); |
|
30610
d4f41f692503
7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents:
24094
diff
changeset
|
63 |
DEBUG_ONLY(Thread::current()->clear_suspendible_thread();) |
24094 | 64 |
_nthreads--; |
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
65 |
if (_suspend_all && is_synchronized()) { |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
66 |
// This leave completes a request, so inform the requestor. |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
67 |
_synchronize_wakeup->signal(); |
24094 | 68 |
} |
69 |
} |
|
70 |
||
71 |
void SuspendibleThreadSet::yield() { |
|
30610
d4f41f692503
7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents:
24094
diff
changeset
|
72 |
assert(Thread::current()->is_suspendible_thread(), "Must have joined"); |
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
73 |
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); |
24094 | 74 |
if (_suspend_all) { |
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
75 |
_nthreads_stopped++; |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
76 |
if (is_synchronized()) { |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
77 |
if (ConcGCYieldTimeout > 0) { |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
78 |
double now = os::elapsedTime(); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
79 |
guarantee((now - _suspend_all_start) * 1000.0 < (double)ConcGCYieldTimeout, "Long delay"); |
24094 | 80 |
} |
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
81 |
// This yield completes the request, so inform the requestor. |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
82 |
_synchronize_wakeup->signal(); |
24094 | 83 |
} |
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
84 |
while (_suspend_all) { |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
85 |
ml.wait(Mutex::_no_safepoint_check_flag); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
86 |
} |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
87 |
assert(_nthreads_stopped > 0, "Invalid"); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
88 |
_nthreads_stopped--; |
24094 | 89 |
} |
90 |
} |
|
91 |
||
92 |
void SuspendibleThreadSet::synchronize() { |
|
93 |
assert(Thread::current()->is_VM_thread(), "Must be the VM thread"); |
|
94 |
if (ConcGCYieldTimeout > 0) { |
|
95 |
_suspend_all_start = os::elapsedTime(); |
|
96 |
} |
|
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
97 |
{ |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
98 |
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
99 |
assert(!_suspend_all, "Only one at a time"); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
100 |
_suspend_all = true; |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
101 |
if (is_synchronized()) { |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
102 |
return; |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
103 |
} |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
104 |
} // Release lock before semaphore wait. |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
105 |
|
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
106 |
// Semaphore initial count is zero. To reach here, there must be at |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
107 |
// least one not yielded thread in the set, e.g. is_synchronized() |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
108 |
// was false before the lock was released. A thread in the set will |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
109 |
// signal the semaphore iff it is the last to yield or leave while |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
110 |
// there is an active suspend request. So there will be exactly one |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
111 |
// signal, which will increment the semaphore count to one, which |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
112 |
// will then be consumed by this wait, returning it to zero. No |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
113 |
// thread can exit yield or enter the set until desynchronize is |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
114 |
// called, so there are no further opportunities for the semaphore |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
115 |
// being signaled until we get back here again for some later |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
116 |
// synchronize call. Hence, there is no need to re-check for |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
117 |
// is_synchronized after the wait; it will always be true there. |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
118 |
_synchronize_wakeup->wait(); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
119 |
|
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
120 |
#ifdef ASSERT |
24094 | 121 |
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); |
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
122 |
assert(_suspend_all, "STS not synchronizing"); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
123 |
assert(is_synchronized(), "STS not synchronized"); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
124 |
#endif |
24094 | 125 |
} |
126 |
||
127 |
void SuspendibleThreadSet::desynchronize() { |
|
128 |
assert(Thread::current()->is_VM_thread(), "Must be the VM thread"); |
|
129 |
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); |
|
37188
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
130 |
assert(_suspend_all, "STS not synchronizing"); |
390e3a2e7cee
8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents:
30764
diff
changeset
|
131 |
assert(is_synchronized(), "STS not synchronized"); |
24094 | 132 |
_suspend_all = false; |
133 |
ml.notify_all(); |
|
134 |
} |