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