All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Richard Weinberger (sigma star gmbh)" <rw@kernel.org>
To: xenomai@lists.linux.dev
Cc: upstream+xenomai@sigma-star.at, Richard Weinberger <richard@nod.at>
Subject: [PATCH 1/7] lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex
Date: Wed, 25 Nov 2026 22:34:13 +0100	[thread overview]
Message-ID: <20261125213419.2363742-2-rw@kernel.org> (raw)
In-Reply-To: <20261125213419.2363742-1-rw@kernel.org>

From: Richard Weinberger <richard@nod.at>

rt_mutex_acquire_timed() returns -EPERM unless the caller is an
Alchemy task, although the lock is a plain POSIX mutex. The Alchemy
TCB is only used to record the owner for rt_mutex_inquire().

Only reject asynchronous contexts and report NO_ALCHEMY_TASK as owner
for non-Alchemy callers.

Signed-off-by: Richard Weinberger <richard@nod.at>
---
 include/alchemy/mutex.h |  3 ++-
 lib/alchemy/mutex.c     | 19 +++++++++++++------
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/include/alchemy/mutex.h b/include/alchemy/mutex.h
index 2c4212f33..ef391a294 100644
--- a/include/alchemy/mutex.h
+++ b/include/alchemy/mutex.h
@@ -42,7 +42,8 @@ typedef struct RT_MUTEX RT_MUTEX;
  */
 struct RT_MUTEX_INFO {
 	/**
-	 * Current mutex owner, or NO_ALCHEMY_TASK if unlocked. This
+	 * Current mutex owner, or NO_ALCHEMY_TASK if unlocked or
+	 * owned by a thread which is not an Alchemy task. This
 	 * information is in essence transient, and may not be valid
 	 * anymore once used by the caller.
 	 */
diff --git a/lib/alchemy/mutex.c b/lib/alchemy/mutex.c
index 6bec2d9bb..9609f1fa5 100644
--- a/lib/alchemy/mutex.c
+++ b/lib/alchemy/mutex.c
@@ -285,8 +285,13 @@ out:
  * waiting on it. In such event, @a mutex is no more valid upon return
  * of this service.
  *
- * - -EPERM is returned if this service should block, but was not
- * called from a Xenomai thread.
+ * - -EPERM is returned if this service was called from an
+ * asynchronous context, or over Cobalt, from a thread which is not a
+ * Cobalt thread (i.e. a regular POSIX thread which was not shadowed).
+ *
+ * @note Any Xenomai thread may acquire an Alchemy mutex, including a
+ * Cobalt POSIX thread. However, only Alchemy tasks are reported as
+ * the current owner by rt_mutex_inquire().
  *
  * @apitags{xthread-only, switch-primary}
  *
@@ -303,11 +308,12 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 	struct timespec ts;
 	int ret = 0;
 
-	/* This must be an alchemy task. */
-	current = alchemy_task_current();
-	if (current == NULL)
+	if (threadobj_irq_p())
 		return -EPERM;
 
+	/* May be NULL if the caller is not an Alchemy task. */
+	current = alchemy_task_current();
+
 	/*
 	 * Try the fast path first. Note that we don't need any
 	 * protected section here: the caller should have provided for
@@ -362,7 +368,8 @@ done:
 		ret = -EWOULDBLOCK;
 		break;
 	case 0:
-		mcb->owner.handle = mainheap_ref(current, uintptr_t);
+		mcb->owner.handle = current ?
+			mainheap_ref(current, uintptr_t) : NO_ALCHEMY_TASK.handle;
 	}
 
 	return ret;
-- 
2.55.0


  reply	other threads:[~2026-11-25 21:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 21:34 [PATCH 0/7] Alchemy: Fix calling context checks and documentation Richard Weinberger (sigma star gmbh)
2026-11-25 21:34 ` Richard Weinberger (sigma star gmbh) [this message]
2026-11-26  6:14   ` [PATCH 1/7] lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex Jan Kiszka
2026-11-26  7:53     ` Richard Weinberger
2026-11-25 21:34 ` [PATCH 2/7] lib/alchemy/task: Allow rt_task_yield() from any thread Richard Weinberger (sigma star gmbh)
2026-11-25 21:34 ` [PATCH 3/7] lib/alchemy: Document the actual calling context requirements Richard Weinberger (sigma star gmbh)
2026-11-25 21:34 ` [PATCH 4/7] lib/alchemy/pipe: Do not require a Xenomai task for blocking reads Richard Weinberger (sigma star gmbh)
2026-11-25 21:34 ` [PATCH 5/7] lib/alchemy/event: Require a Xenomai task only for waiting over Mercury Richard Weinberger (sigma star gmbh)
2026-11-25 21:34 ` [PATCH 6/7] lib/alchemy: Require a Xenomai task only when actually waiting Richard Weinberger (sigma star gmbh)
2026-11-26  6:09   ` Jan Kiszka
2026-11-25 21:34 ` [PATCH 7/7] testsuite/smokey/alchemytests: Test Alchemy services from POSIX threads Richard Weinberger (sigma star gmbh)

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=20261125213419.2363742-2-rw@kernel.org \
    --to=rw@kernel.org \
    --cc=richard@nod.at \
    --cc=upstream+xenomai@sigma-star.at \
    --cc=xenomai@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.