* [PATCH 1/7] lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex
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)
2026-11-26 6:14 ` Jan Kiszka
2026-11-25 21:34 ` [PATCH 2/7] lib/alchemy/task: Allow rt_task_yield() from any thread Richard Weinberger (sigma star gmbh)
` (5 subsequent siblings)
6 siblings, 1 reply; 11+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-25 21:34 UTC (permalink / raw)
To: xenomai; +Cc: upstream+xenomai, Richard Weinberger
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
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH 1/7] lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex
2026-11-25 21:34 ` [PATCH 1/7] lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex Richard Weinberger (sigma star gmbh)
@ 2026-11-26 6:14 ` Jan Kiszka
2026-11-26 7:53 ` Richard Weinberger
0 siblings, 1 reply; 11+ messages in thread
From: Jan Kiszka @ 2026-11-26 6:14 UTC (permalink / raw)
To: Richard Weinberger (sigma star gmbh), xenomai
Cc: upstream+xenomai, Richard Weinberger
On 25.11.26 22:34, Richard Weinberger (sigma star gmbh) wrote:
> 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;
NO_ALCHEMY_TASK == no owner - you are changing at least internal
semantics, if not worse.
I'm still in favor of fixing the docs, rather than adding this new mode
and trying to figure out which corner cases we missed.
Jan
--
Siemens AG, Foundational Technologies
Linux Expert Center
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH 1/7] lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex
2026-11-26 6:14 ` Jan Kiszka
@ 2026-11-26 7:53 ` Richard Weinberger
0 siblings, 0 replies; 11+ messages in thread
From: Richard Weinberger @ 2026-11-26 7:53 UTC (permalink / raw)
To: Richard Weinberger (sigma star gmbh), xenomai, upstream
Cc: upstream+xenomai, Jan Kiszka
On Donnerstag, 26. November 2026 07:14 'Jan Kiszka' via upstream wrote:
> NO_ALCHEMY_TASK == no owner - you are changing at least internal
> semantics, if not worse.
>
> I'm still in favor of fixing the docs, rather than adding this new mode
> and trying to figure out which corner cases we missed.
I hoped that you at least won't hate patch 1/7... :-D
But yeah, let's stay on the safe side and fixup the docs.
Thanks,
//richard
--
sigma star gmbh | Eduard-Bodem-Gasse 6, 6020 Innsbruck, AUT UID/VAT Nr:
ATU 66964118 | FN: 374287y
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 2/7] lib/alchemy/task: Allow rt_task_yield() from any thread
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 ` [PATCH 1/7] lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex Richard Weinberger (sigma star gmbh)
@ 2026-11-25 21:34 ` 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)
` (4 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-25 21:34 UTC (permalink / raw)
To: xenomai; +Cc: upstream+xenomai, Richard Weinberger
From: Richard Weinberger <richard@nod.at>
threadobj_yield() is a plain sched_yield(), there is no need to
require a Copperplate thread. Only reject asynchronous contexts.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
lib/alchemy/task.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/lib/alchemy/task.c b/lib/alchemy/task.c
index 73844d34a..dcb3de42c 100644
--- a/lib/alchemy/task.c
+++ b/lib/alchemy/task.c
@@ -1309,14 +1309,14 @@ out:
*
* @return Zero is returned upon success. Otherwise:
*
- * - -EPERM is returned if this service was called from an invalid
- * context.
+ * - -EPERM is returned if this service was called from an
+ * asynchronous context.
*
- * @apitags{xthread-only, switch-primary}
+ * @apitags{thread-unrestricted, switch-primary}
*/
int rt_task_yield(void)
{
- if (!threadobj_current_p())
+ if (threadobj_irq_p())
return -EPERM;
threadobj_yield();
--
2.55.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH 3/7] lib/alchemy: Document the actual calling context requirements
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 ` [PATCH 1/7] lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex Richard Weinberger (sigma star gmbh)
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 ` 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)
` (3 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-25 21:34 UTC (permalink / raw)
To: xenomai; +Cc: upstream+xenomai, Richard Weinberger
From: Richard Weinberger <richard@nod.at>
Many services document -EPERM for callers which are not "Xenomai
threads", but actually require a Copperplate TCB (Alchemy, pSOS or
VxWorks task, or the main thread), or even an Alchemy task. Cobalt
POSIX threads are Xenomai threads, yet refused.
Introduce the term "Xenomai task" in the glossary and document the
requirement which is actually enforced. Also fix a few wrong errno
values, API tags and copy&paste errors along the way.
No functional change.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
lib/alchemy/alarm.c | 9 -----
lib/alchemy/buffer.c | 11 +++---
lib/alchemy/cond.c | 3 +-
lib/alchemy/event.c | 9 +++--
lib/alchemy/heap.c | 7 ++--
lib/alchemy/mutex.c | 3 +-
lib/alchemy/pipe.c | 11 +++---
lib/alchemy/queue.c | 11 +++---
lib/alchemy/sem.c | 3 +-
lib/alchemy/task.c | 78 ++++++++++++++++++++++--------------------
lib/copperplate/init.c | 10 ++++++
11 files changed, 85 insertions(+), 70 deletions(-)
diff --git a/lib/alchemy/alarm.c b/lib/alchemy/alarm.c
index 14cb4e248..9d5171175 100644
--- a/lib/alchemy/alarm.c
+++ b/lib/alchemy/alarm.c
@@ -155,9 +155,6 @@ static void alarm_handler(struct timerobj *tmobj)
* - -EEXIST is returned if the @a name is conflicting with an already
* registered alarm.
*
- * - -EPERM is returned if this service was called from an
- * asynchronous context.
- *
* @apitags{mode-unrestricted, switch-secondary}
*
* @note Alarms are process-private objects and thus cannot be shared
@@ -237,9 +234,6 @@ out:
*
* - -EINVAL is returned if @a alarm is not a valid alarm descriptor.
*
- * - -EPERM is returned if this service was called from an
- * asynchronous context.
- *
* @apitags{mode-unrestricted, switch-secondary}
*/
#ifndef DOXYGEN_CPP
@@ -298,9 +292,6 @@ out:
*
* - -EINVAL is returned if @a alarm is not a valid alarm descriptor.
*
- * - -EPERM is returned if this service was called from an invalid
- * context.
- *
* @apitags{xthread-only, switch-primary}
*
* @note Each of the initial @a value and @a interval is interpreted
diff --git a/lib/alchemy/buffer.c b/lib/alchemy/buffer.c
index b9526304d..4f826fb27 100644
--- a/lib/alchemy/buffer.c
+++ b/lib/alchemy/buffer.c
@@ -424,8 +424,8 @@ out:
* waiting for data. In such event, @a bf 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 not called from a Xenomai
+ * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
*
* @note A short read (i.e. fewer bytes returned than requested by @a
* len) may happen whenever a pathological use of the buffer is
@@ -664,8 +664,8 @@ out:
* waiting for buffer space. In such event, @a bf 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 not called from a Xenomai
+ * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
*
* @apitags{xthread-nowait, switch-primary}
*/
@@ -914,7 +914,8 @@ out:
* the specified amount of time.
*
* - -EPERM is returned if this service should block, but was not
- * called from a Xenomai thread.
+ * called from a Xenomai task. Over Cobalt, this error is also returned
+ * if the caller is not a Xenomai thread, even if it should not block.
*
* @apitags{xthread-nowait, switch-primary}
*
diff --git a/lib/alchemy/cond.c b/lib/alchemy/cond.c
index 4409e3f5b..1a4ccacc3 100644
--- a/lib/alchemy/cond.c
+++ b/lib/alchemy/cond.c
@@ -458,7 +458,8 @@ int rt_cond_inquire(RT_COND *cond, RT_COND_INFO *info)
* the specified amount of time.
*
* - -EPERM is returned if this service should block, but was not
- * called from a Xenomai thread.
+ * called from a Xenomai task. Over Cobalt, this error is also returned
+ * if the caller is not a Xenomai thread, even if it should not block.
*
* @apitags{xthread-nowait, switch-primary}
*
diff --git a/lib/alchemy/event.c b/lib/alchemy/event.c
index 8ba76f333..8428db539 100644
--- a/lib/alchemy/event.c
+++ b/lib/alchemy/event.c
@@ -378,8 +378,10 @@ out:
* sleeping on it. In such a case, @a event 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 not called from a Xenomai
+ * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }. Over
+ * Cobalt, this error is also returned if the caller is not a Xenomai
+ * thread.
*
* @apitags{xthread-nowait, switch-primary}
*/
@@ -583,7 +585,8 @@ out:
* the specified amount of time.
*
* - -EPERM is returned if this service should block, but was not
- * called from a Xenomai thread.
+ * called from a Xenomai task. Over Cobalt, this error is also returned
+ * if the caller is not a Xenomai thread, even if it should not block.
*
* @apitags{xthread-nowait, switch-primary}
*
diff --git a/lib/alchemy/heap.c b/lib/alchemy/heap.c
index 140fdc656..fb3424f5f 100644
--- a/lib/alchemy/heap.c
+++ b/lib/alchemy/heap.c
@@ -408,8 +408,8 @@ out:
* waiting for a block. In such event, @a heap 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 not called from a Xenomai
+ * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
*
* @apitags{xthread-nowait, switch-primary}
*
@@ -639,7 +639,8 @@ out:
* the specified amount of time.
*
* - -EPERM is returned if this service should block, but was not
- * called from a Xenomai thread.
+ * called from a Xenomai task. Over Cobalt, this error is also returned
+ * if the caller is not a Xenomai thread, even if it should not block.
*
* @apitags{xthread-nowait, switch-primary}
*
diff --git a/lib/alchemy/mutex.c b/lib/alchemy/mutex.c
index 9609f1fa5..75d7ff3d3 100644
--- a/lib/alchemy/mutex.c
+++ b/lib/alchemy/mutex.c
@@ -499,7 +499,8 @@ out:
* the specified amount of time.
*
* - -EPERM is returned if this service should block, but was not
- * called from a Xenomai thread.
+ * called from a Xenomai task. Over Cobalt, this error is also returned
+ * if the caller is not a Xenomai thread, even if it should not block.
*
* @apitags{xthread-nowait, switch-primary}
*
diff --git a/lib/alchemy/pipe.c b/lib/alchemy/pipe.c
index a266a380f..b5ada2c7d 100644
--- a/lib/alchemy/pipe.c
+++ b/lib/alchemy/pipe.c
@@ -289,7 +289,7 @@ out:
* @fn ssize_t rt_pipe_read(RT_PIPE *pipe, void *buf, size_t size, RTIME timeout)
* @brief Read from a pipe (with relative scalar timeout).
*
- * This routine is a variant of rt_queue_read_timed() accepting a
+ * This routine is a variant of rt_pipe_read_timed() accepting a
* relative timeout specification expressed as a scalar value.
*
* @param pipe The pipe descriptor.
@@ -354,7 +354,7 @@ ssize_t rt_pipe_read(RT_PIPE *pipe,
* @fn ssize_t rt_pipe_read_until(RT_PIPE *pipe, void *buf, size_t size, RTIME abs_timeout)
* @brief Read from a pipe (with absolute scalar timeout).
*
- * This routine is a variant of rt_queue_read_timed() accepting an
+ * This routine is a variant of rt_pipe_read_timed() accepting an
* absolute timeout specification expressed as a scalar value.
*
* @param pipe The pipe descriptor.
@@ -420,8 +420,8 @@ ssize_t rt_pipe_read(RT_PIPE *pipe,
* waiting for a message. In such event, @a pipe 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 not called from a Xenomai
+ * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
*
* @apitags{xthread-nowait, switch-primary}
*/
@@ -637,7 +637,8 @@ ssize_t rt_pipe_stream(RT_PIPE *pipe,
* the specified amount of time.
*
* - -EPERM is returned if this service should block, but was not
- * called from a Xenomai thread.
+ * called from a Xenomai task. Over Cobalt, this error is also returned
+ * if the caller is not a Xenomai thread, even if it should not block.
*
* @apitags{xthread-nowait}
*
diff --git a/lib/alchemy/queue.c b/lib/alchemy/queue.c
index 0dba63f2b..e91973920 100644
--- a/lib/alchemy/queue.c
+++ b/lib/alchemy/queue.c
@@ -792,8 +792,8 @@ out:
* waiting for a message. In such event, @a q 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 not called from a Xenomai
+ * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
*
* @apitags{xthread-nowait, switch-primary}
*/
@@ -952,8 +952,8 @@ out:
* waiting for a message. In such event, @a q 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 not called from a Xenomai
+ * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
*
* @apitags{xthread-nowait, switch-primary}
*/
@@ -1159,7 +1159,8 @@ out:
* the specified amount of time.
*
* - -EPERM is returned if this service should block, but was not
- * called from a Xenomai thread.
+ * called from a Xenomai task. Over Cobalt, this error is also returned
+ * if the caller is not a Xenomai thread, even if it should not block.
*
* @apitags{xthread-nowait, switch-primary}
*
diff --git a/lib/alchemy/sem.c b/lib/alchemy/sem.c
index e88c6e4d6..6a561574c 100644
--- a/lib/alchemy/sem.c
+++ b/lib/alchemy/sem.c
@@ -536,7 +536,8 @@ out:
* the specified amount of time.
*
* - -EPERM is returned if this service should block, but was not
- * called from a Xenomai thread.
+ * called from a Xenomai task. Over Cobalt, this error is also returned
+ * if the caller is not a Xenomai thread, even if it should not block.
*
* @apitags{xthread-nowait, switch-primary}
*
diff --git a/lib/alchemy/task.c b/lib/alchemy/task.c
index dcb3de42c..2f61c8580 100644
--- a/lib/alchemy/task.c
+++ b/lib/alchemy/task.c
@@ -473,8 +473,8 @@ out:
*
* - -EINVAL is returned if @a task is not a valid task descriptor.
*
- * - -EPERM is returned if @a task is NULL and this service was called
- * from an invalid context. In addition, this error is always raised
+ * - -EPERM is returned if @a task is NULL but the caller is not an
+ * Alchemy task. In addition, this error is always raised
* when this service is called from asynchronous context, such as a
* timer/alarm handler.
*
@@ -562,10 +562,12 @@ int rt_task_join(RT_TASK *task)
*
* @return Zero is returned upon success. Otherwise:
*
- * - -EINVAL is returned if @a task is NULL but the caller is not a
- * Xenomai task, or if @a task is non-NULL but not a valid task
+ * - -EINVAL is returned if @a task is non-NULL but not a valid task
* descriptor.
*
+ * - -EPERM is returned if @a task is NULL but the caller is not an
+ * Alchemy task.
+ *
* - -EINVAL is returned if @a cpus contains no processors that are
* currently physically on the system and permitted to the process
* according to any restrictions that may be imposed by the "cpuset"
@@ -707,12 +709,13 @@ out:
* - -EEXIST is returned if the @a name is conflicting with an already
* registered task.
*
- * - -EBUSY is returned if the caller is not a regular POSIX thread.
+ * - -EBUSY is returned if the caller already is an Alchemy, pSOS or
+ * VxWorks task.
*
* - -EPERM is returned if this service was called from an invalid
* context, e.g. interrupt handler.
*
- * @apitags{pthread-only, switch-secondary, switch-primary}
+ * @apitags{thread-unrestricted, switch-secondary, switch-primary}
*
* @sideeffect Over Cobalt, if the caller is a plain POSIX thread, it
* is turned into a Xenomai _shadow_ thread, with full access to all
@@ -809,10 +812,12 @@ undo:
*
* @return Zero is returned upon success. Otherwise:
*
- * - -EINVAL is returned if @a task is NULL but the caller is not a
- * Xenomai task, or if @a task is non-NULL but not a valid task
+ * - -EINVAL is returned if @a task is non-NULL but not a valid task
* descriptor.
*
+ * - -EPERM is returned if @a task is NULL but the caller is not an
+ * Alchemy task.
+ *
* - -ETIMEDOUT is returned if @a idate is different from TM_INFINITE
* and represents a date in the past.
*
@@ -919,8 +924,8 @@ out:
* task. If @a overruns_r is non-NULL, the count of pending overruns
* is written to the pointed memory location.
*
- * - -EPERM is returned if this service was called from an invalid
- * context.
+ * - -EPERM is returned if this service was not called from a Xenomai
+ * task.
*
* @apitags{xthread-only, switch-primary}
*
@@ -957,13 +962,11 @@ int rt_task_wait_period(unsigned long *overruns_r)
*
* - -ETIMEDOUT is returned if @a date has already elapsed.
*
- * - -EPERM is returned if this service was called from an invalid
- * context.
+ * - -EPERM is returned if this service was not called from a Xenomai
+ * task.
*
* @apitags{xthread-only, switch-primary}
*
- * @note The caller must be an Alchemy task if @a task is NULL.
- *
* @note The @a date value is interpreted as a multiple of the Alchemy
* clock resolution (see --alchemy-clock-resolution option, defaults
* to 1 nanosecond).
@@ -1058,7 +1061,7 @@ int rt_task_sleep(RTIME delay)
*
* @return See rt_task_create().
*
- * @apitags{mode-unrestricted, switch-secondary}
+ * @apitags{xthread-only, mode-unrestricted, switch-secondary}
*
* @sideeffect see rt_task_create().
*/
@@ -1126,12 +1129,11 @@ int rt_task_same(RT_TASK *task1, RT_TASK *task2)
*
* @return Zero is returned upon success. Otherwise:
*
- * - -EINVAL is returned if @a task is NULL but the caller is not a
- * Xenomai task, or if @a task is non-NULL but not a valid task
+ * - -EINVAL is returned if @a task is non-NULL but not a valid task
* descriptor.
*
- * - -EPERM is returned if @a task is NULL and this service was called
- * from an invalid context.
+ * - -EPERM is returned if @a task is NULL but the caller is not an
+ * Alchemy task.
*
* @apitags{mode-unrestricted, switch-primary}
*
@@ -1256,8 +1258,8 @@ RT_TASK *rt_task_self(void)
* - -EINVAL is returned if @a task is not a valid task descriptor, or
* if @a prio is invalid.
*
- * - -EPERM is returned if @a task is NULL and this service was called
- * from an invalid context.
+ * - -EPERM is returned if @a task is NULL but the caller is not an
+ * Alchemy task.
*
* @apitags{mode-unrestricted, switch-primary, switch-secondary}
*
@@ -1389,8 +1391,8 @@ out:
* - -EINVAL is returned if @a task is not a valid task descriptor, or
* if @a prio is invalid.
*
- * - -EPERM is returned if @a task is NULL and this service was called
- * from an invalid context.
+ * - -EPERM is returned if @a task is NULL but the caller is not an
+ * Alchemy task.
*
* @apitags{mode-unrestricted, switch-primary}
*
@@ -1483,11 +1485,12 @@ out:
*
* @return Zero is returned upon success. Otherwise:
*
- * - -EINVAL is returned if @a task is not a valid task descriptor, or
- * if any bit from @a clrmask or @a setmask is invalid.
-
- * - -EPERM is returned if this service was called from an invalid
- * context.
+ * - -EINVAL is returned if any bit from @a clrmask or @a setmask is
+ * invalid.
+ *
+ * - -EPERM is returned if this service was not called from an Alchemy
+ * task, or if it was called from an asynchronous context with bits
+ * other than T_LOCK in @a clrmask or @a setmask.
*
* @apitags{xthread-only, switch-primary}
*
@@ -1549,8 +1552,8 @@ out:
* - -EINVAL is returned if @a task is not a valid task descriptor, or
* if @a prio is invalid.
*
- * - -EPERM is returned if @a task is NULL and this service was called
- * from an invalid context.
+ * - -EPERM is returned if @a task is NULL but the caller is not an
+ * Alchemy task.
*
* @apitags{mode-unrestricted, switch-primary}
*
@@ -1699,8 +1702,8 @@ out:
*
* - -EINVAL is returned if @a task is not a valid task descriptor.
*
- * - -EPERM is returned if this service was called from an invalid
- * context.
+ * - -EPERM is returned if this service was not called from a Xenomai
+ * task.
*
* - -ENOBUFS is returned if @a mcb_r does not point at a message area
* large enough to collect the remote task's reply. This includes the
@@ -1906,8 +1909,8 @@ out:
* token should be passed to rt_task_reply(), in order to send back a
* reply to and unblock the remote task appropriately. Otherwise:
*
- * - -EPERM is returned if this service was called from an invalid
- * context.
+ * - -EPERM is returned if this service was not called from an Alchemy
+ * task.
*
* - -EINTR is returned if rt_task_unblock() was called for the
* current task before a message was received.
@@ -2038,8 +2041,8 @@ out:
* calling rt_task_send(). In such a case, the remote task also
* receives -ENOBUFS on return from rt_task_send().
*
- * - -EPERM is returned if this service was called from an invalid
- * context.
+ * - -EPERM is returned if this service was not called from an Alchemy
+ * task.
*
* @apitags{xthread-only, switch-primary}
*/
@@ -2147,7 +2150,8 @@ out:
* the specified amount of time.
*
* - -EPERM is returned if this service should block, but was not
- * called from a Xenomai thread.
+ * called from a Xenomai task. Over Cobalt, this error is also returned
+ * if the caller is not a Xenomai thread, even if it should not block.
*
* @apitags{xthread-nowait, switch-primary}
*
diff --git a/lib/copperplate/init.c b/lib/copperplate/init.c
index 01969cfea..3eea6b51f 100644
--- a/lib/copperplate/init.c
+++ b/lib/copperplate/init.c
@@ -337,6 +337,16 @@ copperplate_setup_call(copperplate_interface);
* excluding such threads which have been promoted to the real-time
* domain afterwards (aka "shadowed") over Cobalt.
*
+ * @attention By Xenomai task, we mean the subset of Xenomai threads
+ * which have a Copperplate thread control block: tasks created or
+ * shadowed by one of the RTOS APIs based on Copperplate (Alchemy,
+ * pSOS, VxWorks), and the thread which initialized the Xenomai
+ * libraries (i.e. normally main()). Cobalt/POSIX threads created by
+ * the POSIX API are Xenomai threads, but not Xenomai tasks. Some
+ * services further require the caller to be a task of a particular
+ * API, e.g. an Alchemy task created by rt_task_create() or
+ * rt_task_shadow().
+ *
* @par
* <b>Context tags</b>
* <TABLE>
--
2.55.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH 4/7] lib/alchemy/pipe: Do not require a Xenomai task for blocking reads
2026-11-25 21:34 [PATCH 0/7] Alchemy: Fix calling context checks and documentation Richard Weinberger (sigma star gmbh)
` (2 preceding siblings ...)
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 ` 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)
` (2 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-25 21:34 UTC (permalink / raw)
To: xenomai; +Cc: upstream+xenomai, Richard Weinberger
From: Richard Weinberger <richard@nod.at>
rt_pipe_read*() block in the RTDM socket layer, the Copperplate TCB is
not used. Allow any Cobalt thread to wait, only reject asynchronous
contexts.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
lib/alchemy/pipe.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/lib/alchemy/pipe.c b/lib/alchemy/pipe.c
index b5ada2c7d..a289f150d 100644
--- a/lib/alchemy/pipe.c
+++ b/lib/alchemy/pipe.c
@@ -327,7 +327,7 @@ ssize_t rt_pipe_read(RT_PIPE *pipe,
if (timeout == TM_NONBLOCK)
flags = MSG_DONTWAIT;
else {
- if (!threadobj_current_p())
+ if (threadobj_irq_p())
return -EPERM;
if (timeout != TM_INFINITE) {
clockobj_ticks_to_timespec(&alchemy_clock, timeout,
@@ -420,8 +420,8 @@ ssize_t rt_pipe_read(RT_PIPE *pipe,
* waiting for a message. In such event, @a pipe is no more valid upon
* return of this service.
*
- * - -EPERM is returned if this service was not called from a Xenomai
- * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
+ * - -EPERM is returned if this service should block, but was called
+ * from an asynchronous context.
*
* @apitags{xthread-nowait, switch-primary}
*/
@@ -442,7 +442,7 @@ ssize_t rt_pipe_read_timed(RT_PIPE *pipe,
if (alchemy_poll_mode(abs_timeout))
flags = MSG_DONTWAIT;
else {
- if (!threadobj_current_p())
+ if (threadobj_irq_p())
return -EPERM;
if (abs_timeout) {
__RT(clock_gettime(CLOCK_COPPERPLATE, &now));
--
2.55.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH 5/7] lib/alchemy/event: Require a Xenomai task only for waiting over Mercury
2026-11-25 21:34 [PATCH 0/7] Alchemy: Fix calling context checks and documentation Richard Weinberger (sigma star gmbh)
` (3 preceding siblings ...)
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 ` 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-25 21:34 ` [PATCH 7/7] testsuite/smokey/alchemytests: Test Alchemy services from POSIX threads Richard Weinberger (sigma star gmbh)
6 siblings, 0 replies; 11+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-25 21:34 UTC (permalink / raw)
To: xenomai; +Cc: upstream+xenomai, Richard Weinberger
From: Richard Weinberger <richard@nod.at>
Over Cobalt, rt_event_wait*() is based on a Cobalt event which any
Cobalt thread may wait for. Only the Mercury implementation needs a
Copperplate TCB, and only when actually waiting. Move the check there
and keep rejecting blocking calls from asynchronous contexts.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
lib/alchemy/event.c | 10 +++++-----
lib/copperplate/eventobj.c | 5 +++++
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/lib/alchemy/event.c b/lib/alchemy/event.c
index 8428db539..c08b224c7 100644
--- a/lib/alchemy/event.c
+++ b/lib/alchemy/event.c
@@ -378,10 +378,10 @@ out:
* sleeping on it. In such a case, @a event is no more valid upon
* return of this service.
*
- * - -EPERM is returned if this service was not called from a Xenomai
- * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }. Over
- * Cobalt, this error is also returned if the caller is not a Xenomai
- * thread.
+ * - -EPERM is returned if this service should block, but was called
+ * from an asynchronous context, or over Mercury not from a Xenomai
+ * task. Over Cobalt, this error is also returned if the caller is not
+ * a Xenomai thread.
*
* @apitags{xthread-nowait, switch-primary}
*/
@@ -393,7 +393,7 @@ int rt_event_wait_timed(RT_EVENT *event,
struct alchemy_event *evcb;
struct service svc;
- if (!threadobj_current_p() && !alchemy_poll_mode(abs_timeout))
+ if (threadobj_irq_p() && !alchemy_poll_mode(abs_timeout))
return -EPERM;
if (mode & ~EVOBJ_ANY)
diff --git a/lib/copperplate/eventobj.c b/lib/copperplate/eventobj.c
index 28b7c167f..c5a726a89 100644
--- a/lib/copperplate/eventobj.c
+++ b/lib/copperplate/eventobj.c
@@ -229,6 +229,11 @@ int eventobj_wait(struct eventobj *evobj,
goto done;
}
+ if (!threadobj_current_p()) {
+ ret = -EPERM;
+ goto done;
+ }
+
wait = threadobj_prepare_wait(struct eventobj_wait_struct);
wait->value = bits;
wait->mode = mode;
--
2.55.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH 6/7] lib/alchemy: Require a Xenomai task only when actually waiting
2026-11-25 21:34 [PATCH 0/7] Alchemy: Fix calling context checks and documentation Richard Weinberger (sigma star gmbh)
` (4 preceding siblings ...)
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 ` 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)
6 siblings, 1 reply; 11+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-25 21:34 UTC (permalink / raw)
To: xenomai; +Cc: upstream+xenomai, Richard Weinberger
From: Richard Weinberger <richard@nod.at>
The queue, buffer and heap services refuse non-polling calls from
threads without a Copperplate TCB upfront, even if the request can be
satisfied immediately. The TCB is only needed to wait, so check for it
there.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
lib/alchemy/buffer.c | 24 ++++++++++++++----------
lib/alchemy/heap.c | 12 +++++++-----
lib/alchemy/queue.c | 24 ++++++++++++++----------
3 files changed, 35 insertions(+), 25 deletions(-)
diff --git a/lib/alchemy/buffer.c b/lib/alchemy/buffer.c
index 4f826fb27..66e37413c 100644
--- a/lib/alchemy/buffer.c
+++ b/lib/alchemy/buffer.c
@@ -424,8 +424,8 @@ out:
* waiting for data. In such event, @a bf is no more valid upon return
* of this service.
*
- * - -EPERM is returned if this service was not called from a Xenomai
- * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
+ * - -EPERM is returned if this service should block, but was not
+ * called from a Xenomai task.
*
* @note A short read (i.e. fewer bytes returned than requested by @a
* len) may happen whenever a pathological use of the buffer is
@@ -468,9 +468,6 @@ ssize_t rt_buffer_read_timed(RT_BUFFER *bf,
if (len == 0)
return 0;
- if (!threadobj_current_p() && !alchemy_poll_mode(abs_timeout))
- return -EPERM;
-
CANCEL_DEFER(svc);
bcb = get_alchemy_buffer(bf, &syns, &ret);
@@ -547,6 +544,11 @@ redo:
goto redo;
}
+ if (!threadobj_current_p()) {
+ ret = -EPERM;
+ goto done;
+ }
+
if (wait == NULL)
wait = threadobj_prepare_wait(struct alchemy_buffer_wait);
@@ -664,8 +666,8 @@ out:
* waiting for buffer space. In such event, @a bf is no more valid
* upon return of this service.
*
- * - -EPERM is returned if this service was not called from a Xenomai
- * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
+ * - -EPERM is returned if this service should block, but was not
+ * called from a Xenomai task.
*
* @apitags{xthread-nowait, switch-primary}
*/
@@ -687,9 +689,6 @@ ssize_t rt_buffer_write_timed(RT_BUFFER *bf,
if (len == 0)
return 0;
- if (!threadobj_current_p() && !alchemy_poll_mode(abs_timeout))
- return -EPERM;
-
CANCEL_DEFER(svc);
bcb = get_alchemy_buffer(bf, &syns, &ret);
@@ -754,6 +753,11 @@ ssize_t rt_buffer_write_timed(RT_BUFFER *bf,
goto done;
}
+ if (!threadobj_current_p()) {
+ ret = -EPERM;
+ goto done;
+ }
+
if (wait == NULL)
wait = threadobj_prepare_wait(struct alchemy_buffer_wait);
diff --git a/lib/alchemy/heap.c b/lib/alchemy/heap.c
index fb3424f5f..06f2598ef 100644
--- a/lib/alchemy/heap.c
+++ b/lib/alchemy/heap.c
@@ -408,8 +408,8 @@ out:
* waiting for a block. In such event, @a heap is no more valid upon
* return of this service.
*
- * - -EPERM is returned if this service was not called from a Xenomai
- * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
+ * - -EPERM is returned if this service should block, but was not
+ * called from a Xenomai task.
*
* @apitags{xthread-nowait, switch-primary}
*
@@ -431,9 +431,6 @@ int rt_heap_alloc_timed(RT_HEAP *heap,
void *p = NULL;
int ret = 0;
- if (!threadobj_current_p() && !alchemy_poll_mode(abs_timeout))
- return -EPERM;
-
CANCEL_DEFER(svc);
hcb = get_alchemy_heap(heap, &syns, &ret);
@@ -468,6 +465,11 @@ int rt_heap_alloc_timed(RT_HEAP *heap,
goto done;
}
+ if (!threadobj_current_p()) {
+ ret = -EPERM;
+ goto done;
+ }
+
wait = threadobj_prepare_wait(struct alchemy_heap_wait);
wait->size = size;
diff --git a/lib/alchemy/queue.c b/lib/alchemy/queue.c
index e91973920..18ef51f39 100644
--- a/lib/alchemy/queue.c
+++ b/lib/alchemy/queue.c
@@ -792,8 +792,8 @@ out:
* waiting for a message. In such event, @a q is no more valid upon
* return of this service.
*
- * - -EPERM is returned if this service was not called from a Xenomai
- * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
+ * - -EPERM is returned if this service should block, but was not
+ * called from a Xenomai task.
*
* @apitags{xthread-nowait, switch-primary}
*/
@@ -808,9 +808,6 @@ ssize_t rt_queue_receive_timed(RT_QUEUE *queue, void **bufp,
ssize_t ret;
int err = 0;
- if (!threadobj_current_p() && !alchemy_poll_mode(abs_timeout))
- return -EPERM;
-
CANCEL_DEFER(svc);
qcb = get_alchemy_queue(queue, &syns, &err);
@@ -834,6 +831,11 @@ wait:
goto done;
}
+ if (!threadobj_current_p()) {
+ ret = -EPERM;
+ goto done;
+ }
+
wait = threadobj_prepare_wait(struct alchemy_queue_wait);
wait->local_bufsz = 0;
@@ -952,8 +954,8 @@ out:
* waiting for a message. In such event, @a q is no more valid upon
* return of this service.
*
- * - -EPERM is returned if this service was not called from a Xenomai
- * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
+ * - -EPERM is returned if this service should block, but was not
+ * called from a Xenomai task.
*
* @apitags{xthread-nowait, switch-primary}
*/
@@ -969,9 +971,6 @@ ssize_t rt_queue_read_timed(RT_QUEUE *queue,
ssize_t ret;
int err = 0;
- if (!threadobj_current_p() && !alchemy_poll_mode(abs_timeout))
- return -EPERM;
-
if (size == 0)
return 0;
@@ -995,6 +994,11 @@ wait:
goto done;
}
+ if (!threadobj_current_p()) {
+ ret = -EPERM;
+ goto done;
+ }
+
wait = threadobj_prepare_wait(struct alchemy_queue_wait);
wait->local_buf = buf;
wait->local_bufsz = size;
--
2.55.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH 6/7] lib/alchemy: Require a Xenomai task only when actually waiting
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
0 siblings, 0 replies; 11+ messages in thread
From: Jan Kiszka @ 2026-11-26 6:09 UTC (permalink / raw)
To: Richard Weinberger (sigma star gmbh), xenomai
Cc: upstream+xenomai, Richard Weinberger
On 25.11.26 22:34, Richard Weinberger (sigma star gmbh) wrote:
> From: Richard Weinberger <richard@nod.at>
>
> The queue, buffer and heap services refuse non-polling calls from
> threads without a Copperplate TCB upfront, even if the request can be
> satisfied immediately. The TCB is only needed to wait, so check for it
> there.
>
> Signed-off-by: Richard Weinberger <richard@nod.at>
> ---
> lib/alchemy/buffer.c | 24 ++++++++++++++----------
> lib/alchemy/heap.c | 12 +++++++-----
> lib/alchemy/queue.c | 24 ++++++++++++++----------
> 3 files changed, 35 insertions(+), 25 deletions(-)
>
> diff --git a/lib/alchemy/buffer.c b/lib/alchemy/buffer.c
> index 4f826fb27..66e37413c 100644
> --- a/lib/alchemy/buffer.c
> +++ b/lib/alchemy/buffer.c
> @@ -424,8 +424,8 @@ out:
> * waiting for data. In such event, @a bf is no more valid upon return
> * of this service.
> *
> - * - -EPERM is returned if this service was not called from a Xenomai
> - * task, unless @a abs_timeout is { .tv_sec = 0, .tv_nsec = 0 }.
> + * - -EPERM is returned if this service should block, but was not
> + * called from a Xenomai task.
> *
> * @note A short read (i.e. fewer bytes returned than requested by @a
> * len) may happen whenever a pathological use of the buffer is
> @@ -468,9 +468,6 @@ ssize_t rt_buffer_read_timed(RT_BUFFER *bf,
> if (len == 0)
> return 0;
>
> - if (!threadobj_current_p() && !alchemy_poll_mode(abs_timeout))
> - return -EPERM;
> -
> CANCEL_DEFER(svc);
>
> bcb = get_alchemy_buffer(bf, &syns, &ret);
This one takes syncobj_lock. Similar for all the get_alchemy_* below.
Jan
--
Siemens AG, Foundational Technologies
Linux Expert Center
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 7/7] testsuite/smokey/alchemytests: Test Alchemy services from POSIX threads
2026-11-25 21:34 [PATCH 0/7] Alchemy: Fix calling context checks and documentation Richard Weinberger (sigma star gmbh)
` (5 preceding siblings ...)
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-25 21:34 ` Richard Weinberger (sigma star gmbh)
6 siblings, 0 replies; 11+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-25 21:34 UTC (permalink / raw)
To: xenomai; +Cc: upstream+xenomai, Richard Weinberger
From: Richard Weinberger <richard@nod.at>
Check that a Cobalt POSIX thread, which is no Alchemy task, may use
mutexes, rt_task_yield() and pipes, and use queues, buffers, heaps and
events as long as it does not have to wait for a Copperplate
synchronization object.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
testsuite/smokey/alchemytests/Makefile.am | 10 +
testsuite/smokey/alchemytests/alchemytests.c | 2 +
testsuite/smokey/alchemytests/mutex-3.c | 158 +++++++++++
testsuite/smokey/alchemytests/posix-1.c | 259 +++++++++++++++++++
4 files changed, 429 insertions(+)
create mode 100644 testsuite/smokey/alchemytests/mutex-3.c
create mode 100644 testsuite/smokey/alchemytests/posix-1.c
diff --git a/testsuite/smokey/alchemytests/Makefile.am b/testsuite/smokey/alchemytests/Makefile.am
index fd9e909de..4ee2ab486 100644
--- a/testsuite/smokey/alchemytests/Makefile.am
+++ b/testsuite/smokey/alchemytests/Makefile.am
@@ -21,6 +21,8 @@ test_PROGRAMS = \
alchemytests_mq3 \
alchemytests_mutex1 \
alchemytests_mutex2 \
+ alchemytests_mutex3 \
+ alchemytests_posix1 \
alchemytests_sem1 \
alchemytests_sem2 \
alchemytests_task1 \
@@ -79,6 +81,14 @@ alchemytests_mutex2_SOURCES = mutex-2.c
alchemytests_mutex2_CPPFLAGS = $(alchemycppflags)
alchemytests_mutex2_LDADD = $(alchemyldadd)
alchemytests_mutex2_LDFLAGS = @XENO_AUTOINIT_LDFLAGS@
+alchemytests_mutex3_SOURCES = mutex-3.c
+alchemytests_mutex3_CPPFLAGS = $(alchemycppflags)
+alchemytests_mutex3_LDADD = $(alchemyldadd)
+alchemytests_mutex3_LDFLAGS = @XENO_AUTOINIT_LDFLAGS@
+alchemytests_posix1_SOURCES = posix-1.c
+alchemytests_posix1_CPPFLAGS = $(alchemycppflags)
+alchemytests_posix1_LDADD = $(alchemyldadd)
+alchemytests_posix1_LDFLAGS = @XENO_AUTOINIT_LDFLAGS@
alchemytests_mq1_SOURCES = mq-1.c
alchemytests_mq1_CPPFLAGS = $(alchemycppflags)
alchemytests_mq1_LDADD = $(alchemyldadd)
diff --git a/testsuite/smokey/alchemytests/alchemytests.c b/testsuite/smokey/alchemytests/alchemytests.c
index 569b73375..85019c35d 100644
--- a/testsuite/smokey/alchemytests/alchemytests.c
+++ b/testsuite/smokey/alchemytests/alchemytests.c
@@ -15,6 +15,8 @@ static const char * const tests[] = {
"alchemytests_mq3",
"alchemytests_mutex1",
"alchemytests_mutex2",
+ "alchemytests_mutex3",
+ "alchemytests_posix1",
#ifdef CONFIG_XENO_COBALT
"alchemytests_pipe1",
#endif
diff --git a/testsuite/smokey/alchemytests/mutex-3.c b/testsuite/smokey/alchemytests/mutex-3.c
new file mode 100644
index 000000000..f3c2b0f43
--- /dev/null
+++ b/testsuite/smokey/alchemytests/mutex-3.c
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Check that Alchemy mutexes may be acquired by any Xenomai thread,
+ * not only by Alchemy tasks, and that the owner reported by
+ * rt_mutex_inquire() is only an Alchemy task.
+ *
+ * __RT() is required since the Alchemy tests are not linked with the
+ * POSIX symbol wrappers, but we want a Cobalt thread here.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <copperplate/traceobj.h>
+#include <alchemy/task.h>
+#include <alchemy/mutex.h>
+
+static struct traceobj trobj;
+
+static int tseq[] = {
+ 1, 2, 3, 4, 5, 6, 7, 8, 9,
+};
+
+static RT_TASK t_a;
+
+static RT_MUTEX mutex;
+
+static sem_t held, done;
+
+static void *posix_thread(void *arg)
+{
+ int ret;
+
+ traceobj_enter(&trobj);
+
+ /* Used to fail with -EPERM for non-Alchemy callers. */
+ ret = rt_mutex_acquire(&mutex, TM_NONBLOCK);
+ traceobj_check(&trobj, ret, 0);
+
+ /* Alchemy mutexes are recursive. */
+ ret = rt_mutex_acquire(&mutex, TM_INFINITE);
+ traceobj_check(&trobj, ret, 0);
+
+ ret = rt_task_yield();
+ traceobj_check(&trobj, ret, 0);
+
+ traceobj_mark(&trobj, 2);
+
+ __RT(sem_post(&held));
+ __RT(sem_wait(&done));
+
+ traceobj_mark(&trobj, 4);
+
+ ret = rt_mutex_release(&mutex);
+ traceobj_check(&trobj, ret, 0);
+
+ ret = rt_mutex_release(&mutex);
+ traceobj_check(&trobj, ret, 0);
+
+ traceobj_exit(&trobj);
+
+ return NULL;
+}
+
+static void task_a(void *arg)
+{
+ int ret;
+
+ traceobj_enter(&trobj);
+
+ ret = rt_mutex_acquire(&mutex, TM_INFINITE);
+ traceobj_check(&trobj, ret, 0);
+
+ traceobj_mark(&trobj, 6);
+
+ __RT(sem_post(&held));
+ __RT(sem_wait(&done));
+
+ ret = rt_mutex_release(&mutex);
+ traceobj_check(&trobj, ret, 0);
+
+ traceobj_mark(&trobj, 8);
+
+ traceobj_exit(&trobj);
+}
+
+int main(int argc, char *const argv[])
+{
+ struct sched_param param = { .sched_priority = 10 };
+ RT_MUTEX_INFO info;
+ pthread_attr_t attr;
+ pthread_t tid;
+ int ret;
+
+ traceobj_init(&trobj, argv[0], sizeof(tseq) / sizeof(int));
+
+ __RT(sem_init(&held, 0, 0));
+ __RT(sem_init(&done, 0, 0));
+
+ ret = rt_mutex_create(&mutex, "MUTEX");
+ traceobj_check(&trobj, ret, 0);
+
+ traceobj_mark(&trobj, 1);
+
+ pthread_attr_init(&attr);
+ pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
+ pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
+ pthread_attr_setschedparam(&attr, ¶m);
+ ret = __RT(pthread_create(&tid, &attr, posix_thread, NULL));
+ traceobj_check(&trobj, ret, 0);
+ pthread_attr_destroy(&attr);
+
+ __RT(sem_wait(&held));
+
+ traceobj_mark(&trobj, 3);
+
+ /* We are not an Alchemy task either: must not get -EPERM. */
+ ret = rt_mutex_acquire(&mutex, TM_NONBLOCK);
+ traceobj_check(&trobj, ret, -EWOULDBLOCK);
+
+ ret = rt_mutex_inquire(&mutex, &info);
+ traceobj_check(&trobj, ret, 0);
+ traceobj_assert(&trobj, info.owner.handle == NO_ALCHEMY_TASK.handle);
+
+ __RT(sem_post(&done));
+
+ ret = __RT(pthread_join(tid, NULL));
+ traceobj_check(&trobj, ret, 0);
+
+ traceobj_mark(&trobj, 5);
+
+ ret = rt_task_create(&t_a, "taskA", 0, 20, 0);
+ traceobj_check(&trobj, ret, 0);
+
+ ret = rt_task_start(&t_a, task_a, NULL);
+ traceobj_check(&trobj, ret, 0);
+
+ __RT(sem_wait(&held));
+
+ traceobj_mark(&trobj, 7);
+
+ ret = rt_mutex_inquire(&mutex, &info);
+ traceobj_check(&trobj, ret, 0);
+ traceobj_assert(&trobj, info.owner.handle == t_a.handle);
+
+ __RT(sem_post(&done));
+
+ traceobj_join(&trobj);
+
+ traceobj_mark(&trobj, 9);
+
+ ret = rt_mutex_delete(&mutex);
+ traceobj_check(&trobj, ret, 0);
+
+ traceobj_verify(&trobj, tseq, sizeof(tseq) / sizeof(int));
+
+ exit(0);
+}
diff --git a/testsuite/smokey/alchemytests/posix-1.c b/testsuite/smokey/alchemytests/posix-1.c
new file mode 100644
index 000000000..faed2829a
--- /dev/null
+++ b/testsuite/smokey/alchemytests/posix-1.c
@@ -0,0 +1,259 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Call Alchemy services from a POSIX thread, which is no Xenomai
+ * task. -EPERM is only expected when the caller would have to wait
+ * on a Copperplate synchronization object.
+ *
+ * __RT() is required since the Alchemy tests are not linked with the
+ * POSIX symbol wrappers, but we want a Cobalt thread here.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <copperplate/traceobj.h>
+#include <alchemy/task.h>
+#include <alchemy/timer.h>
+#include <alchemy/event.h>
+#include <alchemy/queue.h>
+#include <alchemy/buffer.h>
+#include <alchemy/heap.h>
+#ifdef CONFIG_XENO_COBALT
+#include <alchemy/pipe.h>
+#endif
+#include <smokey/smokey.h>
+
+static struct traceobj trobj;
+
+static RT_EVENT event;
+
+static RT_QUEUE queue;
+
+static RT_BUFFER buffer;
+
+static RT_HEAP heap;
+
+static RTIME short_delay;
+
+static const char msg[] = "hello";
+
+#ifdef CONFIG_XENO_COBALT
+
+static RT_PIPE mpipe;
+
+static int pipe_minor = -1;
+
+static sem_t pipe_ready;
+
+static void test_pipe(void)
+{
+ char buf[sizeof(msg)];
+ ssize_t ret;
+
+ if (pipe_minor < 0)
+ return;
+
+ ret = rt_pipe_read(&mpipe, buf, sizeof(buf), short_delay);
+ traceobj_check(&trobj, ret, -ETIMEDOUT);
+
+ __RT(sem_post(&pipe_ready));
+
+ ret = rt_pipe_read(&mpipe, buf, sizeof(buf), TM_INFINITE);
+ traceobj_check(&trobj, ret, sizeof(msg));
+ traceobj_assert(&trobj, strcmp(buf, msg) == 0);
+}
+
+static void feed_pipe(void)
+{
+ char *rtp;
+ int fd;
+
+ if (pipe_minor < 0)
+ return;
+
+ __RT(sem_wait(&pipe_ready));
+
+ traceobj_assert(&trobj, asprintf(&rtp, "/dev/rtp%d", pipe_minor) > 0);
+ fd = open(rtp, O_WRONLY);
+ free(rtp);
+ traceobj_assert(&trobj, fd >= 0);
+ traceobj_assert(&trobj, write(fd, msg, sizeof(msg)) == sizeof(msg));
+ close(fd);
+}
+
+static void create_pipe(void)
+{
+ pipe_minor = rt_pipe_create(&mpipe, "pipe", P_MINOR_AUTO, 0);
+ if (pipe_minor < 0) {
+ notice("pipe test skipped (no kernel support)");
+ return;
+ }
+
+ __RT(sem_init(&pipe_ready, 0, 0));
+}
+
+static void delete_pipe(void)
+{
+ if (pipe_minor >= 0)
+ traceobj_check(&trobj, rt_pipe_delete(&mpipe), 0);
+}
+
+#else
+
+static inline void test_pipe(void) { }
+static inline void feed_pipe(void) { }
+static inline void create_pipe(void) { }
+static inline void delete_pipe(void) { }
+
+#endif
+
+static void test_event(void)
+{
+ unsigned int mask;
+ int ret;
+
+ ret = rt_event_wait(&event, 0x1, &mask, EV_ANY, TM_INFINITE);
+ traceobj_check(&trobj, ret, 0);
+ traceobj_assert(&trobj, mask == 0x1);
+
+ ret = rt_event_wait(&event, 0x2, &mask, EV_ANY, short_delay);
+#ifdef CONFIG_XENO_COBALT
+ traceobj_check(&trobj, ret, -ETIMEDOUT);
+#else
+ traceobj_check(&trobj, ret, -EPERM);
+#endif
+}
+
+static void test_queue(void)
+{
+ char buf[sizeof(msg)];
+ ssize_t ret;
+ void *p;
+
+ ret = rt_queue_read(&queue, buf, sizeof(buf), TM_INFINITE);
+ traceobj_check(&trobj, ret, sizeof(msg));
+ traceobj_assert(&trobj, strcmp(buf, msg) == 0);
+
+ ret = rt_queue_receive(&queue, &p, TM_INFINITE);
+ traceobj_check(&trobj, ret, sizeof(msg));
+ traceobj_assert(&trobj, strcmp(p, msg) == 0);
+ traceobj_check(&trobj, rt_queue_free(&queue, p), 0);
+
+ ret = rt_queue_read(&queue, buf, sizeof(buf), TM_NONBLOCK);
+ traceobj_check(&trobj, ret, -EWOULDBLOCK);
+
+ ret = rt_queue_read(&queue, buf, sizeof(buf), short_delay);
+ traceobj_check(&trobj, ret, -EPERM);
+
+ ret = rt_queue_receive(&queue, &p, TM_INFINITE);
+ traceobj_check(&trobj, ret, -EPERM);
+}
+
+static void test_buffer(void)
+{
+ char buf[64];
+ ssize_t ret;
+
+ memset(buf, 0, sizeof(buf));
+
+ ret = rt_buffer_write(&buffer, buf, 16, TM_INFINITE);
+ traceobj_check(&trobj, ret, 16);
+
+ ret = rt_buffer_read(&buffer, buf, 16, TM_INFINITE);
+ traceobj_check(&trobj, ret, 16);
+
+ ret = rt_buffer_read(&buffer, buf, 16, short_delay);
+ traceobj_check(&trobj, ret, -EPERM);
+
+ ret = rt_buffer_write(&buffer, buf, sizeof(buf), TM_INFINITE);
+ traceobj_check(&trobj, ret, sizeof(buf));
+
+ ret = rt_buffer_write(&buffer, buf, 1, short_delay);
+ traceobj_check(&trobj, ret, -EPERM);
+
+ ret = rt_buffer_read(&buffer, buf, sizeof(buf), TM_NONBLOCK);
+ traceobj_check(&trobj, ret, sizeof(buf));
+}
+
+static void test_heap(void)
+{
+ void *p1, *p2, *p3;
+ int ret;
+
+ ret = rt_heap_alloc(&heap, 8192, TM_INFINITE, &p1);
+ traceobj_check(&trobj, ret, 0);
+
+ ret = rt_heap_alloc(&heap, 8192, TM_INFINITE, &p2);
+ traceobj_check(&trobj, ret, 0);
+
+ ret = rt_heap_alloc(&heap, 8192, short_delay, &p3);
+ traceobj_check(&trobj, ret, -EPERM);
+
+ traceobj_check(&trobj, rt_heap_free(&heap, p1), 0);
+ traceobj_check(&trobj, rt_heap_free(&heap, p2), 0);
+}
+
+static void *posix_thread(void *arg)
+{
+ test_event();
+ test_queue();
+ test_buffer();
+ test_heap();
+ test_pipe();
+
+ return NULL;
+}
+
+int main(int argc, char *const argv[])
+{
+ struct sched_param param = { .sched_priority = 10 };
+ pthread_attr_t attr;
+ pthread_t tid;
+ int ret;
+
+ traceobj_init(&trobj, argv[0], 0);
+
+ short_delay = rt_timer_ns2ticks(1000000);
+
+ ret = rt_event_create(&event, "event", 0x1, EV_PRIO);
+ traceobj_check(&trobj, ret, 0);
+
+ ret = rt_queue_create(&queue, "queue", 4096, Q_UNLIMITED, Q_FIFO);
+ traceobj_check(&trobj, ret, 0);
+ ret = rt_queue_write(&queue, msg, sizeof(msg), Q_NORMAL);
+ traceobj_check(&trobj, ret, 0);
+ ret = rt_queue_write(&queue, msg, sizeof(msg), Q_NORMAL);
+ traceobj_check(&trobj, ret, 0);
+
+ ret = rt_buffer_create(&buffer, "buffer", 64, B_FIFO);
+ traceobj_check(&trobj, ret, 0);
+
+ ret = rt_heap_create(&heap, "heap", 16384, H_PRIO);
+ traceobj_check(&trobj, ret, 0);
+
+ create_pipe();
+
+ pthread_attr_init(&attr);
+ pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
+ pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
+ pthread_attr_setschedparam(&attr, ¶m);
+ ret = __RT(pthread_create(&tid, &attr, posix_thread, NULL));
+ traceobj_check(&trobj, ret, 0);
+ pthread_attr_destroy(&attr);
+
+ feed_pipe();
+
+ ret = __RT(pthread_join(tid, NULL));
+ traceobj_check(&trobj, ret, 0);
+
+ delete_pipe();
+ traceobj_check(&trobj, rt_heap_delete(&heap), 0);
+ traceobj_check(&trobj, rt_buffer_delete(&buffer), 0);
+ traceobj_check(&trobj, rt_queue_delete(&queue), 0);
+ traceobj_check(&trobj, rt_event_delete(&event), 0);
+
+ exit(0);
+}
--
2.55.0
^ permalink raw reply related [flat|nested] 11+ messages in thread