2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-22 05:22:45 +00:00

Use re-entrant mutex to protect global state.

Add pymutex.hpp which implements a re-entrant mutex on top of Python's
PyMutex.  Add BOOST_PYTHON_LOCK_STATE() macro that uses RAII to lock
mutable global state as required.
This commit is contained in:
Neil Schemenauer
2025-10-13 22:42:42 -07:00
parent 6f5f3b6607
commit cfbefe893c
6 changed files with 147 additions and 6 deletions

View File

@@ -11,6 +11,7 @@
#include <boost/python/handle.hpp>
#include <boost/python/detail/raw_pyobject.hpp>
#include <boost/python/detail/pymutex.hpp>
#include <boost/python/cast.hpp>
#include <vector>
@@ -145,6 +146,8 @@ namespace
inline bool visit(rvalue_from_python_chain const* chain)
{
BOOST_PYTHON_LOCK_STATE();
visited_t::iterator const p = std::lower_bound(visited.begin(), visited.end(), chain);
if (p != visited.end() && *p == chain)
return false;
@@ -157,9 +160,11 @@ namespace
{
unvisit(rvalue_from_python_chain const* chain)
: chain(chain) {}
~unvisit()
{
BOOST_PYTHON_LOCK_STATE();
visited_t::iterator const p = std::lower_bound(visited.begin(), visited.end(), chain);
assert(p != visited.end());
visited.erase(p);