Skip to contentSkip to conversation

Alchemy: Fix rt_task_unblock() for RT_MUTEX

PATCH In review Richard Weinberger 5 participants 16 messages Mar 30, 2022 – Jan 10, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Alchemy: Fix rt_task_unblock() for RT_MUTEX

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() shall not return EINTR, this requirement breaks rt_task_unblock() if a RT_TASK blocks on a RT_MUTEX.

To restore the functionality provide a new function, pthread_mutex_lock_eintr_np(). It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  2 +-
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 15 insertions(+), 3 deletions(-)
include/cobalt/pthread.h +2 -0
index 3e9bd47053bc..2994c2467219 100644
@@ -62,6 +62,8 @@ COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t *mutex));
 
 COBALT_DECL(int, pthread_mutex_lock(pthread_mutex_t *mutex));
 
+COBALT_DECL(int, pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex));
+
 COBALT_DECL(int, pthread_mutex_timedlock(pthread_mutex_t *mutex,
 					 const struct timespec *to));
 
lib/alchemy/mutex.c +1 -1
index f8933858647a..bb97395142aa 100644
@@ -327,7 +327,7 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 
 	/* Slow path. */
 	if (abs_timeout == NULL) {
-		ret = -__RT(pthread_mutex_lock(&mcb->lock));
+		ret = -__RT(pthread_mutex_lock_eintr_np(&mcb->lock));
 		goto done;
 	}
 
lib/cobalt/mutex.c +12 -2
index 73e45a1c4396..2ef02a175c13 100644
@@ -314,7 +314,7 @@ COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex))
  *
  * @apitags{xthread-only, switch-primary}
  */
-COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+static int __pthread_mutex_lock(pthread_mutex_t *mutex, bool want_eintr)
 {
 	struct cobalt_mutex_shadow *_mutex =
 		&((union cobalt_mutex_union *)mutex)->shadow_mutex;
@@ -373,7 +373,7 @@ slow_path:
 
 	do
 		ret = XENOMAI_SYSCALL1(sc_cobalt_mutex_lock, _mutex);
-	while (ret == -EINTR);
+	while (ret == -EINTR && !want_eintr);
 
 	if (ret == 0)
 		_mutex->lockcnt = 1;
@@ -392,6 +392,16 @@ protect:
 	goto fast_path;
 }
 
+COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, false);
+}
+
+COBALT_IMPL(int, pthread_mutex_lock_eintr_np, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, true);
+}
+
 /**
  * Attempt, during a bounded time, to lock a mutex.
  *
Signature · 1 line
2.26.2
#1 lore raw

Hi Richard,

Quoted 6 lines · Richard Weinberger via Xenomai

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() shall not return EINTR, this requirement breaks rt_task_unblock() if a RT_TASK blocks on a RT_MUTEX.

I can't say much about the alchemy skin. Never used that myself. Have you checked the history if such a change was / could be intentional? CCing Jan...

The following comments are more or less implementation details...

Quoted 15 lines

To restore the functionality provide a new function, pthread_mutex_lock_eintr_np(). It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  2 +-
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 15 insertions(+), 3 deletions(-)
include/cobalt/pthread.h +0 -0
index 3e9bd47053bc..2994c2467219 100644

include/cobalt/pthread.h COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t *mutex)); · line 66

Quoted 5 lines
include/cobalt/pthread.h +2 -0
@@ -62,6 +62,8 @@ COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t *mutex));
 
 COBALT_DECL(int, pthread_mutex_lock(pthread_mutex_t *mutex));
 
+COBALT_DECL(int, pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex));
+

COBALT_DECL should only be necessary for functions that live in the libc namespace and that need to be "intercepted" / affected by wrapping at linker level.

Your function looks libcobalt specific and as such should probably not live in the pthread_mutex_ namespace and generating the wrappers seems unnecessary.

Quoted 43 lines
include/cobalt/pthread.h +0 -0
 COBALT_DECL(int, pthread_mutex_timedlock(pthread_mutex_t *mutex,
 					 const struct timespec *to));
 
lib/alchemy/mutex.c +1 -1
index f8933858647a..bb97395142aa 100644
@@ -327,7 +327,7 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 
 	/* Slow path. */
 	if (abs_timeout == NULL) {
-		ret = -__RT(pthread_mutex_lock(&mcb->lock));
+		ret = -__RT(pthread_mutex_lock_eintr_np(&mcb->lock));
 		goto done;
 	}
 
