2 * Copyright (c) 1997-2006 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.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
40 * File: am-utils/amd/clock.c
47 * Modeled on kernel object of the same name.
48 * See usual references.
50 * Use of a heap-based mechanism was rejected:
51 * 1. more complex implementation needed.
52 * 2. not obvious that a list is too slow for Amd.
57 #endif /* HAVE_CONFIG_H */
61 void reschedule_timeouts(time_t now, time_t then);
63 typedef struct callout callout;
65 callout *c_next; /* List of callouts */
66 callout_fun *c_fn; /* Function to call */
67 opaque_t c_arg; /* Argument to pass to call */
68 time_t c_time; /* Time of call */
69 int c_id; /* Unique identifier */
72 static callout callouts; /* List of pending callouts */
73 static callout *free_callouts; /* Cache of free callouts */
74 static int nfree_callouts; /* Number on free list */
75 static int callout_id; /* Next free callout identifier */
77 time_t next_softclock; /* Time of next call to softclock() */
81 * Number of callout slots we keep on the free list
83 #define CALLOUT_FREE_SLOP 10
86 * Global assumption: valid id's are non-zero.
88 #define CID_ALLOC() (++callout_id)
95 callout *cp = free_callouts;
99 free_callouts = free_callouts->c_next;
102 return ALLOC(struct callout);
107 free_callout(callout *cp)
109 if (nfree_callouts > CALLOUT_FREE_SLOP) {
112 cp->c_next = free_callouts;
120 * Schedule a callout.
122 * (*fn)(fn_arg) will be called at clocktime(NULL) + secs
125 timeout(u_int secs, callout_fun *fn, opaque_t fn_arg)
128 time_t t = clocktime(NULL) + secs;
131 * Allocate and fill in a new callout structure
133 callout *cpnew = alloc_callout();
134 cpnew->c_arg = fn_arg;
137 cpnew->c_id = CID_ALLOC();
139 if (t < next_softclock)
143 * Find the correct place in the list
145 for (cp = &callouts; (cp2 = cp->c_next); cp = cp2)
146 if (cp2->c_time >= t)
156 * Return callout identifier
163 * De-schedule a callout
169 for (cp = &callouts; (cp2 = cp->c_next); cp = cp2) {
170 if (cp2->c_id == id) {
171 cp->c_next = cp2->c_next;
180 * Reschedule after clock changed
183 reschedule_timeouts(time_t now, time_t then)
187 for (cp = callouts.c_next; cp; cp = cp->c_next) {
188 if (cp->c_time >= now && cp->c_time <= then) {
189 plog(XLOG_WARNING, "job %d rescheduled to run immediately", cp->c_id);
190 dlog("rescheduling job %d back %ld seconds",
191 cp->c_id, (long) (cp->c_time - now));
192 next_softclock = cp->c_time = now;
208 if (task_notify_todo)
211 now = clocktime(NULL);
214 * While there are more callouts waiting...
216 while ((cp = callouts.c_next) && cp->c_time <= now) {
218 * Extract first from list, save fn & fn_arg and
219 * unlink callout from list and free.
220 * Finally call function.
222 * The free is done first because
223 * it is quite common that the
224 * function will call timeout()
225 * and try to allocate a callout
227 callout_fun *fn = cp->c_fn;
228 opaque_t fn_arg = cp->c_arg;
230 callouts.c_next = cp->c_next;
235 } while (task_notify_todo);
238 * Return number of seconds to next event,
239 * or 0 if there is no event.
241 if ((cp = callouts.c_next))
242 return cp->c_time - now;