hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2008 Efixo <gilles.chanteperdrix@xenomai.org>
 *
 * 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.
 */
#ifndef _COBALT_KERNEL_SELECT_H
#define _COBALT_KERNEL_SELECT_H
 
#include <cobalt/kernel/list.h>
#include <cobalt/kernel/thread.h>
 
/**
 * @addtogroup cobalt_core_select
 * @{
 */
 
#define XNSELECT_READ      0
#define XNSELECT_WRITE     1
#define XNSELECT_EXCEPT    2
#define XNSELECT_MAX_TYPES 3
 
struct xnselector {
   struct xnsynch synchbase;
   struct fds {
       fd_set expected;
       fd_set pending;
   } fds [XNSELECT_MAX_TYPES];
   struct list_head destroy_link;
   struct list_head bindings; /* only used by xnselector_destroy */
};
 
#define __NFDBITS__    (8 * sizeof(unsigned long))
#define __FDSET_LONGS__    (__FD_SETSIZE/__NFDBITS__)
#define    __FDELT__(d)    ((d) / __NFDBITS__)
#define    __FDMASK__(d)    (1UL << ((d) % __NFDBITS__))
 
static inline void __FD_SET__(unsigned long __fd, __kernel_fd_set *__fdsetp)
{
        unsigned long __tmp = __fd / __NFDBITS__;
        unsigned long __rem = __fd % __NFDBITS__;
        __fdsetp->fds_bits[__tmp] |= (1UL<<__rem);
}
 
static inline void __FD_CLR__(unsigned long __fd, __kernel_fd_set *__fdsetp)
{
        unsigned long __tmp = __fd / __NFDBITS__;
        unsigned long __rem = __fd % __NFDBITS__;
        __fdsetp->fds_bits[__tmp] &= ~(1UL<<__rem);
}
 
static inline int __FD_ISSET__(unsigned long __fd, const __kernel_fd_set *__p)
{
        unsigned long __tmp = __fd / __NFDBITS__;
        unsigned long __rem = __fd % __NFDBITS__;
        return (__p->fds_bits[__tmp] & (1UL<<__rem)) != 0;
}
 
static inline void __FD_ZERO__(__kernel_fd_set *__p)
{
   unsigned long *__tmp = __p->fds_bits;
   int __i;
 
   __i = __FDSET_LONGS__;
   while (__i) {
       __i--;
       *__tmp = 0;
       __tmp++;
   }
}
 
struct xnselect {
   struct list_head bindings;
};
 
#define DECLARE_XNSELECT(name) struct xnselect name
 
struct xnselect_binding {
   struct xnselector *selector;
   struct xnselect *fd;
   unsigned int type;
   unsigned int bit_index;
   struct list_head link;  /* link in selected fds list. */
   struct list_head slink; /* link in selector list */
};
 
void xnselect_init(struct xnselect *select_block);
 
int xnselect_bind(struct xnselect *select_block,
         struct xnselect_binding *binding,
         struct xnselector *selector,
         unsigned int type,
         unsigned int bit_index,
         unsigned int state);
 
int __xnselect_signal(struct xnselect *select_block, unsigned int 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 int state)
{
   if (!list_empty(&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);
 
int xnselect_mount(void);
 
int xnselect_umount(void);
 
/** @} */
 
#endif /* _COBALT_KERNEL_SELECT_H */