1 /* clock.c */
2
3 #include "inet.h"
4 #include "proto.h"
5 #include "generic/assert.h"
6 #include "generic/buf.h"
7 #include "generic/clock.h"
8 #include "generic/type.h"
9
10 INIT_PANIC();
11
12 FORWARD _PROTOTYPE( void clck_fast_release, (timer_t *timer) );
13 FORWARD _PROTOTYPE( void set_timer, (void) );
14
15 PRIVATE time_t curr_time;
16 PRIVATE timer_t *timer_chain;
17 PRIVATE time_t next_timeout;
18
19 PUBLIC time_t get_time()
20 {
21 if (!curr_time)
22 {
23 static message mess;
24
25 mess.m_type= GET_UPTIME;
26 if (sendrec (CLOCK, &mess) < 0)
27 ip_panic(("unable to sendrec"));
28 if (mess.m_type != OK)
29 ip_panic(("can't read clock"));
30 curr_time= mess.NEW_TIME;
31 }
32 return curr_time;
33 }
34
35 PUBLIC void set_time (tim)
36 time_t tim;
37 {
38 curr_time= tim;
39 }
40
41 PUBLIC void clck_init()
42 {
43 curr_time= 0;
44 next_timeout= 0;
45 timer_chain= 0;
46 }
47
48 PUBLIC void reset_time()
49 {
50 curr_time= 0;
51 }
52
53 PUBLIC void clck_timer(timer, timeout, func, fd)
54 timer_t *timer;
55 time_t timeout;
56 timer_func_t func;
57 int fd;
58 {
59 timer_t *timer_index;
60
61 #if DEBUG & 256
62 { time_t curr_tim= get_time(); where();
63 printf("clck_timer(0x%x, now%c%d HZ, 0x%x, %d)\n", timer,
64 timeout >= curr_tim ? '+' : '-',
65 timeout >= curr_tim ? timeout - curr_tim : curr_tim - timeout,
66 func, fd); }
67 #endif
68 clck_fast_release(timer);
69 timer->tim_next= 0;
70 timer->tim_func= func;
71 timer->tim_ref= fd;
72 timer->tim_time= timeout;
73
74 if (!timer_chain)
75 timer_chain= timer;
76 else if (timeout < timer_chain->tim_time)
77 {
78 timer->tim_next= timer_chain;
79 timer_chain= timer;
80 }
81 else
82 {
83 timer_index= timer_chain;
84 while (timer_index->tim_next &&
85 timer_index->tim_next->tim_time < timeout)
86 timer_index= timer_index->tim_next;
87 timer->tim_next= timer_index->tim_next;
88 timer_index->tim_next= timer;
89 }
90 if (timer_chain->tim_time != next_timeout)
91 set_timer();
92 }
93
94 PUBLIC void clck_tick (mess)
95 message *mess;
96 {
97 #if DEBUG & 256
98 { where(); printf("in clck_tick()\n"); }
99 #endif
100 next_timeout= 0;
101 set_timer();
102 }
103
104 PRIVATE void clck_fast_release (timer)
105 timer_t *timer;
106 {
107 timer_t *timer_index;
108
109 if (timer == timer_chain)
110 timer_chain= timer_chain->tim_next;
111 else
112 {
113 timer_index= timer_chain;
114 while (timer_index && timer_index->tim_next != timer)
115 timer_index= timer_index->tim_next;
116 if (timer_index)
117 timer_index->tim_next= timer->tim_next;
118 }
119 }
120
121 PRIVATE void set_timer()
122 {
123 time_t new_time;
124 time_t curr_time;
125 timer_t *timer_index;
126
127 #if DEBUG & 256
128 { where(); printf("in set_timer()\n"); }
129 #endif
130 curr_time= get_time();
131
132 while (timer_chain && timer_chain->tim_time<=curr_time)
133 {
134 timer_index= timer_chain;
135 timer_chain= timer_chain->tim_next;
136 #if DEBUG & 256
137 { where(); printf("calling tim_func: 0x%x(%d, ..)\n",
138 timer_index->tim_func, timer_index->tim_ref); }
139 #endif
140 (*timer_index->tim_func)(timer_index->tim_ref, timer_index);
141 }
142 if (timer_chain)
143 new_time= timer_chain->tim_time;
144 else
145 new_time= 0;
146 if (new_time != next_timeout)
147 {
148 static message mess;
149
150 next_timeout= new_time;
151 assert (!new_time || new_time > curr_time);
152
153 if (new_time)
154 new_time -= curr_time;
155
156 mess.m_type= SET_SYNC_AL;
157 mess.CLOCK_PROC_NR= THIS_PROC;
158 mess.DELTA_TICKS= new_time;
159 if (sendrec (CLOCK, &mess) < 0)
160 ip_panic(("unable to sendrec"));
161 if (mess.m_type != OK)
162 ip_panic(("can't set timer"));
163 }
164 }
165
166 void clck_untimer (timer)
167 timer_t *timer;
168 {
169 clck_fast_release (timer);
170 set_timer();
171 }
172
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.