Merge branch 'vendor/GCC44'
[dragonfly.git] / contrib / amd / amd / clock.c
1 /*
2  * Copyright (c) 1997-1999 Erez Zadok
3  * Copyright (c) 1989 Jan-Simon Pendry
4  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5  * Copyright (c) 1989 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry at Imperial College, London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgment:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      %W% (Berkeley) %G%
40  *
41  * $Id: clock.c,v 1.3 1999/01/13 23:30:58 ezk Exp $
42  *
43  */
44
45 /*
46  * Callouts.
47  *
48  * Modeled on kernel object of the same name.
49  * See usual references.
50  *
51  * Use of a heap-based mechanism was rejected:
52  * 1.  more complex implementation needed.
53  * 2.  not obvious that a list is too slow for Amd.
54  */
55
56 #ifdef HAVE_CONFIG_H
57 # include <config.h>
58 #endif /* HAVE_CONFIG_H */
59 #include <am_defs.h>
60 #include <amd.h>
61
62 int timeout(u_int secs, void (*fn) (voidp), voidp closure);
63 void reschedule_timeouts(time_t now, time_t then);
64
65 typedef struct callout callout;
66 struct callout {
67   callout *c_next;              /* List of callouts */
68   void (*c_fn) (voidp);         /* Function to call */
69   voidp c_closure;              /* Closure to pass to call */
70   time_t c_time;                /* Time of call */
71   int c_id;                     /* Unique identifier */
72 };
73
74 static callout callouts;        /* List of pending callouts */
75 static callout *free_callouts;  /* Cache of free callouts */
76 static int nfree_callouts;      /* Number on free list */
77 static int callout_id;          /* Next free callout identifier */
78
79 time_t next_softclock;          /* Time of next call to softclock() */
80
81
82 /*
83  * Number of callout slots we keep on the free list
84  */
85 #define CALLOUT_FREE_SLOP       10
86
87 /*
88  * Global assumption: valid id's are non-zero.
89  */
90 #define CID_ALLOC(struct )      (++callout_id)
91 #define CID_UNDEF               (0)
92
93
94 static callout *
95 alloc_callout(void)
96 {
97   callout *cp = free_callouts;
98
99   if (cp) {
100     --nfree_callouts;
101     free_callouts = free_callouts->c_next;
102     return cp;
103   }
104   return ALLOC(struct callout);
105 }
106
107
108 static void
109 free_callout(callout *cp)
110 {
111   if (nfree_callouts > CALLOUT_FREE_SLOP) {
112     XFREE(cp);
113   } else {
114     cp->c_next = free_callouts;
115     free_callouts = cp;
116     nfree_callouts++;
117   }
118 }
119
120
121 /*
122  * Schedule a callout.
123  *
124  * (*fn)(closure) will be called at clocktime() + secs
125  */
126 int
127 timeout(u_int secs, void (*fn) (voidp), voidp closure)
128 {
129   callout *cp, *cp2;
130   time_t t = clocktime() + secs;
131
132   /*
133    * Allocate and fill in a new callout structure
134    */
135   callout *cpnew = alloc_callout();
136   cpnew->c_closure = closure;
137   cpnew->c_fn = fn;
138   cpnew->c_time = t;
139   cpnew->c_id = CID_ALLOC(struct );
140
141   if (t < next_softclock)
142     next_softclock = t;
143
144   /*
145    * Find the correct place in the list
146    */
147   for (cp = &callouts; (cp2 = cp->c_next); cp = cp2)
148     if (cp2->c_time >= t)
149       break;
150
151   /*
152    * And link it in
153    */
154   cp->c_next = cpnew;
155   cpnew->c_next = cp2;
156
157   /*
158    * Return callout identifier
159    */
160   return cpnew->c_id;
161 }
162
163
164 /*
165  * De-schedule a callout
166  */
167 void
168 untimeout(int id)
169 {
170   callout *cp, *cp2;
171   for (cp = &callouts; (cp2 = cp->c_next); cp = cp2) {
172     if (cp2->c_id == id) {
173       cp->c_next = cp2->c_next;
174       free_callout(cp2);
175       break;
176     }
177   }
178 }
179
180
181 /*
182  * Reschedule after clock changed
183  */
184 void
185 reschedule_timeouts(time_t now, time_t then)
186 {
187   callout *cp;
188
189   for (cp = callouts.c_next; cp; cp = cp->c_next) {
190     if (cp->c_time >= now && cp->c_time <= then) {
191       plog(XLOG_WARNING, "job %d rescheduled to run immediately", cp->c_id);
192 #ifdef DEBUG
193       dlog("rescheduling job %d back %ld seconds",
194            cp->c_id, (long) (cp->c_time - now));
195 #endif /* DEBUG */
196       next_softclock = cp->c_time = now;
197     }
198   }
199 }
200
201
202 /*
203  * Clock handler
204  */
205 int
206 softclock(void)
207 {
208   time_t now;
209   callout *cp;
210
211   do {
212     if (task_notify_todo)
213       do_task_notify();
214
215     now = clocktime();
216
217     /*
218      * While there are more callouts waiting...
219      */
220     while ((cp = callouts.c_next) && cp->c_time <= now) {
221       /*
222        * Extract first from list, save fn & closure and
223        * unlink callout from list and free.
224        * Finally call function.
225        *
226        * The free is done first because
227        * it is quite common that the
228        * function will call timeout()
229        * and try to allocate a callout
230        */
231       void (*fn) (voidp) = cp->c_fn;
232       voidp closure = cp->c_closure;
233
234       callouts.c_next = cp->c_next;
235       free_callout(cp);
236       (*fn) (closure);
237     }
238
239   } while (task_notify_todo);
240
241   /*
242    * Return number of seconds to next event,
243    * or 0 if there is no event.
244    */
245   if ((cp = callouts.c_next))
246     return cp->c_time - now;
247   return 0;
248 }