From patchwork Thu Sep 28 14:37:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erik Schilling X-Patchwork-Id: 727880 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18088E732DE for ; Thu, 28 Sep 2023 14:37:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230242AbjI1Ohh (ORCPT ); Thu, 28 Sep 2023 10:37:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230338AbjI1Ohg (ORCPT ); Thu, 28 Sep 2023 10:37:36 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E144D136 for ; Thu, 28 Sep 2023 07:37:34 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-53639fb0ba4so636189a12.0 for ; Thu, 28 Sep 2023 07:37:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1695911853; x=1696516653; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=7WODGD/gepLPbhfzAeHSaZzlAxaMM0w9oPhS08axsLk=; b=VwmAZHIGOhifK+8bWpDlLPHxpilRVDtaX0wkPdURheBilfJPD9rd98LyqsGV0KXZ6j M2VhWgAdR47sm46Ukkisxx65oixg40zTgYWf5aEMz9LoZEfUglpzWTfC2BxoqFqDu+mz 3BV9+X7Pc6d0P2JM+UVRO/y60S3Cp4GpxEAhsba7BH9Pyp2aXR9NqVvS9VFNIOb2nCHD WHFVuLsWPXHkedlPJ6ofugQNdfnyWfirYs7Iobz71rXKyZ6faY9HoUt3YUKlm2fxeRcx 8Vz52B73Z8dsbf+8OeFg4w+EEkk3WajCvYBONv8J8bRovCCaMBGjpaMeL0tAXwIrIPrW oNZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695911853; x=1696516653; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7WODGD/gepLPbhfzAeHSaZzlAxaMM0w9oPhS08axsLk=; b=LHRnZzwjuJfMciLfpbIBVFglNwl11qH7JVT+fm1xujitqzYAoMqJJjQvwKR3Unpj8p S6iakoWoXsuAZofItLUpWAxHgFWQCme9zaXojwqAmTGE8BHnGMkXL2JlaTSWWcLa3soD CO5tf9otOlpexLvxENqU+OvmqnZSxTTSCyNz1Gjqz/gEKgHTTkT2K01fgxbvAiFxlXy4 nc6ByQpu3GGPAZuWISllMGj4uwCGlTC4ZA8aLSAUl51sBwI0T5ONkF7ME2j9wcymynLE +Lm8v9GPq0WkPJG25nzdz6wG1CXI4CSvzXdOHWblWEgBka2hyCpELM1wKO2DfYwhuepg ATgw== X-Gm-Message-State: AOJu0Yw+JJcenTDNN0CVbaAzCDU11myh0J11AXgnmm+g4F5W8LbX/FCv JEQmlXsrcf8ku141e1rSmCg0Zg== X-Google-Smtp-Source: AGHT+IE9iPN345uEaqu53oc3pY7GTp2qhW1fslqm1EMpg1vtbBX5+u9z7r0YUzJCzi/Wl8AOxYhrcQ== X-Received: by 2002:aa7:c1c4:0:b0:531:1f3b:cb3b with SMTP id d4-20020aa7c1c4000000b005311f3bcb3bmr1366470edp.6.1695911853469; Thu, 28 Sep 2023 07:37:33 -0700 (PDT) Received: from [192.168.1.149] (i5C7438D4.versanet.de. [92.116.56.212]) by smtp.gmail.com with ESMTPSA id d15-20020aa7d68f000000b005333922efb0sm9789688edr.78.2023.09.28.07.37.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 07:37:32 -0700 (PDT) From: Erik Schilling Date: Thu, 28 Sep 2023 16:37:28 +0200 Subject: [libgpiod][PATCH 1/3] doc: drop unneeded

