add/re-enable at_wini debug output
[minix3.git] / servers / inet / clock.c
blob24a85982d168b6107760f28aab6c043702dece38
1 /*
2 clock.c
4 Copyright 1995 Philip Homburg
5 */
7 #include "inet.h"
8 #include "proto.h"
9 #include "generic/assert.h"
10 #include "generic/buf.h"
11 #include "generic/clock.h"
12 #include "generic/type.h"
14 THIS_FILE
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;
22 #ifdef __minix_vmd
23 PRIVATE int clck_tasknr= ANY;
24 #endif
26 FORWARD _PROTOTYPE( void clck_fast_release, (timer_t *timer) );
27 FORWARD _PROTOTYPE( void set_timer, (void) );
29 PUBLIC void clck_init()
31 int r;
33 clck_call_expire= 0;
34 curr_time= 0;
35 prev_time= 0;
36 next_timeout= 0;
37 timer_chain= 0;
39 #ifdef __minix_vmd
40 r= sys_findproc(CLOCK_NAME, &clck_tasknr, 0);
41 if (r != OK)
42 ip_panic(( "unable to find clock task: %d\n", r ));
43 #endif
46 PUBLIC time_t get_time()
48 if (!curr_time)
50 #ifdef __minix_vmd
51 static message mess;
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;
59 #else /* Minix 3 */
60 int s;
61 if ((s=getuptime(&curr_time)) != OK)
62 ip_panic(("can't read clock"));
63 #endif
64 assert(curr_time >= prev_time);
66 return curr_time;
69 PUBLIC void set_time (tim)
70 time_t tim;
72 if (!curr_time && tim >= prev_time)
74 /* Some code assumes that no time elapses while it is
75 * running.
77 curr_time= tim;
79 else if (!curr_time)
81 DBLOCK(0x20, printf("set_time: new time %ld < prev_time %ld\n",
82 tim, prev_time));
86 PUBLIC void reset_time()
88 prev_time= curr_time;
89 curr_time= 0;
92 PUBLIC void clck_timer(timer, timeout, func, fd)
93 timer_t *timer;
94 time_t timeout;
95 timer_func_t func;
96 int fd;
98 timer_t *timer_index;
100 if (timer->tim_active)
101 clck_fast_release(timer);
102 assert(!timer->tim_active);
104 timer->tim_next= 0;
105 timer->tim_func= func;
106 timer->tim_ref= fd;
107 timer->tim_time= timeout;
108 timer->tim_active= 1;
110 if (!timer_chain)
111 timer_chain= timer;
112 else if (timeout < timer_chain->tim_time)
114 timer->tim_next= timer_chain;
115 timer_chain= timer;
117 else
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)
127 set_timer();
130 PUBLIC void clck_tick (mess)
131 message *mess;
133 next_timeout= 0;
134 set_timer();
137 PRIVATE void clck_fast_release (timer)
138 timer_t *timer;
140 timer_t *timer_index;
142 if (!timer->tim_active)
143 return;
145 if (timer == timer_chain)
146 timer_chain= timer_chain->tim_next;
147 else
149 timer_index= timer_chain;
150 while (timer_index && timer_index->tim_next != timer)
151 timer_index= timer_index->tim_next;
152 assert(timer_index);
153 timer_index->tim_next= timer->tim_next;
155 timer->tim_active= 0;
158 PRIVATE void set_timer()
160 time_t new_time;
161 time_t curr_time;
163 if (!timer_chain)
164 return;
166 curr_time= get_time();
167 new_time= timer_chain->tim_time;
168 if (new_time <= curr_time)
170 clck_call_expire= 1;
171 return;
174 if (next_timeout == 0 || new_time < next_timeout)
176 #ifdef __minix_vmd
177 static message mess;
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"));
190 #else /* Minix 3 */
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"));
196 #endif
200 PUBLIC void clck_untimer (timer)
201 timer_t *timer;
203 clck_fast_release (timer);
204 set_timer();
207 PUBLIC void clck_expire_timers()
209 time_t curr_time;
210 timer_t *timer_index;
212 clck_call_expire= 0;
214 if (timer_chain == NULL)
215 return;
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);
226 set_timer();
230 * $PchId: clock.c,v 1.10 2005/06/28 14:23:40 philip Exp $