@@ -23,8 +23,9 @@ class BoolWrapper {
23
23
std::atomic<bool > value_{false };
24
24
};
25
25
26
- #ifdef PYBIND11_HAS_BARRIER
27
- bool test_scoped_critical_section (const py::handle &cls) {
26
+ #if defined(PYBIND11_HAS_BARRIER) && defined(Py_GIL_DISABLED)
27
+
28
+ void test_scoped_critical_section (const py::handle &cls) {
28
29
auto barrier = std::barrier (2 );
29
30
auto bool_wrapper = cls (false );
30
31
bool output = false ;
@@ -47,10 +48,12 @@ bool test_scoped_critical_section(const py::handle &cls) {
47
48
t1.join ();
48
49
t2.join ();
49
50
50
- return output;
51
+ if (!output) {
52
+ throw std::runtime_error (" Scoped critical section test failed: output is false" );
53
+ }
51
54
}
52
55
53
- std::pair< bool , bool > test_scoped_critical_section2 (const py::handle &cls) {
56
+ void test_scoped_critical_section2 (const py::handle &cls) {
54
57
auto barrier = std::barrier (3 );
55
58
auto bool_wrapper1 = cls (false );
56
59
auto bool_wrapper2 = cls (false );
@@ -84,10 +87,13 @@ std::pair<bool, bool> test_scoped_critical_section2(const py::handle &cls) {
84
87
t2.join ();
85
88
t3.join ();
86
89
87
- return output;
90
+ if (!output.first || !output.second ) {
91
+ throw std::runtime_error (
92
+ " Scoped critical section test with two objects failed: output is false" );
93
+ }
88
94
}
89
95
90
- bool test_scoped_critical_section2_same_object_no_deadlock (const py::handle &cls) {
96
+ void test_scoped_critical_section2_same_object_no_deadlock (const py::handle &cls) {
91
97
auto barrier = std::barrier (2 );
92
98
auto bool_wrapper = cls (false );
93
99
bool output = false ;
@@ -110,8 +116,18 @@ bool test_scoped_critical_section2_same_object_no_deadlock(const py::handle &cls
110
116
t1.join ();
111
117
t2.join ();
112
118
113
- return output;
119
+ if (!output) {
120
+ throw std::runtime_error (
121
+ " Scoped critical section test with same object failed: output is false" );
122
+ }
114
123
}
124
+
125
+ #else
126
+
127
+ void test_scoped_critical_section (const py::handle &) {}
128
+ void test_scoped_critical_section2 (const py::handle &) {}
129
+ void test_scoped_critical_section2_same_object_no_deadlock (const py::handle &) {}
130
+
115
131
#endif
116
132
117
133
TEST_SUBMODULE (scoped_critical_section, m) {
@@ -132,14 +148,12 @@ TEST_SUBMODULE(scoped_critical_section, m) {
132
148
#ifdef PYBIND11_HAS_BARRIER
133
149
m.attr (" has_barrier" ) = true ;
134
150
135
- m.def (" test_scoped_critical_section" , [BoolWrapperHandle]() -> bool {
136
- return test_scoped_critical_section (BoolWrapperHandle);
137
- });
138
- m.def (" test_scoped_critical_section2" , [BoolWrapperHandle]() -> std::pair<bool , bool > {
139
- return test_scoped_critical_section2 (BoolWrapperHandle);
140
- });
141
- m.def (" test_scoped_critical_section2_same_object_no_deadlock" , [BoolWrapperHandle]() -> bool {
142
- return test_scoped_critical_section2_same_object_no_deadlock (BoolWrapperHandle);
151
+ m.def (" test_scoped_critical_section" ,
152
+ [BoolWrapperHandle]() -> void { test_scoped_critical_section (BoolWrapperHandle); });
153
+ m.def (" test_scoped_critical_section2" ,
154
+ [BoolWrapperHandle]() -> void { test_scoped_critical_section2 (BoolWrapperHandle); });
155
+ m.def (" test_scoped_critical_section2_same_object_no_deadlock" , [BoolWrapperHandle]() -> void {
156
+ test_scoped_critical_section2_same_object_no_deadlock (BoolWrapperHandle);
143
157
});
144
158
#else
145
159
m.attr (" has_barrier" ) = false ;
0 commit comments