@@ -192,7 +192,7 @@ Object callWithCExtLockAndFrame(
192
192
@ CachedLibrary (limit = "getCacheLimit()" ) InteropLibrary receivers ,
193
193
@ Cached ArrayToObjectArrayNode arrayToObjectArrayNode ,
194
194
@ Cached TranslateInteropExceptionNode translateInteropExceptionNode ,
195
- @ Cached InlinedConditionProfile lockCExtProfile ,
195
+ @ Cached InlinedConditionProfile useCExtLockProfile ,
196
196
@ Cached InlinedConditionProfile ownedProfile ,
197
197
@ Cached RunMarkOnExitNode runMarksNode ) {
198
198
final ExtensionCallStack extensionStack = getLanguage ()
@@ -202,29 +202,21 @@ Object callWithCExtLockAndFrame(
202
202
extensionStack .push (keywordsGiven , specialVariables , block );
203
203
try {
204
204
final Object [] args = arrayToObjectArrayNode .executeToObjectArray (argsArray );
205
- final boolean lockCExt = lockCExtProfile .profile (this ,
206
- getContext ().getOptions ().CEXT_LOCK && useCExtLock );
207
205
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
+ }
211
212
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 );
228
220
}
229
221
}
230
222
@@ -252,7 +244,7 @@ Object callWithCExtLockAndFrame(
252
244
@ CachedLibrary (limit = "getCacheLimit()" ) InteropLibrary receivers ,
253
245
@ Cached ArrayToObjectArrayNode arrayToObjectArrayNode ,
254
246
@ Cached TranslateInteropExceptionNode translateInteropExceptionNode ,
255
- @ Cached InlinedConditionProfile lockCExtProfile ,
247
+ @ Cached InlinedConditionProfile useCExtLockProfile ,
256
248
@ Cached InlinedConditionProfile ownedProfile ,
257
249
@ Cached RunMarkOnExitNode runMarksNode ,
258
250
@ Cached UnwrapNode unwrapNode ) {
@@ -261,31 +253,22 @@ Object callWithCExtLockAndFrame(
261
253
extensionStack .push (keywordsGiven , specialVariables , block );
262
254
try {
263
255
final Object [] args = arrayToObjectArrayNode .executeToObjectArray (argsArray );
264
- final boolean lockCExt = lockCExtProfile .profile (this ,
265
- getContext ().getOptions ().CEXT_LOCK && useCExtLock );
266
256
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
+ }
270
263
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 );
289
272
}
290
273
}
291
274
@@ -309,27 +292,23 @@ Object callWithCExtLock(Object receiver, RubyArray argsArray, boolean useCExtLoc
309
292
@ CachedLibrary (limit = "getCacheLimit()" ) InteropLibrary receivers ,
310
293
@ Cached ArrayToObjectArrayNode arrayToObjectArrayNode ,
311
294
@ Cached TranslateInteropExceptionNode translateInteropExceptionNode ,
312
- @ Cached InlinedConditionProfile lockCExtProfile ,
295
+ @ Cached InlinedConditionProfile useCExtLockProfile ,
313
296
@ Cached InlinedConditionProfile ownedProfile ) {
314
297
Object [] args = arrayToObjectArrayNode .executeToObjectArray (argsArray );
315
- final boolean lockCExt = lockCExtProfile .profile (this , getContext ().getOptions ().CEXT_LOCK && useCExtLock );
316
298
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
+ }
320
305
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 {
332
307
return InteropNodes .execute (this , receiver , args , receivers , translateInteropExceptionNode );
308
+ } finally {
309
+ if (mustLock ) {
310
+ MutexOperations .unlockInternal (lock );
311
+ }
333
312
}
334
313
335
314
}
@@ -343,24 +322,22 @@ public abstract static class SendWithoutCExtLockBaseNode extends PrimitiveArrayA
343
322
public Object sendWithoutCExtLock (VirtualFrame frame , Object receiver , RubySymbol method , Object block ,
344
323
ArgumentsDescriptor descriptor , Object [] args ,
345
324
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
+ }
349
333
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 {
362
335
return dispatchNode .callWithFrameAndBlock (config , frame , receiver , method .getString (), block ,
363
336
descriptor , args );
337
+ } finally {
338
+ if (mustUnlock ) {
339
+ MutexOperations .internalLockEvenWithException (getContext (), lock , this );
340
+ }
364
341
}
365
342
}
366
343
}
0 commit comments