Skip to content

Commit 249e124

Browse files
committed
Release C extension mutex when waiting for fds.
1 parent 31b74b1 commit 249e124

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

lib/truffle/truffle/cext.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,14 +1635,24 @@ def rb_iterate(iteration, iterated_object, callback, callback_arg)
16351635
def rb_thread_wait_fd(fd)
16361636
io = IO.for_fd(fd)
16371637
io.autoclose = false
1638-
IO.select([io])
1638+
owned = rb_release_cext_mutex
1639+
begin
1640+
IO.select([io])
1641+
ensure
1642+
rb_acquire_cext_mutex if owned
1643+
end
16391644
nil
16401645
end
16411646

16421647
def rb_thread_fd_writable(fd)
16431648
io = IO.for_fd(fd)
16441649
io.autoclose = false
1645-
_r, w, _e = IO.select(nil, [io])
1650+
owned = rb_release_cext_mutex
1651+
begin
1652+
_r, w, _e = IO.select(nil, [io])
1653+
ensure
1654+
rb_acquire_cext_mutex if owned
1655+
end
16461656
w.size
16471657
end
16481658

@@ -1661,7 +1671,12 @@ def rb_wait_for_single_fd(fd, events, tv_secs, tv_usecs)
16611671
if tv_secs > 0 || tv_usecs > 0
16621672
timeout = tv_secs + tv_usecs/1.0e6
16631673
end
1664-
r, w, e = IO.select(read, write, error, *timeout)
1674+
owned = rb_release_cext_mutex
1675+
begin
1676+
r, w, e = IO.select(read, write, error, *timeout)
1677+
ensure
1678+
rb_acquire_cext_mutex if owned
1679+
end
16651680
if r.nil? # timeout
16661681
0
16671682
else

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,44 @@ private Object execute(TruffleObject receiver, Object[] args, Node executeNode,
199199

200200
}
201201

202+
@CoreMethod(names = "rb_acquire_cext_mutex")
203+
public abstract static class AcquireCExtMutexNode extends CoreMethodArrayArgumentsNode {
204+
@Specialization
205+
public boolean acquireMutex(VirtualFrame frame,
206+
@Cached("create()") BranchProfile exceptionProfile,
207+
@Cached("createBinaryProfile()") ConditionProfile ownedProfile) {
208+
if (getContext().getOptions().CEXT_LOCK) {
209+
final ReentrantLock lock = getContext().getCExtensionsLock();
210+
boolean owned = lock.isHeldByCurrentThread();
211+
212+
if (ownedProfile.profile(!owned)) {
213+
MutexOperations.lockInternal(getContext(), lock, this);
214+
return true;
215+
}
216+
}
217+
return false;
218+
}
219+
}
220+
221+
@CoreMethod(names = "rb_release_cext_mutex")
222+
public abstract static class ReleaseCExtMutexNode extends CoreMethodArrayArgumentsNode {
223+
@Specialization
224+
public boolean releaseMutex(VirtualFrame frame,
225+
@Cached("create()") BranchProfile exceptionProfile,
226+
@Cached("createBinaryProfile()") ConditionProfile ownedProfile) {
227+
if (getContext().getOptions().CEXT_LOCK) {
228+
final ReentrantLock lock = getContext().getCExtensionsLock();
229+
boolean owned = lock.isHeldByCurrentThread();
230+
231+
if (ownedProfile.profile(owned)) {
232+
MutexOperations.unlockInternal(lock);
233+
return true;
234+
}
235+
}
236+
return false;
237+
}
238+
}
239+
202240
@CoreMethod(names = "rb_ulong2num", onSingleton = true, required = 1)
203241
public abstract static class ULong2NumNode extends CoreMethodArrayArgumentsNode {
204242

0 commit comments

Comments
 (0)