test41: relax maximum timer tick rate
[minix.git] / servers / inet / clock.c
blob42f59ce752132e5a595f471548188d8979d9ede5
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"
13 THIS_FILE
15 int clck_call_expire;
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);
25 void clck_init()
27 clck_call_expire= 0;
28 curr_time= 0;
29 prev_time= 0;
30 next_timeout= 0;
31 timer_chain= 0;
34 time_t get_time()
36 if (!curr_time)
38 if (getuptime(&curr_time) != OK)
39 ip_panic(("can't read clock"));
40 assert(curr_time >= prev_time);
42 return curr_time;
45 void set_time (tim)
46 time_t tim;
48 if (!curr_time && tim >= prev_time)
50 /* Some code assumes that no time elapses while it is
51 * running.
53 curr_time= tim;
55 else if (!curr_time)
57 DBLOCK(0x20, printf("set_time: new time %i < prev_time %i\n",
58 tim, prev_time));
62 void reset_time()
64 prev_time= curr_time;
65 curr_time= 0;
68 void clck_timer(timer, timeout, func, fd)
69 timer_t *timer;
70 time_t timeout;
71 timer_func_t func;
72 int fd;
74 timer_t *timer_index;
76 if (timer->tim_active)
77 clck_fast_release(timer);
78 assert(!timer->tim_active);
80 timer->tim_next= 0;
81 timer->tim_func= func;
82 timer->tim_ref= fd;
83 timer->tim_time= timeout;
84 timer->tim_active= 1;
86 if (!timer_chain)
87 timer_chain= timer;
88 else if (timeout < timer_chain->tim_time)
90 timer->tim_next= timer_chain;
91 timer_chain= timer;
93 else
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)
103 set_timer();
106 void clck_tick (mess)
107 message *mess;
109 next_timeout= 0;
110 set_timer();
113 static void clck_fast_release (timer)
114 timer_t *timer;
116 timer_t *timer_index;
118 if (!timer->tim_active)
119 return;
121 if (timer == timer_chain)
122 timer_chain= timer_chain->tim_next;
123 else
125 timer_index= timer_chain;
126 while (timer_index && timer_index->tim_next != timer)
127 timer_index= timer_index->tim_next;
128 assert(timer_index);
129 timer_index->tim_next= timer->tim_next;
131 timer->tim_active= 0;
134 static void set_timer()
136 time_t new_time;
137 time_t curr_time;
139 if (!timer_chain)
140 return;
142 curr_time= get_time();
143 new_time= timer_chain->tim_time;
144 if (new_time <= curr_time)
146 clck_call_expire= 1;
147 return;
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)
161 timer_t *timer;
163 clck_fast_release (timer);
164 set_timer();
167 void clck_expire_timers()
169 time_t curr_time;
170 timer_t *timer_index;
172 clck_call_expire= 0;
174 if (timer_chain == NULL)
175 return;
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);
186 set_timer();
190 * $PchId: clock.c,v 1.10 2005/06/28 14:23:40 philip Exp $