-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-128182: Add per-object memory access synchronization to ctypes
#128490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
3bb21ff
8c4c49a
da293c1
d01b757
8bb980f
d60f2e2
de4c4ea
23d8e21
bdee2b2
9f272d9
32a885f
1c92b4b
ff34cc2
804f1b6
bae3c5b
d720d9a
04b3252
6401bbc
9d35e86
ce94234
d6a4bc8
5a4c1d6
774a6ec
c8f2cd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -870,6 +870,35 @@ invalid non-\ ``NULL`` pointers would crash Python):: | |
| ValueError: NULL pointer access | ||
| >>> | ||
|
|
||
| .. _ctypes-thread-safety: | ||
|
|
||
| Thread Safety Without The GIL | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| In Python 3.13, the :term:`GIL` may be disabled on :term:`experimental free threaded <free threading>` builds. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is orthogonal but whenever I review FT PRs, we always use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A |
||
| In ctypes, reads and writes to a single object concurrently is safe, but not across multiple objects.:: | ||
ZeroIntensity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| >>> number = c_int(42) | ||
| >>> pointer_a = pointer(number) | ||
| >>> pointer_b = pointer(number) | ||
ZeroIntensity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| In the above, it's only safe for one object to read and write to the address at once if the :term:`GIL` is disabled. | ||
ZeroIntensity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| So, ``pointer_a`` can be shared and written to across multiple threads, but only if ``pointer_b`` | ||
| is not also attempting to do the same. If this is an issue, consider using a :class:`threading.Lock` | ||
| to synchronize access to memory.:: | ||
ZeroIntensity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| >>> import threading | ||
ZeroIntensity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| >>> lock = threading.Lock() | ||
| >>> # Thread 1 | ||
| >>> with lock: | ||
| ... pointer_a.contents = 24 | ||
| >>> # Thread 2 | ||
| >>> with lock: | ||
| ... pointer_b.contents = 42 | ||
|
|
||
| .. seealso:: | ||
|
|
||
| :func:`sys._is_gil_enabled` if you would like to dynamically synchronize your application. | ||
ZeroIntensity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| .. _ctypes-type-conversions: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix crash when using :mod:`ctypes` pointers concurrently on the :term:`free | ||
| threaded <free threading>` build. |
Uh oh!
There was an error while loading. Please reload this page.