Skip to contentSkip to conversation

Alchemy: Fix rt_task_unblock() for RT_MUTEX

PATCH Superseded Richard Weinberger 2 participants 3 messages Mar 26, 2024 – Apr 8, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 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() and pthread_mutex_timedlocks 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() and __pthread_mutex_timedlock()
It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  5 +++++
 lib/alchemy/mutex.c      |  8 ++++++++
 lib/cobalt/mutex.c       | 31 ++++++++++++++++++++++++++-----
 3 files changed, 39 insertions(+), 5 deletions(-)
include/cobalt/pthread.h +5 -0
index 1a8991f5c..4c0680053 100644
@@ -170,6 +170,11 @@ 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_timedlock_eintr_np(pthread_mutex_t *mutex, const struct timespec *to);
+
 #ifdef __cplusplus
 }
 #endif
lib/alchemy/mutex.c +8 -0
index 9681e124e..27b936ac5 100644
@@ -327,7 +327,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;
 	}
 
@@ -338,7 +342,11 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 	 * the user timeout into something POSIX understands.
 	 */
 	clockobj_convert_clocks(&alchemy_clock, abs_timeout, CLOCK_REALTIME, &ts);
+#ifdef CONFIG_XENO_MERCURY
 	ret = -__RT(pthread_mutex_timedlock(&mcb->lock, &ts));
+#else
+	ret = -pthread_mutex_timedlock_eintr_np(&mcb->lock, &ts);
+#endif
 done:
 	switch (ret) {
 	case -ENOTRECOVERABLE:
lib/cobalt/mutex.c +26 -5
index c77d10684..3cd975a3f 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);
+}
+
+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.
  *
@@ -423,8 +433,8 @@ protect:
  *
  * @apitags{xthread-only, switch-primary}
  */
-COBALT_IMPL_TIME64(int, pthread_mutex_timedlock, __pthread_mutex_timedlock64,
-		   (pthread_mutex_t *mutex, const struct timespec *to))
+static int __pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *to,
+				     bool want_eintr)
 {
 	struct cobalt_mutex_shadow *_mutex =
 		&((union cobalt_mutex_union *)mutex)->shadow_mutex;
@@ -484,7 +494,7 @@ slow_path:
 #else
 		ret = XENOMAI_SYSCALL2(sc_cobalt_mutex_timedlock, _mutex, to);
 #endif
-	} while (ret == -EINTR);
+	} while (ret == -EINTR && !want_eintr);
 
 	if (ret == 0)
 		_mutex->lockcnt = 1;
@@ -502,6 +512,17 @@ protect:
 	goto fast_path;
 }
 