lib/cobalt/mutex.c +3 -2
index 73e45a1c4396..2ef02a175c13 100644
@@ -314,7 +314,7 @@ COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex))
  *
  * @apitags{xthread-only, switch-primary}
  */
-COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+static int __pthread_mutex_lock(pthread_mutex_t *mutex, bool want_eintr)
 {
 	struct cobalt_mutex_shadow *_mutex =
 		&((union cobalt_mutex_union *)mutex)->shadow_mutex;
@@ -373,7 +373,7 @@ slow_path:
 
 	do
 		ret = XENOMAI_SYSCALL1(sc_cobalt_mutex_lock, _mutex);
-	while (ret == -EINTR);
+	while (ret == -EINTR && !want_eintr);
 
 	if (ret == 0)
 		_mutex->lockcnt = 1;
@@ -392,6 +392,16 @@ protect:
 	goto fast_path;
 }
 
+COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))

lib/cobalt/mutex.c protect: · line 405

Quoted 10 lines
lib/cobalt/mutex.c +8 -0
+{
+	return __pthread_mutex_lock(mutex, false);
+}
+
 
 
+COBALT_IMPL(int, pthread_mutex_lock_eintr_np, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, true);
+}

Same as above. Looks libcobalt specific and the wrapping part seems unnecessary.