tags MIME-Version: 1.0 Message-Id: <20230928-rust-send-trait-v1-1-30b4f59d13cb@linaro.org> References: <20230928-rust-send-trait-v1-0-30b4f59d13cb@linaro.org> In-Reply-To: <20230928-rust-send-trait-v1-0-30b4f59d13cb@linaro.org> To: Linux-GPIO Cc: Bartosz Golaszewski , Viresh Kumar , Erik Schilling X-Mailer: b4 0.13-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1695911851; l=2019; i=erik.schilling@linaro.org; s=20230523; h=from:subject:message-id; bh=g3Dy1qiZHiiDC2hQfis0DEnXyaS70xPR1po/ulJVrEQ=; b=gXdXiIKzIdy9dnesUCsFEt+qXe1uMMnns/ZfrvaDsGW0H4cbDAnu0ZELvsbEiwACc380SdpsC 0lLI7CwAMiXD+cRxzBQgboEGdlO0XRms8T3jBdebT5g+5SRcD0CTp4p X-Developer-Key: i=erik.schilling@linaro.org; a=ed25519; pk=/nNqy8/YOEdthj1epXl5FgwCTKEiVqTqqnVN1jVal7s= Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Even before Doxygen gained Markdown support, empty lines were considered as paragraphs. Changelogs indicate that this was the case since at least the doxygen 1.2 series (where I found a mentiond that something around this behaviour was fixed). So at least works in Doxygen versions released after 2001 [1]. [1] https://sourceforge.net/p/doxygen/mailman/message/172899/ Signed-off-by: Erik Schilling --- include/gpiod.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/gpiod.h b/include/gpiod.h index 71ae798..be51c3a 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -22,11 +22,11 @@ extern "C" { * This is the complete documentation of the public API made available to * users of libgpiod. * - *

The API is logically split into several sections. For each opaque data + * The API is logically split into several sections. For each opaque data * class, there's a set of functions for manipulating it. Together they can be * thought of as objects and their methods in OOP parlance. * - *

