[PATCH v2 01/20] lib/cobalt: Introduce cond.h
Florian Bezdeka <[email protected]>
| Newsgroups | dev.linux.lists.xenomai |
|---|---|
| Message-ID | <20260302-wip-flo-fix-time64-32bit-native-v2-1-7caefce29bcd@siemens.com> |
Some code will be re-used later by the time64_t compile unit. Move that code into cond.h, so code duplication is avoided. No modifications to the code itself. Signed-off-by: Florian Bezdeka <[email protected]> --- lib/cobalt/Makefile.am | 1 + lib/cobalt/cond.c | 67 ++------------------------------------- lib/cobalt/cond.h | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 65 deletions(-) diff --git a/lib/cobalt/Makefile.am b/lib/cobalt/Makefile.am index ed7e503899f4852388fb3bf204bfe91936009d74..b1098413fbf3a8f6a5f0a85d6aceb9916b14149a 100644 --- a/lib/cobalt/Makefile.am +++ b/lib/cobalt/Makefile.am @@ -1,6 +1,7 @@ pkgconfigdir = $(libdir)/pkgconfig noinst_HEADERS = \ + cond.h \ current.h \ umm.h \ internal.h diff --git a/lib/cobalt/cond.c b/lib/cobalt/cond.c index 2d11f768edc0fe4e89944a96333d2dd76d174172..7be7fca01f53930f8504945206b2ee6ba6be1ba5 100644 --- a/lib/cobalt/cond.c +++ b/lib/cobalt/cond.c @@ -20,6 +20,8 @@ #include <string.h> #include <pthread.h> #include <asm/xenomai/syscall.h> + +#include "cond.h" #include "current.h" #include "internal.h" @@ -147,32 +149,6 @@ COBALT_IMPL(int, pthread_cond_init, (pthread_cond_t *cond, return 0; } -static int __attribute__((cold)) - cobalt_cond_autoinit_type(const pthread_cond_t *cond) -{ - static const pthread_cond_t cond_initializer = - PTHREAD_COND_INITIALIZER; - - return memcmp(cond, &cond_initializer, sizeof(cond_initializer)) == 0 ? - 0 : -1; -} - -static int __attribute__((cold)) - cobalt_cond_doautoinit(union cobalt_cond_union *ucond) -{ - if (cobalt_cond_autoinit_type(&ucond->native_cond) < 0) - return EINVAL; - - return __COBALT(pthread_cond_init(&ucond->native_cond, NULL)); -} - -static inline int cobalt_cond_autoinit(union cobalt_cond_union *ucond) -{ - if (ucond->shadow_cond.magic != COBALT_COND_MAGIC) - return cobalt_cond_doautoinit(ucond); - return 0; -} - /** * @fn int pthread_cond_destroy(pthread_cond_t *cond) * @brief Destroy a condition variable @@ -208,45 +184,6 @@ COBALT_IMPL(int, pthread_cond_destroy, (pthread_cond_t *cond)) return -XENOMAI_SYSCALL1( sc_cobalt_cond_destroy, _cond); } -struct cobalt_cond_cleanup_t { - struct cobalt_cond_shadow *cond; - struct cobalt_mutex_shadow *mutex; - unsigned count; - int err; -}; - -static void __pthread_cond_cleanup(void *data) -{ - struct cobalt_cond_cleanup_t *c = (struct cobalt_cond_cleanup_t *)data; - xnhandle_t cur = cobalt_get_current(); - int err; - - /* if we still own the mutex, cond_wait_prologue wasn't called yet */ - if (xnsynch_fast_owner_check(mutex_get_ownerp(c->mutex), cur) == 0) - return; - - do { - err = XENOMAI_SYSCALL2(sc_cobalt_cond_wait_epilogue, - c->cond, c->mutex); - } while (err == -EINTR); - - c->mutex->lockcnt = c->count; -} - -static inline int do_sc_cond_wait_prologue(struct cobalt_cond_shadow *cnd, - struct cobalt_mutex_shadow *mx, - int *err, int timed, - const struct timespec *abstime) -{ -#ifdef __USE_TIME_BITS64 - long sc_nr = sc_cobalt_cond_wait_prologue64; -#else - long sc_nr = sc_cobalt_cond_wait_prologue; -#endif - - return XENOMAI_SYSCALL5(sc_nr, cnd, mx, err, timed, abstime); -} - /** * Wait on a condition variable. * diff --git a/lib/cobalt/cond.h b/lib/cobalt/cond.h new file mode 100644 index 0000000000000000000000000000000000000000..88b252cb72590b514f95a31c8d8849b3e4f3856f --- /dev/null +++ b/lib/cobalt/cond.h @@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: LGPL-2.0-or-later */ + +/* + * Copyright (C) 2026 Florian Bezdeka <[email protected]>. + * Copyright (C) 2005 Philippe Gerum <[email protected]>. + */ + +#ifndef _LIB_COBALT_COND_H +#define _LIB_COBALT_COND_H + +#include "current.h" +#include "internal.h" + +#include <asm/xenomai/syscall.h> +#include <cobalt/uapi/cond.h> + +#include <string.h> +#include <pthread.h> + +struct cobalt_cond_cleanup_t { + struct cobalt_cond_shadow *cond; + struct cobalt_mutex_shadow *mutex; + unsigned count; + int err; +}; + +static int __attribute__((cold)) +cobalt_cond_autoinit_type(const pthread_cond_t *cond) +{ + static const pthread_cond_t cond_initializer = PTHREAD_COND_INITIALIZER; + + return memcmp(cond, &cond_initializer, sizeof(cond_initializer)) == 0 ? + 0 : + -1; +} + +static int __attribute__((cold)) +cobalt_cond_doautoinit(union cobalt_cond_union *ucond) +{ + if (cobalt_cond_autoinit_type(&ucond->native_cond) < 0) + return EINVAL; + + return __COBALT(pthread_cond_init(&ucond->native_cond, NULL)); +} + +static inline int cobalt_cond_autoinit(union cobalt_cond_union *ucond) +{ + if (ucond->shadow_cond.magic != COBALT_COND_MAGIC) + return cobalt_cond_doautoinit(ucond); + return 0; +} + +static void __pthread_cond_cleanup(void *data) +{ + struct cobalt_cond_cleanup_t *c = (struct cobalt_cond_cleanup_t *)data; + xnhandle_t cur = cobalt_get_current(); + int err; + + /* if we still own the mutex, cond_wait_prologue wasn't called yet */ + if (xnsynch_fast_owner_check(mutex_get_ownerp(c->mutex), cur) == 0) + return; + + do { + err = XENOMAI_SYSCALL2(sc_cobalt_cond_wait_epilogue, c->cond, + c->mutex); + } while (err == -EINTR); + + c->mutex->lockcnt = c->count; +} + +static inline int do_sc_cond_wait_prologue(struct cobalt_cond_shadow *cnd, + struct cobalt_mutex_shadow *mx, + int *err, int timed, + const struct timespec *abstime) +{ +#ifdef __USE_TIME_BITS64 + long sc_nr = sc_cobalt_cond_wait_prologue64; +#else + long sc_nr = sc_cobalt_cond_wait_prologue; +#endif + + return XENOMAI_SYSCALL5(sc_nr, cnd, mx, err, timed, abstime); +} + +#endif //_LIB_COBALT_COND_H -- 2.53.0