Quoted 4 lines
lib/cobalt/mutex.c +1 -0
+
 /**

* Attempt, during a bounded time, to lock a mutex. *

#2 lore raw

----- Ursprüngliche Mail -----

Quoted 4 lines

Von: "Florian Bezdeka" <[email protected]>
I can't say much about the alchemy skin. Never used that myself. Have you checked the history if such a change was / could be intentional? CCing Jan...

Well, an application that relies on rt_task_unblock() now no longer works with Xenomai 3. It used to work with 2.6.

In 2019 this was briefly discussed:
https://www.mail-archive.com/[email protected]/msg15990.html

Finally I found some cycles to work on that.

With my changes at least the application works again and the tests on the customer side pass.

Quoted 29 lines

The following comments are more or less implementation details...

Quoted 16 lines

To restore the functionality provide a new function, pthread_mutex_lock_eintr_np().
It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  2 +-
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 15 insertions(+), 3 deletions(-)
include/cobalt/pthread.h +0 -0
index 3e9bd47053bc..2994c2467219 100644

include/cobalt/pthread.h COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t · line 67

Quoted 6 lines
include/cobalt/pthread.h +2 -0
@@ -62,6 +62,8 @@ COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t
 *mutex));
 
 COBALT_DECL(int, pthread_mutex_lock(pthread_mutex_t *mutex));
 
+COBALT_DECL(int, pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex));
+

COBALT_DECL should only be necessary for functions that live in the libc namespace and that need to be "intercepted" / affected by wrapping at linker level.

Quoted 3 lines

Your function looks libcobalt specific and as such should probably not live in the pthread_mutex_ namespace and generating the wrappers seems unnecessary.

Ok!

[...]

Quoted 7 lines

include/cobalt/pthread.h COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t

Quoted 4 lines
include/cobalt/pthread.h +4 -0
+COBALT_IMPL(int, pthread_mutex_lock_eintr_np, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, true);
+}

Same as above. Looks libcobalt specific and the wrapping part seems unnecessary.

Ok.

Thanks,
//richard

#3 lore raw
Quoted 9 lines · Richard Weinberger

----- Ursprüngliche Mail -----

Quoted 4 lines

Von: "Florian Bezdeka" <[email protected]>
I can't say much about the alchemy skin. Never used that myself. Have you checked the history if such a change was / could be intentional? CCing Jan...

Well, an application that relies on rt_task_unblock() now no longer works with Xenomai 3. It used to work with 2.6.

Quoted 7 lines

In 2019 this was briefly discussed:
https://www.mail-archive.com/[email protected]/msg15990.html

Finally I found some cycles to work on that.

With my changes at least the application works again and the tests on the customer side pass.

We would need such / similar tests as well inside the Xenomai testsuite to make sure we don't break it again.

Quoted 50 lines
Quoted 30 lines

The following comments are more or less implementation details...

Quoted 16 lines

To restore the functionality provide a new function, pthread_mutex_lock_eintr_np().
It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  2 +-
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 15 insertions(+), 3 deletions(-)
include/cobalt/pthread.h +0 -0
index 3e9bd47053bc..2994c2467219 100644

include/cobalt/pthread.h COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t · line 67

Quoted 6 lines
include/cobalt/pthread.h +2 -0
@@ -62,6 +62,8 @@ COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t
 *mutex));
 
 COBALT_DECL(int, pthread_mutex_lock(pthread_mutex_t *mutex));
 
+COBALT_DECL(int, pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex));
+

COBALT_DECL should only be necessary for functions that live in the libc namespace and that need to be "intercepted" / affected by wrapping at linker level.

Quoted 3 lines

Your function looks libcobalt specific and as such should probably not live in the pthread_mutex_ namespace and generating the wrappers seems unnecessary.

Ok!

[...]

Quoted 7 lines

include/cobalt/pthread.h COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t

Quoted 4 lines
include/cobalt/pthread.h +4 -0
+COBALT_IMPL(int, pthread_mutex_lock_eintr_np, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, true);
+}

Same as above. Looks libcobalt specific and the wrapping part seems unnecessary.

Ok.

Thanks,
//richard

#4 lore raw
Quoted 5 lines · Florian via Xenomai
Quoted 2 lines

With my changes at least the application works again and the tests on the customer side pass.

We would need such / similar tests as well inside the Xenomai testsuite to make sure we don't break it again.

I'm a bit unsure where to place the test.
Under testsuite/ seems to be not a single alchemy related test. Shall I create a new folder?

Thanks,
//richard

#5 lore raw
Quoted 11 lines · Richard Weinberger via Xenomai
Quoted 5 lines · Florian via Xenomai
Quoted 2 lines

With my changes at least the application works again and the tests on the customer side pass.

We would need such / similar tests as well inside the Xenomai testsuite to make sure we don't break it again.

I'm a bit unsure where to place the test.
Under testsuite/ seems to be not a single alchemy related test. Shall I create a new folder?

There is lib/alchemy/testsuite/ - but also
https://gitlab.com/Xenomai/xenomai-hacker-space/-/issues/32.

Jan

Signature · 2 lines
Siemens AG, Technology
Competence Center Embedded Linux
#6 lore raw

----- Ursprüngliche Mail -----

Quoted 15 lines

Von: "Jan Kiszka" <[email protected]>
An: "Richard Weinberger" <[email protected]>, "Florian Bezdeka" <[email protected]>
CC: "richard" <[email protected]>, "xenomai" <[email protected]>
Gesendet: Montag, 4. April 2022 13:20:55
Betreff: Re: [PATCH] Alchemy: Fix rt_task_unblock() for RT_MUTEX

Quoted 8 lines · Richard Weinberger via Xenomai
Quoted 5 lines · Florian via Xenomai
Quoted 2 lines

With my changes at least the application works again and the tests on the customer side pass.

We would need such / similar tests as well inside the Xenomai testsuite to make sure we don't break it again.

Quoted 6 lines
Quoted 3 lines

I'm a bit unsure where to place the test.
Under testsuite/ seems to be not a single alchemy related test. Shall I create a new folder?

There is lib/alchemy/testsuite/ - but also
https://gitlab.com/Xenomai/xenomai-hacker-space/-/issues/32.

:-S

Thanks for the pointer.

I fear there are more TODOs, just gave some tests a try. LD_LIBRARY_PATH=/usr/xenomai/lib64 lib/alchemy/testsuite/task-5.c.exe
[9] at task-5.c:79
[1] at task-5.c:23
[10] at task-5.c:87
[3] at task-5.c:40
[11] at task-5.c:95
[4] at task-5.c:45
[5] at task-5.c:50
[6] at task-5.c:55
[2] at task-5.c:28
[7] at task-5.c:60
0"001.868| BUG in __traceobj_check_abort(): [FGND] wrong return status:

              task-5.c:63 => EINVAL (want OK)

Thanks,
//richard

#7 lore raw
In reply to Richard Weinberger
Quoted 34 lines · Richard Weinberger via Xenomai

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() shall not return EINTR, this requirement breaks rt_task_unblock() if a RT_TASK blocks on a RT_MUTEX.

To restore the functionality provide a new function, pthread_mutex_lock_eintr_np(). It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  2 +-
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 15 insertions(+), 3 deletions(-)
include/cobalt/pthread.h +2 -0
index 3e9bd47053bc..2994c2467219 100644
@@ -62,6 +62,8 @@ COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t *mutex));
 
 COBALT_DECL(int, pthread_mutex_lock(pthread_mutex_t *mutex));
 
+COBALT_DECL(int, pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex));
+
 COBALT_DECL(int, pthread_mutex_timedlock(pthread_mutex_t *mutex,
 					 const struct timespec *to));
 
lib/alchemy/mutex.c +0 -0
index f8933858647a..bb97395142aa 100644

lib/alchemy/mutex.c int rt_mutex_acquire_timed(RT_MUTEX *mutex, · line 330

Quoted 5 lines
lib/alchemy/mutex.c +1 -1
@@ -327,7 +327,7 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 
 	/* Slow path. */
 	if (abs_timeout == NULL) {
-		ret = -__RT(pthread_mutex_lock(&mcb->lock));
+		ret = -__RT(pthread_mutex_lock_eintr_np(&mcb->lock));

This won't build for mercury, will it?

Jan

Quoted 42 lines
lib/alchemy/mutex.c +0 -0
 		goto done;
 	}
 
