|
| 1 | +// Copyright 2025 Boost.Python Contributors |
| 2 | +// Distributed under the Boost Software License, Version 1.0. (See |
| 3 | +// accompanying file LICENSE_1_0.txt or copy at |
| 4 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +#ifndef BOOST_PYTHON_DETAIL_PYMUTEX_HPP |
| 7 | +#define BOOST_PYTHON_DETAIL_PYMUTEX_HPP |
| 8 | + |
| 9 | +#include <boost/python/detail/prefix.hpp> |
| 10 | +#ifdef Py_GIL_DISABLED |
| 11 | +// needed for pymutex wrapper |
| 12 | +#include <atomic> |
| 13 | +#include <cstddef> |
| 14 | +#endif |
| 15 | + |
| 16 | +namespace boost { namespace python { namespace detail { |
| 17 | + |
| 18 | +#ifdef Py_GIL_DISABLED |
| 19 | + |
| 20 | +// Re-entrant wrapper around PyMutex for free-threaded Python |
| 21 | +// Similar to _PyRecursiveMutex or threading.RLock |
| 22 | +class pymutex { |
| 23 | + PyMutex m_mutex; |
| 24 | + std::atomic<unsigned long> m_owner; |
| 25 | + std::size_t m_level; |
| 26 | + |
| 27 | +public: |
| 28 | + pymutex() : m_mutex({}), m_owner(0), m_level(0) {} |
| 29 | + |
| 30 | + // Non-copyable, non-movable |
| 31 | + pymutex(const pymutex&) = delete; |
| 32 | + pymutex& operator=(const pymutex&) = delete; |
| 33 | + |
| 34 | + void lock() { |
| 35 | + unsigned long thread = PyThread_get_thread_ident(); |
| 36 | + if (m_owner.load(std::memory_order_relaxed) == thread) { |
| 37 | + m_level++; |
| 38 | + return; |
| 39 | + } |
| 40 | + PyMutex_Lock(&m_mutex); |
| 41 | + m_owner.store(thread, std::memory_order_relaxed); |
| 42 | + // m_level should be 0 when we acquire the lock |
| 43 | + } |
| 44 | + |
| 45 | + void unlock() { |
| 46 | + unsigned long thread = PyThread_get_thread_ident(); |
| 47 | + // Verify current thread owns the lock |
| 48 | + if (m_owner.load(std::memory_order_relaxed) != thread) { |
| 49 | + // This should never happen - programming error |
| 50 | + return; |
| 51 | + } |
| 52 | + if (m_level > 0) { |
| 53 | + m_level--; |
| 54 | + return; |
| 55 | + } |
| 56 | + m_owner.store(0, std::memory_order_relaxed); |
| 57 | + PyMutex_Unlock(&m_mutex); |
| 58 | + } |
| 59 | + |
| 60 | + bool is_locked_by_current_thread() const { |
| 61 | + unsigned long thread = PyThread_get_thread_ident(); |
| 62 | + return m_owner.load(std::memory_order_relaxed) == thread; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | + |
| 67 | +// RAII lock guard for pymutex |
| 68 | +class pymutex_guard { |
| 69 | + pymutex& m_mutex; |
| 70 | + |
| 71 | +public: |
| 72 | + explicit pymutex_guard(pymutex& mutex) : m_mutex(mutex) { |
| 73 | + m_mutex.lock(); |
| 74 | + } |
| 75 | + |
| 76 | + ~pymutex_guard() { |
| 77 | + m_mutex.unlock(); |
| 78 | + } |
| 79 | + |
| 80 | + // Non-copyable, non-movable |
| 81 | + pymutex_guard(const pymutex_guard&) = delete; |
| 82 | + pymutex_guard& operator=(const pymutex_guard&) = delete; |
| 83 | +}; |
| 84 | + |
| 85 | +// Global mutex for protecting all Boost.Python internal state |
| 86 | +// Similar to pybind11's internals.mutex |
| 87 | +BOOST_PYTHON_DECL pymutex& get_global_mutex(); |
| 88 | + |
| 89 | +// Macro for acquiring the global lock |
| 90 | +// Similar to pybind11's PYBIND11_LOCK_INTERNALS |
| 91 | +#define BOOST_PYTHON_LOCK_STATE() \ |
| 92 | + ::boost::python::detail::pymutex_guard lock(::boost::python::detail::get_global_mutex()) |
| 93 | + |
| 94 | +#else |
| 95 | + |
| 96 | +// No-op macro when not in free-threaded mode |
| 97 | +#define BOOST_PYTHON_LOCK_STATE() |
| 98 | + |
| 99 | +#endif // Py_GIL_DISABLED |
| 100 | + |
| 101 | +}}} // namespace boost::python::detail |
| 102 | + |
| 103 | +#endif // BOOST_PYTHON_DETAIL_PYMUTEX_HPP |
0 commit comments