tests: don't use the same chip object from different threads
PATCH 3/3 bindings: python: tests: don't use the same chip from different threads
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
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 use a global request function instead.
--- bindings/python/tests/tests_info_event.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
bindings/python/tests/tests_info_event.py +3 -3
@@ -37,9 +37,9 @@ class InfoEventDataclassBehavior(TestCase): event.line_info = 4 -def request_reconfigure_release_line(chip, offset): +def request_reconfigure_release_line(chip_path, offset): time.sleep(0.1) - with chip.request_lines(config={offset: None}) as request: + with gpiod.request_lines(chip_path, config={offset: None}) as request: time.sleep(0.1) request.reconfigure_lines( config={offset: gpiod.LineSettings(direction=Direction.OUTPUT)}
@@ -95,7 +95,7 @@ class WatchingInfoEventWorks(TestCase): self.assertEqual(info.direction, Direction.INPUT) self.thread = threading.Thread( - target=partial(request_reconfigure_release_line, self.chip, 7) + target=partial(request_reconfigure_release_line, self.sim.dev_path, 7) ) self.thread.start()
Signature · 1 line
2.39.2
PATCH 2/3 bindings: cxx: tests: don't use the same chip from different threads
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
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.
--- bindings/cxx/tests/tests-info-event.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
bindings/cxx/tests/tests-info-event.cpp +7 -4
@@ -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();
Signature · 1 line
2.39.2
PATCH 1/3 tests: don't use the same chip object from different threads
From: Bartosz Golaszewski <[email protected]>
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.
--- tests/tests-info-event.c | 62 +++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 29 deletions(-)
tests/tests-info-event.c +33 -29
@@ -49,43 +49,59 @@ GPIOD_TEST_CASE(event_timeout) }struct request_ctx { -struct gpiod_chip *chip; -struct gpiod_line_config *line_cfg; -struct gpiod_line_settings *settings; +const char *path;guint offset; };static gpointer request_reconfigure_release_line (gpointer data) { +g_autoptr (struct_gpiod_line_settings) settings =NULL ; +g_autoptr (struct_gpiod_line_config) line_cfg =NULL ;g_autoptr (struct_gpiod_line_request) request =NULL ; +g_autoptr (struct_gpiod_chip) chip =NULL ;struct request_ctx *ctx = data;gint ret; + chip =gpiod_chip_open (ctx->path); +g_assert_nonnull (chip); +if (g_test_failed ()) +return NULL ; + + line_cfg =gpiod_line_config_new (); +g_assert_nonnull (line_cfg); +if (g_test_failed ()) +return NULL ; + + settings =gpiod_line_settings_new (); +g_assert_nonnull (settings); +if (g_test_failed ()) +return NULL ; +g_usleep (1000 ); - ret =gpiod_line_config_add_line_settings (ctx->line_cfg, &ctx->offset, -1 , ctx->settings); + ret =gpiod_line_config_add_line_settings (line_cfg, &ctx->offset, +1 , settings); g_assert_cmpint(ret, ==, 0);if (g_test_failed ())return NULL ; - request =gpiod_chip_request_lines (ctx->chip,NULL , ctx->line_cfg); + request =gpiod_chip_request_lines (chip,NULL , line_cfg);g_assert_nonnull (request);if (g_test_failed ())return NULL ;g_usleep (1000 ); -gpiod_line_config_reset (ctx->line_cfg); -gpiod_line_settings_set_direction (ctx->settings, +gpiod_line_config_reset (line_cfg); +gpiod_line_settings_set_direction (settings,GPIOD_LINE_DIRECTION_OUTPUT ); - ret =gpiod_line_config_add_line_settings (ctx->line_cfg, &ctx->offset, -1 , ctx->settings); + ret =gpiod_line_config_add_line_settings (line_cfg, &ctx->offset, +1 , settings); g_assert_cmpint(ret, ==, 0);if (g_test_failed ())return NULL ; - ret =gpiod_line_request_reconfigure_lines (request, ctx->line_cfg); + ret =gpiod_line_request_reconfigure_lines (request, line_cfg); g_assert_cmpint(ret, ==, 0);if (g_test_failed ())return NULL ;
@@ -106,25 +122,19 @@ GPIOD_TEST_CASE(request_reconfigure_release_events)g_autoptr (struct_gpiod_info_event) request_event =NULL ;g_autoptr (struct_gpiod_info_event) reconfigure_event =NULL ;g_autoptr (struct_gpiod_info_event) release_event =NULL ; -g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; -g_autoptr (struct_gpiod_line_settings) settings =NULL ;g_autoptr (GThread) thread =NULL ;struct gpiod_line_info *request_info, *reconfigure_info, *release_info;guint64 request_ts, reconfigure_ts, release_ts;struct request_ctx ctx; +const char *chip_path =g_gpiosim_chip_get_dev_path (sim);gint ret; - chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); - line_cfg =gpiod_test_create_line_config_or_fail (); - settings =gpiod_test_create_line_settings_or_fail (); - + chip =gpiod_test_open_chip_or_fail (chip_path); info =gpiod_test_chip_watch_line_info_or_fail (chip,3 );g_assert_false (gpiod_line_info_is_used (info)); - ctx.chip = chip; - ctx.line_cfg = line_cfg; - ctx.settings = settings; + ctx.path = chip_path; ctx.offset =3 ; thread = g_thread_new("request-release",
@@ -199,26 +209,20 @@ GPIOD_TEST_CASE(chip_fd_can_be_polled)g_autoptr (struct_gpiod_chip) chip =NULL ;g_autoptr (struct_gpiod_line_info) info =NULL ;g_autoptr (struct_gpiod_info_event) event =NULL ; -g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; -g_autoptr (struct_gpiod_line_settings) settings =NULL ;g_autoptr (GThread) thread =NULL ; +const char *chip_path =g_gpiosim_chip_get_dev_path (sim);struct gpiod_line_info *evinfo;struct request_ctx ctx;struct timespec ts;struct pollfd pfd;gint ret, fd; - chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); - settings =gpiod_test_create_line_settings_or_fail (); - line_cfg =gpiod_test_create_line_config_or_fail (); - + chip =gpiod_test_open_chip_or_fail (chip_path); info =gpiod_test_chip_watch_line_info_or_fail (chip,3 );g_assert_false (gpiod_line_info_is_used (info)); - ctx.chip = chip; - ctx.line_cfg = line_cfg; - ctx.settings = settings; + ctx.path = chip_path; ctx.offset =3 ; thread = g_thread_new("request-release",
Signature · 1 line
2.39.2
Quoted 8 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
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 <[email protected]>Signed-off-by Bartosz Golaszewski <[email protected]>
Quoted 138 lines
--- tests/tests-info-event.c | 62 +++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 29 deletions(-)tests/tests-info-event.c +33
-29@@ -49,43 +49,59 @@ GPIOD_TEST_CASE(event_timeout) }struct request_ctx { -struct gpiod_chip *chip; -struct gpiod_line_config *line_cfg; -struct gpiod_line_settings *settings; +const char *path;guint offset; };static gpointer request_reconfigure_release_line (gpointer data) { +g_autoptr (struct_gpiod_line_settings) settings =NULL ; +g_autoptr (struct_gpiod_line_config) line_cfg =NULL ;g_autoptr (struct_gpiod_line_request) request =NULL ; +g_autoptr (struct_gpiod_chip) chip =NULL ;struct request_ctx *ctx = data;gint ret; + chip =gpiod_chip_open (ctx->path); +g_assert_nonnull (chip); +if (g_test_failed ()) +return NULL ; + + line_cfg =gpiod_line_config_new (); +g_assert_nonnull (line_cfg); +if (g_test_failed ()) +return NULL ; + + settings =gpiod_line_settings_new (); +g_assert_nonnull (settings); +if (g_test_failed ()) +return NULL ; +g_usleep (1000 ); - ret =gpiod_line_config_add_line_settings (ctx->line_cfg, &ctx->offset, -1 , ctx->settings); + ret =gpiod_line_config_add_line_settings (line_cfg, &ctx->offset, +1 , settings); g_assert_cmpint(ret, ==, 0);if (g_test_failed ())return NULL ; - request =gpiod_chip_request_lines (ctx->chip,NULL , ctx->line_cfg); + request =gpiod_chip_request_lines (chip,NULL , line_cfg);g_assert_nonnull (request);if (g_test_failed ())return NULL ;g_usleep (1000 ); -gpiod_line_config_reset (ctx->line_cfg); -gpiod_line_settings_set_direction (ctx->settings, +gpiod_line_config_reset (line_cfg); +gpiod_line_settings_set_direction (settings,GPIOD_LINE_DIRECTION_OUTPUT ); - ret =gpiod_line_config_add_line_settings (ctx->line_cfg, &ctx->offset, -1 , ctx->settings); + ret =gpiod_line_config_add_line_settings (line_cfg, &ctx->offset, +1 , settings); g_assert_cmpint(ret, ==, 0);if (g_test_failed ())return NULL ; - ret =gpiod_line_request_reconfigure_lines (request, ctx->line_cfg); + ret =gpiod_line_request_reconfigure_lines (request, line_cfg); g_assert_cmpint(ret, ==, 0);if (g_test_failed ())return NULL ;@@ -106,25 +122,19 @@ GPIOD_TEST_CASE(request_reconfigure_release_events)g_autoptr (struct_gpiod_info_event) request_event =NULL ;g_autoptr (struct_gpiod_info_event) reconfigure_event =NULL ;g_autoptr (struct_gpiod_info_event) release_event =NULL ; -g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; -g_autoptr (struct_gpiod_line_settings) settings =NULL ;g_autoptr (GThread) thread =NULL ;struct gpiod_line_info *request_info, *reconfigure_info, *release_info;guint64 request_ts, reconfigure_ts, release_ts;struct request_ctx ctx; +const char *chip_path =g_gpiosim_chip_get_dev_path (sim);gint ret; - chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); - line_cfg =gpiod_test_create_line_config_or_fail (); - settings =gpiod_test_create_line_settings_or_fail (); - + chip =gpiod_test_open_chip_or_fail (chip_path); info =gpiod_test_chip_watch_line_info_or_fail (chip,3 );g_assert_false (gpiod_line_info_is_used (info)); - ctx.chip = chip; - ctx.line_cfg = line_cfg; - ctx.settings = settings; + ctx.path = chip_path; ctx.offset =3 ; thread = g_thread_new("request-release",@@ -199,26 +209,20 @@ GPIOD_TEST_CASE(chip_fd_can_be_polled)g_autoptr (struct_gpiod_chip) chip =NULL ;g_autoptr (struct_gpiod_line_info) info =NULL ;g_autoptr (struct_gpiod_info_event) event =NULL ; -g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; -g_autoptr (struct_gpiod_line_settings) settings =NULL ;g_autoptr (GThread) thread =NULL ; +const char *chip_path =g_gpiosim_chip_get_dev_path (sim);struct gpiod_line_info *evinfo;struct request_ctx ctx;struct timespec ts;struct pollfd pfd;gint ret, fd; - chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); - settings =gpiod_test_create_line_settings_or_fail (); - line_cfg =gpiod_test_create_line_config_or_fail (); - + chip =gpiod_test_open_chip_or_fail (chip_path); info =gpiod_test_chip_watch_line_info_or_fail (chip,3 );g_assert_false (gpiod_line_info_is_used (info)); - ctx.chip = chip; - ctx.line_cfg = line_cfg; - ctx.settings = settings; + ctx.path = chip_path; ctx.offset =3 ; thread = g_thread_new("request-release",
Re: [libgpiod][PATCH 2/3] bindings: cxx: tests: don't use the same chip from different threads
In reply to Bartosz GolaszewskiQuoted 8 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
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 <[email protected]>Signed-off-by Bartosz Golaszewski <[email protected]>
Quoted 50 lines
--- bindings/cxx/tests/tests-info-event.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)bindings/cxx/tests/tests-info-event.cpp +7
-4@@ -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();
Re: [libgpiod][PATCH 3/3] bindings: python: tests: don't use the same chip from different threads
In reply to Bartosz GolaszewskiQuoted 8 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
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 use a global request function instead.
Reported-by Erik Schilling <[email protected]>Signed-off-by Bartosz Golaszewski <[email protected]>
Quoted 29 lines
--- bindings/python/tests/tests_info_event.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)bindings/python/tests/tests_info_event.py +3
-3@@ -37,9 +37,9 @@ class InfoEventDataclassBehavior(TestCase): event.line_info = 4 -def request_reconfigure_release_line(chip, offset): +def request_reconfigure_release_line(chip_path, offset): time.sleep(0.1) - with chip.request_lines(config={offset: None}) as request: + with gpiod.request_lines(chip_path, config={offset: None}) as request: time.sleep(0.1) request.reconfigure_lines( config={offset: gpiod.LineSettings(direction=Direction.OUTPUT)}@@ -95,7 +95,7 @@ class WatchingInfoEventWorks(TestCase): self.assertEqual(info.direction, Direction.INPUT) self.thread = threading.Thread( - target=partial(request_reconfigure_release_line, self.chip, 7) + target=partial(request_reconfigure_release_line, self.sim.dev_path, 7) ) self.thread.start()