2 * \brief Timed event scheduler
4 * \author Norman Feske <norman.feske@genode-labs.com>
8 * Copyright (C) 2005-2009
9 * Genode Labs, Feske & Helmuth Systementwicklung GbR
11 * This file is part of the Genode OS framework, which is distributed
12 * under the terms of the GNU General Public License version 2.
15 #include <l4/scout-gfx/tick>
20 static Tick *head = 0; /* head of tick list */
21 static Tick::Time now = 0; /* recent time (updated by handle function) */
25 /* do not enqueue twice */
28 // printf("enqueue twice? ticks scheduled=%d\n", ticks_scheduled());
33 /* if ticklist is empty add first element */
41 /* if deadline is smaller than any other deadline, put it on the head */
42 if ((int)_deadline - (int)now < (int)head->_deadline - (int)now)
49 /* find list element with a higher deadline */
51 while (curr->_next && ((int)curr->_next->_deadline - (int)now < (int)_deadline - (int)now))
54 /* if end of list is reached, append new element */
61 /* insert element in middle of list */
78 /* find predecessor in tick queue */
80 for (curr = head; curr && (curr->_next != this); curr = curr->_next)
83 /* tick is not enqueued */
87 /* skip us in tick queue */
94 void Tick::schedule(Time period)
99 _deadline = now; /* first deadline is overdue */
105 int Tick::ticks_scheduled()
108 printf("now=%d\n", (int)now);
109 for (Tick *curr = head; curr; curr = curr->_next, num_ticks++)
110 printf("ticks_scheduled:\n %d: curr=%p, deadline=%d\n",
111 (int)num_ticks, curr, (int)curr->_deadline);
116 void Tick::handle(Time curr_time)
121 while ((curr = head) && ((int)head->_deadline - (int)now < 0)) {
123 /* remove tick from head of the list */
129 /* do not reschedule if tick function returns 0 */
130 if (!curr->on_tick()) continue;
132 /* schedule next event */
133 if (curr->_deadline == 0)
134 curr->_deadline = now;
136 curr->_deadline += curr->_period;