4 Copyright 1995 Philip Homburg
9 #include "generic/assert.h"
10 #include "generic/buf.h"
11 #include "generic/clock.h"
17 static time_t curr_time
;
18 static time_t prev_time
;
19 static timer_t
*timer_chain
;
20 static time_t next_timeout
;
22 static void clck_fast_release(timer_t
*timer
);
23 static void set_timer(void);
38 if (getuptime(&curr_time
) != OK
)
39 ip_panic(("can't read clock"));
40 assert(curr_time
>= prev_time
);
48 if (!curr_time
&& tim
>= prev_time
)
50 /* Some code assumes that no time elapses while it is
57 DBLOCK(0x20, printf("set_time: new time %i < prev_time %i\n",
68 void clck_timer(timer
, timeout
, func
, fd
)
76 if (timer
->tim_active
)
77 clck_fast_release(timer
);
78 assert(!timer
->tim_active
);
81 timer
->tim_func
= func
;
83 timer
->tim_time
= timeout
;
88 else if (timeout
< timer_chain
->tim_time
)
90 timer
->tim_next
= timer_chain
;
95 timer_index
= timer_chain
;
96 while (timer_index
->tim_next
&&
97 timer_index
->tim_next
->tim_time
< timeout
)
98 timer_index
= timer_index
->tim_next
;
99 timer
->tim_next
= timer_index
->tim_next
;
100 timer_index
->tim_next
= timer
;
102 if (next_timeout
== 0 || timer_chain
->tim_time
< next_timeout
)
106 void clck_tick (mess
)
113 static void clck_fast_release (timer
)
116 timer_t
*timer_index
;
118 if (!timer
->tim_active
)
121 if (timer
== timer_chain
)
122 timer_chain
= timer_chain
->tim_next
;
125 timer_index
= timer_chain
;
126 while (timer_index
&& timer_index
->tim_next
!= timer
)
127 timer_index
= timer_index
->tim_next
;
129 timer_index
->tim_next
= timer
->tim_next
;
131 timer
->tim_active
= 0;
134 static void set_timer()
142 curr_time
= get_time();
143 new_time
= timer_chain
->tim_time
;
144 if (new_time
<= curr_time
)
150 if (next_timeout
== 0 || new_time
< next_timeout
)
152 next_timeout
= new_time
;
153 new_time
-= curr_time
;
155 if (sys_setalarm(new_time
, 0) != OK
)
156 ip_panic(("can't set timer"));
160 void clck_untimer (timer
)
163 clck_fast_release (timer
);
167 void clck_expire_timers()
170 timer_t
*timer_index
;
174 if (timer_chain
== NULL
)
177 curr_time
= get_time();
178 while (timer_chain
&& timer_chain
->tim_time
<=curr_time
)
180 assert(timer_chain
->tim_active
);
181 timer_chain
->tim_active
= 0;
182 timer_index
= timer_chain
;
183 timer_chain
= timer_chain
->tim_next
;
184 (*timer_index
->tim_func
)(timer_index
->tim_ref
, timer_index
);
190 * $PchId: clock.c,v 1.10 2005/06/28 14:23:40 philip Exp $