forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.c
More file actions
191 lines (162 loc) · 3.2 KB
/
Copy pathclock.c
File metadata and controls
191 lines (162 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
clock.c
Copyright 1995 Philip Homburg
*/
#include "inet.h"
#include "proto.h"
#include "generic/assert.h"
#include "generic/buf.h"
#include "generic/clock.h"
THIS_FILE
int clck_call_expire;
static clock_t curr_time;
static clock_t prev_time;
static minix_timer_t *timer_chain;
static time_t next_timeout;
static void clck_fast_release(minix_timer_t *timer);
static void set_timer(void);
void clck_init()
{
clck_call_expire= 0;
curr_time= 0;
prev_time= 0;
next_timeout= 0;
timer_chain= 0;
}
time_t get_time()
{
if (!curr_time)
{
if (getticks(&curr_time) != OK)
ip_panic(("can't read clock"));
assert(curr_time >= prev_time);
}
return curr_time;
}
void set_time (tim)
clock_t tim;
{
if (!curr_time && tim >= prev_time)
{
/* Some code assumes that no time elapses while it is
* running.
*/
curr_time= tim;
}
else if (!curr_time)
{
DBLOCK(0x20, printf("set_time: new time %lu < prev_time %lu\n",
tim, prev_time));
}
}
void reset_time()
{
prev_time= curr_time;
curr_time= 0;
}
void clck_timer(timer, timeout, func, fd)
minix_timer_t *timer;
time_t timeout;
timer_func_t func;
int fd;
{
minix_timer_t *timer_index;
if (timer->tim_active)
clck_fast_release(timer);
assert(!timer->tim_active);
timer->tim_next= 0;
timer->tim_func= func;
timer->tim_ref= fd;
timer->tim_time= timeout;
timer->tim_active= 1;
if (!timer_chain)
timer_chain= timer;
else if (timeout < timer_chain->tim_time)
{
timer->tim_next= timer_chain;
timer_chain= timer;
}
else
{
timer_index= timer_chain;
while (timer_index->tim_next &&
timer_index->tim_next->tim_time < timeout)
timer_index= timer_index->tim_next;
timer->tim_next= timer_index->tim_next;
timer_index->tim_next= timer;
}
if (next_timeout == 0 || timer_chain->tim_time < next_timeout)
set_timer();
}
void clck_tick (mess)
message *mess;
{
next_timeout= 0;
set_timer();
}
static void clck_fast_release (timer)
minix_timer_t *timer;
{
minix_timer_t *timer_index;
if (!timer->tim_active)
return;
if (timer == timer_chain)
timer_chain= timer_chain->tim_next;
else
{
timer_index= timer_chain;
while (timer_index && timer_index->tim_next != timer)
timer_index= timer_index->tim_next;
assert(timer_index);
timer_index->tim_next= timer->tim_next;
}
timer->tim_active= 0;
}
static void set_timer()
{
time_t new_time;
time_t now;
if (!timer_chain)
return;
now= get_time();
new_time= timer_chain->tim_time;
if (new_time <= now)
{
clck_call_expire= 1;
return;
}
if (next_timeout == 0 || new_time < next_timeout)
{
next_timeout= new_time;
new_time -= now;
if (sys_setalarm(new_time, 0) != OK)
ip_panic(("can't set timer"));
}
}
void clck_untimer (timer)
minix_timer_t *timer;
{
clck_fast_release (timer);
set_timer();
}
void clck_expire_timers()
{
time_t now;
minix_timer_t *timer_index;
clck_call_expire= 0;
if (timer_chain == NULL)
return;
now= get_time();
while (timer_chain && timer_chain->tim_time<=now)
{
assert(timer_chain->tim_active);
timer_chain->tim_active= 0;
timer_index= timer_chain;
timer_chain= timer_chain->tim_next;
(*timer_index->tim_func)(timer_index->tim_ref, timer_index);
}
set_timer();
}
/*
* $PchId: clock.c,v 1.10 2005/06/28 14:23:40 philip Exp $
*/