lib/cobalt/mutex.c +12 -2
index 73e45a1c4396..2ef02a175c13 100644
@@ -314,7 +314,7 @@ COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex))
  *
  * @apitags{xthread-only, switch-primary}
  */
-COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+static int __pthread_mutex_lock(pthread_mutex_t *mutex, bool want_eintr)
 {
 	struct cobalt_mutex_shadow *_mutex =
 		&((union cobalt_mutex_union *)mutex)->shadow_mutex;
@@ -373,7 +373,7 @@ slow_path:
 
 	do
 		ret = XENOMAI_SYSCALL1(sc_cobalt_mutex_lock, _mutex);
-	while (ret == -EINTR);
+	while (ret == -EINTR && !want_eintr);
 
 	if (ret == 0)
 		_mutex->lockcnt = 1;
@@ -392,6 +392,16 @@ protect:
 	goto fast_path;
 }
 
+COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, false);
+}
+
+COBALT_IMPL(int, pthread_mutex_lock_eintr_np, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, true);
+}
+
 /**
  * Attempt, during a bounded time, to lock a mutex.
  *
Signature · 2 lines
Siemens AG, Technology
Competence Center Embedded Linux
#8 lore raw

----- Ursprüngliche Mail -----

Quoted 43 lines

Von: "Jan Kiszka" <[email protected]>
An: "richard" <[email protected]>, "xenomai" <[email protected]>
Gesendet: Montag, 4. April 2022 13:21:45
Betreff: Re: [PATCH] Alchemy: Fix rt_task_unblock() for RT_MUTEX

Quoted 37 lines · Richard Weinberger via Xenomai

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() shall not return EINTR, this requirement breaks rt_task_unblock() if a RT_TASK blocks on a RT_MUTEX.

To restore the functionality provide a new function, pthread_mutex_lock_eintr_np().
It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  2 +-
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 15 insertions(+), 3 deletions(-)
include/cobalt/pthread.h +2 -0
index 3e9bd47053bc..2994c2467219 100644
@@ -62,6 +62,8 @@ COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t
 *mutex));
 
 COBALT_DECL(int, pthread_mutex_lock(pthread_mutex_t *mutex));
 
+COBALT_DECL(int, pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex));
+
 COBALT_DECL(int, pthread_mutex_timedlock(pthread_mutex_t *mutex,
 					 const struct timespec *to));
lib/alchemy/mutex.c +0 -0
index f8933858647a..bb97395142aa 100644
@@ -327,7 +327,7 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 
Quoted 6 lines

lib/alchemy/mutex.c int rt_mutex_acquire_timed(RT_MUTEX *mutex, · line 330

Quoted 4 lines
lib/alchemy/mutex.c +1 -1
 	/* Slow path. */
 	if (abs_timeout == NULL) {
-		ret = -__RT(pthread_mutex_lock(&mcb->lock));
+		ret = -__RT(pthread_mutex_lock_eintr_np(&mcb->lock));

This won't build for mercury, will it?

Uff yes. Just tried building with --with-core=mercury and it failed. ;-\

So, when mercury is enabled, alchemy will use NPTL. This means rt_task_unblock() cannot work on mercury because pthread_mutex_lock() will not return EINTR either.

Fixing the build for mercury should be easy by adding an ifdef CONFIG_XENO_MERCURY. But I'm not sure how to fix unblocking for the mercury case.

Thanks,
//richard

#9 lore raw
Quoted 51 lines · Richard Weinberger

----- Ursprüngliche Mail -----

Quoted 43 lines

Von: "Jan Kiszka" <[email protected]>
An: "richard" <[email protected]>, "xenomai" <[email protected]>
Gesendet: Montag, 4. April 2022 13:21:45
Betreff: Re: [PATCH] Alchemy: Fix rt_task_unblock() for RT_MUTEX

Quoted 37 lines · Richard Weinberger via Xenomai

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() shall not return EINTR, this requirement breaks rt_task_unblock() if a RT_TASK blocks on a RT_MUTEX.

To restore the functionality provide a new function, pthread_mutex_lock_eintr_np().
It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  2 +-
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 15 insertions(+), 3 deletions(-)
include/cobalt/pthread.h +2 -0
index 3e9bd47053bc..2994c2467219 100644
@@ -62,6 +62,8 @@ COBALT_DECL(int, pthread_mutex_destroy(pthread_mutex_t
 *mutex));
 
 COBALT_DECL(int, pthread_mutex_lock(pthread_mutex_t *mutex));
 
+COBALT_DECL(int, pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex));
+
 COBALT_DECL(int, pthread_mutex_timedlock(pthread_mutex_t *mutex,
 					 const struct timespec *to));
