@@ -23,8 +23,9 @@ class BoolWrapper {
2323 std::atomic<bool > value_{false };
2424};
2525
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) {
2829 auto barrier = std::barrier (2 );
2930 auto bool_wrapper = cls (false );
3031 bool output = false ;
@@ -47,10 +48,12 @@ bool test_scoped_critical_section(const py::handle &cls) {
4748 t1.join ();
4849 t2.join ();
4950
50- return output;
51+ if (!output) {
52+ throw std::runtime_error (" Scoped critical section test failed: output is false" );
53+ }
5154}
5255
53- std::pair< bool , bool > test_scoped_critical_section2 (const py::handle &cls) {
56+ void test_scoped_critical_section2 (const py::handle &cls) {
5457 auto barrier = std::barrier (3 );
5558 auto bool_wrapper1 = cls (false );
5659 auto bool_wrapper2 = cls (false );
@@ -84,10 +87,13 @@ std::pair<bool, bool> test_scoped_critical_section2(const py::handle &cls) {
8487 t2.join ();
8588 t3.join ();
8689
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+ }
8894}
8995
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) {
9197 auto barrier = std::barrier (2 );
9298 auto bool_wrapper = cls (false );
9399 bool output = false ;
@@ -110,8 +116,18 @@ bool test_scoped_critical_section2_same_object_no_deadlock(const py::handle &cls
110116 t1.join ();
111117 t2.join ();
112118
113- return output;
119+ if (!output) {
120+ throw std::runtime_error (
121+ " Scoped critical section test with same object failed: output is false" );
122+ }
114123}
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+
115131#endif
116132
117133TEST_SUBMODULE (scoped_critical_section, m) {
@@ -132,14 +148,12 @@ TEST_SUBMODULE(scoped_critical_section, m) {
132148#ifdef PYBIND11_HAS_BARRIER
133149 m.attr (" has_barrier" ) = true ;
134150
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);
143157 });
144158#else
145159 m.attr (" has_barrier" ) = false ;
0 commit comments