-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathGIL.jl
More file actions
56 lines (52 loc) · 1.79 KB
/
GIL.jl
File metadata and controls
56 lines (52 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@testitem "unlock and lock" begin
# This calls Python's time.sleep(1) twice concurrently. Since sleep() unlocks the
# GIL, these can happen in parallel if Julia has at least 2 threads.
function threaded_sleep()
PythonCall.GIL.unlock() do
Threads.@threads for i = 1:2
PythonCall.GIL.lock() do
pyimport("time").sleep(1)
end
end
end
end
# one run to ensure it's compiled
threaded_sleep()
# now time it
t = @timed threaded_sleep()
# if we have at least 2 threads, the sleeps run in parallel and take about a second
if Threads.nthreads() ≥ 2
@test 0.9 < t.time < 1.2
end
@test PythonCall.GIL.hasgil()
PythonCall.GIL.unlock() do
@test !Base.islocked(PythonCall.GIL._jl_gil_lock)
PythonCall.GIL.lock() do
@test Base.islocked(PythonCall.GIL._jl_gil_lock)
end
end
end
@testitem "@unlock and @lock" begin
# This calls Python's time.sleep(1) twice concurrently. Since sleep() unlocks the
# GIL, these can happen in parallel if Julia has at least 2 threads.
function threaded_sleep()
PythonCall.GIL.@unlock Threads.@threads for i = 1:2
PythonCall.GIL.@lock pyimport("time").sleep(1)
end
end
# one run to ensure it's compiled
threaded_sleep()
# now time it
t = @timed threaded_sleep()
# if we have at least 2 threads, the sleeps run in parallel and take about a second
if Threads.nthreads() ≥ 2
@test 0.9 < t.time < 1.2
end
@test PythonCall.GIL.hasgil()
PythonCall.GIL.@unlock begin
@test !Base.islocked(PythonCall.GIL._jl_gil_lock)
PythonCall.GIL.@lock begin
@test Base.islocked(PythonCall.GIL._jl_gil_lock)
end
end
end