lib/alchemy/mutex.c +0 -0
index f8933858647a..bb97395142aa 100644
@@ -327,7 +327,7 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 
Quoted 6 lines

lib/alchemy/mutex.c int rt_mutex_acquire_timed(RT_MUTEX *mutex, · line 330

Quoted 4 lines
lib/alchemy/mutex.c +1 -1
 	/* Slow path. */
 	if (abs_timeout == NULL) {
-		ret = -__RT(pthread_mutex_lock(&mcb->lock));
+		ret = -__RT(pthread_mutex_lock_eintr_np(&mcb->lock));

This won't build for mercury, will it?

Quoted 8 lines

Uff yes. Just tried building with --with-core=mercury and it failed. ;-\

So, when mercury is enabled, alchemy will use NPTL. This means rt_task_unblock() cannot work on mercury because pthread_mutex_lock() will not return EINTR either.

Fixing the build for mercury should be easy by adding an ifdef CONFIG_XENO_MERCURY. But I'm not sure how to fix unblocking for the mercury case.

Either by ignoring that case on mercury (it has not predecessor in Xenomai 2, and it can't be fixed via posix means) or by redefining the behavior for everyone, i.e. by exempting rt_mutex_acquire* from rt_task_unblock.

Jan

Signature · 2 lines
Siemens AG, Technology
Competence Center Embedded Linux
#10 lore raw

----- Ursprüngliche Mail -----

Quoted 7 lines

Von: "Jan Kiszka" <[email protected]>

Quoted 6 lines

Uff yes. Just tried building with --with-core=mercury and it failed. ;-\

So, when mercury is enabled, alchemy will use NPTL. This means rt_task_unblock() cannot work on mercury because pthread_mutex_lock() will not return EINTR either.

Quoted 8 lines
Quoted 3 lines

Fixing the build for mercury should be easy by adding an ifdef CONFIG_XENO_MERCURY.
But I'm not sure how to fix unblocking for the mercury case.

Either by ignoring that case on mercury (it has not predecessor in Xenomai 2, and it can't be fixed via posix means) or by redefining the behavior for everyone, i.e. by exempting rt_mutex_acquire* from rt_task_unblock.

I'd strongly vote for option #1. The application I work an cleans up deadlocks with rt_task_unblock().
This seems to work quite well on Xenomai 2.

Thanks,
//richard

#11 lore raw

PATCH v2 1/1 Alchemy: Fix rt_task_unblock() for RT_MUTEX

In reply to Richard Weinberger

From: Richard Weinberger <[email protected]>

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() shall not return EINTR, this requirement breaks rt_task_unblock() if a RT_TASK blocks on a RT_MUTEX.

To restore the functionality provide a new flag to __pthread_mutex_lock(). It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  4 ++++
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
include/cobalt/pthread.h +2 -0
index 0451f6704..2d645c5d9 100644
@@ -174,6 +174,8 @@ int pthread_attr_getpersonality_ex(const pthread_attr_ex_t *attr_ex,
 int pthread_attr_setpersonality_ex(pthread_attr_ex_t *attr_ex,
 				   int personality);
 
+int pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex);
+
 int pthread_mutex_gc_np(void);
 #ifdef __cplusplus
 }
lib/alchemy/mutex.c +4 -0
index 3b27a04b5..45db692a7 100644
@@ -399,7 +399,11 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 
 	/* Slow path. */
 	if (abs_timeout == NULL) {
+#ifdef CONFIG_XENO_MERCURY
 		ret = -__RT(pthread_mutex_lock(&mcb->lock));
+#else
+		ret = -pthread_mutex_lock_eintr_np(&mcb->lock);
+#endif
 		goto done;
 	}
 
lib/cobalt/mutex.c +12 -2
index 634be35d3..28d2557eb 100644
@@ -411,7 +411,7 @@ COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex))
  *
  * @apitags{xthread-only, switch-primary}
  */
-COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+static int __pthread_mutex_lock(pthread_mutex_t *mutex, bool want_eintr)
 {
 	struct cobalt_mutex_shadow *_mutex =
 		&((union cobalt_mutex_union *)mutex)->shadow_mutex;
@@ -470,7 +470,7 @@ slow_path:
 
 	do
 		ret = XENOMAI_SYSCALL1(sc_cobalt_mutex_lock, _mutex);
-	while (ret == -EINTR);
+	while (ret == -EINTR && !want_eintr);
 
 	if (ret == 0)
 		_mutex->lockcnt = 1;
@@ -489,6 +489,16 @@ protect:
 	goto fast_path;
 }
 
+COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, false);
+}
+
+int pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex)
+{
+	return __pthread_mutex_lock(mutex, true);
+}
+
 /**
  * Attempt, during a bounded time, to lock a mutex.
  *
Signature · 1 line
2.26.2
#12 lore raw
Quoted 53 lines · Aaron Marcher

From: Richard Weinberger <[email protected]>

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() shall not return EINTR, this requirement breaks rt_task_unblock() if a RT_TASK blocks on a RT_MUTEX.

To restore the functionality provide a new flag to __pthread_mutex_lock(). It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  4 ++++
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
include/cobalt/pthread.h +2 -0
index 0451f6704..2d645c5d9 100644
@@ -174,6 +174,8 @@ int pthread_attr_getpersonality_ex(const pthread_attr_ex_t *attr_ex,
 int pthread_attr_setpersonality_ex(pthread_attr_ex_t *attr_ex,
 				   int personality);
 
+int pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex);
+
 int pthread_mutex_gc_np(void);
 #ifdef __cplusplus
 }
lib/alchemy/mutex.c +4 -0
index 3b27a04b5..45db692a7 100644
@@ -399,7 +399,11 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 
 	/* Slow path. */
 	if (abs_timeout == NULL) {
+#ifdef CONFIG_XENO_MERCURY
 		ret = -__RT(pthread_mutex_lock(&mcb->lock));
+#else
+		ret = -pthread_mutex_lock_eintr_np(&mcb->lock);
+#endif
 		goto done;
 	}
 
