From patchwork Fri Apr 18 14:09:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 882313 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp831336wrs; Fri, 18 Apr 2025 07:12:26 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUpFoxjAnjwTkhuXDPRAjJgwztvPGi2kt4jdB+xasuSJGGdPVOHx4A0VfENvqh9Noyw8zkNzg==@linaro.org X-Google-Smtp-Source: AGHT+IH13LNOa/8QHUuKYDhBt9em2ujeThdK38evwESKubcIdxaWho83AqQhctdnBRiLGXsoXyno X-Received: by 2002:a05:6000:18a8:b0:391:1218:d5f7 with SMTP id ffacd0b85a97d-39efbad7d6amr2128605f8f.40.1744985546057; Fri, 18 Apr 2025 07:12:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1744985546; cv=none; d=google.com; s=arc-20240605; b=FQTRtrOJAh9URa1K0QtqGE4f0W4QmG0kMGF5Ls4P2dzRoRdFldji1Y8hGCPJ1oHNHl jw9Cuv/OUCzrIWIkzfp5A2jgcdY0q8/LuB9JgL6ncFi3h2K5LVka+8PtDjlI4G9A7Qai EHqrwI528hhRqeXpor5xajxWj2mWrh0VHhQyD7RKpp3FoZG3fdWLkMaEgYTre68hdA7y cpdAdq7BDrOqv/Gnq5op6PlukBaWYcaK8nFMXwT7mroprxV7ToUEBopdL8YHxB5//OfM WGZVfLI1UY/H86B0yyDZ/uim9xgZNJDLf8JlfazcAUo/vsd8ON/hKrO0Aggnbw4XOsz6 E8SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=30iCggiQxsg+n7n81V9I0g3OaEhSTqDQMaxbYhLD4SI=; fh=mu6nz4wWAs4ff4HfOrQmILvkL7+/OL9o4TpNHf8BG70=; b=cvNO9fvBfBuK3P6lt3IzKi09dodkUqezM96txImOvsPa8lruti5fdMJpMg8UGklLXA XyK4ETBavSIWOYTx/Bw8IeKBQ+3B+1cr3HXQWZkNALQNuVp3S6XCKOwH+6Xs3mTMPx0S cFzqQH4sJhuoI5LRLvM0uYM4biudNk4xZ5i2ntxjwe/jjYQ/nyp1GBcJhzmNxAswSwaI EHp73QeQ3mpR+jjFzsAIg4IGOi8vDzIHOcGic8JNGR8eeB/MeXG90Y2SHn7L6ZodlN1w 1ssB4HvLRGvc2as0zSkHR2fDoPogIg7rF1VQUvgqjgzkqB9hubpYtYxkxmfOGa+Vf/LV RELg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HMeNkWfH; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ffacd0b85a97d-39efa48c69bsi1283576f8f.427.2025.04.18.07.12.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Apr 2025 07:12:26 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HMeNkWfH; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 08A66832BC; Fri, 18 Apr 2025 16:11:44 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="HMeNkWfH"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1C26C832AF; Fri, 18 Apr 2025 16:11:41 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 10B928321D for ; Fri, 18 Apr 2025 16:11:39 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-43d0782d787so13681395e9.0 for ; Fri, 18 Apr 2025 07:11:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1744985498; x=1745590298; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=30iCggiQxsg+n7n81V9I0g3OaEhSTqDQMaxbYhLD4SI=; b=HMeNkWfHYoMfYnwjXrAgtWXzTI4uI+A9ufD3AgVNAPhH9UX8TYktdvf4Ts1h0F6IBU oCszz05eIuk2/VA8rkGMSAAB6qV3zAlbWHRwESTSC8ogl3Bn7uqpp/JZEe5HAF9ZtxJK jsldiVkB5nvTtHf/j22MAkxCYQv4xgM0dusg0VeYcEdKxx04Qf8sivgP411j6eXCH67P C6ADA4CQ1ADaL+vjnUu9hVqWVCxYg66GmOwWcBcA0Oho406v6ADifGhzbvfU/zEHaWmJ fc/T5AuDcs2h6LLEqzFAas0sr0LgE7pNpsrzJorSsaqiuJbIAzAjxf7X7WiINUo2cnp7 WL8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744985498; x=1745590298; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=30iCggiQxsg+n7n81V9I0g3OaEhSTqDQMaxbYhLD4SI=; b=Cd3XI3l07VeSu0RBHY8gJkuajYDUzx/3cRGe7CzbHSkOQLW29ox/Jz0ExQa5NJwSCd aLWA48pGuxCBIBv9P/LLlZd/QpIQgK6OgE2ca0hKuNIl7ci9+2d8nNgN4acJhztz6Nxh XYmVkx73fmeC7YMYYpedjWPD8L3JM5TStQhS33F/83liNd+NORPPJo1NJGinRy5Kuy3t jYz6GPqGKJa702kGY1F9YXxFNECKsJ8F97cjNTHMxkJuFLEFQRpz67AWdkFbRxPntkHy gNPwILDUETLWzpf1nIBXyIdzVhFfvCFOEyjtIv5fXtLx3/Ln0qGrlg+kKpVQlQ4B6WIz LnhA== X-Gm-Message-State: AOJu0YxZLZUCpxuSLAk9GLg0xaFZ1UZ2qsHGiiNQ9EQAHe1s7MucH4wN Hwc2e8aowlL7iSBcnNT4xMo28MApzlU2veEMTWSSVulMFCcSbKmq3F7DwCWnHtOW8xuaRqDqxyH YxtDZ3w== X-Gm-Gg: ASbGncupkpXVNGpUBKIeHgujA6Zxg4cviDylzqXJr43nFdklSh438Y7sgUR6Jx+gcr2 bHEfKDWOyJZYrj7wbaPD6XRdHYUAmFmuq9Q1ViyNgGuzlVff5PfgVaKAzEcoE2092vbl8g1+jzD zDsc/v1AaUPxE4U2j/vBOqzlGVt4rdOg3dDtva61u2izG45Cpocw0K0up9qEnosVMPuu+cI/3xB 4lz274XPzV3rqqjBmefX9yKxP2Ng8t2MWbtg93TuZ+v/isrdZjzSqNfex+qGUfjVKXbbNRFP7AG BsDdKXLFiRxu03vh9gYbmlBQXyASyM1afBddtAPFjufaIdXGHA== X-Received: by 2002:a05:600c:3b9b:b0:439:643a:c8d5 with SMTP id 5b1f17b1804b1-4406aa872fdmr28696685e9.0.1744985498282; Fri, 18 Apr 2025 07:11:38 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:1526:5ffd:d63c:cc85]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4406d5bbcc3sm23734385e9.19.2025.04.18.07.11.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Apr 2025 07:11:37 -0700 (PDT) From: Jerome Forissier To: U-Boot mailing list Cc: Jerome Forissier , Ilias Apalodimas , Tom Rini Subject: [PATCH v6 07/17] uthread: add uthread_mutex Date: Fri, 18 Apr 2025 16:09:35 +0200 Message-ID: <20250418141114.2056981-8-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250418141114.2056981-1-jerome.forissier@linaro.org> References: <20250418141114.2056981-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add struct uthread_mutex and uthread_mutex_lock(), uthread_mutex_trylock(), uthread_mutex_unlock() to protect shared data structures from concurrent modifications. Signed-off-by: Jerome Forissier --- Changes in v6: - uthread_mutex_trylock(): return -EBUSY instead of EBUSY include/uthread.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++ lib/uthread.c | 27 +++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/include/uthread.h b/include/uthread.h index f796a16f25f..89fa552a6f6 100644 --- a/include/uthread.h +++ b/include/uthread.h @@ -50,6 +50,23 @@ struct uthread { struct list_head list; }; +/** + * Internal state of a struct uthread_mutex + */ +enum uthread_mutex_state { + UTHREAD_MUTEX_UNLOCKED = 0, + UTHREAD_MUTEX_LOCKED = 1 +}; + +/** + * Uthread mutex + */ +struct uthread_mutex { + enum uthread_mutex_state state; +}; + +#define UTHREAD_MUTEX_INITIALIZER { .state = UTHREAD_MUTEX_UNLOCKED } + #ifdef CONFIG_UTHREAD /** @@ -94,6 +111,44 @@ unsigned int uthread_grp_new_id(void); */ bool uthread_grp_done(unsigned int grp_id); +/** + * uthread_mutex_lock() - lock a mutex + * + * If the cwmutexlock is available (i.e., not owned by any other thread), then + * it is locked for use by the current thread. Otherwise the current thread + * blocks: it enters a wait loop by scheduling other threads until the mutex + * becomes unlocked. + * + * @mutex: pointer to the mutex to lock + * Return: 0 on success, in which case the lock is owned by the calling thread. + * != 0 otherwise (the lock is not owned by the calling thread). + */ +int uthread_mutex_lock(struct uthread_mutex *mutex); + +/** + * uthread_mutex_trylock() - lock a mutex if not currently locked + * + * Similar to uthread_mutex_lock() except return immediately if the mutex is + * locked already. + * + * @mutex: pointer to the mutex to lock + * Return: 0 on success, in which case the lock is owned by the calling thread. + * EBUSY if the mutex is already locked by another thread. Any other non-zero + * value on error. + */ +int uthread_mutex_trylock(struct uthread_mutex *mutex); + +/** + * uthread_mutex_unlock() - unlock a mutex + * + * The mutex is assumed to be owned by the calling thread on entry. On exit, it + * is unlocked. + * + * @mutex: pointer to the mutex to unlock + * Return: 0 on success, != 0 on error + */ +int uthread_mutex_unlock(struct uthread_mutex *mutex); + #else static inline int uthread_create(struct uthread *uthr, void (*fn)(void *), @@ -119,5 +174,10 @@ static inline bool uthread_grp_done(unsigned int grp_id) return true; } +/* These are macros for convenience on the caller side */ +#define uthread_mutex_lock(_mutex) ({ 0; }) +#define uthread_mutex_trylock(_mutex) ({ 0 }) +#define uthread_mutex_unlock(_mutex) ({ 0; }) + #endif /* CONFIG_UTHREAD */ #endif /* _UTHREAD_H_ */ diff --git a/lib/uthread.c b/lib/uthread.c index aa264b1d95f..062fca7d209 100644 --- a/lib/uthread.c +++ b/lib/uthread.c @@ -8,6 +8,7 @@ */ #include +#include #include #include #include @@ -136,3 +137,29 @@ bool uthread_grp_done(unsigned int grp_id) return true; } + +int uthread_mutex_lock(struct uthread_mutex *mutex) +{ + while (mutex->state == UTHREAD_MUTEX_LOCKED) + uthread_schedule(); + + mutex->state = UTHREAD_MUTEX_LOCKED; + return 0; +} + +int uthread_mutex_trylock(struct uthread_mutex *mutex) +{ + if (mutex->state == UTHREAD_MUTEX_UNLOCKED) { + mutex->state = UTHREAD_MUTEX_LOCKED; + return 0; + } + + return -EBUSY; +} + +int uthread_mutex_unlock(struct uthread_mutex *mutex) +{ + mutex->state = UTHREAD_MUTEX_UNLOCKED; + + return 0; +}