Support for select v2.
cover Support for select v2.
Hi,
after some tests, bug fixes and optimizations, here comes a second version of the patch-set adding select support to xenomai posix skin.
Regards.
Signature · 1 line
Gilles Chanteperdrix.
PATCH 1/5 select-like services support to xenomai core.
This patch adds select-like services support to xenomai core.
Changes:
- the xnselect function now ignores null fd_sets, which makes it faster in the common case when several fd_sets are null;
- the timeout is now passed to xnselect as an (xnticks_t, xntmode_t) couple, which avoids xnselect to suspend forever the caller when a computed timeout happens to be XN_INFINITE.
Stats:
include/nucleus/select.h | 111 ++++++++++++ ksrc/nucleus/Makefile | 4 ksrc/nucleus/select.c | 412 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 525 insertions(+), 2 deletions(-)
Signature · 1 line
Gilles Chanteperdrix.
nucleus/select.h +111 -0
@@ -0,0 +1,111 @@ +/*!\file select.h +* \brief file descriptors events multiplexing header. +* \author Gilles Chanteperdrix +* +* Copyright (C) 2008 Efixo <[email protected]> +* +* Xenomai is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation; either version 2 of the License, +* or (at your option) any later version. +* +* Xenomai is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with Xenomai; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +* 02111-1307, USA. +* +* \ingroup select +*/ + +#ifndef XNSELECT_H +#define XNSELECT_H + +/*! \addtogroup select +*@{*/ + +#include <nucleus/thread.h> + +#define XNSELECT_READ 0 +#define XNSELECT_WRITE 1 +#define XNSELECT_EXCEPT 2 +#define XNSELECT_MAX_TYPES 3 + +struct xnselector { +xnsynch_t synchbase; +struct fds { +fd_set expected; +fd_set pending; + } fds [XNSELECT_MAX_TYPES ]; +xnqueue_t bindings;/* only used by xnselector_destroy */ +}; + +struct xnselect { +xnqueue_t bindings; +}; + +struct xnselect_binding { +struct xnselector *selector; +struct xnselect *fd; +unsigned type; +unsigned bit_index; +xnholder_t link;/* link in selected fds list. */ +xnholder_t slink;/* link in selector list */ +}; + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +void xnselect_init (struct xnselect *select_block); + +int xnselect_bind (struct xnselect *select_block, +struct xnselect_binding *binding, +struct xnselector *selector, +unsigned type, +unsigned bit_index, +unsigned state); + +int __xnselect_signal (struct xnselect *select_block,unsigned state); + +/** +* Signal a file descriptor state change. +* +* @param select_block pointer to an @a xnselect structure representing the file +* descriptor whose state changed; +* @param state new value of the state. +* +* @retval 1 if rescheduling is needed; +* @retval 0 otherwise. +*/ +static inline int +xnselect_signal (struct xnselect *select_block,unsigned state) +{ +if (!emptyq_p (&select_block->bindings)) +return __xnselect_signal (select_block, state); +return 0 ; +} + +void xnselect_destroy (struct xnselect *select_block); + +int xnselector_init (struct xnselector *selector); + +int xnselect (struct xnselector *selector, +fd_set *out_fds[XNSELECT_MAX_TYPES ], +fd_set *in_fds[XNSELECT_MAX_TYPES ], +int nfds, +xnticks_t timeout,xntmode_t timeout_mode); + +void xnselector_destroy (struct xnselector *selector); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +/*@}*/ + +#endif /* XNSELECT_H */
nucleus/select.c +412 -0
412 lines not on this page · raw message
nucleus/Makefile +2 -2
@@ -4,7 +4,7 @@ ifeq ($(PATCHLEVEL),6) obj-$(CONFIG_XENO_OPT_NUCLEUS) += xeno_nucleus.o -xeno_nucleus-y := heap.o intr.o module.o pod.o synch.o thread.o timebase.o timer.o +xeno_nucleus-y := heap.o intr.o module.o pod.o synch.o thread.o timebase.o timer.o select.o xeno_nucleus-$(CONFIG_XENO_OPT_PERVASIVE) += shadow.o
@@ -26,7 +26,7 @@ obj-$(CONFIG_XENO_OPT_NUCLEUS) := xeno_n list-multi := xeno_nucleus.o -xeno_nucleus-objs := heap.o intr.o module.o pod.o synch.o thread.o timebase.o timer.o +xeno_nucleus-objs := heap.o intr.o module.o pod.o synch.o thread.o timebase.o timer.o select.o opt_objs-y := opt_objs-$(CONFIG_XENO_OPT_PERVASIVE) += shadow.o
PATCH 5/5 RTDM: check file descriptor owner.
This patch makes RTDM service rtdm_context_get check the owner of a file descriptor. This will prevent a process to close an RTDM file descriptor belonging to another process.
Stats:
include/rtdm/rtdm.h | 2 +- include/rtdm/rtdm_driver.h | 2 +- ksrc/skins/posix/syscall.c | 5 +++-- ksrc/skins/rtdm/core.c | 15 +++++++++------ 4 files changed, 14 insertions(+), 10 deletions(-)
Signature · 1 line
Gilles Chanteperdrix.
include/rtdm/rtdm.h +1 -1
@@ -248,7 +248,7 @@ ssize_t __rt_dev_recvmsg(rtdm_user_info_ssize_t __rt_dev_sendmsg (rtdm_user_info_t *user_info,int fd,const struct msghdr *msg,int flags);struct xnselector ; -int __rt_dev_select_bind (int fd, +int __rt_dev_select_bind (rtdm_user_info_t *user_info,int fd,struct xnselector *selector,unsigned type,unsigned index);#endif /* __KERNEL__ */
include/rtdm/rtdm_driver.h +1 -1
@@ -461,7 +461,7 @@ int rtdm_dev_unregister(struct rtdm_devi#define rtdm_getpeername rt_dev_getpeername#define rtdm_shutdown rt_dev_shutdown -struct rtdm_dev_context *rtdm_context_get (int fd); +struct rtdm_dev_context *rtdm_context_get (rtdm_user_info_t *user_info,int fd);#ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */ static inline void rtdm_context_lock (struct rtdm_dev_context *context)
ksrc/skins/posix/syscall.c +3 -2
@@ -1898,7 +1898,7 @@ static int fd_valid_p(int fd)if (fd >= rtdm_fd_start) {struct rtdm_dev_context *ctx; - ctx =rtdm_context_get (fd - rtdm_fd_start); + ctx =rtdm_context_get (current, fd - rtdm_fd_start);if (ctx) {rtdm_context_unlock (ctx);return 1 ;
@@ -1930,7 +1930,8 @@ static int select_bind_one(struct xnseleconst int rtdm_fd_start =FD_SETSIZE -RTDM_FD_MAX ;if (fd >= rtdm_fd_start) -return __rt_dev_select_bind (fd - rtdm_fd_start, +return __rt_dev_select_bind (current, + fd - rtdm_fd_start, selector, type, fd);#endif /* CONFIG_XENO_SKIN_RTDM */
ksrc/skins/rtdm/core.c +9 -6
@@ -70,7 +70,7 @@ DEFINE_XNLOCK(rt_fildes_lock);* * Rescheduling: never. */ -struct rtdm_dev_context *rtdm_context_get (int fd) +struct rtdm_dev_context *rtdm_context_get (rtdm_user_info_t *user_info,int fd) {struct rtdm_dev_context *context;spl_t s;
@@ -82,7 +82,9 @@ struct rtdm_dev_context *rtdm_context_ge context = fildes_table[fd].context;if (unlikely (!context || -test_bit (RTDM_CLOSING , &context->context_flags))) { +test_bit (RTDM_CLOSING , &context->context_flags) || + context->reserved.owner != (user_info + ? user_info->mm :NULL ))) {xnlock_put_irqrestore (&rt_fildes_lock, s);return NULL ; }
@@ -294,7 +296,7 @@ err_out:EXPORT_SYMBOL (__rt_dev_socket); -int __rt_dev_select_bind (int fd, +int __rt_dev_select_bind (rtdm_user_info_t *user_info,int fd,struct xnselector *selector,unsigned type,unsigned index) {
@@ -302,7 +304,7 @@ int __rt_dev_select_bind(int fd,struct rtdm_operations *ops;int ret; - context =rtdm_context_get (fd); + context =rtdm_context_get (user_info, fd); ret = -EBADF ;if (unlikely (!context))
@@ -338,7 +340,8 @@ again: context = fildes_table[fd].context; -if (unlikely (!context)) { +if (unlikely (!context || + (user_info && context->reserved.owner != user_info->mm))) {xnlock_put_irqrestore (&rt_fildes_lock, s); goto err_out;/* -EBADF */ }
@@ -434,7 +437,7 @@ do { \struct rtdm_operations *ops; \int ret; \ \ - context =rtdm_context_get (fd); \ + context =rtdm_context_get (user_info, fd); \ ret = -EBADF ; \if (unlikely (!context)) \ goto err_out; \
Re: [Xenomai-core] [PATCH 5/5] RTDM: check file descriptor owner.
Quoted 3 lines · Gilles Chanteperdrix
This patch makes RTDM service
rtdm_context_getcheck the owner of a file descriptor. This will prevent a process to close an RTDM file descriptor belonging to another process.
I assume this is a safety belt against crazy applications, right? Hope one day we can solve this via process-local FD name spaces.
I'm OK with this if you keep the API of rtdm_context_get as is and add an internal checked service __rtdm_context_get(user_info, fd) instead. rtdm_context_get would then call __rtdm_context_get(NULL, fd) because it is supposed to operate on kernel file descriptors only.
Jan
Re: [Xenomai-core] [PATCH 5/5] RTDM: check file descriptor owner.
Quoted 6 lines · Jan Kiszka
Quoted 3 lines · Gilles Chanteperdrix
This patch makes RTDM service
rtdm_context_getcheck the owner of a file descriptor. This will prevent a process to close an RTDM file descriptor belonging to another process.I assume this is a safety belt against crazy applications, right?
Not that much crazy: there is no way for an application to know if some file descriptors were passed to it accross exec, so the only way to be sure to avoid leaks is to forcibly close all file descriptors. When such an application was compiled for Xenomai, it suddenly closed all file descriptors in the system.
Quoted 1 line
Hope one day we can solve this via process-local FD name spaces.
I am thinking about this FD rework, we should start writing our requirements in the wiki.
Quoted 5 lines
I'm OK with this if you keep the API of
rtdm_context_getas is and add an internal checked service__rtdm_context_get(user_info, fd)instead.rtdm_context_getwould then call__rtdm_context_get(NULL, fd)because it is supposed to operate on kernel file descriptors only.
Ok, will do that.
Signature · 1 line
Gilles Chanteperdrix.
PATCH 2/5 select-like services support to rtdm.
In reply to Gilles ChanteperdrixThis patch adds select-like services support to rtdm.
No changes.
Stats:
include/rtdm/rtdm.h | 4 include/rtdm/rtdm_driver.h | 148 +++++++++++++- ksrc/skins/rtdm/core.c | 26 ++ ksrc/skins/rtdm/device.c | 10 + ksrc/skins/rtdm/drvlib.c | 446 ++++++++++++++++++++++++++++++++++----------- 5 files changed, 512 insertions(+), 122 deletions(-)
Signature · 1 line
Gilles Chanteperdrix.
rtdm/rtdm_driver.h +133 -15
@@ -38,6 +38,7 @@#include <nucleus/heap.h> #include <nucleus/pod.h> #include <nucleus/synch.h> +#include <nucleus/select.h> #include <rtdm/rtdm.h> /* debug support */
@@ -262,6 +263,11 @@ typedef ssize_t (*rtdm_sendmsg_handler_ttypedef int (*rtdm_rt_handler_t )(struct rtdm_dev_context *context,rtdm_user_info_t *user_info,void *arg); +typedef int (*rtdm_select_bind_handler_t )(struct rtdm_dev_context *context, +struct xnselector *selector, +unsigned type, +unsigned index); +/** * Device operations */
@@ -304,6 +310,8 @@ struct rtdm_operations {/** Transmit message handler for non-real-time context (optional) */ rtdm_sendmsg_handler_t sendmsg_nrt;/** @} Message-Oriented Device Operations */ + +rtdm_select_bind_handler_t select_bind; };struct rtdm_devctx_reserved {
@@ -1004,30 +1012,92 @@ typedef struct {xnsynch_t synch_base; } rtdm_event_t; -#define RTDM_EVENT_PENDING XNSYNCH_SPARE1 +typedef struct { +rtdm_event_t event; +struct xnselect select_block; +}rtdm_select_event_t ; -void rtdm_event_init (rtdm_event_t *event,unsigned long pending); -int rtdm_event_wait (rtdm_event_t *event); -int rtdm_event_timedwait (rtdm_event_t *event,nanosecs_rel_t timeout, -rtdm_toseq_t *timeout_seq); -void rtdm_event_signal (rtdm_event_t *event); +#define RTDM_EVENT_PENDING XNSYNCH_SPARE1 -void rtdm_event_clear (rtdm_event_t *event); +#ifndef TYPE_EQUAL +#define TYPE_EQUAL (object, type) \ + __builtin_types_compatible_p(typeof(object), type *) +#endif /* !TYPE_EQUAL */ + +#define PICK_VOIDEVENTOP(op, object, args...) \ + do { \ + if (TYPE_EQUAL((object), rtdm_event_t)) \ + _rtdm_event_##op((rtdm_event_t *)(object), ##args); \ + else if (TYPE_EQUAL((object), rtdm_select_event_t)) \ + _rtdm_select_event_##op((rtdm_select_event_t *)(object), ##args); \ + } while (0) + +#define PICK_INTEVENTOP(op, object, args...) \ + ({ \ + int err = -ENOSYS; \ + if (TYPE_EQUAL((object), rtdm_event_t)) \ + err = _rtdm_event_##op((rtdm_event_t *)(object), ##args); \ + else if (TYPE_EQUAL((object), rtdm_select_event_t)) \ + err = _rtdm_select_event_##op((rtdm_select_event_t *)(object), ##args); \ + err; \ + })#ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */ +void _rtdm_event_init (rtdm_event_t *event,unsigned long pending); +void _rtdm_select_event_init (rtdm_select_event_t *event,unsigned long pending); +#define rtdm_event_init (event, pending) PICK_VOIDEVENTOP(init, event, pending) + +int rtdm_select_event_bind (rtdm_select_event_t *event, +struct xnselector *selector, +unsigned type, +unsigned bit_index); + +int _rtdm_event_wait (rtdm_event_t *event); +int _rtdm_select_event_wait (rtdm_select_event_t *event); +#define rtdm_event_wait (event) PICK_INTEVENTOP(wait, event) + +int _rtdm_event_timedwait (rtdm_event_t *event,nanosecs_rel_t timeout, +rtdm_toseq_t *timeout_seq); +int _rtdm_select_event_timedwait (rtdm_select_event_t *event, +nanosecs_rel_t timeout, +rtdm_toseq_t *timeout_seq); +#define rtdm_event_timedwait (event, timeout, timeout_seq) \ + PICK_INTEVENTOP(timedwait, event, timeout, timeout_seq) + +void _rtdm_event_signal (rtdm_event_t *event); +void _rtdm_select_event_signal (rtdm_select_event_t *event); +#define rtdm_event_signal (event) PICK_VOIDEVENTOP(signal, event) + +void _rtdm_event_clear (rtdm_event_t *event); +void _rtdm_select_event_clear (rtdm_select_event_t *event); +#define rtdm_event_clear (event) PICK_VOIDEVENTOP(clear, event) +void __rtdm_synch_flush (xnsynch_t *synch,unsigned long reason); -static inline void rtdm_event_pulse (rtdm_event_t *event) +static inline void _rtdm_event_pulse (rtdm_event_t *event) {trace_mark (xn_rtdm_event_pulse,"event %p" , event);__rtdm_synch_flush (&event->synch_base,0 ); } -static inline void rtdm_event_destroy (rtdm_event_t *event) +static inline void _rtdm_select_event_pulse (rtdm_select_event_t *event) +{ +_rtdm_event_pulse (&event->event); +} +#define rtdm_event_pulse (event) PICK_VOIDEVENTOP(pulse, event) + +static inline void _rtdm_event_destroy (rtdm_event_t *event) {trace_mark (xn_rtdm_event_destroy,"event %p" , event);__rtdm_synch_flush (&event->synch_base,XNRMID ); } + +static inline void _rtdm_select_event_destroy (rtdm_select_event_t *event) +{ +xnselect_destroy (&event->select_block); +_rtdm_event_destroy (&event->event); +} +#define rtdm_event_destroy (event) PICK_VOIDEVENTOP(destroy, event);#endif /* !DOXYGEN_CPP */ /* --- semaphore services --- */
@@ -1037,18 +1107,66 @@ typedef struct {xnsynch_t synch_base; } rtdm_sem_t; -void rtdm_sem_init (rtdm_sem_t *sem,unsigned long value); -int rtdm_sem_down (rtdm_sem_t *sem); -int rtdm_sem_timeddown (rtdm_sem_t *sem,nanosecs_rel_t timeout, -rtdm_toseq_t *timeout_seq); -void rtdm_sem_up (rtdm_sem_t *sem); +typedef struct { +rtdm_sem_t sem; +struct xnselect select_block; +}rtdm_select_sem_t ;#ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */ -static inline void rtdm_sem_destroy (rtdm_sem_t *sem) +#define PICK_VOIDSEMOP(op, object, args...) \ + do { \ + if (TYPE_EQUAL((object), rtdm_sem_t)) \ + _rtdm_sem_##op((rtdm_sem_t *)(object), ##args); \ + else if (TYPE_EQUAL((object), rtdm_select_sem_t)) \ + _rtdm_select_sem_##op((rtdm_select_sem_t *)(object), ##args); \ + } while (0) + +#define PICK_INTSEMOP(op, object, args...) \ + ({ \ + int err = -ENOSYS; \ + if (TYPE_EQUAL((object), rtdm_sem_t)) \ + err = _rtdm_sem_##op((rtdm_sem_t *)(object), ##args); \ + else if (TYPE_EQUAL((object), rtdm_select_sem_t)) \ + err = _rtdm_select_sem_##op((rtdm_select_sem_t *)(object), ##args); \ + err; \ + }) + +void _rtdm_sem_init (rtdm_sem_t *sem,unsigned long value); +void _rtdm_select_sem_init (rtdm_select_sem_t *sem,unsigned long value); +#define rtdm_sem_init (sem, value) PICK_VOIDSEMOP(init, sem, value) + +int rtdm_select_sem_bind (rtdm_select_sem_t *sem, +struct xnselector *selector, +unsigned type, +unsigned bit_index); + +int _rtdm_sem_down (rtdm_sem_t *sem); +int _rtdm_select_sem_down (rtdm_select_sem_t *sem); +#define rtdm_sem_down (sem) PICK_INTSEMOP(down, sem) + +int _rtdm_sem_timeddown (rtdm_sem_t *sem,nanosecs_rel_t timeout, +rtdm_toseq_t *timeout_seq); +int _rtdm_select_sem_timeddown (rtdm_select_sem_t *sem,nanosecs_rel_t timeout, +rtdm_toseq_t *timeout_seq); +#define rtdm_sem_timeddown (sem, timeout, timeout_seq) \ + PICK_INTSEMOP(timeddown, sem, timeout, timeout_seq) + +void _rtdm_sem_up (rtdm_sem_t *sem); +void _rtdm_select_sem_up (rtdm_select_sem_t *sem); +#define rtdm_sem_up (sem) PICK_VOIDSEMOP(up, sem) + +static inline void _rtdm_sem_destroy (rtdm_sem_t *sem) {trace_mark (xn_rtdm_sem_destroy,"sem %p" , sem);__rtdm_synch_flush (&sem->synch_base,XNRMID ); } + +static inline void _rtdm_select_sem_destroy (rtdm_select_sem_t *sem) +{ +xnselect_destroy (&sem->select_block); +_rtdm_sem_destroy (&sem->sem); +} +#define rtdm_sem_destroy (sem) PICK_VOIDSEMOP(destroy, sem)#endif /* !DOXYGEN_CPP */ /* --- mutex services --- */
rtdm/rtdm.h +4 -0
@@ -247,6 +247,10 @@ ssize_t __rt_dev_recvmsg(rtdm_user_info_ struct msghdr *msg, int flags);ssize_t __rt_dev_sendmsg (rtdm_user_info_t *user_info,int fd,const struct msghdr *msg,int flags); +struct xnselector ; +int __rt_dev_select_bind (int fd, +struct xnselector *selector, +unsigned type,unsigned index);#endif /* __KERNEL__ */ /* Define RTDM_NO_DEFAULT_USER_API to switch off the default rt_dev_xxx
skins/rtdm/device.c +10 -0
@@ -70,6 +70,14 @@ int rtdm_no_support(void)return -ENOSYS ; } +int rtdm_select_bind_no_support (struct rtdm_dev_context *context, +struct xnselector *selector, +unsigned type, +unsigned index) +{ +return -EBADF ; +} +static inline int get_name_hash (const char *str,int limit,int hashkey_mask) {int hash =0 ;
@@ -242,6 +250,8 @@ int rtdm_dev_register(struct rtdm_deviceSET_DEFAULT_OP_IF_NULL (device->ops, write);SET_DEFAULT_OP_IF_NULL (device->ops, recvmsg);SET_DEFAULT_OP_IF_NULL (device->ops, sendmsg); +if (!device->ops.select_bind) + device->ops.select_bind = rtdm_select_bind_no_support;atomic_set (&device->reserved.refcount,0 ); device->reserved.exclusive_context =NULL ;
skins/rtdm/drvlib.c +339 -107
542 lines not on this page · raw message
skins/rtdm/core.c +26 -0
@@ -294,6 +294,32 @@ err_out:EXPORT_SYMBOL (__rt_dev_socket); +int __rt_dev_select_bind (int fd, +struct xnselector *selector, +unsigned type,unsigned index) +{ +struct rtdm_dev_context *context; +struct rtdm_operations *ops; +int ret; + + context =rtdm_context_get (fd); + + ret = -EBADF ; +if (unlikely (!context)) + goto err_out; + + ops = context->ops; + + ret = ops->select_bind (context, selector, type, index); + + XENO_ASSERT(RTDM, !rthal_local_irq_test(), rthal_local_irq_enable();); + +rtdm_context_unlock (context); + + err_out: +return ret; +} +int __rt_dev_close (rtdm_user_info_t *user_info,int fd) {struct rtdm_dev_context *context;
PATCH 3/5 posix skin kernel-space support for user-space select.
In reply to Gilles ChanteperdrixThis patch adds user-space select support to xenomai posix skin kernel space.
Changes:
- select handles null fd_sets (by passing them to xnselect);
- select returns -EBADF without allocating a struct xnselector, in the common case when the wrapped select is called with Linux-only file descriptors;
- walking fd_sets to bind unbound file descriptors is now optimized by using find_first_bit/find_next_bit;
- the timeout, when not null, is passed as an XN_ABSOLUTE value, so it will be handled as a relative timeout if time changes, but will have the proper effect if xnsynch_sleep_on is called several times with this timeout value;
- select now returns the remaining time before timeout.
Stats:
include/posix/syscall.h | 1 ksrc/skins/posix/internal.h | 26 ++++++ ksrc/skins/posix/mq.c | 78 ++++++++++++++++++- ksrc/skins/posix/syscall.c | 177 ++++++++++++++++++++++++++++++++++++++++++++ ksrc/skins/posix/thread.c | 5 + ksrc/skins/posix/thread.h | 4 6 files changed, 287 insertions(+), 4 deletions(-)
Signature · 1 line
Gilles Chanteperdrix.
posix/syscall.h +1 -0
@@ -100,6 +100,7 @@#define __pse51_condattr_setpshared 74#define __pse51_thread_getschedparam 75#define __pse51_thread_kill 76 +#define __pse51_select 77#ifdef __KERNEL__
skins/posix/syscall.c +177 -0
195 lines not on this page · raw message
skins/posix/mq.c +75 -3
135 lines not on this page · raw message
skins/posix/thread.c +5 -0
@@ -84,6 +84,10 @@ static void thread_delete_hook(xnthread_pse51_mark_deleted (thread);pse51_signal_cleanup_thread (thread);pse51_timer_cleanup_thread (thread); +if (thread->selector) { +xnselector_destroy (thread->selector); + thread->selector =NULL ; + }switch (thread_getdetachstate (thread)) {case PTHREAD_CREATE_DETACHED :
@@ -216,6 +220,7 @@ int pthread_create(pthread_t *tid,pse51_signal_init_thread (thread, cur);pse51_tsd_init_thread (thread);pse51_timer_init_thread (thread); + thread->selector =NULL ;if (thread->attr.policy ==SCHED_RR ) {xnthread_time_slice (&thread->threadbase) = pse51_time_slice;
skins/posix/thread.h +4 -0
@@ -21,6 +21,7 @@#define _POSIX_THREAD_H#include <posix/internal.h> +#include <nucleus/select.h> typedef unsigned long long pse51_sigset_t ;
@@ -90,6 +91,9 @@ struct pse51_thread {/* For timers. */ xnqueue_t timersq; +/* For select. */ +struct xnselector *selector; +#ifdef CONFIG_XENO_OPT_PERVASIVE struct pse51_hkey hkey;#endif /* CONFIG_XENO_OPT_PERVASIVE */
skins/posix/internal.h +25 -1
@@ -22,6 +22,7 @@#include <nucleus/xenomai.h> #include <nucleus/core.h> #include <nucleus/ppd.h> +#include <nucleus/select.h> #include <posix/posix.h> #include <posix/registry.h>
@@ -153,12 +154,32 @@ static inline xnticks_t ts2ticks_ceil(coreturn rem ? ticks+1 : ticks; } +static inline xnticks_t tv2ticks_ceil (const struct timeval *tv) +{ +xntime_t nsecs = tv->tv_usec *1000 ; +unsigned long rem; +xnticks_t ticks; +if (tv->tv_sec) + nsecs += (xntime_t ) tv->tv_sec *ONE_BILLION ; + ticks =xnarch_ulldiv (nsecs,xntbase_get_tickval (pse51_tbase), &rem); +return rem ? ticks+1 : ticks; +} + +static inline void ticks2tv (struct timeval *tv,xnticks_t ticks) +{ +unsigned long nsecs; + tv->tv_sec =xnarch_uldivrem (xntbase_ticks2ns (pse51_tbase, ticks), +ONE_BILLION , + &nsecs); + tv->tv_usec = nsecs /1000 ; +} +static inline xnticks_t clock_get_ticks (clockid_t clock_id) {if (clock_id ==CLOCK_REALTIME )return xntbase_get_time (pse51_tbase);else -return xntbase_ns2ticks (pse51_tbase,xnpod_get_cpu_time ()); +return xntbase_get_jiffies (pse51_tbase); }static inline int clock_flag (int flag,clockid_t clock_id)
@@ -179,4 +200,7 @@ static inline int clock_flag(int flag, c return -EINVAL; } +int pse51_mq_select_bind (mqd_t fd,struct xnselector *selector, +unsigned type,unsigned index); +#endif /* !_POSIX_INTERNAL_H */
PATCH 4/5 posix skin user-space support for select.
In reply to Gilles ChanteperdrixThis patch adds support for select to xenomai posix skin user-space.
No changes.
Stats:
include/posix/sys/select.h | 23 +++++++++++++++++++++++ src/skins/posix/Makefile.am | 1 + src/skins/posix/posix.wrappers | 1 + src/skins/posix/select.c | 26 ++++++++++++++++++++++++++ src/skins/posix/wrappers.c | 9 +++++++++ 5 files changed, 60 insertions(+)
Signature · 1 line
Gilles Chanteperdrix.
posix/sys/select.h +23 -0
@@ -0,0 +1,23 @@ +#ifndef _XENO_POSIX_SELECT_H +#define _XENO_POSIX_SELECT_H + +#if !(defined(__KERNEL__) || defined(__XENO_SIM__)) + +#include_next <sys/select.h> + +#ifdef __cplusplus +extern "C" { +#endif + +extern int __real_select (int __nfds,fd_set *__restrict __readfds, +fd_set *__restrict __writefds, +fd_set *__restrict __exceptfds, +struct timeval *__restrict __timeout); + +#ifdef __cplusplus +} +#endif + +#endif /* !(__KERNEL__ || __XENO_SIM__) */ + +#endif /* _XENO_POSIX_SELECT_H */
skins/posix/wrappers.c +9 -0
@@ -30,6 +30,7 @@#include <unistd.h> #include <sys/ioctl.h> #include <sys/mman.h> +#include <sys/select.h> /* sched */ int __real_pthread_setschedparam(pthread_t thread,
@@ -235,3 +236,11 @@ int __real_munmap(void *addr, size_t len {return munmap (addr, len); } + +int __real_select (int __nfds,fd_set *__restrict __readfds, +fd_set *__restrict __writefds, +fd_set *__restrict __exceptfds, +struct timeval *__restrict __timeout) +{ +return select (__nfds, __readfds, __writefds, __exceptfds, __timeout); +}
skins/posix/select.c +26 -0
@@ -0,0 +1,26 @@ +#include <errno.h> +#include <posix/syscall.h> +#include <sys/select.h> + +extern int __pse51_muxid; + +int __wrap_select (int __nfds,fd_set *__restrict __readfds, +fd_set *__restrict __writefds, +fd_set *__restrict __exceptfds, +struct timeval *__restrict __timeout) +{ +int err; + + err =XENOMAI_SKINCALL5 (__pse51_muxid, __pse51_select, __nfds, + __readfds, __writefds, __exceptfds, __timeout); + +if (err == -EBADF || err == -EPERM ) +return __real_select (__nfds, __readfds, + __writefds, __exceptfds, __timeout); + +if (err >0 ) +return err; + + errno = -err; +return -1 ; +}
skins/posix/posix.wrappers +1 -0
@@ -87,3 +87,4 @@ --wrap shm_unlink --wrap mmap --wrap munmap +--wrap select
skins/posix/Makefile.am +1 -0
@@ -15,6 +15,7 @@ libpthread_rt_la_SOURCES = \ mutex.c \ shm.c \ interrupt.c \ + select.c \ rtdm.c \ wrappers.c
Quoted 4 lines · Gilles Chanteperdrix
Hi,
after some tests, bug fixes and optimizations, here comes a second version of the patch-set adding select support to xenomai posix skin.
It's on my agenda for today or tomorrow to have a closer look at this. Just one general question already: Why is it "select" and not "poll and select"? Is it trivial to extend later on?
Jan
Quoted 9 lines · Jan Kiszka
Quoted 4 lines · Gilles Chanteperdrix
Hi,
after some tests, bug fixes and optimizations, here comes a second version of the patch-set adding select support to xenomai posix skin.
It's on my agenda for today or tomorrow to have a closer look at this. Just one general question already: Why is it "select" and not "poll and select"? Is it trivial to extend later on?
Because making an O(1) poll is impossible, when entering poll, you have to scan the whole list of file descriptors for new descriptors, and this will take O(n). In select implementation, this is done with bounded bit operations so remains O(1). Of course, the downside is that you are limited to FD_SETSIZE (that is 1024) descriptors, but I do not find this limitation unbearable for real-time applications.
Anyway, you can implement poll in user-space to use select. I saw this for instance in Asterisk sources.
Signature · 1 line
Gilles Chanteperdrix.
Quoted 9 lines · Jan Kiszka
Quoted 4 lines · Gilles Chanteperdrix
Hi,
after some tests, bug fixes and optimizations, here comes a second version of the patch-set adding select support to xenomai posix skin.
It's on my agenda for today or tomorrow to have a closer look at this. Just one general question already: Why is it "select" and not "poll and select"? Is it trivial to extend later on?
The patch I sent for adding select support to RTnet sockets was uselessly complicated. Here is a simpler one.
Signature · 1 line
Gilles Chanteperdrix.
ipv4/af_inet.c +1 -0
@@ -294,6 +294,7 @@ static struct rtdm_device ipv4_device = ioctl_nrt: rt_udp_ioctl, recvmsg_rt: rt_udp_recvmsg, sendmsg_rt: rt_udp_sendmsg, + select_bind: rt_socket_select_bind, #ifdef CONFIG_RTNET_RTDM_SELECT poll_rt: rt_udp_poll,/* there should be only the function poll() */
include/rtnet_socket.h +5 -1
@@ -51,7 +51,7 @@ struct rtsocket {unsigned int priority;nanosecs_rel_t timeout;/* receive timeout, 0 for infinite */ -rtdm_sem_t pending_sem; +rtdm_select_sem_t pending_sem;#ifdef CONFIG_RTNET_RTDM_SELECT wait_queue_primitive_t *wakeup_select;/* for selecting calls - this SHOULD be the head of a wait
@@ -96,6 +96,10 @@ static inline struct rtdm_dev_context *r atomic_dec(&(rt_socket_context(sock)->close_lock_count))extern int rt_socket_init (struct rtdm_dev_context *context); +extern int rt_socket_select_bind (struct rtdm_dev_context *context, +struct xnselector *selector, +unsigned type, +unsigned index);extern int rt_socket_cleanup (struct rtdm_dev_context *context); extern int rt_socket_common_ioctl(struct rtdm_dev_context *context, rtdm_user_info_t *user_info,
packet/af_packet.c +2 -1
@@ -467,7 +467,8 @@ static struct rtdm_device packet_proto ioctl_rt: rt_packet_ioctl, ioctl_nrt: rt_packet_ioctl, recvmsg_rt: rt_packet_recvmsg, - sendmsg_rt: rt_packet_sendmsg + sendmsg_rt: rt_packet_sendmsg, + select_bind: rt_socket_select_bind, }, device_class: RTDM_CLASS_NETWORK,
socket.c +18 -0
@@ -87,7 +87,25 @@ int rt_socket_init(struct rtdm_dev_contereturn 0 ; } +int rt_socket_select_bind (struct rtdm_dev_context *context, +struct xnselector *selector, +unsigned type, +unsigned index) +{ +struct rtsocket *sock = (struct rtsocket *)&context->dev_private; + +switch (type) { +case XNSELECT_READ : +return rtdm_select_sem_bind (&sock->pending_sem, selector, type, index); + +case XNSELECT_WRITE : +case XNSELECT_EXCEPT : +return -EBADF ; + } +return -EINVAL ; +} +EXPORT_SYMBOL (rt_socket_select_bind);/*** * rt_socket_cleanup - releases resources allocated for the socket
Quoted 5 lines · Gilles Chanteperdrix
Hi,
after some tests, bug fixes and optimizations, here comes a second version of the patch-set adding select support to xenomai posix skin.
Puh, a lot of code, and my brain is still full of KGDB stuff I'm trying to polish for mainline since days.
The approach looks consistent to me. I would just suggest to make the whole thing configurable, so that it doesn't add overhead to systems which do not use it.
Out of curiosity: Did you already have a chance to compare a multi-threaded demo application with one based on these new select services?
I guess your current test cases are built on top of RTnet, aren't they? Would be nice, also for regression testing, to have some demo for /examples, e.g. exploiting the CAN stack (because it has virtual CAN, thus it is usable without hardware).
Jan
Quoted 7 lines · Jan Kiszka
Quoted 5 lines · Gilles Chanteperdrix
Hi,
after some tests, bug fixes and optimizations, here comes a second version of the patch-set adding select support to xenomai posix skin.
Quoted 6 lines
Puh, a lot of code, and my brain is still full of KGDB stuff I'm trying to polish for mainline since days.
The approach looks consistent to me. I would just suggest to make the whole thing configurable, so that it doesn't add overhead to systems which do not use it.
The core support does not take that much code. The RTDM patch however is huge, that is because we duplicate each rtdm_event and rtdm_sem service for the select-enabled objects. What would you think of adding select support to the rtdm_event and rtdm_sem objects without creating the new rtdm_select_event_t and rtdm_select_sem_t objects if the whole select support was configurable ? That is if select support is not selected, RTDM is as before, if select is enabled, rtdm_event_t and rtdm_sem_t objects may be used with select.
Quoted 3 lines
Out of curiosity: Did you already have a chance to compare a multi-threaded demo application with one based on these new select services?
We compared two standard Linux applications, one using select, the other being threaded, and there is no question: the select based scales much better. But as usual, it was on low end ARM hardware.
Quoted 5 lines
I guess your current test cases are built on top of RTnet, aren't they? Would be nice, also for regression testing, to have some demo for /examples, e.g. exploiting the CAN stack (because it has virtual CAN, thus it is usable without hardware).
Ok, will probably do that.
Signature · 1 line
Gilles Chanteperdrix.
Quoted 7 lines · Jan Kiszka
Quoted 5 lines · Gilles Chanteperdrix
Hi,
after some tests, bug fixes and optimizations, here comes a second version of the patch-set adding select support to xenomai posix skin.
Quoted 6 lines
Puh, a lot of code, and my brain is still full of KGDB stuff I'm trying to polish for mainline since days.
The approach looks consistent to me. I would just suggest to make the whole thing configurable, so that it doesn't add overhead to systems which do not use it.
I am getting back to this select stuff. So I have a question, would you agree if the select support was made configurable, but if when this support was enabled, support for select was added to vanilla rtdm_event_t and rtdm_sem_t ? This would ease maintenance, since it would avoid a lot of code duplication, without adding to much overhead in my opinion (an empty list test when signaling the objects).
Quoted 8 lines
Out of curiosity: Did you already have a chance to compare a multi-threaded demo application with one based on these new select services?
I guess your current test cases are built on top of RTnet, aren't they? Would be nice, also for regression testing, to have some demo for /examples, e.g. exploiting the CAN stack (because it has virtual CAN, thus it is usable without hardware).
Ok, I had a look at the CAN stack, if I understand correctly when receiving, the recv_sem defined in rcan_socket.h is used, and when sending, the tx_sem defined in rtcan_dev.h is used. Can you confirm this ?
Signature · 1 line
Gilles Chanteperdrix.
Quoted 15 lines · Gilles Chanteperdrix
Quoted 7 lines · Jan Kiszka
Quoted 5 lines · Gilles Chanteperdrix
Hi,
after some tests, bug fixes and optimizations, here comes a second version of the patch-set adding select support to xenomai posix skin.
Quoted 6 lines
Puh, a lot of code, and my brain is still full of KGDB stuff I'm trying to polish for mainline since days.
The approach looks consistent to me. I would just suggest to make the whole thing configurable, so that it doesn't add overhead to systems which do not use it.
Quoted 6 lines
I am getting back to this select stuff. So I have a question, would you agree if the select support was made configurable, but if when this support was enabled, support for select was added to vanilla
rtdm_event_tandrtdm_sem_t? This would ease maintenance, since it would avoid a lot of code duplication, without adding to much overhead in my opinion (an empty list test when signaling the objects).
Looking at the code again, this really makes sense, go for it.
Quoted 10 lines
Quoted 9 lines
Out of curiosity: Did you already have a chance to compare a multi-threaded demo application with one based on these new select services?
I guess your current test cases are built on top of RTnet, aren't they? Would be nice, also for regression testing, to have some demo for /examples, e.g. exploiting the CAN stack (because it has virtual CAN, thus it is usable without hardware).
Quoted 4 lines
Ok, I had a look at the CAN stack, if I understand correctly when receiving, the
recv_semdefined inrcan_socket.his used, and when sending, thetx_semdefined inrtcan_dev.his used. Can you confirm this ?
Yep, that's correct. rx_sem counts received packets, tx_sem the number of available output slots in the CAN port.
Jan