bindings: cxx: tests: don't use the same chip from different threads
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 19 Sep 2023 09:31:41 +0000 (11:31 +0200)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Mon, 25 Sep 2023 07:00:11 +0000 (09:00 +0200)
There are no thread-safety guarantees in libgpiod. Let's not reuse the
chip object created in one thread to generate info events in another but
create a second chip for that purpose instead.

Reported-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Erik Schilling <erik.schilling@linaro.org>
bindings/cxx/tests/tests-info-event.cpp

index 249b1e8161c43d2385f5107b4dc516907944a725..21c0ef0013f3fa6ab5ce48aa2da94568dc2ecfa2 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <catch2/catch.hpp>
 #include <chrono>
+#include <filesystem>
 #include <gpiod.hpp>
 #include <sstream>
 #include <thread>
@@ -17,11 +18,11 @@ using event_type = ::gpiod::info_event::event_type;
 
 namespace {
 
-void request_reconfigure_release_line(::gpiod::chip& chip)
+void request_reconfigure_release_line(const ::std::filesystem::path& chip_path)
 {
        ::std::this_thread::sleep_for(::std::chrono::milliseconds(10));
 
-       auto request = chip
+       auto request = ::gpiod::chip(chip_path)
                .prepare_request()
                .add_line_settings(7, ::gpiod::line_settings())
                .do_request();
@@ -48,7 +49,9 @@ TEST_CASE("Lines can be watched", "[info-event][chip]")
                .set_num_lines(8)
                .build();
 
-       ::gpiod::chip chip(sim.dev_path());
+       const auto chip_path = sim.dev_path();
+
+       ::gpiod::chip chip(chip_path);
 
        SECTION("watch_line_info() returns line info")
        {
@@ -74,7 +77,7 @@ TEST_CASE("Lines can be watched", "[info-event][chip]")
 
                REQUIRE(info.direction() == direction::INPUT);
 
-               ::std::thread thread(request_reconfigure_release_line, ::std::ref(chip));
+               ::std::thread thread(request_reconfigure_release_line, ::std::ref(chip_path));
 
                REQUIRE(chip.wait_info_event(::std::chrono::seconds(1)));
                auto event = chip.read_info_event();