/src/pybind11/include/pybind11/gil_simple.h
Line | Count | Source |
1 | | // Copyright (c) 2016-2025 The Pybind Development Team. |
2 | | // All rights reserved. Use of this source code is governed by a |
3 | | // BSD-style license that can be found in the LICENSE file. |
4 | | |
5 | | #pragma once |
6 | | |
7 | | #include "detail/common.h" |
8 | | |
9 | | #include <cassert> |
10 | | |
11 | | PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) |
12 | | |
13 | | class gil_scoped_acquire_simple { |
14 | | PyGILState_STATE state; |
15 | | |
16 | | public: |
17 | 0 | gil_scoped_acquire_simple() : state{PyGILState_Ensure()} {} |
18 | | gil_scoped_acquire_simple(const gil_scoped_acquire_simple &) = delete; |
19 | | gil_scoped_acquire_simple &operator=(const gil_scoped_acquire_simple &) = delete; |
20 | 0 | ~gil_scoped_acquire_simple() { PyGILState_Release(state); } |
21 | | }; |
22 | | |
23 | | class gil_scoped_release_simple { |
24 | | PyThreadState *state; |
25 | | |
26 | | public: |
27 | | // PRECONDITION: The GIL must be held when this constructor is called. |
28 | 0 | gil_scoped_release_simple() { |
29 | 0 | assert(PyGILState_Check()); |
30 | 0 | state = PyEval_SaveThread(); |
31 | 0 | } |
32 | | gil_scoped_release_simple(const gil_scoped_release_simple &) = delete; |
33 | | gil_scoped_release_simple &operator=(const gil_scoped_release_simple &) = delete; |
34 | 0 | ~gil_scoped_release_simple() { PyEval_RestoreThread(state); } |
35 | | }; |
36 | | |
37 | | PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |