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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
#include <pybind11/critical_section.h>
#include "pybind11_tests.h"
#include <atomic>
#include <chrono>
#include <thread>
#include <utility>
#if defined(PYBIND11_CPP20) && defined(__has_include) && __has_include(<barrier>)
# define PYBIND11_HAS_BARRIER 1
# include <barrier>
#endif
namespace test_scoped_critical_section_ns {
void test_one_nullptr() { py::scoped_critical_section lock{py::handle{}}; }
void test_two_nullptrs() { py::scoped_critical_section lock{py::handle{}, py::handle{}}; }
void test_first_nullptr() {
py::dict d;
py::scoped_critical_section lock{py::handle{}, d};
}
void test_second_nullptr() {
py::dict d;
py::scoped_critical_section lock{d, py::handle{}};
}
// Referenced test implementation: https://github.com/PyO3/pyo3/blob/v0.25.0/src/sync.rs#L874
class BoolWrapper {
public:
explicit BoolWrapper(bool value) : value_{value} {}
bool get() const { return value_.load(std::memory_order_acquire); }
void set(bool value) { value_.store(value, std::memory_order_release); }
private:
std::atomic_bool value_{false};
};
#if defined(PYBIND11_HAS_BARRIER)
// Modifying the C/C++ members of a Python object from multiple threads requires a critical section
// to ensure thread safety and data integrity.
// These tests use a scoped critical section to ensure that the Python object is accessed in a
// thread-safe manner.
void test_scoped_critical_section(const py::handle &cls) {
auto barrier = std::barrier(2);
auto bool_wrapper = cls(false);
bool output = false;
{
// Release the GIL to allow run threads in parallel.
py::gil_scoped_release gil_release{};
std::thread t1([&]() {
// Use gil_scoped_acquire to ensure we have a valid Python thread state
// before entering the critical section. Otherwise, the critical section
// will cause a segmentation fault.
py::gil_scoped_acquire ensure_tstate{};
// Enter the critical section with the same object as the second thread.
py::scoped_critical_section lock{bool_wrapper};
// At this point, the object is locked by this thread via the scoped_critical_section.
// This barrier will ensure that the second thread waits until this thread has released
// the critical section before proceeding.
barrier.arrive_and_wait();
// Sleep for a short time to simulate some work in the critical section.
// This sleep is necessary to test the locking mechanism properly.
std::this_thread::sleep_for(std::chrono::milliseconds(10));
auto *bw = bool_wrapper.cast<BoolWrapper *>();
bw->set(true);
});
std::thread t2([&]() {
// This thread will wait until the first thread has entered the critical section due to
// the barrier.
barrier.arrive_and_wait();
{
// Use gil_scoped_acquire to ensure we have a valid Python thread state
// before entering the critical section. Otherwise, the critical section
// will cause a segmentation fault.
py::gil_scoped_acquire ensure_tstate{};
// Enter the critical section with the same object as the first thread.
py::scoped_critical_section lock{bool_wrapper};
// At this point, the critical section is released by the first thread, the value
// is set to true.
auto *bw = bool_wrapper.cast<BoolWrapper *>();
output = bw->get();
}
});
t1.join();
t2.join();
}
if (!output) {
throw std::runtime_error("Scoped critical section test failed: output is false");
}
}
void test_scoped_critical_section2(const py::handle &cls) {
auto barrier = std::barrier(3);
auto bool_wrapper1 = cls(false);
auto bool_wrapper2 = cls(false);
std::pair<bool, bool> output{false, false};
{
// Release the GIL to allow run threads in parallel.
py::gil_scoped_release gil_release{};
std::thread t1([&]() {
// Use gil_scoped_acquire to ensure we have a valid Python thread state
// before entering the critical section. Otherwise, the critical section
// will cause a segmentation fault.
py::gil_scoped_acquire ensure_tstate{};
// Enter the critical section with two different objects.
// This will ensure that the critical section is locked for both objects.
py::scoped_critical_section lock{bool_wrapper1, bool_wrapper2};
// At this point, objects are locked by this thread via the scoped_critical_section.
// This barrier will ensure that other threads wait until this thread has released
// the critical section before proceeding.
barrier.arrive_and_wait();
// Sleep for a short time to simulate some work in the critical section.
// This sleep is necessary to test the locking mechanism properly.
std::this_thread::sleep_for(std::chrono::milliseconds(10));
auto *bw1 = bool_wrapper1.cast<BoolWrapper *>();
auto *bw2 = bool_wrapper2.cast<BoolWrapper *>();
bw1->set(true);
bw2->set(true);
});
std::thread t2([&]() {
// This thread will wait until the first thread has entered the critical section due to
// the barrier.
barrier.arrive_and_wait();
{
// Use gil_scoped_acquire to ensure we have a valid Python thread state
// before entering the critical section. Otherwise, the critical section
// will cause a segmentation fault.
py::gil_scoped_acquire ensure_tstate{};
// Enter the critical section with the same object as the first thread.
py::scoped_critical_section lock{bool_wrapper1};
// At this point, the critical section is released by the first thread, the value
// is set to true.
auto *bw1 = bool_wrapper1.cast<BoolWrapper *>();
output.first = bw1->get();
}
});
std::thread t3([&]() {
// This thread will wait until the first thread has entered the critical section due to
// the barrier.
barrier.arrive_and_wait();
{
// Use gil_scoped_acquire to ensure we have a valid Python thread state
// before entering the critical section. Otherwise, the critical section
// will cause a segmentation fault.
py::gil_scoped_acquire ensure_tstate{};
// Enter the critical section with the same object as the first thread.
py::scoped_critical_section lock{bool_wrapper2};
// At this point, the critical section is released by the first thread, the value
// is set to true.
auto *bw2 = bool_wrapper2.cast<BoolWrapper *>();
output.second = bw2->get();
}
});
t1.join();
t2.join();
t3.join();
}
if (!output.first || !output.second) {
throw std::runtime_error(
"Scoped critical section test with two objects failed: output is false");
}
}
void test_scoped_critical_section2_same_object_no_deadlock(const py::handle &cls) {
auto barrier = std::barrier(2);
auto bool_wrapper = cls(false);
bool output = false;
{
// Release the GIL to allow run threads in parallel.
py::gil_scoped_release gil_release{};
std::thread t1([&]() {
// Use gil_scoped_acquire to ensure we have a valid Python thread state
// before entering the critical section. Otherwise, the critical section
// will cause a segmentation fault.
py::gil_scoped_acquire ensure_tstate{};
// Enter the critical section with the same object as the second thread.
py::scoped_critical_section lock{bool_wrapper, bool_wrapper}; // same object used here
// At this point, the object is locked by this thread via the scoped_critical_section.
// This barrier will ensure that the second thread waits until this thread has released
// the critical section before proceeding.
barrier.arrive_and_wait();
// Sleep for a short time to simulate some work in the critical section.
// This sleep is necessary to test the locking mechanism properly.
std::this_thread::sleep_for(std::chrono::milliseconds(10));
auto *bw = bool_wrapper.cast<BoolWrapper *>();
bw->set(true);
});
std::thread t2([&]() {
// This thread will wait until the first thread has entered the critical section due to
// the barrier.
barrier.arrive_and_wait();
{
// Use gil_scoped_acquire to ensure we have a valid Python thread state
// before entering the critical section. Otherwise, the critical section
// will cause a segmentation fault.
py::gil_scoped_acquire ensure_tstate{};
// Enter the critical section with the same object as the first thread.
py::scoped_critical_section lock{bool_wrapper};
// At this point, the critical section is released by the first thread, the value
// is set to true.
auto *bw = bool_wrapper.cast<BoolWrapper *>();
output = bw->get();
}
});
t1.join();
t2.join();
}
if (!output) {
throw std::runtime_error(
"Scoped critical section test with same object failed: output is false");
}
}
#else
void test_scoped_critical_section(const py::handle &) {}
void test_scoped_critical_section2(const py::handle &) {}
void test_scoped_critical_section2_same_object_no_deadlock(const py::handle &) {}
#endif
} // namespace test_scoped_critical_section_ns
TEST_SUBMODULE(scoped_critical_section, m) {
using namespace test_scoped_critical_section_ns;
m.def("test_one_nullptr", test_one_nullptr);
m.def("test_two_nullptrs", test_two_nullptrs);
m.def("test_first_nullptr", test_first_nullptr);
m.def("test_second_nullptr", test_second_nullptr);
auto BoolWrapperClass = py::class_<BoolWrapper>(m, "BoolWrapper")
.def(py::init<bool>())
.def("get", &BoolWrapper::get)
.def("set", &BoolWrapper::set);
auto BoolWrapperHandle = py::handle(BoolWrapperClass);
(void) BoolWrapperHandle.ptr(); // suppress unused variable warning
m.attr("has_barrier") =
#ifdef PYBIND11_HAS_BARRIER
true;
#else
false;
#endif
m.def("test_scoped_critical_section",
[BoolWrapperHandle]() -> void { test_scoped_critical_section(BoolWrapperHandle); });
m.def("test_scoped_critical_section2",
[BoolWrapperHandle]() -> void { test_scoped_critical_section2(BoolWrapperHandle); });
m.def("test_scoped_critical_section2_same_object_no_deadlock", [BoolWrapperHandle]() -> void {
test_scoped_critical_section2_same_object_no_deadlock(BoolWrapperHandle);
});
}
|