General note on error handling: all functions exported by libgpiod that + * General note on error handling: all functions exported by libgpiod that * can fail, set errno to one of the error values defined in errno.h upon * failure. The way of notifying the caller that an error occurred varies * between functions, but in general a function that returns an int, returns -1 @@ -35,7 +35,7 @@ extern "C" { * codes for every function as they propagate errors from the underlying libc * functions. * - *

In general libgpiod functions are NULL-aware. For functions that are + * In general libgpiod functions are NULL-aware. For functions that are * logically methods of data classes - ones that take a pointer to the object * of that class as the first argument - passing a NULL pointer will result in * the program aborting the execution. For non-methods, init functions and From patchwork Thu Sep 28 14:37:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erik Schilling X-Patchwork-Id: 727879 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C682EE732DF for ; Thu, 28 Sep 2023 14:37:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230325AbjI1Ohi (ORCPT ); Thu, 28 Sep 2023 10:37:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230228AbjI1Ohh (ORCPT ); Thu, 28 Sep 2023 10:37:37 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5EDBF9 for ; Thu, 28 Sep 2023 07:37:35 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-5333fb34be3so15778442a12.1 for ; Thu, 28 Sep 2023 07:37:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1695911854; x=1696516654; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Nl271QbtK8eF3rZh9aQr3lSeAmlYw2FvavGVw4unDuc=; b=LJw4gKTct4Vye+Ig7rRCrTtwkXod3dLXzZ2DtZvVWXOpCdv3k2dXjRRxo8eWJ+vhkg SRwBIAZ5f2Y9A2VLR9Wfj+M/Gg4Do64Iey6nD8PAxajQUuQwNFUSHoHWG61YvZlL2e4q WUzwZu1duYeSmUdhEH7WeXgdDR6kRPNhT8Hp0106utk9q8zjAjh6RDOHBT7aA6aSZ3Sa r6ltIRmFFzEgwDgIkHILuvRRjrK+fXMpsWO68oPSlNDW+vJN2CCts68tPvjllG5Gh7Co 40nIM8gmdjvdDwmquEwrltS8Wbn19gpTNhMzQ3LU1ZI/YFmxZDiq0aJjZaAPdFC2W+om Jqqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695911854; x=1696516654; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Nl271QbtK8eF3rZh9aQr3lSeAmlYw2FvavGVw4unDuc=; b=jdDqafmVfUvUrAOf0pFaA19l6Y5lnHHBQpKdg0Y7PqShZhTtsoQuKbICmX/rr+/ffh TRTq1cV7zvTS40MVPDA2BuyW2RsvuAQ7cvX7Od9T9cZQYDpECxwhINzzARObc2TT8cDB q5zd3SraYFWvPQvqXM3n87n9dChYNr0gwqObwkbgeEYhDfKr/KIiqSCRfGOaE2G8vXAy Nz+Gro6dJwi3TgdttFp7p4qGEGq7Sl7j7lcuXMB0b83Gtfwp18Rbo46Ux5h/w/mGxc/C kKoaF89AFZH42FB+yL9w/CGs9UvVJezx9lAVrySD2Q2iRPoRvu0KClS16uBVN5RtGHNm 9lhQ== X-Gm-Message-State: AOJu0YzfRiF9bHaemxfKAjQkmRlVfKsXM+KLd7SfExsYJfX/Wt7Kiacg ZFezaFuquZJfXnF8oo6WqLS6sw== X-Google-Smtp-Source: AGHT+IGPS680xZ4SwUNqKBK5poyvE7e1wCmMUmc6Jd+PJeL0zEmtZQ4zO9WvBNquT2fW81jlbIn3Uw== X-Received: by 2002:a50:fa85:0:b0:534:63e:d0b7 with SMTP id w5-20020a50fa85000000b00534063ed0b7mr1467350edr.23.1695911854353; Thu, 28 Sep 2023 07:37:34 -0700 (PDT) Received: from [192.168.1.149] (i5C7438D4.versanet.de. [92.116.56.212]) by smtp.gmail.com with ESMTPSA id d15-20020aa7d68f000000b005333922efb0sm9789688edr.78.2023.09.28.07.37.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 07:37:33 -0700 (PDT) From: Erik Schilling Date: Thu, 28 Sep 2023 16:37:29 +0200 Subject: [libgpiod][PATCH 2/3] doc: document thread safety guarantees MIME-Version: 1.0 Message-Id: <20230928-rust-send-trait-v1-2-30b4f59d13cb@linaro.org> References: <20230928-rust-send-trait-v1-0-30b4f59d13cb@linaro.org> In-Reply-To: <20230928-rust-send-trait-v1-0-30b4f59d13cb@linaro.org> To: Linux-GPIO Cc: Bartosz Golaszewski , Viresh Kumar , Erik Schilling X-Mailer: b4 0.13-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1695911851; l=2954; i=erik.schilling@linaro.org; s=20230523; h=from:subject:message-id; bh=nR+zmqX4gKUQnG4QTHQSoiq4soz1CDiTU5rpMkJ4r7I=; b=NJi17dwY4kast7Ihi30zb7HHrHjW2fRS+MiKPdoAj7lhyuVjOY57AsAfPkQ4o6oGWES8NfzAx SiSP6kGOHTLDJU08VDMh2Aykykgdaj2LEcHpF5CFqbOPZ5sh5G9gvPL X-Developer-Key: i=erik.schilling@linaro.org; a=ed25519; pk=/nNqy8/YOEdthj1epXl5FgwCTKEiVqTqqnVN1jVal7s= Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org This mostly adds the information from the linked thread to the doxygen documentation. Summarized: - libgpiod object's require synchronization - individual objects may be used concurrently by different threds Link: https://lore.kernel.org/r/CVHO091CC80Y.3KUOSLSOBVL0T@ablu-work Signed-off-by: Erik Schilling --- include/gpiod.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/gpiod.h b/include/gpiod.h index be51c3a..d86c6ac 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -42,6 +42,14 @@ extern "C" { * methods that take a pointer as any of the subsequent arguments, the handling * of a NULL-pointer depends on the implementation and may range from gracefully * handling it, ignoring it or returning an error. + * + * libgpiod is thread-aware but does not provide any further thread-safety + * guarantees. This requires the user to ensure that at most one thread may + * work with an object at any time. Sharing objects across threads is allowed + * if a suitable synchronization mechanism serializes the access. Different, + * standalone objects can safely be used concurrently. Most libgpiod objects + * are standalone. Exceptions - such as events allocated in buffers - exist and + * are noted in the documentation. */ /** @@ -624,6 +632,12 @@ uint64_t gpiod_info_event_get_timestamp_ns(struct gpiod_info_event *event); * @return Returns a pointer to the line-info object associated with the event. * The object lifetime is tied to the event object, so the pointer must * be not be freed by the caller. + * @warning Thread-safety: + * Since the line-info object is tied to the event, different threads + * may not operate on the event and line-info at the same time. The + * line-info can be copied using ::gpiod_line_info_copy in order to + * create a standalone object - which then may safely be used from a + * different thread concurrently. */ struct gpiod_line_info * gpiod_info_event_get_line_info(struct gpiod_info_event *event); @@ -1297,6 +1311,12 @@ void gpiod_edge_event_buffer_free(struct gpiod_edge_event_buffer *buffer); * @return Pointer to an event stored in the buffer. The lifetime of the * event is tied to the buffer object. Users must not free the event * returned by this function. + * @warning Thread-safety: + * Since events are tied to the buffer instance, different threads + * may not operate on the buffer and any associated events at the same + * time. Events can be copied using ::gpiod_edge_event_copy in order + * to create a standalone objects - which each may safely be used from + * a different thread concurrently. */ struct gpiod_edge_event * gpiod_edge_event_buffer_get_event(struct gpiod_edge_event_buffer *buffer, From patchwork Thu Sep 28 14:37:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erik Schilling X-Patchwork-Id: 727429 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9AB0E732E0 for ; Thu, 28 Sep 2023 14:37:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230228AbjI1Ohj (ORCPT ); Thu, 28 Sep 2023 10:37:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230501AbjI1Ohi (ORCPT ); Thu, 28 Sep 2023 10:37:38 -0400 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E53E19D for ; Thu, 28 Sep 2023 07:37:37 -0700 (PDT) Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-534659061afso7021584a12.3 for ; Thu, 28 Sep 2023 07:37:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1695911855; x=1696516655; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=W5BxhjMaBD9Q6vKzv0NJ0uo9X/jQrD+Md0yYIciYwbo=; b=sk43Y3Cc861o1ngmJtNmNoa4PHRrPLdjVBOdj7MXIPHRP3XWwmuKgE3FSUAULrfoR/ y8K47HOYBYQSS7SWXdagQnD5KZdRmYLVqXp7OJrOFwsRt/x3Cr36wyRh1XhIeffTQB29 XMyrhGgBc85llk0c4wlO3bQJmXK8bscQnC7S1A5ukPkgQD7SnyUCAun+WDMJJ82KtIoy mcgSi9Laf+vrYLO3a5VoX/zpONKUf6nbD9oPHF7CNlTmSb0PT1Ng/0g88KHdn9PpLCD4 DHS5bL/uKGnR1/Cps2P0EwyMDmFaW5ibDPsVsdeyp6W0RudjQTLQ2UEUqf3R9u2WVyxj B43w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695911855; x=1696516655; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=W5BxhjMaBD9Q6vKzv0NJ0uo9X/jQrD+Md0yYIciYwbo=; b=SiJ8DAXfKGUt1oeh40YUaiDBDjwe3vsNF6IB+5LYVKDHCfv0ZD1S5eWihrUrGDMsmw dhC493Qwp2ABMrpqrW8gYbY+QLnVjQZi7YfC597HNcRngVZfZPLsBHFVc1eZQDOsvDrV ET0paGzPvX+pt5r391r8uRQQkmZrBLqpZvv57i+Hejvm5CkMTlnzgWMak3BPfPlf6K0t CR81Y+3pACFPOlGL+SP6LTz44Y8DmbJAc6RRiO+ibiaq9uhEbEhZ0kNRM3PyqRpBE1Xt zA095ODXhUwD3Wg30Sqtf99fiV/Y7wDabY+/tKRBIP1jy6Jkbdx6wL19eEtjrjw5trbE r/cQ== X-Gm-Message-State: AOJu0YxemizkA6TQ7sfejd/pEF2OdWTMqqnrvlro/rllnGbWamf6mzhS /XRT39ZD9heQZ/8BubH3vKHIXg== X-Google-Smtp-Source: AGHT+IFVe0qStMR5EZPqdBbCqRrLHqdaysAGC2AsTmAGKsJ2QAFGUhIm1TXERDEE84301ExbYGDm/g== X-Received: by 2002:aa7:de09:0:b0:533:4d45:36ad with SMTP id h9-20020aa7de09000000b005334d4536admr1544569edv.11.1695911855361; Thu, 28 Sep 2023 07:37:35 -0700 (PDT) Received: from [192.168.1.149] (i5C7438D4.versanet.de. [92.116.56.212]) by smtp.gmail.com with ESMTPSA id d15-20020aa7d68f000000b005333922efb0sm9789688edr.78.2023.09.28.07.37.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 07:37:34 -0700 (PDT) From: Erik Schilling Date: Thu, 28 Sep 2023 16:37:30 +0200 Subject: [libgpiod][PATCH 3/3] bindings: rust: mark all owning types as `Send` MIME-Version: 1.0 Message-Id: <20230928-rust-send-trait-v1-3-30b4f59d13cb@linaro.org> References: <20230928-rust-send-trait-v1-0-30b4f59d13cb@linaro.org> In-Reply-To: <20230928-rust-send-trait-v1-0-30b4f59d13cb@linaro.org> To: Linux-GPIO Cc: Bartosz Golaszewski , Viresh Kumar , Erik Schilling X-Mailer: b4 0.13-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1695911851; l=6050; i=erik.schilling@linaro.org; s=20230523; h=from:subject:message-id; bh=DQjcV18Zzml7GwrqlL1NGFIcqUrahm+fL8twaU6BToA=; b=QBbrY83HMRAeGnyyupgg7o4aEZ8w0AGTkPiJ5CY0WhK9ee+kPm1LqJ22PisOIizbZylkTFKip AXQ73gjgOrqCNpzoHqsld/YLgumernfbuBOuCnyKJ+iIeZ41RgILe4k X-Developer-Key: i=erik.schilling@linaro.org; a=ed25519; pk=/nNqy8/YOEdthj1epXl5FgwCTKEiVqTqqnVN1jVal7s= Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org The thread-safety rules of libgpiod allow individual object instances to be used from different threads. So far, this was not actually possible with the Rust bindings. Not being `Send` disallowed the user to transfer the ownership to different threads. Rust also has a `Sync` marker. That one would even allow sending references of objects to other threads. Since we wrap a lot of C functions with `fn foo(&self)` signatures, that would not be safe. libgpiod does not allow concurrent API calls to the same object instance - which Rust would allow for read-only references. Thus, we do not define that one. Chip was already modeled correctly. line::Info is not marked as Send since it may either be owning or non- owning. That problem is fixed as part of a separate pull request [1]. [1] https://lore.kernel.org/r/20230927-rust-line-info-soundness-v1-0-990dce6f18ab@linaro.org Link: https://lore.kernel.org/r/CVHO091CC80Y.3KUOSLSOBVL0T@ablu-work Signed-off-by: Erik Schilling Acked-by: Viresh Kumar --- bindings/rust/libgpiod/src/edge_event.rs | 4 ++++ bindings/rust/libgpiod/src/event_buffer.rs | 8 ++++++++ bindings/rust/libgpiod/src/info_event.rs | 4 ++++ bindings/rust/libgpiod/src/line_config.rs | 4 ++++ bindings/rust/libgpiod/src/line_request.rs | 4 ++++ bindings/rust/libgpiod/src/line_settings.rs | 4 ++++ bindings/rust/libgpiod/src/request_config.rs | 4 ++++ 7 files changed, 32 insertions(+) diff --git a/bindings/rust/libgpiod/src/edge_event.rs b/bindings/rust/libgpiod/src/edge_event.rs index 0c0cfbc..639f033 100644 --- a/bindings/rust/libgpiod/src/edge_event.rs +++ b/bindings/rust/libgpiod/src/edge_event.rs @@ -24,6 +24,10 @@ use super::{ #[derive(Debug, Eq, PartialEq)] pub struct Event(*mut gpiod::gpiod_edge_event); +// SAFETY: Event models a wrapper around an owned gpiod_edge_event and may +// be safely sent to other threads. +unsafe impl Send for Event {} + impl Event { pub fn event_clone(event: &Event) -> Result { // SAFETY: `gpiod_edge_event` is guaranteed to be valid here. diff --git a/bindings/rust/libgpiod/src/event_buffer.rs b/bindings/rust/libgpiod/src/event_buffer.rs index 2e4bfd3..68d6e2f 100644 --- a/bindings/rust/libgpiod/src/event_buffer.rs +++ b/bindings/rust/libgpiod/src/event_buffer.rs @@ -68,6 +68,14 @@ pub struct Buffer { events: Vec<*mut gpiod::gpiod_edge_event>, } +// SAFETY: Buffer models an owned gpiod_edge_event_buffer. However, there may +// be events tied to it. Concurrent access from multiple threads to a buffer +// and its associated events is not allowed by the C lib. +// In Rust, those events will always be borrowed from a buffer instance. Thus, +// either Rust prevents the user to move the Buffer while there are still +// borrowed events, or we can safely send the the Buffer. +unsafe impl Send for Buffer {} + impl Buffer { /// Create a new edge event buffer. /// diff --git a/bindings/rust/libgpiod/src/info_event.rs b/bindings/rust/libgpiod/src/info_event.rs index db60600..1e26b37 100644 --- a/bindings/rust/libgpiod/src/info_event.rs +++ b/bindings/rust/libgpiod/src/info_event.rs @@ -25,6 +25,10 @@ pub struct Event { pub(crate) event: *mut gpiod::gpiod_info_event, } +// SAFETY: Event models a wrapper around an owned gpiod_info_event and may be +// safely sent to other threads. +unsafe impl Send for Event {} + impl Event { /// Get a single chip's line's status change event. pub(crate) fn new(event: *mut gpiod::gpiod_info_event) -> Self { diff --git a/bindings/rust/libgpiod/src/line_config.rs b/bindings/rust/libgpiod/src/line_config.rs index f4f03f1..d0a4aba 100644 --- a/bindings/rust/libgpiod/src/line_config.rs +++ b/bindings/rust/libgpiod/src/line_config.rs @@ -28,6 +28,10 @@ pub struct Config { pub(crate) config: *mut gpiod::gpiod_line_config, } +// SAFETY: Config models a wrapper around an owned gpiod_line_config and may be +// safely sent to other threads. +unsafe impl Send for Config {} + impl Config { /// Create a new line config object. pub fn new() -> Result { diff --git a/bindings/rust/libgpiod/src/line_request.rs b/bindings/rust/libgpiod/src/line_request.rs index a5697d6..64ef05d 100644 --- a/bindings/rust/libgpiod/src/line_request.rs +++ b/bindings/rust/libgpiod/src/line_request.rs @@ -20,6 +20,10 @@ pub struct Request { pub(crate) request: *mut gpiod::gpiod_line_request, } +// SAFETY: Request models a wrapper around an owned gpiod_line_request and may +// be safely sent to other threads. +unsafe impl Send for Request {} + impl Request { /// Request a set of lines for exclusive usage. pub(crate) fn new(request: *mut gpiod::gpiod_line_request) -> Result { diff --git a/bindings/rust/libgpiod/src/line_settings.rs b/bindings/rust/libgpiod/src/line_settings.rs index f0b3e9c..c81d118 100644 --- a/bindings/rust/libgpiod/src/line_settings.rs +++ b/bindings/rust/libgpiod/src/line_settings.rs @@ -25,6 +25,10 @@ pub struct Settings { pub(crate) settings: *mut gpiod::gpiod_line_settings, } +// SAFETY: Settings models a wrapper around an owned gpiod_line_settings and may +// be safely sent to other threads. +unsafe impl Send for Settings {} + impl Settings { /// Create a new line settings object. pub fn new() -> Result { diff --git a/bindings/rust/libgpiod/src/request_config.rs b/bindings/rust/libgpiod/src/request_config.rs index 5bde7c6..9b66cc9 100644 --- a/bindings/rust/libgpiod/src/request_config.rs +++ b/bindings/rust/libgpiod/src/request_config.rs @@ -20,6 +20,10 @@ pub struct Config { pub(crate) config: *mut gpiod::gpiod_request_config, } +// SAFETY: Config models a wrapper around an owned gpiod_request_config and may +// be safely sent to other threads. +unsafe impl Send for Config {} + impl Config { /// Create a new request config object. pub fn new() -> Result {