Skip to contentSkip to conversation

Alchemy: Fix calling context checks and documentation

PATCH In review Richard Weinberger (sigma star gmbh) 2 participants 10 messages Nov 25 – Nov 26

lore
Share & tools
lore
#0 lore raw

cover Alchemy: Fix calling context checks and documentation

From: Richard Weinberger <[email protected]>

Several Alchemy services refuse callers which are not Copperplate tasks, although they do not need a Copperplate TCB at all, or only need one when actually waiting. As a result, Cobalt POSIX threads, which are Xenomai threads according to the API documentation, get -EPERM from rt_mutex_acquire(), rt_task_yield(), rt_pipe_read() or rt_event_wait(), and even from queue/buffer/heap calls which could be satisfied immediately.

Also the documentation is inaccurate. It keeps referring to "Xenomai threads" where a Copperplate task, or even an Alchemy task, is actually required.

At first I thought fixing the documentation would be the best way to go. Then I remembered a customer application which does mix the Alchemy and POSIX skins, so I gave improving the Alchemy layer a try.

Patch 6/7 is worthy of discussion, I'm not so sure whether it really brings a real gain.

Thanks,
//richard

Richard Weinberger (7):
lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex lib/alchemy/task: Allow rt_task_yield() from any thread lib/alchemy: Document the actual calling context requirements lib/alchemy/pipe: Do not require a Xenomai task for blocking reads lib/alchemy/event: Require a Xenomai task only for waiting over
Mercury
lib/alchemy: Require a Xenomai task only when actually waiting testsuite/smokey/alchemytests: Test Alchemy services from POSIX
threads

 include/alchemy/mutex.h                      |   3 +-
 lib/alchemy/alarm.c                          |   9 -
 lib/alchemy/buffer.c                         |  23 +-
 lib/alchemy/cond.c                           |   3 +-
 lib/alchemy/event.c                          |  11 +-
 lib/alchemy/heap.c                           |  13 +-
 lib/alchemy/mutex.c                          |  22 +-
 lib/alchemy/pipe.c                           |  15 +-
 lib/alchemy/queue.c                          |  23 +-
 lib/alchemy/sem.c                            |   3 +-
 lib/alchemy/task.c                           |  86 +++---
 lib/copperplate/eventobj.c                   |   5 +
 lib/copperplate/init.c                       |  10 +
 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 +++++++++++++++++++
 17 files changed, 561 insertions(+), 94 deletions(-)
 create mode 100644 testsuite/smokey/alchemytests/mutex-3.c
 create mode 100644 testsuite/smokey/alchemytests/posix-1.c
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/7 lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex

From: Richard Weinberger <[email protected]>

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.

---
 include/alchemy/mutex.h |  3 ++-
 lib/alchemy/mutex.c     | 19 +++++++++++++------
 2 files changed, 15 insertions(+), 7 deletions(-)
include/alchemy/mutex.h +2 -1
index 2c4212f33..ef391a294 100644
@@ -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.
 	 */
lib/alchemy/mutex.c +13 -6
index 6bec2d9bb..9609f1fa5 100644
@@ -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;
Signature · 1 line
2.55.0
#3 lore raw

PATCH 2/7 lib/alchemy/task: Allow rt_task_yield() from any thread

In reply to Richard Weinberger (sigma star gmbh)

From: Richard Weinberger <[email protected]>

threadobj_yield() is a plain sched_yield(), there is no need to require a Copperplate thread. Only reject asynchronous contexts.

---
 lib/alchemy/task.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
lib/alchemy/task.c +4 -4
index 73844d34a..dcb3de42c 100644
@@ -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();
Signature · 1 line
2.55.0
#4 lore raw

PATCH 3/7 lib/alchemy: Document the actual calling context requirements

In reply to Richard Weinberger (sigma star gmbh)

From: Richard Weinberger <[email protected]>

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.

---
 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(-)
lib/alchemy/alarm.c +0 -9
index 14cb4e248..9d5171175 100644
@@ -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
lib/alchemy/buffer.c +6 -5
index b9526304d..4f826fb27 100644
@@ -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}
  *
lib/alchemy/cond.c +2 -1
index 4409e3f5b..1a4ccacc3 100644
@@ -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}
  *
lib/alchemy/event.c +6 -3
index 8ba76f333..8428db539 100644
@@ -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}
  *
lib/alchemy/heap.c +4 -3
index 140fdc656..fb3424f5f 100644
@@ -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}
  *
lib/alchemy/mutex.c +2 -1
index 9609f1fa5..75d7ff3d3 100644
@@ -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}
  *
lib/alchemy/pipe.c +6 -5
index a266a380f..b5ada2c7d 100644
@@ -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}
  *
lib/alchemy/queue.c +6 -5
index 0dba63f2b..e91973920 100644
@@ -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}
  *
