author | tschatzl |
Thu, 06 Aug 2015 15:49:50 +0200 | |
changeset 32185 | 49a57ff2c3cb |
parent 30764 | fec48bf5a827 |
child 37188 | 390e3a2e7cee |
permissions | -rw-r--r-- |
24094 | 1 |
/* |
30613
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
2 |
* Copyright (c) 2014, 2015, 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 |
||
30764 | 25 |
#ifndef SHARE_VM_GC_G1_SUSPENDIBLETHREADSET_HPP |
26 |
#define SHARE_VM_GC_G1_SUSPENDIBLETHREADSET_HPP |
|
24094 | 27 |
|
28 |
#include "memory/allocation.hpp" |
|
29 |
||
30 |
// A SuspendibleThreadSet is a set of threads that can be suspended. |
|
31 |
// A thread can join and later leave the set, and periodically yield. |
|
32 |
// If some thread (not in the set) requests, via synchronize(), that |
|
33 |
// the threads be suspended, then the requesting thread is blocked |
|
34 |
// until all the threads in the set have yielded or left the set. Threads |
|
35 |
// may not enter the set when an attempted suspension is in progress. The |
|
36 |
// suspending thread later calls desynchronize(), allowing the suspended |
|
37 |
// threads to continue. |
|
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 | 42 |
private: |
43 |
static uint _nthreads; |
|
44 |
static uint _nthreads_stopped; |
|
45 |
static bool _suspend_all; |
|
46 |
static double _suspend_all_start; |
|
47 |
||
48 |
// Add the current thread to the set. May block if a suspension is in progress. |
|
49 |
static void join(); |
|
50 |
||
51 |
// Removes the current thread from the set. |
|
52 |
static void leave(); |
|
53 |
||
30613
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
54 |
public: |
24094 | 55 |
// Returns true if an suspension is in progress. |
56 |
static bool should_yield() { return _suspend_all; } |
|
57 |
||
58 |
// Suspends the current thread if a suspension is in progress. |
|
59 |
static void yield(); |
|
60 |
||
61 |
// Returns when all threads in the set are suspended. |
|
62 |
static void synchronize(); |
|
63 |
||
64 |
// Resumes all suspended threads in the set. |
|
65 |
static void desynchronize(); |
|
66 |
}; |
|
67 |
||
68 |
class SuspendibleThreadSetJoiner : public StackObj { |
|
30613
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
69 |
private: |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
70 |
bool _active; |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
71 |
|
24094 | 72 |
public: |
30613
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
73 |
SuspendibleThreadSetJoiner(bool active = true) : _active(active) { |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
74 |
if (_active) { |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
75 |
SuspendibleThreadSet::join(); |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
76 |
} |
24094 | 77 |
} |
78 |
||
79 |
~SuspendibleThreadSetJoiner() { |
|
30613
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
80 |
if (_active) { |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
81 |
SuspendibleThreadSet::leave(); |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
82 |
} |
24094 | 83 |
} |
84 |
||
85 |
bool should_yield() { |
|
30613
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
86 |
if (_active) { |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
87 |
return SuspendibleThreadSet::should_yield(); |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
88 |
} else { |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
89 |
return false; |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
90 |
} |
24094 | 91 |
} |
92 |
||
93 |
void yield() { |
|
30613
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
94 |
assert(_active, "Thread has not joined the suspendible thread set"); |
24094 | 95 |
SuspendibleThreadSet::yield(); |
96 |
} |
|
97 |
}; |
|
98 |
||
30613
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
99 |
class SuspendibleThreadSetLeaver : public StackObj { |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
100 |
private: |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
101 |
bool _active; |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
102 |
|
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
103 |
public: |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
104 |
SuspendibleThreadSetLeaver(bool active = true) : _active(active) { |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
105 |
if (_active) { |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
106 |
SuspendibleThreadSet::leave(); |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
107 |
} |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
108 |
} |
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 |
~SuspendibleThreadSetLeaver() { |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
111 |
if (_active) { |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
112 |
SuspendibleThreadSet::join(); |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
113 |
} |
a7815bb05ae2
8079579: Add SuspendibleThreadSetLeaver and make SuspendibleThreadSet::joint()/leave() private
pliden
parents:
24094
diff
changeset
|
114 |
} |
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 |
|
30764 | 117 |
#endif // SHARE_VM_GC_G1_SUSPENDIBLETHREADSET_HPP |