lib/cobalt/mutex.c +0 -0
index 634be35d3..28d2557eb 100644

lib/cobalt/mutex.c COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex)) · line 414

Quoted 5 lines
lib/cobalt/mutex.c +1 -1
@@ -411,7 +411,7 @@ COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex))
  *
  * @apitags{xthread-only, switch-primary}
  */
-COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+static int __pthread_mutex_lock(pthread_mutex_t *mutex, bool want_eintr)

Let's call it 'interruptible'.

Quoted 13 lines
lib/cobalt/mutex.c +1 -1
 {
 	struct cobalt_mutex_shadow *_mutex =
 		&((union cobalt_mutex_union *)mutex)->shadow_mutex;
@@ -470,7 +470,7 @@ slow_path:
 
 	do
 		ret = XENOMAI_SYSCALL1(sc_cobalt_mutex_lock, _mutex);
-	while (ret == -EINTR);
+	while (ret == -EINTR && !want_eintr);
 
 	if (ret == 0)
 		_mutex->lockcnt = 1;

lib/cobalt/mutex.c protect: · line 497

Quoted 9 lines
lib/cobalt/mutex.c +6 -0
@@ -489,6 +489,16 @@ protect:
 	goto fast_path;
 }
 
+COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, false);
+}
+
+int pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex)