lib/alchemy/sem.c +2 -1
index e88c6e4d6..6a561574c 100644
@@ -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}
  *
lib/alchemy/task.c +41 -37

187 lines not on this page · raw message

lib/copperplate/init.c +10 -0
index 01969cfea..3eea6b51f 100644
@@ -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>
Signature · 1 line
2.55.0
#5 lore raw

PATCH 4/7 lib/alchemy/pipe: Do not require a Xenomai task for blocking reads

In reply to Richard Weinberger (sigma star gmbh)

From: Richard Weinberger <[email protected]>

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.

---
 lib/alchemy/pipe.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
lib/alchemy/pipe.c +4 -4
index b5ada2c7d..a289f150d 100644
@@ -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));
Signature · 1 line
2.55.0
#6 lore raw

PATCH 5/7 lib/alchemy/event: Require a Xenomai task only for waiting over Mercury

In reply to Richard Weinberger (sigma star gmbh)

From: Richard Weinberger <[email protected]>

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.

---
 lib/alchemy/event.c        | 10 +++++-----
 lib/copperplate/eventobj.c |  5 +++++
 2 files changed, 10 insertions(+), 5 deletions(-)
lib/alchemy/event.c +5 -5
index 8428db539..c08b224c7 100644
@@ -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)
lib/copperplate/eventobj.c +5 -0
index 28b7c167f..c5a726a89 100644
@@ -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;
Signature · 1 line
2.55.0
#7 lore raw

PATCH 6/7 lib/alchemy: Require a Xenomai task only when actually waiting

In reply to Richard Weinberger (sigma star gmbh)

From: Richard Weinberger <[email protected]>

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.

---
 lib/alchemy/buffer.c | 24 ++++++++++++++----------
 lib/alchemy/heap.c   | 12 +++++++-----
 lib/alchemy/queue.c  | 24 ++++++++++++++----------
 3 files changed, 35 insertions(+), 25 deletions(-)
lib/alchemy/buffer.c +14 -10
index 4f826fb27..66e37413c 100644
@@ -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);
 
lib/alchemy/heap.c +7 -5
index fb3424f5f..06f2598ef 100644
@@ -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;
 
lib/alchemy/queue.c +14 -10
index e91973920..18ef51f39 100644
@@ -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;
Signature · 1 line
2.55.0
#9 lore raw

PATCH 7/7 testsuite/smokey/alchemytests: Test Alchemy services from POSIX threads

In reply to Richard Weinberger (sigma star gmbh)

From: Richard Weinberger <[email protected]>

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.

---
 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
testsuite/smokey/alchemytests/Makefile.am +10 -0
index fd9e909de..4ee2ab486 100644
@@ -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)
testsuite/smokey/alchemytests/alchemytests.c +2 -0
index 569b73375..85019c35d 100644
@@ -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
testsuite/smokey/alchemytests/mutex-3.c new file +158 -0

158 lines not on this page · raw message

testsuite/smokey/alchemytests/posix-1.c new file +259 -0

259 lines not on this page · raw message

Signature · 1 line
2.55.0
#8 lore raw

Re: [PATCH 6/7] lib/alchemy: Require a Xenomai task only when actually waiting

In reply to Richard Weinberger (sigma star gmbh)
Quoted 30 lines · Richard Weinberger (sigma star gmbh)

From: Richard Weinberger <[email protected]>

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.

---
 lib/alchemy/buffer.c | 24 ++++++++++++++----------
 lib/alchemy/heap.c   | 12 +++++++-----
 lib/alchemy/queue.c  | 24 ++++++++++++++----------
 3 files changed, 35 insertions(+), 25 deletions(-)
lib/alchemy/buffer.c +2 -2
index 4f826fb27..66e37413c 100644
@@ -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

lib/alchemy/buffer.c ssize_t rt_buffer_read_timed(RT_BUFFER *bf, · line 473

Quoted 9 lines
lib/alchemy/buffer.c +0 -3
@@ -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

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center
#2 lore raw

Re: [PATCH 1/7] lib/alchemy/mutex: Allow any Xenomai thread to acquire a mutex

In reply to Richard Weinberger (sigma star gmbh)
Quoted 66 lines · Richard Weinberger (sigma star gmbh)

From: Richard Weinberger <[email protected]>

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.

---
 include/alchemy/mutex.h |  3 ++-
 lib/alchemy/mutex.c     | 19 +++++++++++++------
 2 files changed, 15 insertions(+), 7 deletions(-)
include/alchemy/mutex.h +2 -1
index 2c4212f33..ef391a294 100644
@@ -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.
 	 */
lib/alchemy/mutex.c +11 -5
index 6bec2d9bb..9609f1fa5 100644
@@ -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

lib/alchemy/mutex.c done: · line 372

Quoted 6 lines
lib/alchemy/mutex.c +2 -1
@@ -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

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center