summaryrefslogtreecommitdiff
path: root/server/timer.c
blob: c671c8b7b2368b2c564b24b70b23f3dffc95d756 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains functions for managing multiple timeouts.
 *
 *	Created by:	John T. Kohl
 *	Derived from timer_manager_ by Ken Raeburn
 *
 *	$Source$
 *	$Author$
 *
 */

#ifndef SABER
#ifndef lint
static char rcsid[] =
    "$Id$";
#endif /* lint */
#endif /* SABER */

/*
 * timer_manager_ -- routines for handling timers in login_shell
 * (and elsewhere)
 *
 * Copyright 1986 Student Information Processing Board,
 * Massachusetts Institute of Technology
 *
 * written by Ken Raeburn

Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
documentation, and that the name of M.I.T. and the Student
Information Processing Board not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
M.I.T. and the Student Information Processing Board
make no representations about the suitability of
this software for any purpose.  It is provided "as is"
without express or implied warranty.

 */


/*
 * External functions:
 *
 * timer timer_set_rel (time_rel, proc, arg)
 *	long time_rel;
 *	void (*proc)();
 *	caddr_t arg;
 * timer timer_set_abs (time_abs, proc, arg)
 *	long time_abs;
 *	void (*proc)();
 *	caddr_t arg;
 *
 * void timer_reset(tmr)
 *	timer tmr;
 *
 * void timer_process()
 *
 */

#include <stdio.h>
#include "zserver.h"

long nexttimo = 0L;			/* the Unix time of the next
					   alarm */
static timer timers = NULL;
static long right_now;

#ifdef __STDC__
# define        P(s) s
#else
# define P(s) ()
#endif

static void timer_botch P((void*)), insert_timer P((timer)),
       add_timer P((timer));

#undef P

/*
 * timer_set_rel(time_rel, proc)
 *   time_rel: alarm time relative to now, in seconds
 *   proc: subroutine to be called (no args, returns void)
 *
 * creates a "timer" and adds it to the current list, returns "timer"
 */

timer timer_set_rel (time_rel, proc, arg)
     long time_rel;
#ifdef __STDC__
     void (*proc)(void*);
#else
     void (*proc)();
#endif
     void *arg;
{
	timer new_t;
	right_now = NOW;
	new_t = (timer) xmalloc(TIMER_SIZE);
	if (new_t == NULL) return(NULL);
	ALARM_TIME(new_t) = time_rel + right_now;
	ALARM_FUNC(new_t) = proc;
	ALARM_NEXT(new_t) = NULL;
	ALARM_PREV(new_t) = NULL;
	ALARM_ARG(new_t)  = arg;
	add_timer(new_t);
	return(new_t);
}

#ifdef notdef
/* currently unused */

/*
 * timer_set_abs (time_abs, proc, arg)
 *   time_abs: alarm time, absolute
 *   proc: routine to call when timer expires
 *   arg:  argument to routine
 *
 * functions like timer_set_rel
 */

timer timer_set_abs (time_abs, proc, arg)
     long time_abs;
     void (*proc)();
     caddr_t arg;
{
	timer new_t;

	new_t = (timer)xmalloc(TIMER_SIZE);
	if (new_t == NULL) return(NULL);
	ALARM_TIME(new_t) = time_abs;
	ALARM_FUNC(new_t) = proc;
	ALARM_NEXT(new_t) = NULL;
	ALARM_PREV(new_t) = NULL;
	ALARM_ARG(new_t)  = arg;
	add_timer(new_t);
	return(new_t);
}
#endif /* notdef */

/*
 * timer_reset
 *
 * args:
 *   tmr: timer to be removed from the list
 *
 * removes any timers matching tmr and reallocates list
 *
 */

void
timer_reset(tmr)
     timer tmr;
{
	if (!ALARM_PREV(tmr) || !ALARM_NEXT(tmr)) {
		syslog (LOG_ERR, "timer_reset() of unscheduled timer\n");
		abort();
	}
	if (tmr == timers) {
		syslog (LOG_ERR,"timer_reset of timer head\n");
		abort();
	}
	xremque(tmr);
	ALARM_PREV(tmr) = NULL;
	ALARM_NEXT(tmr) = NULL;
	xfree(tmr);
	if (timers == NULL) {
		syslog (LOG_ERR,"reset with no timers\n");
		abort();
	}
	nexttimo = ALARM_TIME(ALARM_NEXT(timers));
}


#define set_timeval(t,s) ((t).tv_sec=(s),(t).tv_usec=0,(t))

/* add_timer(t:timer)
 *
 * args:
 *   t: new "timer" to be added
 *
 * returns:
 *   0 if successful
 *   -1 if error (errno set) -- old time table may have been destroyed
 *
 */
static void
add_timer(new_t)
     timer new_t;
{
	if (ALARM_PREV(new_t) || ALARM_NEXT(new_t)) {
		syslog (LOG_ERR,"add_timer of enqueued timer\n");
		abort();
	}
	insert_timer(new_t);
}

/*
 * insert_timer(t:timer)
 *
 * inserts a timer into the current timer table.
 *
 */

static void
insert_timer(new_t)
     timer new_t;
{
	register timer t;

	if (timers == NULL) {
		timers = (timer) xmalloc(TIMER_SIZE);
		ALARM_NEXT(timers) = timers;
		ALARM_PREV(timers) = timers;
		ALARM_TIME(timers) = 0L;
		ALARM_FUNC(timers) = timer_botch;
		ALARM_ARG(timers)  = (void *) NULL;
	}
	for (t = ALARM_NEXT(timers); t != timers; t = ALARM_NEXT(t)) {
		if (ALARM_TIME(t) > ALARM_TIME(new_t)) {
			xinsque(new_t, ALARM_PREV(t));
			nexttimo = ALARM_TIME(ALARM_NEXT(timers));
			return;
		}
	}
	xinsque(new_t, ALARM_PREV(timers));
	nexttimo = ALARM_TIME(ALARM_NEXT(timers));
	return;
}

/*
 * timer_process -- checks for next timer execution time
 * and execute 
 *
 */

void
timer_process()
{
	register timer t;
	timer_proc queue;
	void * queue_arg;
	int valid = 0;

	right_now = NOW;
	t=ALARM_NEXT(timers);
	/* note that in the case that there are no timers, the ALARM_TIME
	   is set to 0L, which is what the main loop expects as the
	   nexttimo when we have no timout work to do */
	nexttimo = ALARM_TIME(t);
	if (t != timers && right_now >= ALARM_TIME(t)) {
		/*
		 * This one goes off NOW..
		 * Enqueue the function, and delete the timer.
		 */
		valid = 1;
		queue_arg = ALARM_ARG(t);
		queue = ALARM_FUNC(t);
		xremque(t); 
	        ALARM_PREV(t) = NULL;
		ALARM_NEXT(t) = NULL;
		ALARM_FUNC(t) = timer_botch;
		ALARM_ARG(t)  = (caddr_t) NULL;
		xfree(t);	
	}
	
	if (valid) {
		(queue)(queue_arg);
	}
	return;
}

static void
timer_botch(arg)
     void *arg;
{
	syslog(LOG_CRIT, "Timer botch\n");
	abort();
}