4 Copyright 1995 Philip Homburg
9 #include "generic/assert.h"
10 #include "generic/buf.h"
11 #include "generic/clock.h"
12 #include "generic/type.h"
16 PUBLIC
int clck_call_expire
;
18 PRIVATE
time_t curr_time
;
19 PRIVATE
time_t prev_time
;
20 PRIVATE timer_t
*timer_chain
;
21 PRIVATE
time_t next_timeout
;
23 PRIVATE
int clck_tasknr
= ANY
;
26 FORWARD
_PROTOTYPE( void clck_fast_release
, (timer_t
*timer
) );
27 FORWARD
_PROTOTYPE( void set_timer
, (void) );
29 PUBLIC
void clck_init()
40 r
= sys_findproc(CLOCK_NAME
, &clck_tasknr
, 0);
42 ip_panic(( "unable to find clock task: %d\n", r
));
46 PUBLIC
time_t get_time()
53 mess
.m_type
= GET_UPTIME
;
54 if (sendrec (clck_tasknr
, &mess
) < 0)
55 ip_panic(("unable to sendrec"));
56 if (mess
.m_type
!= OK
)
57 ip_panic(("can't read clock"));
58 curr_time
= mess
.NEW_TIME
;
61 if ((s
=getuptime(&curr_time
)) != OK
)
62 ip_panic(("can't read clock"));
64 assert(curr_time
>= prev_time
);
69 PUBLIC
void set_time (tim
)
72 if (!curr_time
&& tim
>= prev_time
)
74 /* Some code assumes that no time elapses while it is
81 DBLOCK(0x20, printf("set_time: new time %ld < prev_time %ld\n",
86 PUBLIC
void reset_time()
92 PUBLIC
void clck_timer(timer
, timeout
, func
, fd
)
100 if (timer
->tim_active
)
101 clck_fast_release(timer
);
102 assert(!timer
->tim_active
);
105 timer
->tim_func
= func
;
107 timer
->tim_time
= timeout
;
108 timer
->tim_active
= 1;
112 else if (timeout
< timer_chain
->tim_time
)
114 timer
->tim_next
= timer_chain
;
119 timer_index
= timer_chain
;
120 while (timer_index
->tim_next
&&
121 timer_index
->tim_next
->tim_time
< timeout
)
122 timer_index
= timer_index
->tim_next
;
123 timer
->tim_next
= timer_index
->tim_next
;
124 timer_index
->tim_next
= timer
;
126 if (next_timeout
== 0 || timer_chain
->tim_time
< next_timeout
)
130 PUBLIC
void clck_tick (mess
)
137 PRIVATE
void clck_fast_release (timer
)
140 timer_t
*timer_index
;
142 if (!timer
->tim_active
)
145 if (timer
== timer_chain
)
146 timer_chain
= timer_chain
->tim_next
;
149 timer_index
= timer_chain
;
150 while (timer_index
&& timer_index
->tim_next
!= timer
)
151 timer_index
= timer_index
->tim_next
;
153 timer_index
->tim_next
= timer
->tim_next
;
155 timer
->tim_active
= 0;
158 PRIVATE
void set_timer()
166 curr_time
= get_time();
167 new_time
= timer_chain
->tim_time
;
168 if (new_time
<= curr_time
)
174 if (next_timeout
== 0 || new_time
< next_timeout
)
179 next_timeout
= new_time
;
181 new_time
-= curr_time
;
183 mess
.m_type
= SET_SYNC_AL
;
184 mess
.CLOCK_PROC_NR
= this_proc
;
185 mess
.DELTA_TICKS
= new_time
;
186 if (sendrec (clck_tasknr
, &mess
) < 0)
187 ip_panic(("unable to sendrec"));
188 if (mess
.m_type
!= OK
)
189 ip_panic(("can't set timer"));
191 next_timeout
= new_time
;
192 new_time
-= curr_time
;
194 if (sys_setalarm(new_time
, 0) != OK
)
195 ip_panic(("can't set timer"));
200 PUBLIC
void clck_untimer (timer
)
203 clck_fast_release (timer
);
207 PUBLIC
void clck_expire_timers()
210 timer_t
*timer_index
;
214 if (timer_chain
== NULL
)
217 curr_time
= get_time();
218 while (timer_chain
&& timer_chain
->tim_time
<=curr_time
)
220 assert(timer_chain
->tim_active
);
221 timer_chain
->tim_active
= 0;
222 timer_index
= timer_chain
;
223 timer_chain
= timer_chain
->tim_next
;
224 (*timer_index
->tim_func
)(timer_index
->tim_ref
, timer_index
);
230 * $PchId: clock.c,v 1.10 2005/06/28 14:23:40 philip Exp $