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

gh-117511: Make PyMutex public in the non-limited API #117731

Merged
merged 14 commits into from
Jun 20, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Apr 10, 2024

Doc/c-api/init.rst Outdated Show resolved Hide resolved
Doc/c-api/init.rst Outdated Show resolved Hide resolved
Doc/c-api/init.rst Outdated Show resolved Hide resolved
@colesbury colesbury marked this pull request as ready for review June 17, 2024 19:38
@colesbury colesbury requested a review from a team as a code owner June 17, 2024 19:38
@colesbury
Copy link
Contributor Author

@encukou @vstinner - I've updated the docs based on capi-workgroup/decisions#22 (comment).

I'm not sure of the best way to make the functions also exported as regular functions, particularly with the same name. It's easy to export them with different names and _PyMutex_LockSlow and _PyMutex_UnlockSlow are effectively that.

@vstinner
Copy link
Member

I'm not sure of the best way to make the functions also exported as regular functions, particularly with the same name.

You can look at my PR which does exactly that just for Py_TYPE(): https://github.com/python/cpython/pull/120601/files

In short, add an underscore prefix to the static inline function, then use a macro to "rename" the static inline function.


#ifndef Py_LIMITED_API
# define Py_CPYTHON_LOCK_H
# include "cpython/lock.h"
Copy link
Member

Choose a reason for hiding this comment

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

I don't see the value of such header file. Just include cpython/lock.h in Python.h, and check Py_LIMITED_API in cpython/lock.h.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ericsnowcurrently expressed a preference for this style when pyatomic.h was added: #109344 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

IMO, it's helpful to follow a consistent pattern when it comes to the Include/cpython header files. That means in some cases we end up with very minimal header files like this in Include/.

Copy link
Member

Choose a reason for hiding this comment

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

If you mention consistency, there are already many header files in Include/cpython/ which have no companion Include/ header file:

$ grep cpython/ Include/Python.h
#include "cpython/pydebug.h"
#include "cpython/longintrepr.h"
#include "cpython/odictobject.h"
#include "cpython/funcobject.h"
#include "cpython/classobject.h"
#include "cpython/code.h"
#include "cpython/cellobject.h"
#include "cpython/initconfig.h"
#include "cpython/genobject.h"
#include "cpython/picklebufobject.h"
#include "cpython/pytime.h"
#include "cpython/context.h"
#include "cpython/pyctype.h"
#include "cpython/pyfpe.h"
#include "cpython/tracemalloc.h"
#include "cpython/optimizer.h"

@@ -2372,7 +2372,7 @@ new_reference(PyObject *op)
#else
op->ob_tid = _Py_ThreadId();
op->_padding = 0;
op->ob_mutex = (struct _PyMutex){ 0 };
op->ob_mutex = (PyMutex){ 0 };
Copy link
Member

Choose a reason for hiding this comment

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

If there is no public PyMutex_STATIC_INIT, can you maybe add a private one in pycore_lock.h?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's consider that in a separate PR:

  • It's not directly related to making the API public
  • It would make sense to consider that for multiple types (e.g., PyEvent, _PyOnceFlag), not just PyMutex

Python/lock.c Outdated
void
_PyMutex_LockSlow(PyMutex *m)
PyMutex_Lock(PyMutex *m)
Copy link
Member

Choose a reason for hiding this comment

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

You might move it to the end to still use the static inline macro in the following lines.

Include/cpython/lock.h Show resolved Hide resolved
Doc/c-api/init.rst Show resolved Hide resolved
@colesbury colesbury added the needs backport to 3.13 bugs and security fixes label Jun 18, 2024
Doc/c-api/init.rst Outdated Show resolved Hide resolved

#ifndef Py_LIMITED_API
# define Py_CPYTHON_LOCK_H
# include "cpython/lock.h"
Copy link
Member

Choose a reason for hiding this comment

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

If you mention consistency, there are already many header files in Include/cpython/ which have no companion Include/ header file:

$ grep cpython/ Include/Python.h
#include "cpython/pydebug.h"
#include "cpython/longintrepr.h"
#include "cpython/odictobject.h"
#include "cpython/funcobject.h"
#include "cpython/classobject.h"
#include "cpython/code.h"
#include "cpython/cellobject.h"
#include "cpython/initconfig.h"
#include "cpython/genobject.h"
#include "cpython/picklebufobject.h"
#include "cpython/pytime.h"
#include "cpython/context.h"
#include "cpython/pyctype.h"
#include "cpython/pyfpe.h"
#include "cpython/tracemalloc.h"
#include "cpython/optimizer.h"

Doc/c-api/init.rst Show resolved Hide resolved
Python/lock.c Show resolved Hide resolved
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the different update and the doc.

@colesbury got an exception from @Yhg1s (3.13 release manager) to add this API to Python 3.13, god.

@colesbury colesbury merged commit 3af7263 into python:main Jun 20, 2024
38 checks passed
@miss-islington-app
Copy link

Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app
Copy link

Sorry, @colesbury, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 3af7263037de1d0ef63b070fc7bfc2cf042eaebe 3.13

@colesbury colesbury deleted the gh-117511-pymutex branch June 20, 2024 15:29
colesbury added a commit to colesbury/cpython that referenced this pull request Jun 20, 2024
colesbury added a commit to colesbury/cpython that referenced this pull request Jun 20, 2024
colesbury added a commit to colesbury/cpython that referenced this pull request Jun 20, 2024
@bedevere-app
Copy link

bedevere-app bot commented Jun 20, 2024

GH-120800 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jun 20, 2024
colesbury added a commit that referenced this pull request Jun 20, 2024
@vstinner
Copy link
Member

Yeah, congrats!

@erlend-aasland
Copy link
Contributor

@colesbury got an exception from @Yhg1s (3.13 release manager) to add this API to Python 3.13, god.

That is very good news! Glad to see this land.

mrahtz pushed a commit to mrahtz/cpython that referenced this pull request Jun 30, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
@colesbury colesbury removed their assignment Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants