Xenomai API  2.6.5
internal.h
1 /*
2  * Written by Gilles Chanteperdrix <[email protected]>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #ifndef _POSIX_INTERNAL_H
20 #define _POSIX_INTERNAL_H
21 
22 #include <nucleus/xenomai.h>
23 #include <nucleus/ppd.h>
24 #include <nucleus/select.h>
25 #include <posix/posix.h>
26 #include <posix/registry.h>
27 
28 /* debug support */
29 #include <nucleus/assert.h>
30 
31 #ifndef CONFIG_XENO_OPT_DEBUG_POSIX
32 #define CONFIG_XENO_OPT_DEBUG_POSIX 0
33 #endif
34 
35 #define PSE51_MAGIC(n) (0x8686##n##n)
36 #define PSE51_ANY_MAGIC PSE51_MAGIC(00)
37 #define PSE51_THREAD_MAGIC PSE51_MAGIC(01)
38 #define PSE51_THREAD_ATTR_MAGIC PSE51_MAGIC(02)
39 #define PSE51_MUTEX_MAGIC PSE51_MAGIC(03)
40 #define PSE51_MUTEX_ATTR_MAGIC (PSE51_MAGIC(04) & ((1 << 24) - 1))
41 #define PSE51_COND_MAGIC PSE51_MAGIC(05)
42 #define PSE51_COND_ATTR_MAGIC (PSE51_MAGIC(05) & ((1 << 24) - 1))
43 #define PSE51_SEM_MAGIC PSE51_MAGIC(06)
44 #define PSE51_KEY_MAGIC PSE51_MAGIC(07)
45 #define PSE51_ONCE_MAGIC PSE51_MAGIC(08)
46 #define PSE51_MQ_MAGIC PSE51_MAGIC(09)
47 #define PSE51_MQD_MAGIC PSE51_MAGIC(0A)
48 #define PSE51_INTR_MAGIC PSE51_MAGIC(0B)
49 #define PSE51_NAMED_SEM_MAGIC PSE51_MAGIC(0C)
50 #define PSE51_TIMER_MAGIC PSE51_MAGIC(0D)
51 #define PSE51_SHM_MAGIC PSE51_MAGIC(0E)
52 
53 #define PSE51_MIN_PRIORITY XNSCHED_LOW_PRIO
54 #define PSE51_MAX_PRIORITY XNSCHED_HIGH_PRIO
55 
56 #define ONE_BILLION 1000000000
57 
58 #define pse51_obj_active(h,m,t) \
59  ((h) && ((t *)(h))->magic == (m))
60 
61 #define pse51_obj_deleted(h,m,t) \
62  ((h) && ((t *)(h))->magic == ~(m))
63 
64 #define pse51_mark_deleted(t) ((t)->magic = ~(t)->magic)
65 
66 typedef struct {
67  xnqueue_t condq;
68  xnqueue_t intrq;
69  xnqueue_t mutexq;
70  xnqueue_t semq;
71  xnqueue_t threadq;
72  xnqueue_t timerq;
73 } pse51_kqueues_t;
74 
75 #ifdef CONFIG_XENO_OPT_PERVASIVE
76 typedef struct {
77  pse51_kqueues_t kqueues;
78  pse51_assocq_t uqds;
79  pse51_assocq_t usems;
80  pse51_assocq_t umaps;
81  pse51_assocq_t ufds;
82 
83  xnshadow_ppd_t ppd;
84 
85 #define ppd2queues(addr) \
86  ((pse51_queues_t *) ((char *) (addr) - offsetof(pse51_queues_t, ppd)))
87 
88 } pse51_queues_t;
89 
90 extern int pse51_muxid;
91 #endif /* CONFIG_XENO_OPT_PERVASIVE */
92 
93 extern xntbase_t *pse51_tbase;
94 
95 extern pse51_kqueues_t pse51_global_kqueues;
96 
97 #ifdef CONFIG_XENO_OPT_PERVASIVE
98 static inline pse51_queues_t *pse51_queues(void)
99 {
100  xnshadow_ppd_t *ppd;
101  spl_t s;
102 
103  xnlock_get_irqsave(&nklock, s);
104 
105  ppd = xnshadow_ppd_get(pse51_muxid);
106 
107  xnlock_put_irqrestore(&nklock, s);
108 
109  if (!ppd)
110  return NULL;
111 
112  return ppd2queues(ppd);
113 }
114 #endif /* CONFIG_XENO_OPT_PERVASIVE */
115 
116 static inline pse51_kqueues_t *pse51_kqueues(int pshared)
117 {
118 #ifdef CONFIG_XENO_OPT_PERVASIVE
119  xnshadow_ppd_t *ppd;
120 
121  if (pshared || !(ppd = xnshadow_ppd_get(pse51_muxid)))
122  return &pse51_global_kqueues;
123 
124  return &ppd2queues(ppd)->kqueues;
125 #else /* !CONFIG_XENO_OPT_PERVASIVE */
126  return &pse51_global_kqueues;
127 #endif /* !CONFIG_XENO_OPT_PERVASIVE */
128 }
129 
130 static inline void ticks2ts(struct timespec *ts, xnticks_t ticks)
131 {
132  ts->tv_sec = xnarch_divrem_billion(xntbase_ticks2ns(pse51_tbase, ticks),
133  &ts->tv_nsec);
134 }
135 
136 static inline xnticks_t ts2ticks_floor(const struct timespec *ts)
137 {
138  xntime_t nsecs = ts->tv_nsec;
139  if(ts->tv_sec)
140  nsecs += (xntime_t) ts->tv_sec * ONE_BILLION;
141  return xntbase_ns2ticks(pse51_tbase, nsecs);
142 }
143 
144 static inline xnticks_t ts2ticks_ceil(const struct timespec *ts)
145 {
146  xntime_t nsecs = ts->tv_nsec;
147  unsigned long rem;
148  xnticks_t ticks;
149  if(ts->tv_sec)
150  nsecs += (xntime_t) ts->tv_sec * ONE_BILLION;
151  ticks = xnarch_ulldiv(nsecs, xntbase_get_tickval(pse51_tbase), &rem);
152  return rem ? ticks+1 : ticks;
153 }
154 
155 static inline xnticks_t tv2ticks_ceil(const struct timeval *tv)
156 {
157  xntime_t nsecs = tv->tv_usec * 1000;
158  unsigned long rem;
159  xnticks_t ticks;
160  if(tv->tv_sec)
161  nsecs += (xntime_t) tv->tv_sec * ONE_BILLION;
162  ticks = xnarch_ulldiv(nsecs, xntbase_get_tickval(pse51_tbase), &rem);
163  return rem ? ticks+1 : ticks;
164 }
165 
166 static inline void ticks2tv(struct timeval *tv, xnticks_t ticks)
167 {
168  unsigned long nsecs;
169  tv->tv_sec = xnarch_divrem_billion(xntbase_ticks2ns(pse51_tbase, ticks),
170  &nsecs);
171  tv->tv_usec = nsecs / 1000;
172 }
173 
174 static inline xnticks_t clock_get_ticks(clockid_t clock_id)
175 {
176  if(clock_id == CLOCK_REALTIME)
177  return xntbase_get_time(pse51_tbase);
178  else
179  return xntbase_get_jiffies(pse51_tbase);
180 }
181 
182 static inline int clock_flag(int flag, clockid_t clock_id)
183 {
184  switch(flag & TIMER_ABSTIME) {
185  case 0:
186  return XN_RELATIVE;
187 
188  case TIMER_ABSTIME:
189  switch(clock_id) {
190  case CLOCK_MONOTONIC:
191  return XN_ABSOLUTE;
192 
193  case CLOCK_REALTIME:
194  return XN_REALTIME;
195  }
196  }
197  return -EINVAL;
198 }
199 
200 int pse51_mq_select_bind(mqd_t fd, struct xnselector *selector,
201  unsigned type, unsigned index);
202 
203 #endif /* !_POSIX_INTERNAL_H */
xnshadow_ppd_t * xnshadow_ppd_get(unsigned muxid)
Return the per-process data attached to the calling process.
Definition: shadow.c:3014
file descriptors events multiplexing header.
static xnticks_t xntbase_get_time(xntbase_t *base)
Get the clock time for a given time base.
Definition: timebase.h:301