+COBALT_IMPL_TIME64(int, pthread_mutex_timedlock, __pthread_mutex_timedlock64,
+		   (pthread_mutex_t *mutex, const struct timespec *to))
+{
+	return __pthread_mutex_timedlock(mutex, to, false);
+}
+
+int pthread_mutex_timedlock_eintr_np(pthread_mutex_t *mutex, const struct timespec *to)
+{
+	return __pthread_mutex_timedlock(mutex, to, true);
+}
+
 /**
  * Attempt to lock a mutex.
  *
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 alchemytests: Add test case for RT_MUTEX and -EINTR

Make sure rt_task_unblock() can unblock a mutex waiter.

---
 testsuite/smokey/alchemytests/Makefile.am |   5 +
 testsuite/smokey/alchemytests/mutex-2.c   | 111 ++++++++++++++++++++++
 2 files changed, 116 insertions(+)
 create mode 100644 testsuite/smokey/alchemytests/mutex-2.c
testsuite/smokey/alchemytests/Makefile.am +5 -0
index 3525dfa09..eba82f8ec 100644
@@ -20,6 +20,7 @@ test_PROGRAMS =				\
 		alchemytests_mq2	\
 		alchemytests_mq3	\
 		alchemytests_mutex1	\
+		alchemytests_mutex2	\
 		alchemytests_pipe1	\
 		alchemytests_sem1	\
 		alchemytests_sem2	\
@@ -69,6 +70,10 @@ alchemytests_mutex1_SOURCES = mutex-1.c
 alchemytests_mutex1_CPPFLAGS = $(alchemycppflags)
 alchemytests_mutex1_LDADD = $(alchemyldadd)
 alchemytests_mutex1_LDFLAGS = @XENO_AUTOINIT_LDFLAGS@
+alchemytests_mutex2_SOURCES = mutex-2.c
+alchemytests_mutex2_CPPFLAGS = $(alchemycppflags)
+alchemytests_mutex2_LDADD = $(alchemyldadd)
+alchemytests_mutex2_LDFLAGS = @XENO_AUTOINIT_LDFLAGS@
 alchemytests_pipe1_SOURCES = pipe-1.c
 alchemytests_pipe1_CPPFLAGS = $(alchemycppflags)
 alchemytests_pipe1_LDADD = $(alchemyldadd)
testsuite/smokey/alchemytests/mutex-2.c new file +111 -0

111 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw
In reply to Richard Weinberger
Quoted 22 lines · Richard Weinberger

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() and pthread_mutex_timedlocks 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() and __pthread_mutex_timedlock()
It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  5 +++++
 lib/alchemy/mutex.c      |  8 ++++++++
 lib/cobalt/mutex.c       | 31 ++++++++++++++++++++++++++-----
 3 files changed, 39 insertions(+), 5 deletions(-)
include/cobalt/pthread.h +0 -0
index 1a8991f5c..4c0680053 100644

include/cobalt/pthread.h int pthread_attr_getpersonality_ex(const pthread_attr_ex_t *attr_ex, · line 177

Quoted 8 lines
include/cobalt/pthread.h +5 -0
@@ -170,6 +170,11 @@ 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_timedlock_eintr_np(pthread_mutex_t *mutex, const struct timespec *to);
+

It's getting even longer, but pthread_[timed]mutex_lock_interruptible_np seems more readable to me.

Quoted 54 lines
include/cobalt/pthread.h +0 -0
 #ifdef __cplusplus
 }
 #endif
lib/alchemy/mutex.c +8 -0
index 9681e124e..27b936ac5 100644
@@ -327,7 +327,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;
 	}
 
@@ -338,7 +342,11 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 	 * the user timeout into something POSIX understands.
 	 */
 	clockobj_convert_clocks(&alchemy_clock, abs_timeout, CLOCK_REALTIME, &ts);
+#ifdef CONFIG_XENO_MERCURY
 	ret = -__RT(pthread_mutex_timedlock(&mcb->lock, &ts));
+#else
+	ret = -pthread_mutex_timedlock_eintr_np(&mcb->lock, &ts);
+#endif
 done:
 	switch (ret) {
 	case -ENOTRECOVERABLE:
lib/cobalt/mutex.c +2 -2
index c77d10684..3cd975a3f 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;

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

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

Can't we loop over EINTR here? Then we don't need that extra parameter for the inner mutex_lock. Requires a careful check that the prologue can actually be re-executed without side-effects.

Quoted 37 lines
lib/cobalt/mutex.c +13 -3
+
+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.
  *
@@ -423,8 +433,8 @@ protect:
  *
  * @apitags{xthread-only, switch-primary}
  */
-COBALT_IMPL_TIME64(int, pthread_mutex_timedlock, __pthread_mutex_timedlock64,
-		   (pthread_mutex_t *mutex, const struct timespec *to))
+static int __pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *to,
+				     bool want_eintr)
 {
 	struct cobalt_mutex_shadow *_mutex =
 		&((union cobalt_mutex_union *)mutex)->shadow_mutex;
@@ -484,7 +494,7 @@ slow_path:
 #else
 		ret = XENOMAI_SYSCALL2(sc_cobalt_mutex_timedlock, _mutex, to);
 #endif
-	} while (ret == -EINTR);
+	} while (ret == -EINTR && !want_eintr);
 
 	if (ret == 0)
 		_mutex->lockcnt = 1;
@@ -502,6 +512,17 @@ protect:
 	goto fast_path;
 }
 
+COBALT_IMPL_TIME64(int, pthread_mutex_timedlock, __pthread_mutex_timedlock64,
+		   (pthread_mutex_t *mutex, const struct timespec *to))
+{
+	return __pthread_mutex_timedlock(mutex, to, false);

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

Quoted 10 lines
lib/cobalt/mutex.c +7 -0
+}
+
+int pthread_mutex_timedlock_eintr_np(pthread_mutex_t *mutex, const struct timespec *to)
+{
+	return __pthread_mutex_timedlock(mutex, to, true);
+}
+
 /**
  * Attempt to lock a mutex.
  *

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center