1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/shared/suspendibleThreadSet.cpp Fri Apr 11 12:29:24 2014 +0200 1.3 @@ -0,0 +1,93 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "gc_implementation/shared/suspendibleThreadSet.hpp" 1.30 +#include "runtime/mutexLocker.hpp" 1.31 +#include "runtime/thread.inline.hpp" 1.32 + 1.33 +uint SuspendibleThreadSet::_nthreads = 0; 1.34 +uint SuspendibleThreadSet::_nthreads_stopped = 0; 1.35 +bool SuspendibleThreadSet::_suspend_all = false; 1.36 +double SuspendibleThreadSet::_suspend_all_start = 0.0; 1.37 + 1.38 +void SuspendibleThreadSet::join() { 1.39 + MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); 1.40 + while (_suspend_all) { 1.41 + ml.wait(Mutex::_no_safepoint_check_flag); 1.42 + } 1.43 + _nthreads++; 1.44 +} 1.45 + 1.46 +void SuspendibleThreadSet::leave() { 1.47 + MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); 1.48 + assert(_nthreads > 0, "Invalid"); 1.49 + _nthreads--; 1.50 + if (_suspend_all) { 1.51 + ml.notify_all(); 1.52 + } 1.53 +} 1.54 + 1.55 +void SuspendibleThreadSet::yield() { 1.56 + if (_suspend_all) { 1.57 + MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); 1.58 + if (_suspend_all) { 1.59 + _nthreads_stopped++; 1.60 + if (_nthreads_stopped == _nthreads) { 1.61 + if (ConcGCYieldTimeout > 0) { 1.62 + double now = os::elapsedTime(); 1.63 + guarantee((now - _suspend_all_start) * 1000.0 < (double)ConcGCYieldTimeout, "Long delay"); 1.64 + } 1.65 + } 1.66 + ml.notify_all(); 1.67 + while (_suspend_all) { 1.68 + ml.wait(Mutex::_no_safepoint_check_flag); 1.69 + } 1.70 + assert(_nthreads_stopped > 0, "Invalid"); 1.71 + _nthreads_stopped--; 1.72 + ml.notify_all(); 1.73 + } 1.74 + } 1.75 +} 1.76 + 1.77 +void SuspendibleThreadSet::synchronize() { 1.78 + assert(Thread::current()->is_VM_thread(), "Must be the VM thread"); 1.79 + if (ConcGCYieldTimeout > 0) { 1.80 + _suspend_all_start = os::elapsedTime(); 1.81 + } 1.82 + MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); 1.83 + assert(!_suspend_all, "Only one at a time"); 1.84 + _suspend_all = true; 1.85 + while (_nthreads_stopped < _nthreads) { 1.86 + ml.wait(Mutex::_no_safepoint_check_flag); 1.87 + } 1.88 +} 1.89 + 1.90 +void SuspendibleThreadSet::desynchronize() { 1.91 + assert(Thread::current()->is_VM_thread(), "Must be the VM thread"); 1.92 + MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag); 1.93 + assert(_nthreads_stopped == _nthreads, "Invalid"); 1.94 + _suspend_all = false; 1.95 + ml.notify_all(); 1.96 +}