Skip to content

Commit 82fcec1

Browse files
committed
[GR-29429] Let startAutoLoad() enter the safepoint when acquiring the lock
PullRequest: truffleruby/2425
2 parents f18b9df + 070cc74 commit 82fcec1

File tree

7 files changed

+18
-23
lines changed

7 files changed

+18
-23
lines changed

spec/ruby/fixtures/code/concurrent.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Thread.current[:in_concurrent_rb] = true
33

44
if t = Thread.current[:wait_for]
5-
Thread.pass until t.backtrace && t.backtrace.any? { |call| call.include? 'require' }
5+
Thread.pass until t.backtrace && t.backtrace.any? { |call| call.include? 'require' } && t.stop?
66
end
77

88
if Thread.current[:con_raise]

src/main/java/org/truffleruby/core/module/ModuleFields.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public void setAutoloadConstant(RubyContext context, Node currentNode, String na
374374
if (lock.isLocked()) {
375375
// We need to handle the new autoload constant immediately
376376
// if Object.autoload(name, filename) is executed from filename.rb
377-
GetConstantNode.autoloadConstantStart(autoloadConstant);
377+
GetConstantNode.autoloadConstantStart(context, autoloadConstant, currentNode);
378378
}
379379

380380
context.getFeatureLoader().addAutoload(autoloadConstant);

src/main/java/org/truffleruby/core/mutex/MutexOperations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected static void lock(RubyContext context, ReentrantLock lock, RubyThread t
3232
}
3333

3434
@TruffleBoundary
35-
public static void lockInternal(RubyContext context, ReentrantLock lock, RubyNode currentNode) {
35+
public static void lockInternal(RubyContext context, ReentrantLock lock, Node currentNode) {
3636
if (lock.tryLock()) {
3737
return;
3838
}

src/main/java/org/truffleruby/language/AutoloadConstant.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import java.util.concurrent.locks.ReentrantLock;
1313

1414
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
15+
import com.oracle.truffle.api.nodes.Node;
16+
import org.truffleruby.RubyContext;
17+
import org.truffleruby.core.mutex.MutexOperations;
1518
import org.truffleruby.language.library.RubyStringLibrary;
1619

1720
public class AutoloadConstant {
@@ -44,8 +47,8 @@ private ReentrantLock getAutoloadLock() {
4447
}
4548

4649
@TruffleBoundary
47-
public void startAutoLoad() {
48-
getAutoloadLock().lock();
50+
public void startAutoLoad(RubyContext context, Node currentNode) {
51+
MutexOperations.lockInternal(context, getAutoloadLock(), currentNode);
4952
}
5053

5154
@TruffleBoundary

src/main/java/org/truffleruby/language/constants/GetConstantNode.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.truffleruby.language.constants;
1111

12+
import com.oracle.truffle.api.nodes.Node;
1213
import org.truffleruby.RubyContext;
1314
import org.truffleruby.RubyLanguage;
1415
import org.truffleruby.SuppressFBWarnings;
@@ -118,7 +119,7 @@ protected Object autoloadConstant(
118119
}
119120

120121
// Mark the autoload constant as loading already here and not in RequireNode so that recursive lookups act as "being loaded"
121-
autoloadConstantStart(autoloadConstant);
122+
autoloadConstantStart(getContext(), autoloadConstant, this);
122123
try {
123124
callRequireNode.call(coreLibrary().mainObject, "require", feature);
124125

@@ -130,8 +131,8 @@ protected Object autoloadConstant(
130131
}
131132

132133
@TruffleBoundary
133-
public static void autoloadConstantStart(RubyConstant autoloadConstant) {
134-
autoloadConstant.getAutoloadConstant().startAutoLoad();
134+
public static void autoloadConstantStart(RubyContext context, RubyConstant autoloadConstant, Node currentNode) {
135+
autoloadConstant.getAutoloadConstant().startAutoLoad(context, currentNode);
135136

136137
// We need to notify cached lookup that we are autoloading the constant, as constant
137138
// lookup changes based on whether an autoload constant is loading or not (constant

src/main/java/org/truffleruby/language/loader/ReentrantLockFreeingMap.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import java.util.concurrent.ConcurrentHashMap;
1313
import java.util.concurrent.locks.ReentrantLock;
1414

15-
import org.truffleruby.core.thread.ThreadManager;
15+
import org.truffleruby.RubyContext;
16+
import org.truffleruby.core.mutex.MutexOperations;
1617

1718
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1819
import com.oracle.truffle.api.nodes.Node;
@@ -68,19 +69,9 @@ public boolean isCurrentThreadHoldingLock(K key) {
6869
}
6970

7071
@TruffleBoundary
71-
public boolean lock(
72-
Node currentNode,
73-
ThreadManager threadManager,
74-
K key,
75-
final ReentrantLock lock) {
76-
72+
public boolean lock(RubyContext context, K key, ReentrantLock lock, Node currentNode) {
7773
// Also sets status to sleep in MRI
78-
threadManager.runUntilResult(
79-
currentNode,
80-
() -> {
81-
lock.lockInterruptibly();
82-
return ThreadManager.BlockingAction.SUCCESS;
83-
});
74+
MutexOperations.lockInternal(context, lock, currentNode);
8475
// ensure that we are not holding removed lock
8576
if (lock == locks.get(key)) {
8677
return true;

src/main/java/org/truffleruby/language/loader/RequireNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private boolean requireConsideringAutoload(String feature, String expandedPath,
117117
}
118118

119119
for (RubyConstant constant : toAutoload) {
120-
GetConstantNode.autoloadConstantStart(constant);
120+
GetConstantNode.autoloadConstantStart(getContext(), constant, this);
121121
}
122122
}
123123

@@ -168,7 +168,7 @@ private boolean doRequire(String originalFeature, String expandedPath, Object pa
168168
}
169169
}
170170

171-
if (!fileLocks.lock(this, getContext().getThreadManager(), expandedPath, lock)) {
171+
if (!fileLocks.lock(getContext(), expandedPath, lock, this)) {
172172
continue;
173173
}
174174

0 commit comments

Comments
 (0)