Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Bénédikt Tran <[email protected]>
  • Loading branch information
ZeroIntensity and picnixz authored Jan 6, 2025
commit 9f272d94a848ab109322477e2a82cfeb5bd5ac2f
14 changes: 8 additions & 6 deletions Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -872,15 +872,17 @@ invalid non-\ ``NULL`` pointers would crash Python)::

.. _ctypes-thread-safety:

Thread Safety Without The GIL
Thread safety without the GIL
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In Python 3.13, the :term:`GIL` may be disabled on :term:`experimental free threaded <free threading>` builds.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is orthogonal but whenever I review FT PRs, we always use :term:`free threaded `. I believe we can add a new term for free threaded which points to free threading so that we can reduce the docs burden.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A free threaded term sounds like a good idea, but that should probably be in a larger follow-up PR. I'll play around with the wording here though.

In ctypes, reads and writes to a single object concurrently is safe, but not across multiple objects.::
In ctypes, reads and writes to a single object concurrently is safe, but not across multiple objects:

>>> number = c_int(42)
>>> pointer_a = pointer(number)
>>> pointer_b = pointer(number)
.. code-block:: pycon

>>> number = c_int(42)
>>> pointer_a = pointer(number)
>>> pointer_b = pointer(number)

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.
Comment thread
ZeroIntensity marked this conversation as resolved.
Outdated
So, ``pointer_a`` can be shared and written to across multiple threads, but only if ``pointer_b``
Expand All @@ -898,7 +900,7 @@ to synchronize access to memory.::

.. seealso::

:func:`sys._is_gil_enabled` if you would like to dynamically synchronize your application.
Use :func:`sys._is_gil_enabled` to dynamically synchronize your application.

.. _ctypes-type-conversions:

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ def run():
buffer[0] = b"j"

with threading_helper.catch_threading_exception() as cm:
with threading_helper.start_threads((Thread(target=run) for _ in range(25))):
threads = (Thread(target=run) for _ in range(25))
with threading_helper.start_threads(threads):
pass

self.assertIsNone(cm.exc_value)
Comment thread
ZeroIntensity marked this conversation as resolved.
Outdated
Expand Down
4 changes: 2 additions & 2 deletions Modules/_ctypes/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,15 +562,15 @@ static inline void
locked_memcpy_to(CDataObject *self, void *buf, Py_ssize_t size)
{
LOCK_PTR(self);
memcpy(self->b_ptr, buf, size);
(void)memcpy(self->b_ptr, buf, size);
UNLOCK_PTR(self);
}

static inline void
locked_memcpy_from(void *buf, CDataObject *self, Py_ssize_t size)
{
LOCK_PTR(self);
memcpy(buf, self->b_ptr, size);
(void)memcpy(buf, self->b_ptr, size);
UNLOCK_PTR(self);
}

Expand Down