Skip to content

Commit 06b90a4

Browse files
andrykonchineregon
authored andcommitted
Refactor CExtNodes.java to not duplicate the common logic
* No need to check CEXT_LOCK since useCExtLock should always be false if CEXT_LOCK is false, since then the lock should never be locked.
1 parent a3532f0 commit 06b90a4

File tree

2 files changed

+55
-78
lines changed

2 files changed

+55
-78
lines changed

lib/truffle/truffle/cext.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,7 @@ def rb_thread_create(fn, args)
20302030
end
20312031

20322032
def rb_thread_call_with_gvl(function, data)
2033-
Primitive.call_with_cext_lock(POINTER_TO_POINTER_WRAPPER, [function, data], true)
2033+
Primitive.call_with_cext_lock(POINTER_TO_POINTER_WRAPPER, [function, data], CEXT_LOCK)
20342034
end
20352035

20362036
def rb_thread_call_without_gvl(function, data1, unblock, data2)

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

Lines changed: 54 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Object callWithCExtLockAndFrame(
192192
@CachedLibrary(limit = "getCacheLimit()") InteropLibrary receivers,
193193
@Cached ArrayToObjectArrayNode arrayToObjectArrayNode,
194194
@Cached TranslateInteropExceptionNode translateInteropExceptionNode,
195-
@Cached InlinedConditionProfile lockCExtProfile,
195+
@Cached InlinedConditionProfile useCExtLockProfile,
196196
@Cached InlinedConditionProfile ownedProfile,
197197
@Cached RunMarkOnExitNode runMarksNode) {
198198
final ExtensionCallStack extensionStack = getLanguage()
@@ -202,29 +202,21 @@ Object callWithCExtLockAndFrame(
202202
extensionStack.push(keywordsGiven, specialVariables, block);
203203
try {
204204
final Object[] args = arrayToObjectArrayNode.executeToObjectArray(argsArray);
205-
final boolean lockCExt = lockCExtProfile.profile(this,
206-
getContext().getOptions().CEXT_LOCK && useCExtLock);
207205

208-
if (lockCExt) {
209-
final ReentrantLock lock = getContext().getCExtensionsLock();
210-
boolean owned = ownedProfile.profile(this, lock.isHeldByCurrentThread());
206+
final ReentrantLock lock = getContext().getCExtensionsLock();
207+
final boolean mustLock = useCExtLockProfile.profile(this, useCExtLock) &&
208+
!ownedProfile.profile(this, lock.isHeldByCurrentThread());
209+
if (mustLock) {
210+
MutexOperations.lockInternal(getContext(), lock, this);
211+
}
211212

212-
if (!owned) {
213-
MutexOperations.lockInternal(getContext(), lock, this);
214-
}
215-
try {
216-
return InteropNodes.execute(this, receiver, args, receivers, translateInteropExceptionNode);
217-
} finally {
218-
runMarksNode.execute(this, extensionStack);
219-
if (!owned) {
220-
MutexOperations.unlockInternal(lock);
221-
}
222-
}
223-
} else {
224-
try {
225-
return InteropNodes.execute(this, receiver, args, receivers, translateInteropExceptionNode);
226-
} finally {
227-
runMarksNode.execute(this, extensionStack);
213+
try {
214+
return InteropNodes.execute(this, receiver, args, receivers, translateInteropExceptionNode);
215+
} finally {
216+
runMarksNode.execute(this, extensionStack);
217+
218+
if (mustLock) {
219+
MutexOperations.unlockInternal(lock);
228220
}
229221
}
230222

@@ -252,7 +244,7 @@ Object callWithCExtLockAndFrame(
252244
@CachedLibrary(limit = "getCacheLimit()") InteropLibrary receivers,
253245
@Cached ArrayToObjectArrayNode arrayToObjectArrayNode,
254246
@Cached TranslateInteropExceptionNode translateInteropExceptionNode,
255-
@Cached InlinedConditionProfile lockCExtProfile,
247+
@Cached InlinedConditionProfile useCExtLockProfile,
256248
@Cached InlinedConditionProfile ownedProfile,
257249
@Cached RunMarkOnExitNode runMarksNode,
258250
@Cached UnwrapNode unwrapNode) {
@@ -261,31 +253,22 @@ Object callWithCExtLockAndFrame(
261253
extensionStack.push(keywordsGiven, specialVariables, block);
262254
try {
263255
final Object[] args = arrayToObjectArrayNode.executeToObjectArray(argsArray);
264-
final boolean lockCExt = lockCExtProfile.profile(this,
265-
getContext().getOptions().CEXT_LOCK && useCExtLock);
266256

267-
if (lockCExt) {
268-
final ReentrantLock lock = getContext().getCExtensionsLock();
269-
boolean owned = ownedProfile.profile(this, lock.isHeldByCurrentThread());
257+
final ReentrantLock lock = getContext().getCExtensionsLock();
258+
final boolean mustLock = useCExtLockProfile.profile(this, useCExtLock) &&
259+
!ownedProfile.profile(this, lock.isHeldByCurrentThread());
260+
if (mustLock) {
261+
MutexOperations.lockInternal(getContext(), lock, this);
262+
}
270263

271-
if (!owned) {
272-
MutexOperations.lockInternal(getContext(), lock, this);
273-
}
274-
try {
275-
return unwrapNode.execute(this,
276-
InteropNodes.execute(this, receiver, args, receivers, translateInteropExceptionNode));
277-
} finally {
278-
runMarksNode.execute(this, extensionStack);
279-
if (!owned) {
280-
MutexOperations.unlockInternal(lock);
281-
}
282-
}
283-
} else {
284-
try {
285-
return unwrapNode.execute(this,
286-
InteropNodes.execute(this, receiver, args, receivers, translateInteropExceptionNode));
287-
} finally {
288-
runMarksNode.execute(this, extensionStack);
264+
try {
265+
return unwrapNode.execute(this,
266+
InteropNodes.execute(this, receiver, args, receivers, translateInteropExceptionNode));
267+
} finally {
268+
runMarksNode.execute(this, extensionStack);
269+
270+
if (mustLock) {
271+
MutexOperations.unlockInternal(lock);
289272
}
290273
}
291274

@@ -309,27 +292,23 @@ Object callWithCExtLock(Object receiver, RubyArray argsArray, boolean useCExtLoc
309292
@CachedLibrary(limit = "getCacheLimit()") InteropLibrary receivers,
310293
@Cached ArrayToObjectArrayNode arrayToObjectArrayNode,
311294
@Cached TranslateInteropExceptionNode translateInteropExceptionNode,
312-
@Cached InlinedConditionProfile lockCExtProfile,
295+
@Cached InlinedConditionProfile useCExtLockProfile,
313296
@Cached InlinedConditionProfile ownedProfile) {
314297
Object[] args = arrayToObjectArrayNode.executeToObjectArray(argsArray);
315-
final boolean lockCExt = lockCExtProfile.profile(this, getContext().getOptions().CEXT_LOCK && useCExtLock);
316298

317-
if (lockCExt) {
318-
final ReentrantLock lock = getContext().getCExtensionsLock();
319-
boolean owned = ownedProfile.profile(this, lock.isHeldByCurrentThread());
299+
final ReentrantLock lock = getContext().getCExtensionsLock();
300+
final boolean mustLock = useCExtLockProfile.profile(this, useCExtLock) &&
301+
!ownedProfile.profile(this, lock.isHeldByCurrentThread());
302+
if (mustLock) {
303+
MutexOperations.lockInternal(getContext(), lock, this);
304+
}
320305

321-
if (!owned) {
322-
MutexOperations.lockInternal(getContext(), lock, this);
323-
}
324-
try {
325-
return InteropNodes.execute(this, receiver, args, receivers, translateInteropExceptionNode);
326-
} finally {
327-
if (!owned) {
328-
MutexOperations.unlockInternal(lock);
329-
}
330-
}
331-
} else {
306+
try {
332307
return InteropNodes.execute(this, receiver, args, receivers, translateInteropExceptionNode);
308+
} finally {
309+
if (mustLock) {
310+
MutexOperations.unlockInternal(lock);
311+
}
333312
}
334313

335314
}
@@ -343,24 +322,22 @@ public abstract static class SendWithoutCExtLockBaseNode extends PrimitiveArrayA
343322
public Object sendWithoutCExtLock(VirtualFrame frame, Object receiver, RubySymbol method, Object block,
344323
ArgumentsDescriptor descriptor, Object[] args,
345324
DispatchNode dispatchNode, DispatchConfiguration config, InlinedConditionProfile ownedProfile) {
346-
if (getContext().getOptions().CEXT_LOCK) {
347-
final ReentrantLock lock = getContext().getCExtensionsLock();
348-
boolean owned = ownedProfile.profile(this, lock.isHeldByCurrentThread());
325+
final ReentrantLock lock = getContext().getCExtensionsLock();
326+
assert getContext().getOptions().CEXT_LOCK ||
327+
!lock.isHeldByCurrentThread() : "lock held with --cexts-lock=false";
328+
boolean mustUnlock = getContext().getOptions().CEXT_LOCK &&
329+
ownedProfile.profile(this, lock.isHeldByCurrentThread());
330+
if (mustUnlock) {
331+
MutexOperations.unlockInternal(lock);
332+
}
349333

350-
if (owned) {
351-
MutexOperations.unlockInternal(lock);
352-
}
353-
try {
354-
return dispatchNode.callWithFrameAndBlock(config, frame, receiver, method.getString(), block,
355-
descriptor, args);
356-
} finally {
357-
if (owned) {
358-
MutexOperations.internalLockEvenWithException(getContext(), lock, this);
359-
}
360-
}
361-
} else {
334+
try {
362335
return dispatchNode.callWithFrameAndBlock(config, frame, receiver, method.getString(), block,
363336
descriptor, args);
337+
} finally {
338+
if (mustUnlock) {
339+
MutexOperations.internalLockEvenWithException(getContext(), lock, this);
340+
}
364341
}
365342
}
366343
}

0 commit comments

Comments
 (0)