This commit is contained in:
Kim Barrett 2016-03-24 23:42:46 +00:00
commit dcb3e3b3c4
3 changed files with 65 additions and 25 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,7 @@
#include "precompiled.hpp" #include "precompiled.hpp"
#include "gc/g1/suspendibleThreadSet.hpp" #include "gc/g1/suspendibleThreadSet.hpp"
#include "runtime/mutexLocker.hpp" #include "runtime/mutexLocker.hpp"
#include "runtime/semaphore.hpp"
#include "runtime/thread.inline.hpp" #include "runtime/thread.inline.hpp"
uint SuspendibleThreadSet::_nthreads = 0; uint SuspendibleThreadSet::_nthreads = 0;
@ -32,6 +33,19 @@ uint SuspendibleThreadSet::_nthreads_stopped = 0;
bool SuspendibleThreadSet::_suspend_all = false; bool SuspendibleThreadSet::_suspend_all = false;
double SuspendibleThreadSet::_suspend_all_start = 0.0; double SuspendibleThreadSet::_suspend_all_start = 0.0;
static Semaphore* _synchronize_wakeup = NULL;
void SuspendibleThreadSet_init() {
assert(_synchronize_wakeup == NULL, "STS already initialized");
_synchronize_wakeup = new Semaphore();
}
bool SuspendibleThreadSet::is_synchronized() {
assert_lock_strong(STS_lock);
assert(_nthreads_stopped <= _nthreads, "invariant");
return _nthreads_stopped == _nthreads;
}
void SuspendibleThreadSet::join() { void SuspendibleThreadSet::join() {
assert(!Thread::current()->is_suspendible_thread(), "Thread already joined"); assert(!Thread::current()->is_suspendible_thread(), "Thread already joined");
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
@ -48,31 +62,30 @@ void SuspendibleThreadSet::leave() {
assert(_nthreads > 0, "Invalid"); assert(_nthreads > 0, "Invalid");
DEBUG_ONLY(Thread::current()->clear_suspendible_thread();) DEBUG_ONLY(Thread::current()->clear_suspendible_thread();)
_nthreads--; _nthreads--;
if (_suspend_all) { if (_suspend_all && is_synchronized()) {
ml.notify_all(); // This leave completes a request, so inform the requestor.
_synchronize_wakeup->signal();
} }
} }
void SuspendibleThreadSet::yield() { void SuspendibleThreadSet::yield() {
assert(Thread::current()->is_suspendible_thread(), "Must have joined"); assert(Thread::current()->is_suspendible_thread(), "Must have joined");
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
if (_suspend_all) { if (_suspend_all) {
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); _nthreads_stopped++;
if (_suspend_all) { if (is_synchronized()) {
_nthreads_stopped++; if (ConcGCYieldTimeout > 0) {
if (_nthreads_stopped == _nthreads) { double now = os::elapsedTime();
if (ConcGCYieldTimeout > 0) { guarantee((now - _suspend_all_start) * 1000.0 < (double)ConcGCYieldTimeout, "Long delay");
double now = os::elapsedTime();
guarantee((now - _suspend_all_start) * 1000.0 < (double)ConcGCYieldTimeout, "Long delay");
}
} }
ml.notify_all(); // This yield completes the request, so inform the requestor.
while (_suspend_all) { _synchronize_wakeup->signal();
ml.wait(Mutex::_no_safepoint_check_flag);
}
assert(_nthreads_stopped > 0, "Invalid");
_nthreads_stopped--;
ml.notify_all();
} }
while (_suspend_all) {
ml.wait(Mutex::_no_safepoint_check_flag);
}
assert(_nthreads_stopped > 0, "Invalid");
_nthreads_stopped--;
} }
} }
@ -81,18 +94,41 @@ void SuspendibleThreadSet::synchronize() {
if (ConcGCYieldTimeout > 0) { if (ConcGCYieldTimeout > 0) {
_suspend_all_start = os::elapsedTime(); _suspend_all_start = os::elapsedTime();
} }
{
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
assert(!_suspend_all, "Only one at a time");
_suspend_all = true;
if (is_synchronized()) {
return;
}
} // Release lock before semaphore wait.
// Semaphore initial count is zero. To reach here, there must be at
// least one not yielded thread in the set, e.g. is_synchronized()
// was false before the lock was released. A thread in the set will
// signal the semaphore iff it is the last to yield or leave while
// there is an active suspend request. So there will be exactly one
// signal, which will increment the semaphore count to one, which
// will then be consumed by this wait, returning it to zero. No
// thread can exit yield or enter the set until desynchronize is
// called, so there are no further opportunities for the semaphore
// being signaled until we get back here again for some later
// synchronize call. Hence, there is no need to re-check for
// is_synchronized after the wait; it will always be true there.
_synchronize_wakeup->wait();
#ifdef ASSERT
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
assert(!_suspend_all, "Only one at a time"); assert(_suspend_all, "STS not synchronizing");
_suspend_all = true; assert(is_synchronized(), "STS not synchronized");
while (_nthreads_stopped < _nthreads) { #endif
ml.wait(Mutex::_no_safepoint_check_flag);
}
} }
void SuspendibleThreadSet::desynchronize() { void SuspendibleThreadSet::desynchronize() {
assert(Thread::current()->is_VM_thread(), "Must be the VM thread"); assert(Thread::current()->is_VM_thread(), "Must be the VM thread");
MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
assert(_nthreads_stopped == _nthreads, "Invalid"); assert(_suspend_all, "STS not synchronizing");
assert(is_synchronized(), "STS not synchronized");
_suspend_all = false; _suspend_all = false;
ml.notify_all(); ml.notify_all();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -45,6 +45,8 @@ private:
static bool _suspend_all; static bool _suspend_all;
static double _suspend_all_start; static double _suspend_all_start;
static bool is_synchronized();
// Add the current thread to the set. May block if a suspension is in progress. // Add the current thread to the set. May block if a suspension is in progress.
static void join(); static void join();

View File

@ -47,6 +47,7 @@ void eventlog_init();
void mutex_init(); void mutex_init();
void chunkpool_init(); void chunkpool_init();
void perfMemory_init(); void perfMemory_init();
void SuspendibleThreadSet_init() NOT_ALL_GCS_RETURN;
// Initialization done by Java thread in init_globals() // Initialization done by Java thread in init_globals()
void management_init(); void management_init();
@ -93,6 +94,7 @@ void vm_init_globals() {
mutex_init(); mutex_init();
chunkpool_init(); chunkpool_init();
perfMemory_init(); perfMemory_init();
SuspendibleThreadSet_init();
} }