Skip to content
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

Add explanation about @critical_section target directive #1246

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Changes from 2 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
31 changes: 31 additions & 0 deletions development-tools/clinic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1996,6 +1996,37 @@ The generated glue code looks like this:
return return_value;
}

If you want to specify a target object to be locked then you can use a target directive for
``@critical_section``. Since the ``@critical_section`` locks the object which is located
as the first argument implicitly, you will need this feature when the ``@critical_section``
locks an unwanted target object. Note that explicit target declaration only supports up to 2 objects.

Example from :cpy-file:`Modules/_weakref.c`::
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved

/*[clinic input]
@critical_section object
_weakref.getweakrefcount -> Py_ssize_t

object: object
corona10 marked this conversation as resolved.
Show resolved Hide resolved
/
Return the number of weak references to 'object'.
[clinic start generated code]*/

The generated glue code looks like this:

.. code-block:: c

static PyObject *
_weakref_getweakrefs(PyObject *module, PyObject *object)
{
PyObject *return_value = NULL;

Py_BEGIN_CRITICAL_SECTION(object);
return_value = _weakref_getweakrefs_impl(module, object);
Py_END_CRITICAL_SECTION();

return return_value;
}

.. versionadded:: 3.13

Expand Down
Loading