20#ifndef _COBALT_KERNEL_TIMER_H
21#define _COBALT_KERNEL_TIMER_H
23#include <cobalt/kernel/clock.h>
24#include <cobalt/kernel/stat.h>
25#include <cobalt/kernel/list.h>
26#include <cobalt/kernel/assert.h>
27#include <cobalt/kernel/ancillaries.h>
28#include <asm/xenomai/wrappers.h>
34#define XN_INFINITE ((xnticks_t)0)
35#define XN_NONBLOCK ((xnticks_t)-1)
45#define XNTIMER_DEQUEUED 0x00000001
46#define XNTIMER_KILLED 0x00000002
47#define XNTIMER_PERIODIC 0x00000004
48#define XNTIMER_REALTIME 0x00000008
49#define XNTIMER_FIRED 0x00000010
50#define XNTIMER_RUNNING 0x00000020
51#define XNTIMER_KGRAVITY 0x00000040
52#define XNTIMER_UGRAVITY 0x00000080
53#define XNTIMER_IGRAVITY 0
55#define XNTIMER_GRAVITY_MASK (XNTIMER_KGRAVITY|XNTIMER_UGRAVITY)
56#define XNTIMER_INIT_MASK XNTIMER_GRAVITY_MASK
59#define XNTIMER_SPARE0 0x01000000
60#define XNTIMER_SPARE1 0x02000000
61#define XNTIMER_SPARE2 0x04000000
62#define XNTIMER_SPARE3 0x08000000
63#define XNTIMER_SPARE4 0x10000000
64#define XNTIMER_SPARE5 0x20000000
65#define XNTIMER_SPARE6 0x40000000
66#define XNTIMER_SPARE7 0x80000000
69#define XNTIMER_LOPRIO (-999999999)
70#define XNTIMER_STDPRIO 0
71#define XNTIMER_HIPRIO 999999999
74 struct list_head link;
79#define xntlholder_date(h) ((h)->key)
80#define xntlholder_prio(h) ((h)->prio)
81#define xntlist_init(q) INIT_LIST_HEAD(q)
82#define xntlist_empty(q) list_empty(q)
84static inline struct xntlholder *xntlist_head(
struct list_head *q)
89 return list_first_entry(q,
struct xntlholder, link);
92static inline struct xntlholder *xntlist_next(
struct list_head *q,
95 if (list_is_last(&h->link, q))
98 return list_entry(h->link.next,
struct xntlholder, link);
101static inline struct xntlholder *xntlist_second(
struct list_head *q,
102 struct xntlholder *h)
104 return xntlist_next(q, h);
107static inline void xntlist_insert(
struct list_head *q,
struct xntlholder *holder)
109 struct xntlholder *p;
112 list_add(&holder->link, q);
121 list_for_each_entry_reverse(p, q, link) {
122 if ((xnsticks_t) (holder->key - p->key) > 0 ||
123 (holder->key == p->key && holder->prio <= p->prio))
127 list_add(&holder->link, &p->link);
130#define xntlist_remove(q, h) \
133 list_del(&(h)->link); \
136#if defined(CONFIG_XENO_OPT_TIMER_RBTREE)
138#include <linux/rbtree.h>
141 unsigned long long date;
146#define xntimerh_date(h) ((h)->date)
147#define xntimerh_prio(h) ((h)->prio)
148#define xntimerh_init(h) do { } while (0)
155#define xntimerq_init(q) \
157 xntimerq_t *_q = (q); \
158 _q->root = RB_ROOT; \
162#define xntimerq_destroy(q) do { } while (0)
163#define xntimerq_empty(q) ((q)->head == NULL)
165#define xntimerq_head(q) ((q)->head)
167#define xntimerq_next(q, h) \
169 struct rb_node *_node = rb_next(&(h)->link); \
170 _node ? (container_of(_node, xntimerh_t, link)) : NULL; \
173#define xntimerq_second(q, h) xntimerq_next(q, h)
175void xntimerq_insert(xntimerq_t *q, xntimerh_t *holder);
177static inline void xntimerq_remove(xntimerq_t *q, xntimerh_t *holder)
179 if (holder == q->head)
180 q->head = xntimerq_second(q, holder);
182 rb_erase(&holder->link, &q->root);
185typedef struct { } xntimerq_it_t;
187#define xntimerq_it_begin(q,i) ((void) (i), xntimerq_head(q))
188#define xntimerq_it_next(q,i,h) ((void) (i), xntimerq_next((q),(h)))
192typedef struct xntlholder xntimerh_t;
194#define xntimerh_date(h) xntlholder_date(h)
195#define xntimerh_prio(h) xntlholder_prio(h)
196#define xntimerh_init(h) do { } while (0)
198typedef struct list_head xntimerq_t;
200#define xntimerq_init(q) xntlist_init(q)
201#define xntimerq_destroy(q) do { } while (0)
202#define xntimerq_empty(q) xntlist_empty(q)
203#define xntimerq_head(q) xntlist_head(q)
204#define xntimerq_second(q, h) xntlist_second((q),(h))
205#define xntimerq_insert(q, h) xntlist_insert((q),(h))
206#define xntimerq_remove(q, h) xntlist_remove((q),(h))
208typedef struct { } xntimerq_it_t;
210#define xntimerq_it_begin(q,i) ((void) (i), xntlist_head(q))
211#define xntimerq_it_next(q,i,h) ((void) (i), xntlist_next((q),(h)))
221static inline struct xntimerdata *
222xnclock_percpu_timerdata(
struct xnclock *clock,
int cpu)
224 return per_cpu_ptr(clock->timerdata, cpu);
227static inline struct xntimerdata *
228xnclock_this_timerdata(
struct xnclock *clock)
230 return raw_cpu_ptr(clock->timerdata);
234#ifdef CONFIG_XENO_OPT_EXTCLOCK
235 struct xnclock *clock;
239 struct list_head adjlink;
241 unsigned long status;
245 xnticks_t interval_ns;
247 xnticks_t periodic_ticks;
249 xnticks_t start_date;
251 xnticks_t pexpect_ticks;
255 void (*handler)(
struct xntimer *timer);
256#ifdef CONFIG_XENO_OPT_STATS
257#ifdef CONFIG_XENO_OPT_EXTCLOCK
258 struct xnclock *tracker;
261 char name[XNOBJECT_NAME_LEN];
263 struct list_head next_stat;
265 xnstat_counter_t scheduled;
267 xnstat_counter_t fired;
271#ifdef CONFIG_XENO_OPT_EXTCLOCK
273static inline struct xnclock *xntimer_clock(
struct xntimer *timer)
278void xntimer_set_clock(
struct xntimer *timer,
279 struct xnclock *newclock);
283static inline struct xnclock *xntimer_clock(
struct xntimer *timer)
288static inline void xntimer_set_clock(
struct xntimer *timer,
289 struct xnclock *newclock)
291 XENO_BUG_ON(COBALT, newclock != &nkclock);
297static inline struct xnsched *xntimer_sched(
struct xntimer *timer)
302#define xntimer_sched(t) xnsched_current()
305#define xntimer_percpu_queue(__timer) \
307 struct xntimerdata *tmd; \
308 int cpu = xnsched_cpu((__timer)->sched); \
309 tmd = xnclock_percpu_timerdata(xntimer_clock(__timer), cpu); \
313static inline unsigned long xntimer_gravity(
struct xntimer *timer)
315 struct xnclock *clock = xntimer_clock(timer);
317 if (timer->status & XNTIMER_KGRAVITY)
318 return clock->gravity.kernel;
320 if (timer->status & XNTIMER_UGRAVITY)
321 return clock->gravity.user;
323 return clock->gravity.irq;
326static inline void xntimer_update_date(
struct xntimer *timer)
328 xntimerh_date(&timer->aplink) = timer->start_date
329 + xnclock_ns_to_ticks(xntimer_clock(timer),
330 timer->periodic_ticks * timer->interval_ns)
331 - xntimer_gravity(timer);
334static inline xnticks_t xntimer_pexpect(
struct xntimer *timer)
336 return timer->start_date +
337 xnclock_ns_to_ticks(xntimer_clock(timer),
338 timer->pexpect_ticks * timer->interval_ns);
341static inline void xntimer_set_priority(
struct xntimer *timer,
344 xntimerh_prio(&timer->aplink) = prio;
347static inline int xntimer_active_p(
struct xntimer *timer)
349 return timer->sched != NULL;
352static inline int xntimer_running_p(
struct xntimer *timer)
354 return (timer->status & XNTIMER_RUNNING) != 0;
357static inline int xntimer_fired_p(
struct xntimer *timer)
359 return (timer->status & XNTIMER_FIRED) != 0;
362static inline int xntimer_periodic_p(
struct xntimer *timer)
364 return (timer->status & XNTIMER_PERIODIC) != 0;
367void __xntimer_init(
struct xntimer *timer,
368 struct xnclock *clock,
369 void (*handler)(
struct xntimer *timer),
373void xntimer_set_gravity(
struct xntimer *timer,
376#ifdef CONFIG_XENO_OPT_STATS
378#define xntimer_init(__timer, __clock, __handler, __sched, __flags) \
380 __xntimer_init(__timer, __clock, __handler, __sched, __flags); \
381 xntimer_set_name(__timer, #__handler); \
384static inline void xntimer_reset_stats(
struct xntimer *timer)
386 xnstat_counter_set(&timer->scheduled, 0);
387 xnstat_counter_set(&timer->fired, 0);
390static inline void xntimer_account_scheduled(
struct xntimer *timer)
392 xnstat_counter_inc(&timer->scheduled);
395static inline void xntimer_account_fired(
struct xntimer *timer)
397 xnstat_counter_inc(&timer->fired);
400static inline void xntimer_set_name(
struct xntimer *timer,
const char *name)
402 knamecpy(timer->name, name);
407#define xntimer_init __xntimer_init
409static inline void xntimer_reset_stats(
struct xntimer *timer) { }
411static inline void xntimer_account_scheduled(
struct xntimer *timer) { }
413static inline void xntimer_account_fired(
struct xntimer *timer) { }
415static inline void xntimer_set_name(
struct xntimer *timer,
const char *name) { }
419#if defined(CONFIG_XENO_OPT_EXTCLOCK) && defined(CONFIG_XENO_OPT_STATS)
420void xntimer_switch_tracking(
struct xntimer *timer,
421 struct xnclock *newclock);
424void xntimer_switch_tracking(
struct xntimer *timer,
425 struct xnclock *newclock) { }
447 return timer->interval_ns;
450static inline xnticks_t xntimer_expiry(
struct xntimer *timer)
453 return xntimerh_date(&timer->aplink) + xntimer_gravity(timer);
461void __xntimer_stop(
struct xntimer *timer);
465xnticks_t __xntimer_get_timeout(
struct xntimer *timer);
467xnticks_t xntimer_get_interval(
struct xntimer *timer);
469int xntimer_heading_p(
struct xntimer *timer);
473 if (timer->status & XNTIMER_RUNNING)
474 __xntimer_stop(timer);
479 if (!xntimer_running_p(timer))
482 return __xntimer_get_timeout(timer);
485static inline xnticks_t xntimer_get_timeout_stopped(
struct xntimer *timer)
487 return __xntimer_get_timeout(timer);
490static inline void xntimer_enqueue(
struct xntimer *timer,
493 xntimerq_insert(q, &timer->aplink);
494 timer->status &= ~XNTIMER_DEQUEUED;
495 xntimer_account_scheduled(timer);
498static inline void xntimer_dequeue(
struct xntimer *timer,
501 xntimerq_remove(q, &timer->aplink);
502 timer->status |= XNTIMER_DEQUEUED;
506 struct xnthread *waiter,
514void xntimer_migrate(
struct xntimer *timer,
struct xnsched *sched)
516 if (timer->sched != sched)
520void __xntimer_set_affinity(
struct xntimer *timer,
523static inline void xntimer_set_affinity(
struct xntimer *timer,
526 if (sched != xntimer_sched(timer))
527 __xntimer_set_affinity(timer, sched);
532static inline void xntimer_migrate(
struct xntimer *timer,
535 timer->sched = sched;
538static inline void xntimer_set_affinity(
struct xntimer *timer,
541 xntimer_migrate(timer, sched);
546char *xntimer_format_time(xnticks_t ns,
547 char *buf,
size_t bufsz);
unsigned long long xntimer_get_overruns(struct xntimer *timer, struct xnthread *waiter, xnticks_t now)
Get the count of overruns for the last tick.
Definition timer.c:612
xnticks_t xntimer_get_date(struct xntimer *timer)
Return the absolute expiration date.
Definition timer.c:247
int xntimer_start(struct xntimer *timer, xnticks_t value, xnticks_t interval, xntmode_t mode)
Arm a timer.
Definition timer.c:114
static void xntimer_stop(struct xntimer *timer)
Disarm a timer.
Definition timer.h:471
void __xntimer_migrate(struct xntimer *timer, struct xnsched *sched)
Migrate a timer.
Definition timer.c:507
void xntimer_destroy(struct xntimer *timer)
Release a timer object.
Definition timer.c:473
static xnticks_t xntimer_interval(struct xntimer *timer)
Return the timer interval value.
Definition timer.h:445
static xnticks_t xntimer_get_timeout(struct xntimer *timer)
Return the relative expiration date.
Definition timer.h:477
Scheduling information structure.
Definition sched.h:64