-
Notifications
You must be signed in to change notification settings - Fork 914
/
SafepointManager.java
145 lines (114 loc) · 4.28 KB
/
SafepointManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (c) 2014, 2015 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.runtime.subsystems;
import com.oracle.truffle.api.Assumption;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.nodes.InvalidAssumptionException;
import org.jruby.RubyThread.Status;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyThread;
import org.jruby.truffle.runtime.util.Consumer;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Phaser;
public class SafepointManager {
private final RubyContext context;
private final Set<Thread> runningThreads = Collections.newSetFromMap(new ConcurrentHashMap<Thread, Boolean>());
@CompilerDirectives.CompilationFinal private Assumption assumption = Truffle.getRuntime().createAssumption();
private final Phaser phaser = new Phaser();
private Consumer<RubyThread> action;
public SafepointManager(RubyContext context) {
this.context = context;
enterThread();
}
public synchronized void enterThread() {
CompilerAsserts.neverPartOfCompilation();
phaser.register();
runningThreads.add(Thread.currentThread());
}
public void leaveThread() {
CompilerAsserts.neverPartOfCompilation();
phaser.arriveAndDeregister();
runningThreads.remove(Thread.currentThread());
}
public void poll() {
poll(true);
}
public void pollWithoutGlobalLock() {
poll(false);
}
private void poll(boolean holdsGlobalLock) {
try {
assumption.check();
} catch (InvalidAssumptionException e) {
assumptionInvalidated(holdsGlobalLock, false);
}
}
private void assumptionInvalidated(boolean holdsGlobalLock, boolean isDrivingThread) {
RubyThread thread = null;
if (holdsGlobalLock) {
thread = context.getThreadManager().leaveGlobalLock();
}
try {
step(thread, isDrivingThread);
} finally {
if (holdsGlobalLock) {
context.getThreadManager().enterGlobalLock(thread);
}
}
}
private void step(RubyThread thread, boolean isDrivingThread) {
// wait other threads to reach their safepoint
phaser.arriveAndAwaitAdvance();
if (isDrivingThread) {
assumption = Truffle.getRuntime().createAssumption();
}
// wait the assumption to be renewed
phaser.arriveAndAwaitAdvance();
try {
if (thread != null && thread.getStatus() != Status.ABORTING) {
action.accept(thread);
}
} finally {
// wait other threads to finish their action
phaser.arriveAndAwaitAdvance();
}
}
public void pauseAllThreadsAndExecute(Consumer<RubyThread> action) {
pauseAllThreadsAndExecute(true, action);
}
public void pauseAllThreadsAndExecuteFromNonRubyThread(Consumer<RubyThread> action) {
enterThread();
try {
pauseAllThreadsAndExecute(false, action);
} finally {
leaveThread();
}
}
private synchronized void pauseAllThreadsAndExecute(boolean holdsGlobalLock, Consumer<RubyThread> action) {
CompilerDirectives.transferToInterpreter();
this.action = action;
/* this is a potential cause for race conditions,
* but we need to invalidate first so the interrupted threads
* see the invalidation in poll() in their catch(InterruptedException) clause
* and wait on the barrier instead of retrying their blocking action. */
assumption.invalidate();
interruptAllThreads();
assumptionInvalidated(holdsGlobalLock, true);
}
private void interruptAllThreads() {
for (Thread thread : runningThreads) {
thread.interrupt();
}
}
}