src/hotspot/share/gc/shared/suspendibleThreadSet.hpp
author stefank
Fri, 24 Nov 2017 15:48:01 +0100
changeset 48116 8a5e8cd321d9
parent 47701 be620a591379
permissions -rw-r--r--
8191861: Move and refactor hSpaceCounters Reviewed-by: pliden, ehelin, rkennke
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     1
/*
47647
64dba69fc528 8189276: Make SuspendibleThreadSet and related code available to other GCs
rkennke
parents: 47216
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     4
 *
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     8
 *
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    13
 * accompanied this code).
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    14
 *
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    18
 *
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    21
 * questions.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    22
 *
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    23
 */
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    24
47647
64dba69fc528 8189276: Make SuspendibleThreadSet and related code available to other GCs
rkennke
parents: 47216
diff changeset
    25
#ifndef SHARE_GC_SHARED_SUSPENDIBLETHREADSET_HPP
64dba69fc528 8189276: Make SuspendibleThreadSet and related code available to other GCs
rkennke
parents: 47216
diff changeset
    26
#define SHARE_GC_SHARED_SUSPENDIBLETHREADSET_HPP
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    27
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    28
#include "memory/allocation.hpp"
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    29
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    30
// A SuspendibleThreadSet is a set of threads that can be suspended.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    31
// A thread can join and later leave the set, and periodically yield.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    32
// If some thread (not in the set) requests, via synchronize(), that
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    33
// the threads be suspended, then the requesting thread is blocked
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    34
// until all the threads in the set have yielded or left the set. Threads
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    35
// may not enter the set when an attempted suspension is in progress. The
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    36
// suspending thread later calls desynchronize(), allowing the suspended
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    37
// threads to continue.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    38
class SuspendibleThreadSet : public AllStatic {
30613
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    39
  friend class SuspendibleThreadSetJoiner;
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    40
  friend class SuspendibleThreadSetLeaver;
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    41
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    42
private:
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    43
  static uint   _nthreads;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    44
  static uint   _nthreads_stopped;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    45
  static bool   _suspend_all;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    46
  static double _suspend_all_start;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    47
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    48
  static bool is_synchronized();
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    49
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    50
  // Add the current thread to the set. May block if a suspension is in progress.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    51
  static void join();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    52
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    53
  // Removes the current thread from the set.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    54
  static void leave();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    55
30613
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    56
public:
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    57
  // Returns true if an suspension is in progress.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    58
  static bool should_yield() { return _suspend_all; }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    59
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    60
  // Suspends the current thread if a suspension is in progress.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    61
  static void yield();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    62
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    63
  // Returns when all threads in the set are suspended.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    64
  static void synchronize();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    65
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    66
  // Resumes all suspended threads in the set.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    67
  static void desynchronize();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    68
};
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    69
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    70
class SuspendibleThreadSetJoiner : public StackObj {
30613
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    71
private:
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    72
  bool _active;
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    73
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    74
public:
30613
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    75
  SuspendibleThreadSetJoiner(bool active = true) : _active(active) {
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    76
    if (_active) {
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    77
      SuspendibleThreadSet::join();
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    78
    }
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    79
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    80
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    81
  ~SuspendibleThreadSetJoiner() {
30613
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    82
    if (_active) {
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    83
      SuspendibleThreadSet::leave();
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    84
    }
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    85
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    86
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    87
  bool should_yield() {
30613
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    88
    if (_active) {
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    89
      return SuspendibleThreadSet::should_yield();
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    90
    } else {
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    91
      return false;
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    92
    }
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    93
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    94
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    95
  void yield() {
30613
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
    96
    assert(_active, "Thread has not joined the suspendible thread set");
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    97
    SuspendibleThreadSet::yield();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    98
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    99
};
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
   100
30613
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   101
class SuspendibleThreadSetLeaver : public StackObj {
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   102
private:
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   103
  bool _active;
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   104
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   105
public:
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   106
  SuspendibleThreadSetLeaver(bool active = true) : _active(active) {
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   107
    if (_active) {
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   108
      SuspendibleThreadSet::leave();
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   109
    }
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   110
  }
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   111
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   112
  ~SuspendibleThreadSetLeaver() {
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   113
    if (_active) {
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   114
      SuspendibleThreadSet::join();
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   115
    }
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   116
  }
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   117
};
a7815bb05ae2 8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents: 24094
diff changeset
   118
47647
64dba69fc528 8189276: Make SuspendibleThreadSet and related code available to other GCs
rkennke
parents: 47216
diff changeset
   119
#endif // SHARE_GC_SHARED_SUSPENDIBLETHREADSET_HPP