L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
__timeout.h
1
6/*
7 * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
8 * Alexander Warg <warg@os.inf.tu-dresden.de>,
9 * Torsten Frenzel <frenzel@os.inf.tu-dresden.de>
10 * economic rights: Technische Universität Dresden (Germany)
11 *
12 * License: see LICENSE.spdx (in this directory or the directories above)
13 */
14#ifndef L4_SYS_TIMEOUT_H__
15#define L4_SYS_TIMEOUT_H__
16
17#include <l4/sys/l4int.h>
18#include <l4/sys/compiler.h>
19
25
40typedef struct l4_timeout_s {
42} __attribute__((packed)) l4_timeout_s;
43
44
52typedef union l4_timeout_t
53{
55 struct
56 {
57#ifdef __BIG_ENDIAN__
60#else
63#endif
64 } p;
66
67
73#define L4_IPC_TIMEOUT_0 ((l4_timeout_s){0x0400})
74#define L4_IPC_TIMEOUT_NEVER ((l4_timeout_s){0})
75#define L4_IPC_NEVER_INITIALIZER {0}
76#define L4_IPC_NEVER ((l4_timeout_t){0})
77#define L4_IPC_RECV_TIMEOUT_0 ((l4_timeout_t){0x00000400})
78#define L4_IPC_SEND_TIMEOUT_0 ((l4_timeout_t){0x04000000})
79#define L4_IPC_BOTH_TIMEOUT_0 ((l4_timeout_t){0x04000400})
80
85#define L4_TIMEOUT_US_NEVER (~0ULL)
86
91#define L4_TIMEOUT_US_MAX ((1ULL << 41) - 1)
92
94
105l4_timeout_s l4_timeout_rel(unsigned man, unsigned exp) L4_NOTHROW;
106
107
118l4_timeout_t l4_ipc_timeout(unsigned snd_man, unsigned snd_exp,
119 unsigned rcv_man, unsigned rcv_exp) L4_NOTHROW;
120
132
142
152
163
164
175
187
200l4_timeout_s l4_timeout_from_us(l4_uint64_t us) L4_NOTHROW;
201
202/*
203 * Implementation
204 */
205
207l4_timeout_t l4_ipc_timeout(unsigned snd_man, unsigned snd_exp,
208 unsigned rcv_man, unsigned rcv_exp) L4_NOTHROW
209{
210 l4_uint16_t snd = (snd_man & 0x3ff) | ((snd_exp << 10) & 0x7c00);
211 l4_uint16_t rcv = (rcv_man & 0x3ff) | ((rcv_exp << 10) & 0x7c00);
212 return l4_timeout((l4_timeout_s){snd}, (l4_timeout_s){rcv});
213}
214
215
218{
219 return (l4_timeout_t){ ((l4_uint32_t){snd.t} << 16) | rcv.t };
220}
221
222
225{
226 to->p.snd = snd;
227}
228
229
232{
233 to->p.rcv = rcv;
234}
235
236
238l4_timeout_s l4_timeout_rel(unsigned man, unsigned exp) L4_NOTHROW
239{
240 return (l4_timeout_s){(l4_uint16_t)((man & 0x3ff) | ((exp << 10) & 0x7c00))};
241}
242
243
246{
247 if (to.t == 0)
248 return ~0ULL;
249 return (l4_kernel_clock_t)(to.t & 0x3ff) << ((to.t >> 10) & 0x1f);
250}
251
252
255{
256 return to.t & 0x8000;
257}
258
259
262{
264 return 0; /* We cannot retrieve the value ... */
265 else
266 return cur + l4_timeout_rel_get(to);
267}
268
270l4_timeout_s l4_timeout_from_us(l4_uint64_t us) L4_NOTHROW
271{
272 if (us == 0)
273 return L4_IPC_TIMEOUT_0;
274 else if (us == L4_TIMEOUT_US_NEVER || us > L4_TIMEOUT_US_MAX)
276 else
277 {
278 /* Here it is certain that at least one bit in 'us' is set. */
279
280 enum { m_max = 0x3ff, e_max = 0x1f, }; // max values also serve as mask
281
282 l4_uint16_t m = 0; // initialization required by constexpr, optimized away
283 l4_uint16_t v = 0; // initialization required by constexpr, optimized away
284 int e = (63 - __builtin_clzll(us)) - 9;
285 if (e < 0)
286 e = 0;
287
288 /* Here it is certain that '0 <= e <= 31' and '1 <= 2^e <= 2^31':
289 * L4_TIMEOUT_US_MAX = 2^41-1 = 0x000001ffffffffff => e = 31.
290 * Note: 2^41-1 (0x000001ffffffffff) > 1023*2^31 (0x00001ff800000000). */
291
292 /* Round up to next aligned timeout value to not retun too early. */
293 m = (us + ((1ULL << e) - 1)) >> e;
294 /* Bounds check */
295 if (m > m_max)
296 {
297 if (e < e_max)
298 {
299 ++e;
300 m >>= 1;
301 }
302 else
303 m = m_max;
304 }
305
306 /* Here it is certain that '1 <= m <= 1023. Consider the following cases:
307 * o 1 <= us <= 1023: e = 0; 2^e = 1; 1 <= us/1 <= 1023
308 * o 1024 <= us <= 2047: e = 1; 2^e = 2; 512 <= us/2 <= 1023
309 * o 2048 <= us <= 4095: e = 2; 2^e = 4; 512 <= us/4 <= 1023
310 * ...
311 * o 2^31 <= us <= 2^32-1: e = 22; 512 <= us/2^22 <= 1023
312 * o 2^40 <= us <= 2^41-1: e = 31; 512 <= us/2^31 <= 1023
313 *
314 * Dividing by (1<<e) ensures that for all us < 2^41: m < 2^10.
315 *
316 * Maximum possible timeout using this format: L4_TIMEOUT_US_MAX = 2^41-1:
317 * e = 31, m = 1023 => 2'196'875'771'904 us = 610h 14m 35s.
318 */
319
320 /* Without introducing 'v' we had to type-cast the expression to
321 * l4_uint16_t. This cannot be avoided by declaring m and e_pow_10 as
322 * l4_uint16_t due to C++ integer promotion. */
323 v = (e << 10) | m;
324 return (l4_timeout_s){v};
325 }
326}
327
328#endif
L4 compiler related defines.
l4_uint64_t l4_kernel_clock_t
Kernel clock type.
Definition l4int.h:53
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition l4int.h:29
unsigned short int l4_uint16_t
Unsigned 16bit value.
Definition l4int.h:27
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition l4int.h:31
L4_CONSTEXPR l4_timeout_s l4_timeout_rel(unsigned man, unsigned exp) L4_NOTHROW
Get relative timeout consisting of mantissa and exponent.
Definition __timeout.h:238
#define L4_IPC_TIMEOUT_NEVER
never timeout
Definition __timeout.h:74
L4_CONSTEXPR void l4_rcv_timeout(l4_timeout_s rcv, l4_timeout_t *to) L4_NOTHROW
Set receive timeout in given to timeout.
Definition __timeout.h:231
L4_CONSTEXPR l4_kernel_clock_t l4_timeout_get(l4_kernel_clock_t cur, l4_timeout_s to) L4_NOTHROW
Get clock value for a clock + a timeout.
Definition __timeout.h:261
#define L4_IPC_TIMEOUT_0
Timeout constants.
Definition __timeout.h:73
#define L4_TIMEOUT_US_NEVER
The waiting period in microseconds which is interpreted as "never" by l4_timeout_from_us().
Definition __timeout.h:85
L4_CONSTEXPR l4_timeout_t l4_ipc_timeout(unsigned snd_man, unsigned snd_exp, unsigned rcv_man, unsigned rcv_exp) L4_NOTHROW
Convert explicit timeout values to l4_timeout_t type.
Definition __timeout.h:207
L4_CONSTEXPR l4_timeout_t l4_timeout(l4_timeout_s snd, l4_timeout_s rcv) L4_NOTHROW
Combine send and receive timeout in a timeout.
Definition __timeout.h:217
L4_CONSTEXPR void l4_snd_timeout(l4_timeout_s snd, l4_timeout_t *to) L4_NOTHROW
Set send timeout in given to timeout.
Definition __timeout.h:224
#define L4_TIMEOUT_US_MAX
The longest waiting period in microseconds accepted by l4_timeout_from_us().
Definition __timeout.h:91
L4_CONSTEXPR unsigned l4_timeout_is_absolute(l4_timeout_s to) L4_NOTHROW
Return whether the given timeout is absolute or not.
Definition __timeout.h:254
L4_CONSTEXPR l4_kernel_clock_t l4_timeout_rel_get(l4_timeout_s to) L4_NOTHROW
Get clock value of out timeout.
Definition __timeout.h:245
#define L4_NOTHROW
Mark a function declaration and definition as never throwing an exception.
Definition compiler.h:161
#define L4_INLINE
L4 Inline function attribute.
Definition compiler.h:51
#define L4_CONSTEXPR
Constexpr function attribute.
Definition compiler.h:194
Fixed sized integer types, generic version.
Basic timeout specification.
Definition __timeout.h:40
l4_uint16_t t
timeout value
Definition __timeout.h:41
Timeout pair.
Definition __timeout.h:53
struct l4_timeout_t::@311374065204270174372252366326004043071275262153 p
combined timeout
l4_uint32_t raw
raw value
Definition __timeout.h:54
l4_timeout_s snd
send timeout
Definition __timeout.h:62
l4_timeout_s rcv
receive timeout
Definition __timeout.h:61