1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * TimerElem.c
30 *
31 * From v01.00  04/15/90 mbs
32 *    Modified for MP, 1996 by Tuyen Nguyen
33 *   Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
34*/
35
36#include <sys/errno.h>
37#include <sys/types.h>
38#include <sys/param.h>
39#include <machine/spl.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/proc.h>
43#include <sys/filedesc.h>
44#include <sys/fcntl.h>
45#include <sys/mbuf.h>
46#include <sys/socket.h>
47
48#include <netat/sysglue.h>
49#include <netat/appletalk.h>
50#include <netat/at_pcb.h>
51#include <netat/debug.h>
52#include <netat/adsp.h>
53#include <netat/adsp_internal.h>
54
55
56extern	void DoTimerElem(TimerElemPtr);	/* (TimerElemPtr t);
57				 * External routine called to
58				 * process each one. */
59
60/*
61 * InsertTimerElem
62 *
63 * INPUTS:
64 * 	qhead		Address of ptr to first item in list
65 *	t		timer element to link in
66 *	vbl		timer value to use
67 * OUTPUTS:
68 * 	void
69 */
70void InsertTimerElem(qhead, t, val)
71    /* (TimerElemPtr *qhead, TimerElemPtr t, word val) */
72    TimerElemPtr *qhead, t;
73    int val;
74{
75    TimerElemPtr p;		/* parent pointer */
76    TimerElemPtr n;		/* current */
77
78    if (t->onQ) {
79        /*
80	 	* someone else beat us to the punch and put this
81	 	* element back on the queue, just return in this case
82	 	*/
83		return;
84    }
85    p = (TimerElemPtr)qhead;
86
87    while ((n = p->link)) {
88	if (val <= n->timer)	/* Do we go in front of this? */
89	{
90	    n->timer -= val;	/* Yes, adjust his delta */
91	    break;		/* and go link us in */
92	}
93	val -= n->timer;	/* No, subtract off delta from our value */
94	p = n;
95    }				/* while */
96
97    /* It must go after item pointed to by p and in front of item
98     * pointed to by n */
99
100    t->onQ = 1;	/* we're linked in now */
101    p->link = t;		/* parent points to us */
102    t->timer = val;		/* this is our value */
103    t->link = n;		/* we point to n */
104
105}
106
107
108/*
109 * RemoveTimerElem
110 *
111 * INPUTS:
112 *	qhead		Address of ptr to first item in list
113 *	t		timer element to link in
114 * OUTPUTS:
115 * 	void
116 */
117void RemoveTimerElem(qhead, t)	/* (TimerElemPtr *qhead, TimerElemPtr t) */
118    TimerElemPtr *qhead, t;
119{
120    TimerElemPtr p;		/* parent pointer */
121    TimerElemPtr n;		/* current */
122
123    if ( !t->onQ) {
124        /*
125	 	* someone else beat us to the punch and took this
126	 	* element off of the queue, just return in this case
127	 	*/
128		return;
129    }
130    p = (TimerElemPtr)qhead;
131
132    while ((n = p->link))	/* Get next item in queue */
133    {
134	if (n == t)		/* Is it us? */
135	{
136	    if ((p->link = n->link)) /* Link our parent to our child */
137	    {
138		n->link->timer += t->timer; /* and update child's timer */
139	    }
140	    n->onQ = 0;		/* Not on linked list anymore */
141	    break;
142	}
143	p = n;
144    }				/* while */
145
146}
147
148
149/*
150 * TimerQueueTick
151 *
152 * INPUTS:
153 * 	qhead		Address of ptr to first item in list
154 *
155 * OUTPUTS:
156 * 	void
157 */
158void TimerQueueTick(qhead)	/* (TimerElemPtr *qhead) */
159    TimerElemPtr *qhead;
160{
161    TimerElemPtr p;		/* parent pointer */
162    TimerElemPtr n;		/* current */
163
164    p = (TimerElemPtr)qhead;
165    if (p->link) {		/* Is anything on queue? */
166	p->link->timer--;	/* Yes, decrement by a tick */
167	while ((n = p->link) &&
168	       (n->timer == 0)) /* Next guy needs to be serviced */
169	{
170		p->link = n->link;	/* Unlink us */
171		n->onQ	= 0;
172
173		DoTimerElem(n);
174
175		p = (TimerElemPtr)qhead;
176	}				/* while */
177    }
178}
179