Skip to content

Commit

Permalink
nanobind: use gil_scoped_acquire (#3099)
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino authored Oct 3, 2024
1 parent 2c5594a commit 6ae8035
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/nrnpython/nrnpy_hoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2903,7 +2903,7 @@ static Object* rvp_rxd_to_callable_(Object* obj) {


extern "C" NRN_EXPORT PyObject* get_plotshape_data(PyObject* sp) {
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};
PyHocObject* pho = (PyHocObject*) sp;
ShapePlotInterface* spi;
if (!is_obj_type(pho->ho_, "PlotShape")) {
Expand Down Expand Up @@ -3404,7 +3404,7 @@ extern "C" NRN_EXPORT PyObject* nrnpy_hoc() {
nrnpy_nrncore_enable_value_p_ = nrncore_enable_value;
nrnpy_nrncore_file_mode_value_p_ = nrncore_file_mode_value;
nrnpy_rvp_rxd_to_callable = rvp_rxd_to_callable_;
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};

char endian_character = 0;

Expand Down
24 changes: 12 additions & 12 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static PyObject* main_namespace;

struct Py2Nrn final {
~Py2Nrn() {
PyLockGIL lock{};
nanobind::gil_scoped_acquire lock{};
Py_XDECREF(po_);
}
int type_{}; // 0 toplevel
Expand All @@ -43,7 +43,7 @@ Member_func p_members[] = {{nullptr, nullptr}};
static void call_python_with_section(Object* pyact, Section* sec) {
PyObject* po = ((Py2Nrn*) pyact->u.this_pointer)->po_;
PyObject* r;
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};

PyObject* args = PyTuple_Pack(1, (PyObject*) newpysechelp(sec));
r = nrnpy_pyCallObject(po, args);
Expand Down Expand Up @@ -156,7 +156,7 @@ static void py2n_component(Object* ob, Symbol* sym, int nindex, int isfunc) {
Py2Nrn* pn = (Py2Nrn*) ob->u.this_pointer;
PyObject* head = pn->po_;
PyObject* tail;
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};
if (pn->type_ == 0) { // top level
if (!main_module) {
main_module = PyImport_AddModule("__main__");
Expand Down Expand Up @@ -362,7 +362,7 @@ static PyObject* hoccommand_exec_help(Object* ho) {
}

static double praxis_efun(Object* ho, Object* v) {
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};

PyObject* pc = nrnpy_ho2po(ho);
PyObject* pv = nrnpy_ho2po(v);
Expand Down Expand Up @@ -391,7 +391,7 @@ static double praxis_efun(Object* ho, Object* v) {
}

static int hoccommand_exec(Object* ho) {
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};

PyObject* r = hoccommand_exec_help(ho);
if (r == NULL) {
Expand All @@ -411,7 +411,7 @@ static int hoccommand_exec(Object* ho) {
}

static int hoccommand_exec_strret(Object* ho, char* buf, int size) {
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};

PyObject* r = hoccommand_exec_help(ho);
if (r) {
Expand All @@ -438,7 +438,7 @@ static int hoccommand_exec_strret(Object* ho, char* buf, int size) {
static void grphcmdtool(Object* ho, int type, double x, double y, int key) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
PyObject* r;
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};

PyObject* args = PyTuple_Pack(
4, PyInt_FromLong(type), PyFloat_FromDouble(x), PyFloat_FromDouble(y), PyInt_FromLong(key));
Expand All @@ -460,7 +460,7 @@ static void grphcmdtool(Object* ho, int type, double x, double y, int key) {

static Object* callable_with_args(Object* ho, int narg) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};

PyObject* args = PyTuple_New((Py_ssize_t) narg);
if (args == NULL) {
Expand Down Expand Up @@ -493,7 +493,7 @@ static Object* callable_with_args(Object* ho, int narg) {
static double func_call(Object* ho, int narg, int* err) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
PyObject* r;
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};

PyObject* args = PyTuple_New((Py_ssize_t) narg);
if (args == NULL) {
Expand Down Expand Up @@ -549,7 +549,7 @@ static double func_call(Object* ho, int narg, int* err) {

static double guigetval(Object* ho) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};
PyObject* r = NULL;
PyObject* p = PyTuple_GetItem(po, 0);
if (PySequence_Check(p) || PyMapping_Check(p)) {
Expand All @@ -565,7 +565,7 @@ static double guigetval(Object* ho) {

static void guisetval(Object* ho, double x) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};
PyObject* pn = PyFloat_FromDouble(x);
PyObject* p = PyTuple_GetItem(po, 0);
if (PySequence_Check(p) || PyMapping_Check(p)) {
Expand All @@ -578,7 +578,7 @@ static void guisetval(Object* ho, double x) {

static int guigetstr(Object* ho, char** cpp) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};

PyObject* r = PyObject_GetAttr(PyTuple_GetItem(po, 0), PyTuple_GetItem(po, 1));
PyObject* pn = PyObject_Str(r);
Expand Down
16 changes: 0 additions & 16 deletions src/nrnpython/nrnpy_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,6 @@ class PyErr2NRNString {
char* str_;
};


struct PyLockGIL {
PyLockGIL()
: state_{PyGILState_Ensure()} {}
PyLockGIL(PyLockGIL&&) = delete;
PyLockGIL(PyLockGIL const&) = delete;
PyLockGIL& operator=(PyLockGIL&&) = delete;
PyLockGIL& operator=(PyLockGIL const&) = delete;
~PyLockGIL() {
PyGILState_Release(state_);
}

private:
PyGILState_STATE state_;
};

extern void nrnpy_sec_referr();
#define CHECK_SEC_INVALID(sec) \
{ \
Expand Down
9 changes: 6 additions & 3 deletions src/nrnpython/nrnpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <string>
#include <sstream>
#include <fstream>

#include <nanobind/nanobind.h>

extern HocStr* hoc_cbufstr;
extern int nrnpy_nositeflag;
extern std::string nrnpy_pyexe;
Expand Down Expand Up @@ -79,7 +82,7 @@ PyObject* basic_sys_path{};
* @param new_first Path to decode and prepend to sys.path.
*/
void reset_sys_path(std::string_view new_first) {
PyLockGIL _{};
nanobind::gil_scoped_acquire _{};
auto* const path = PySys_GetObject("path");
nrn_assert(path);
// Clear sys.path
Expand Down Expand Up @@ -261,7 +264,7 @@ static int nrnpython_start(int b) {
check("Could not initialise Python", Py_InitializeFromConfig(config));
// Manipulate sys.path, starting from the default values
{
PyLockGIL _{};
nanobind::gil_scoped_acquire _{};
auto* const sys_path = PySys_GetObject("path");
if (!sys_path) {
throw std::runtime_error("Could not get sys.path from C++");
Expand Down Expand Up @@ -376,7 +379,7 @@ static void nrnpython_real() {
#if USE_PYTHON
HocTopContextSet
{
PyLockGIL lock;
nanobind::gil_scoped_acquire lock{};
retval = (PyRun_SimpleString(hoc_gargstr(1)) == 0);
}
HocContextRestore
Expand Down

0 comments on commit 6ae8035

Please sign in to comment.