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