Skip to content

Commit 43a4e58

Browse files
committed
add GIL: PBuiltinFunction
1 parent 8adf9b7 commit 43a4e58

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PBuiltinFunction.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
4848
import com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode;
4949
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
50+
import com.oracle.graal.python.runtime.GilNode;
5051
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5152
import com.oracle.truffle.api.CompilerAsserts;
5253
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -215,19 +216,29 @@ public Object getLazyPythonClass() {
215216
// type(None).__eq__.__get__(None, type(None)) wouldn't bind the method correctly
216217
public Object callUnboundMethodWithState(ThreadState state, Object receiver, Object[] arguments,
217218
@Shared("gotState") @Cached ConditionProfile gotState,
218-
@Shared("callMethod") @Cached CallUnboundMethodNode call) {
219-
VirtualFrame frame = null;
220-
if (gotState.profile(state != null)) {
221-
frame = PArguments.frameForCall(state);
219+
@Shared("callMethod") @Cached CallUnboundMethodNode call, @Shared("gil") @Cached GilNode gil) {
220+
boolean mustRelease = gil.acquire();
221+
try {
222+
VirtualFrame frame = null;
223+
if (gotState.profile(state != null)) {
224+
frame = PArguments.frameForCall(state);
225+
}
226+
return call.execute(frame, this, receiver, arguments);
227+
} finally {
228+
gil.release(mustRelease);
222229
}
223-
return call.execute(frame, this, receiver, arguments);
224230
}
225231

226232
@ExportMessage
227233
public Object callUnboundMethodIgnoreGetExceptionWithState(ThreadState state, Object receiver, Object[] arguments,
228234
@Shared("gotState") @Cached ConditionProfile gotState,
229-
@Shared("callMethod") @Cached CallUnboundMethodNode call) {
230-
return callUnboundMethodWithState(state, receiver, arguments, gotState, call);
235+
@Shared("callMethod") @Cached CallUnboundMethodNode call, @Shared("gil") @Cached GilNode gil) {
236+
boolean mustRelease = gil.acquire();
237+
try {
238+
return callUnboundMethodWithState(state, receiver, arguments, gotState, call, gil);
239+
} finally {
240+
gil.release(mustRelease);
241+
}
231242
}
232243

233244
@GenerateUncached

scripts/gil.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,24 @@ def file_names_filter(f_name, names):
200200

201201

202202
def main(sources, add=True, dry_run=True, check_style=True, single_source=False, source_filter=None,
203-
ignore_filter=None):
203+
ignore_filter=None, count=False):
204204
files = glob.glob("{}**/*.java".format(sources), recursive=True)
205-
if ignore_filter:
205+
if ignore_filter and not count:
206206
files = list(filter(lambda f: not file_names_filter(f, ignore_filter), files))
207-
if source_filter:
207+
if source_filter and not count:
208208
files = list(filter(lambda f: file_names_filter(f, source_filter), files))
209209

210+
cnt = 0
210211
for java_file in files:
211212
with open(java_file, 'r+') as SRC:
212213
source = SRC.read()
213214
if add:
214215
messages, shared = get_messages(source, PTRN_MESSAGE)
215216
if len(messages) > 0:
216-
# if sum(map(lambda m: 0 if m.is_with_gil else 1, messages)) == 0:
217+
if count:
218+
cnt += 1
219+
continue
220+
217221
if 'GilNode gil' in source:
218222
print("[skipping] {}".format(java_file))
219223
continue
@@ -245,7 +249,10 @@ def main(sources, add=True, dry_run=True, check_style=True, single_source=False,
245249
else:
246250
print("removal of the GIL not yet supported")
247251
return
248-
if check_style:
252+
253+
if count:
254+
print("TO PROCESS: {} files".format(cnt))
255+
if check_style and not count:
249256
# running the checkstyle gate (twice)
250257
for i in range(2):
251258
os.system("mx python-gate --tags style,python-license")
@@ -255,6 +262,7 @@ def main(sources, add=True, dry_run=True, check_style=True, single_source=False,
255262
parser = argparse.ArgumentParser()
256263
parser.add_argument("--dry_run", help="do not write any changes, stop after the first file transform",
257264
action="store_true")
265+
parser.add_argument("--count", help="count how many files may need the GIL", action="store_true")
258266
parser.add_argument("--remove", help="remove the GIL", action="store_true")
259267
parser.add_argument("--no_style", help="do not run the style checker", action="store_true")
260268
parser.add_argument("--single", help="stop after modifying the first source", action="store_true")
@@ -264,4 +272,4 @@ def main(sources, add=True, dry_run=True, check_style=True, single_source=False,
264272
args = parser.parse_args()
265273

266274
main(args.sources, add=not args.remove, dry_run=args.dry_run, check_style=not args.no_style,
267-
single_source=args.single, source_filter=args.filter, ignore_filter=args.ignore)
275+
single_source=args.single, source_filter=args.filter, ignore_filter=args.ignore, count=args.count)

0 commit comments

Comments
 (0)