@@ -13,18 +13,27 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
13
13
class scoped_critical_section {
14
14
public:
15
15
#ifdef Py_GIL_DISABLED
16
- explicit scoped_critical_section (handle obj) : has2 (false ) {
17
- PyCriticalSection_Begin (§ion, obj.ptr ());
16
+ scoped_critical_section (handle obj1, handle obj2) : m_ptr1 (obj1.ptr ()), m_ptr2 (obj2.ptr ()) {
17
+ if (m_ptr1 == nullptr ) {
18
+ std::swap (m_ptr1, m_ptr2);
19
+ }
20
+ if (m_ptr2 != nullptr ) {
21
+ PyCriticalSection2_Begin (§ion2, m_ptr1, m_ptr2);
22
+ } else if (m_ptr1 != nullptr ) {
23
+ PyCriticalSection_Begin (§ion, m_ptr1);
24
+ }
18
25
}
19
26
20
- scoped_critical_section (handle obj1, handle obj2) : has2 (true ) {
21
- PyCriticalSection2_Begin (§ion2, obj1.ptr (), obj2.ptr ());
27
+ explicit scoped_critical_section (handle obj) : m_ptr1 (obj.ptr ()) {
28
+ if (m_ptr1 != nullptr ) {
29
+ PyCriticalSection_Begin (§ion, m_ptr1);
30
+ }
22
31
}
23
32
24
33
~scoped_critical_section () {
25
- if (has2 ) {
34
+ if (m_ptr2 != nullptr ) {
26
35
PyCriticalSection2_End (§ion2);
27
- } else {
36
+ } else if (m_ptr1 != nullptr ) {
28
37
PyCriticalSection_End (§ion);
29
38
}
30
39
}
@@ -39,7 +48,8 @@ class scoped_critical_section {
39
48
40
49
private:
41
50
#ifdef Py_GIL_DISABLED
42
- bool has2;
51
+ PyObject *m_ptr1{nullptr };
52
+ PyObject *m_ptr2{nullptr };
43
53
union {
44
54
PyCriticalSection section;
45
55
PyCriticalSection2 section2;
0 commit comments