@@ -121,17 +121,23 @@ public abstract static class CallCWithMutexNode extends PrimitiveArrayArgumentsN
121
121
public Object callCWithMutex (TruffleObject receiver , DynamicObject argsArray ,
122
122
@ Cached ("create()" ) ArrayToObjectArrayNode arrayToObjectArrayNode ,
123
123
@ Cached ("EXECUTE.createNode()" ) Node executeNode ,
124
- @ Cached ("create()" ) BranchProfile exceptionProfile ) {
124
+ @ Cached ("create()" ) BranchProfile exceptionProfile ,
125
+ @ Cached ("createBinaryProfile()" ) ConditionProfile ownedProfile ) {
125
126
final Object [] args = arrayToObjectArrayNode .executeToObjectArray (argsArray );
126
127
127
128
if (getContext ().getOptions ().CEXT_LOCK ) {
128
129
final ReentrantLock lock = getContext ().getCExtensionsLock ();
130
+ boolean owned = lock .isHeldByCurrentThread ();
129
131
130
- MutexOperations .lockInternal (getContext (), lock , this );
131
- try {
132
+ if (ownedProfile .profile (!owned )) {
133
+ MutexOperations .lockInternal (getContext (), lock , this );
134
+ try {
135
+ return execute (receiver , args , executeNode , exceptionProfile );
136
+ } finally {
137
+ MutexOperations .unlockInternal (lock );
138
+ }
139
+ } else {
132
140
return execute (receiver , args , executeNode , exceptionProfile );
133
- } finally {
134
- MutexOperations .unlockInternal (lock );
135
141
}
136
142
} else {
137
143
return execute (receiver , args , executeNode , exceptionProfile );
@@ -158,17 +164,23 @@ public abstract static class CallCWithoutMutexNode extends PrimitiveArrayArgumen
158
164
public Object callCWithoutMutex (TruffleObject receiver , DynamicObject argsArray ,
159
165
@ Cached ("create()" ) ArrayToObjectArrayNode arrayToObjectArrayNode ,
160
166
@ Cached ("EXECUTE.createNode()" ) Node executeNode ,
161
- @ Cached ("create()" ) BranchProfile exceptionProfile ) {
167
+ @ Cached ("create()" ) BranchProfile exceptionProfile ,
168
+ @ Cached ("createBinaryProfile()" ) ConditionProfile ownedProfile ) {
162
169
final Object [] args = arrayToObjectArrayNode .executeToObjectArray (argsArray );
163
170
164
171
if (getContext ().getOptions ().CEXT_LOCK ) {
165
172
final ReentrantLock lock = getContext ().getCExtensionsLock ();
173
+ boolean owned = lock .isHeldByCurrentThread ();
166
174
167
- MutexOperations .unlockInternal (lock );
168
- try {
175
+ if (ownedProfile .profile (owned )) {
176
+ MutexOperations .unlockInternal (lock );
177
+ try {
178
+ return execute (receiver , args , executeNode , exceptionProfile );
179
+ } finally {
180
+ MutexOperations .lockInternal (getContext (), lock , this );
181
+ }
182
+ } else {
169
183
return execute (receiver , args , executeNode , exceptionProfile );
170
- } finally {
171
- MutexOperations .lockInternal (getContext (), lock , this );
172
184
}
173
185
} else {
174
186
return execute (receiver , args , executeNode , exceptionProfile );
0 commit comments