forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-117511: Make PyMutex public in the non-limited API
- Loading branch information
Showing
10 changed files
with
125 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef Py_CPYTHON_LOCK_H | ||
# error "this header file must not be included directly" | ||
#endif | ||
|
||
#define _Py_UNLOCKED 0 | ||
#define _Py_LOCKED 1 | ||
|
||
// A mutex that occupies one byte. The lock can be zero initialized. | ||
// | ||
// Only the two least significant bits are used. The remaining bits should be | ||
// zero: | ||
// 0b00: unlocked | ||
// 0b01: locked | ||
// 0b10: unlocked and has parked threads | ||
// 0b11: locked and has parked threads | ||
// | ||
// Typical initialization: | ||
// PyMutex m = (PyMutex){0}; | ||
// | ||
// Or initialize as global variables: | ||
// static PyMutex m; | ||
// | ||
// Typical usage: | ||
// PyMutex_Lock(&m); | ||
// ... | ||
// PyMutex_Unlock(&m); | ||
typedef struct _PyMutex { | ||
uint8_t v; | ||
} PyMutex; | ||
|
||
// (private) slow path for locking the mutex | ||
PyAPI_FUNC(void) _PyMutex_LockSlow(PyMutex *m); | ||
|
||
// (private) slow path for unlocking the mutex | ||
PyAPI_FUNC(void) _PyMutex_UnlockSlow(PyMutex *m); | ||
|
||
// Locks the mutex. | ||
// | ||
// If the mutex is currently locked, the calling thread will be parked until | ||
// the mutex is unlocked. If the current thread holds the GIL, then the GIL | ||
// will be released while the thread is parked. | ||
static inline void | ||
PyMutex_Lock(PyMutex *m) | ||
{ | ||
uint8_t expected = _Py_UNLOCKED; | ||
if (!_Py_atomic_compare_exchange_uint8(&m->v, &expected, _Py_LOCKED)) { | ||
_PyMutex_LockSlow(m); | ||
} | ||
} | ||
|
||
// Unlocks the mutex. | ||
static inline void | ||
PyMutex_Unlock(PyMutex *m) | ||
{ | ||
uint8_t expected = _Py_LOCKED; | ||
if (!_Py_atomic_compare_exchange_uint8(&m->v, &expected, _Py_UNLOCKED)) { | ||
_PyMutex_UnlockSlow(m); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef Py_LOCK_H | ||
#define Py_LOCK_H | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef Py_LIMITED_API | ||
# define Py_CPYTHON_LOCK_H | ||
# include "cpython/lock.h" | ||
# undef Py_CPYTHON_LOCK_H | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif /* !Py_LOCK_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters