Skip to content

Factor out pybind11/gil_simple.h #5614

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

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ set(PYBIND11_HEADERS
include/pybind11/eval.h
include/pybind11/gil.h
include/pybind11/gil_safe_call_once.h
include/pybind11/gil_simple.h
include/pybind11/iostream.h
include/pybind11/functional.h
include/pybind11/native_enum.h
Expand Down
23 changes: 4 additions & 19 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@

#pragma once

#include "common.h"

#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
# include <pybind11/gil.h>
#endif

#include <pybind11/conduit/pybind11_platform_abi_id.h>
#include <pybind11/gil_simple.h>
#include <pybind11/pytypes.h>

#include "common.h"

#include <exception>
#include <mutex>
#include <thread>
Expand Down Expand Up @@ -425,19 +422,7 @@ PYBIND11_NOINLINE internals &get_internals() {
return **internals_pp;
}

#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
gil_scoped_acquire gil;
#else
// Ensure that the GIL is held since we will need to make Python calls.
// Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
struct gil_scoped_acquire_local {
gil_scoped_acquire_local() : state(PyGILState_Ensure()) {}
gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete;
gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete;
~gil_scoped_acquire_local() { PyGILState_Release(state); }
const PyGILState_STATE state;
} gil;
#endif
gil_scoped_acquire_simple gil;
error_scope err_scope;

dict state_dict = get_python_state_dict();
Expand Down
53 changes: 17 additions & 36 deletions include/pybind11/gil.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@

#pragma once

#include "detail/common.h"
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)

#include <cassert>
# include "detail/common.h"
# include "gil_simple.h"

#if !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

using gil_scoped_acquire = gil_scoped_acquire_simple;
using gil_scoped_release = gil_scoped_release_simple;

PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

#else

# include "detail/common.h"
# include "detail/internals.h"
#endif

# include <cassert>

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

Expand All @@ -26,8 +37,6 @@ PyThreadState *get_thread_state_unchecked();

PYBIND11_NAMESPACE_END(detail)

#if !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)

/* The functions below essentially reproduce the PyGILState_* API using a RAII
* pattern, but there are a few important differences:
*
Expand Down Expand Up @@ -182,34 +191,6 @@ class gil_scoped_release {
bool active = true;
};

#else // PYBIND11_SIMPLE_GIL_MANAGEMENT

class gil_scoped_acquire {
PyGILState_STATE state;

public:
gil_scoped_acquire() : state{PyGILState_Ensure()} {}
gil_scoped_acquire(const gil_scoped_acquire &) = delete;
gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;
~gil_scoped_acquire() { PyGILState_Release(state); }
void disarm() {}
};

class gil_scoped_release {
PyThreadState *state;

public:
// PRECONDITION: The GIL must be held when this constructor is called.
gil_scoped_release() {
assert(PyGILState_Check());
state = PyEval_SaveThread();
}
gil_scoped_release(const gil_scoped_release &) = delete;
gil_scoped_release &operator=(const gil_scoped_release &) = delete;
~gil_scoped_release() { PyEval_RestoreThread(state); }
void disarm() {}
};

#endif // PYBIND11_SIMPLE_GIL_MANAGEMENT

PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

#endif // !PYBIND11_SIMPLE_GIL_MANAGEMENT
37 changes: 37 additions & 0 deletions include/pybind11/gil_simple.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2016-2025 The Pybind Development Team.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#pragma once

#include "detail/common.h"

#include <cassert>

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

class gil_scoped_acquire_simple {
PyGILState_STATE state;

public:
gil_scoped_acquire_simple() : state{PyGILState_Ensure()} {}
gil_scoped_acquire_simple(const gil_scoped_acquire_simple &) = delete;
gil_scoped_acquire_simple &operator=(const gil_scoped_acquire_simple &) = delete;
~gil_scoped_acquire_simple() { PyGILState_Release(state); }
};

class gil_scoped_release_simple {
PyThreadState *state;

public:
// PRECONDITION: The GIL must be held when this constructor is called.
gil_scoped_release_simple() {
assert(PyGILState_Check());
state = PyEval_SaveThread();
}
gil_scoped_release_simple(const gil_scoped_release_simple &) = delete;
gil_scoped_release_simple &operator=(const gil_scoped_release_simple &) = delete;
~gil_scoped_release_simple() { PyEval_RestoreThread(state); }
};

PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
1 change: 1 addition & 0 deletions tests/extra_python_package/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"include/pybind11/functional.h",
"include/pybind11/gil.h",
"include/pybind11/gil_safe_call_once.h",
"include/pybind11/gil_simple.h",
"include/pybind11/iostream.h",
"include/pybind11/native_enum.h",
"include/pybind11/numpy.h",
Expand Down
Loading