Thread-safe garbage collection v3 (ReentrantLock version)#1074
Thread-safe garbage collection v3 (ReentrantLock version)#1074MilesCranmer wants to merge 5 commits intoJuliaPy:masterfrom
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #1074 +/- ##
==========================================
- Coverage 67.75% 67.73% -0.02%
==========================================
Files 20 20
Lines 2025 2030 +5
==========================================
+ Hits 1372 1375 +3
- Misses 653 655 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
|
You might want to try a test-and-test-set pattern: Basically |
|
Wait isn't that what this already does? Or you mean replacing the |
|
Yes, use |
bf2c3eb to
9dbe833
Compare
|
Done! |
|
(Edit: I implemented this as it seems the safer option. Not sure what happens when two threads try to write Old: do we need to make the mutable struct PyBuffer
buf::Py_buffer
PyBuffer() = begin
b = new(Py_buffer(C_NULL, PyPtr_NULL, 0, 0,
0, 0, C_NULL, C_NULL, C_NULL, C_NULL,
C_NULL, C_NULL, C_NULL))
finalizer(pydecref, b)
return b
end
endwhich references: function pydecref(o::PyBuffer)
# note that PyBuffer_Release sets o.obj to NULL, and
# is a no-op if o.obj is already NULL
_finalized[] || ccall(@pysym(:PyBuffer_Release), Cvoid, (Ref{PyBuffer},), o)
o
endNot sure if this is needed or not. |
|
By the way, do you know what the threading advice actually is for the Python C API. Is it only one thread at a time, or only call from the same thread? |
|
It might be time to drop Python 2.7 CI |
Done! #1075 |
I don't see anything related to multi-threading here https://docs.python.org/3/c-api/refcounting.html. Would they have a particular requirement? |
|
https://docs.python.org/3/c-api/init.html#non-python-created-threads
|
|
Ok, we're going to need a redesign here since Julia GC is now multithreaded. Now the question is do we really |
I feel like it would be quite a bit of work to properly incorporate the Python GIL into Julia code. That means you would need to thread-lock every single part of PyCall.jl, no? Thread-locking the garbage collection is the major issue IMO, but it also seems easier to solve (with just this PR)? |
|
Pinging this thread before my teaching starts (and I get buried in work) :) |
|
@stevengj , have you had a chance to take a look? |
This is an alternative strategy to make the GC thread-safe compared with #1073 (#883).
@stevengj @mkitti @marius311
My concern with #1073 is whether only allowing GC to run on a particular thread might cause an issue or lag in performing garbage collection. Instead this allows the GC to run on any thread but they must obtain a lock first. This is the logic:
One question I have is: does
islockedwork in a re-entrant context? (Or does it even matter, and I could just use a SpinLock?)