File: gil_simple.h

package info (click to toggle)
regina-normal 7.4.1-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 154,244 kB
  • sloc: cpp: 295,026; xml: 9,992; sh: 1,344; python: 1,225; perl: 616; ansic: 138; makefile: 26
file content (37 lines) | stat: -rw-r--r-- 1,185 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)