Also here: pthread_mutex_lock_interruptible_np

lib/cobalt/mutex.c protect: · line 504

Quoted 7 lines
lib/cobalt/mutex.c +4 -0
+{
+	return __pthread_mutex_lock(mutex, true);
+}
+
 /**
  * Attempt, during a bounded time, to lock a mutex.
  *

Logic looks good to me.

Jan

Signature · 2 lines
Siemens AG, Technology
Competence Center Embedded Linux
#13 lore raw
Quoted 87 lines · Jan Kiszka
Quoted 53 lines · Aaron Marcher

From: Richard Weinberger <[email protected]>

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() shall not return EINTR, this requirement breaks rt_task_unblock() if a RT_TASK blocks on a RT_MUTEX.

To restore the functionality provide a new flag to __pthread_mutex_lock(). It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  4 ++++
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
include/cobalt/pthread.h +2 -0
index 0451f6704..2d645c5d9 100644
@@ -174,6 +174,8 @@ int pthread_attr_getpersonality_ex(const pthread_attr_ex_t *attr_ex,
 int pthread_attr_setpersonality_ex(pthread_attr_ex_t *attr_ex,
 				   int personality);
 
+int pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex);
+
 int pthread_mutex_gc_np(void);
 #ifdef __cplusplus
 }
lib/alchemy/mutex.c +4 -0
index 3b27a04b5..45db692a7 100644
@@ -399,7 +399,11 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 
 	/* Slow path. */
 	if (abs_timeout == NULL) {
+#ifdef CONFIG_XENO_MERCURY
 		ret = -__RT(pthread_mutex_lock(&mcb->lock));
+#else
+		ret = -pthread_mutex_lock_eintr_np(&mcb->lock);
+#endif
 		goto done;
 	}
 
lib/cobalt/mutex.c +0 -0
index 634be35d3..28d2557eb 100644

lib/cobalt/mutex.c COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex)) · line 414

Quoted 5 lines
lib/cobalt/mutex.c +1 -1
@@ -411,7 +411,7 @@ COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex))
  *
  * @apitags{xthread-only, switch-primary}
  */
-COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+static int __pthread_mutex_lock(pthread_mutex_t *mutex, bool want_eintr)

Let's call it 'interruptible'.

Quoted 13 lines
lib/cobalt/mutex.c +1 -1
 {
 	struct cobalt_mutex_shadow *_mutex =
 		&((union cobalt_mutex_union *)mutex)->shadow_mutex;
@@ -470,7 +470,7 @@ slow_path:
 
 	do
 		ret = XENOMAI_SYSCALL1(sc_cobalt_mutex_lock, _mutex);
-	while (ret == -EINTR);
+	while (ret == -EINTR && !want_eintr);
 
 	if (ret == 0)
 		_mutex->lockcnt = 1;

lib/cobalt/mutex.c protect: · line 497

Quoted 9 lines
lib/cobalt/mutex.c +6 -0
@@ -489,6 +489,16 @@ protect:
 	goto fast_path;
 }
 
+COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, false);
+}
+
+int pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex)

Also here: pthread_mutex_lock_interruptible_np

Quoted 9 lines

lib/cobalt/mutex.c protect: · line 504

