linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Kent Gibson <warthog618@gmail.com>,
	Linus Walleij <linus.walleij@linaro.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Viresh Kumar <viresh.kumar@linaro.org>
Cc: linux-gpio@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@linaro.org>,
	Erik Schilling <erik.schilling@linaro.org>
Subject: [libgpiod][PATCH 1/3] tests: don't use the same chip object from different threads
Date: Tue, 19 Sep 2023 11:31:40 +0200	[thread overview]
Message-ID: <20230919093142.85389-1-brgl@bgdev.pl> (raw)

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

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>
---
 tests/tests-info-event.c | 62 +++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 29 deletions(-)

diff --git a/tests/tests-info-event.c b/tests/tests-info-event.c
index d069811..cbd9e9e 100644
--- a/tests/tests-info-event.c
+++ b/tests/tests-info-event.c
@@ -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",
-- 
2.39.2


             reply	other threads:[~2023-09-19  9:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-19  9:31 Bartosz Golaszewski [this message]
2023-09-19  9:31 ` [libgpiod][PATCH 2/3] bindings: cxx: tests: don't use the same chip from different threads Bartosz Golaszewski
2023-09-19 11:33   ` Erik Schilling
2023-09-19  9:31 ` [libgpiod][PATCH 3/3] bindings: python: " Bartosz Golaszewski
2023-09-19 11:33   ` Erik Schilling
2023-09-19 11:33 ` [libgpiod][PATCH 1/3] tests: don't use the same chip object " Erik Schilling

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230919093142.85389-1-brgl@bgdev.pl \
    --to=brgl@bgdev.pl \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bartosz.golaszewski@linaro.org \
    --cc=erik.schilling@linaro.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=viresh.kumar@linaro.org \
    --cc=warthog618@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).