Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PythonCall"
uuid = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
authors = ["Christopher Doris <github.com/cjdoris>"]
version = "0.9.35"
version = "0.9.36"

[deps]
CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
Expand Down
28 changes: 28 additions & 0 deletions src/GIL/GIL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ if Base.VERSION ≥ v"1.11"
)
end

const LOCK = ReentrantLock()

function __init__()
Base.lock(LOCK)
return nothing
end

"""
lock(f)
Expand All @@ -42,11 +48,16 @@ See [`@lock`](@ref) for the macro form.
This function is experimental. Its semantics may be changed without notice.
"""
function lock(f)
Base.lock(LOCK)
task = current_task()
sticky, task.sticky = task.sticky, true
state = C.PyGILState_Ensure()
try
f()
finally
C.PyGILState_Release(state)
task.sticky = sticky
Base.unlock(LOCK)
end
end

Expand All @@ -67,11 +78,16 @@ The macro equivalent of [`lock`](@ref).
"""
macro lock(expr)
quote
Base.lock($LOCK)
task = current_task()
sticky, task.sticky = task.sticky, true
state = C.PyGILState_Ensure()
try
$(esc(expr))
finally
C.PyGILState_Release(state)
task.sticky = sticky
Base.unlock($LOCK)
end
end
end
Expand All @@ -92,11 +108,17 @@ See [`@unlock`](@ref) for the macro form.
This function is experimental. Its semantics may be changed without notice.
"""
function unlock(f)
if !islocked(LOCK)
error("GIL is not held")
end

Base.unlock(LOCK)
state = C.PyEval_SaveThread()
try
f()
finally
C.PyEval_RestoreThread(state)
Base.lock(LOCK)
end
end

Expand All @@ -117,11 +139,17 @@ The macro equivalent of [`unlock`](@ref).
"""
macro unlock(expr)
quote
if !islocked($LOCK)
error("GIL is not held")
end

Base.unlock($LOCK)
state = C.PyEval_SaveThread()
try
$(esc(expr))
finally
C.PyEval_RestoreThread(state)
Base.lock($LOCK)
end
end
end
Expand Down
Loading