Quoted 7 lines
lib/cobalt/mutex.c +4 -0
+{
+	return __pthread_mutex_lock(mutex, true);
+}
+
 /**
  * Attempt, during a bounded time, to lock a mutex.
  *

Logic looks good to me.

LGTM as well, but: What about testing? Shouldn't we add a test case for that, making sure we do not break it again?

Florian

Quoted 3 lines

Jan

#14 lore raw
Quoted 98 lines · Florian Bezdeka
Quoted 87 lines · Jan Kiszka
Quoted 53 lines · Aaron Marcher

From: Richard Weinberger <[email protected]>

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() shall not return EINTR, this requirement breaks rt_task_unblock() if a RT_TASK blocks on a RT_MUTEX.

To restore the functionality provide a new flag to __pthread_mutex_lock(). It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  2 ++
 lib/alchemy/mutex.c      |  4 ++++
 lib/cobalt/mutex.c       | 14 ++++++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
include/cobalt/pthread.h +2 -0
index 0451f6704..2d645c5d9 100644
@@ -174,6 +174,8 @@ int pthread_attr_getpersonality_ex(const pthread_attr_ex_t *attr_ex,
 int pthread_attr_setpersonality_ex(pthread_attr_ex_t *attr_ex,
 				   int personality);
 
+int pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex);
+
 int pthread_mutex_gc_np(void);
 #ifdef __cplusplus
 }
lib/alchemy/mutex.c +4 -0
index 3b27a04b5..45db692a7 100644
@@ -399,7 +399,11 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 
 	/* Slow path. */
 	if (abs_timeout == NULL) {
+#ifdef CONFIG_XENO_MERCURY
 		ret = -__RT(pthread_mutex_lock(&mcb->lock));
+#else
+		ret = -pthread_mutex_lock_eintr_np(&mcb->lock);
+#endif
 		goto done;
 	}
 
lib/cobalt/mutex.c +0 -0
index 634be35d3..28d2557eb 100644

lib/cobalt/mutex.c COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex)) · line 414

Quoted 5 lines
lib/cobalt/mutex.c +1 -1
@@ -411,7 +411,7 @@ COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex))
  *
  * @apitags{xthread-only, switch-primary}
  */
-COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+static int __pthread_mutex_lock(pthread_mutex_t *mutex, bool want_eintr)

Let's call it 'interruptible'.

Quoted 13 lines
lib/cobalt/mutex.c +1 -1
 {
 	struct cobalt_mutex_shadow *_mutex =
 		&((union cobalt_mutex_union *)mutex)->shadow_mutex;
@@ -470,7 +470,7 @@ slow_path:
 
 	do
 		ret = XENOMAI_SYSCALL1(sc_cobalt_mutex_lock, _mutex);
-	while (ret == -EINTR);
+	while (ret == -EINTR && !want_eintr);
 
 	if (ret == 0)
 		_mutex->lockcnt = 1;

lib/cobalt/mutex.c protect: · line 497

Quoted 9 lines
lib/cobalt/mutex.c +6 -0
@@ -489,6 +489,16 @@ protect:
 	goto fast_path;
 }
 
+COBALT_IMPL(int, pthread_mutex_lock, (pthread_mutex_t *mutex))
+{
+	return __pthread_mutex_lock(mutex, false);
+}
+
+int pthread_mutex_lock_eintr_np(pthread_mutex_t *mutex)

Also here: pthread_mutex_lock_interruptible_np

Quoted 9 lines

lib/cobalt/mutex.c protect: · line 504

Quoted 7 lines
lib/cobalt/mutex.c +4 -0
+{
+	return __pthread_mutex_lock(mutex, true);
+}
+
 /**
  * Attempt, during a bounded time, to lock a mutex.
  *

Logic looks good to me.

Quoted 3 lines

LGTM as well, but: What about testing? Shouldn't we add a test case for that, making sure we do not break it again?

Ack -> lib/alchemy/testsuite/, likely.

Jan

Signature · 2 lines
Siemens AG, Technology
Competence Center Embedded Linux
#15 lore raw

----- Ursprüngliche Mail -----

Quoted 6 lines

Von: "Jan Kiszka" <[email protected]>

Quoted 3 lines

LGTM as well, but: What about testing? Shouldn't we add a test case for that, making sure we do not break it again?

Ack -> lib/alchemy/testsuite/, likely.

Time to exhume my test suite cleanup patches. :-D

Thanks,
//richard