]> git.saurik.com Git - apple/xnu.git/blame - bsd/netat/adsp_TimerElem.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / netat / adsp_TimerElem.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ff6e181a
A
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
1c79356b 12 *
ff6e181a
A
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ff6e181a
A
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
1c79356b
A
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * TimerElem.c
25 *
26 * From v01.00 04/15/90 mbs
27 * Modified for MP, 1996 by Tuyen Nguyen
28 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
29*/
30
31#include <sys/errno.h>
32#include <sys/types.h>
33#include <sys/param.h>
34#include <machine/spl.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/proc.h>
38#include <sys/filedesc.h>
39#include <sys/fcntl.h>
40#include <sys/mbuf.h>
41#include <sys/socket.h>
42
43#include <netat/sysglue.h>
44#include <netat/appletalk.h>
45#include <netat/at_pcb.h>
46#include <netat/debug.h>
47#include <netat/adsp.h>
48#include <netat/adsp_internal.h>
49
50atlock_t adsptmr_lock;
51
52extern void DoTimerElem(); /* (TimerElemPtr t);
53 * External routine called to
54 * process each one. */
55
56/*
57 * InsertTimerElem
58 *
59 * INPUTS:
60 * qhead Address of ptr to first item in list
61 * t timer element to link in
62 * vbl timer value to use
63 * OUTPUTS:
64 * void
65 */
66void InsertTimerElem(qhead, t, val)
67 /* (TimerElemPtr *qhead, TimerElemPtr t, word val) */
68 TimerElemPtr *qhead, t;
69 int val;
70{
71 TimerElemPtr p; /* parent pointer */
72 TimerElemPtr n; /* current */
73 int s;
74
75 ATDISABLE(s, adsptmr_lock);
76
77 if (t->onQ) {
78 /*
79 * someone else beat us to the punch and put this
80 * element back on the queue, just return in this case
81 */
82 ATENABLE(s, adsptmr_lock);
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 ATENABLE(s, adsptmr_lock);
106}
107
108
109/*
110 * RemoveTimerElem
111 *
112 * INPUTS:
113 * qhead Address of ptr to first item in list
114 * t timer element to link in
115 * OUTPUTS:
116 * void
117 */
118void RemoveTimerElem(qhead, t) /* (TimerElemPtr *qhead, TimerElemPtr t) */
119 TimerElemPtr *qhead, t;
120{
121 TimerElemPtr p; /* parent pointer */
122 TimerElemPtr n; /* current */
123 int s;
124
125 ATDISABLE(s, adsptmr_lock);
126
127 if ( !t->onQ) {
128 /*
129 * someone else beat us to the punch and took this
130 * element off of the queue, just return in this case
131 */
132 ATENABLE(s, adsptmr_lock);
133 return;
134 }
135 p = (TimerElemPtr)qhead;
136
137 while (n = p->link) /* Get next item in queue */
138 {
139 if (n == t) /* Is it us? */
140 {
141 if (p->link = n->link) /* Link our parent to our child */
142 {
143 n->link->timer += t->timer; /* and update child's timer */
144 }
145 n->onQ = 0; /* Not on linked list anymore */
146 break;
147 }
148 p = n;
149 } /* while */
150
151 ATENABLE(s, adsptmr_lock);
152}
153
154
155/*
156 * TimerQueueTick
157 *
158 * INPUTS:
159 * qhead Address of ptr to first item in list
160 *
161 * OUTPUTS:
162 * void
163 */
164void TimerQueueTick(qhead) /* (TimerElemPtr *qhead) */
165 TimerElemPtr *qhead;
166{
167 TimerElemPtr p; /* parent pointer */
168 TimerElemPtr n; /* current */
169 int s;
170
171 ATDISABLE(s, adsptmr_lock);
172
173 p = (TimerElemPtr)qhead;
174 if (p->link) /* Is anything on queue? */
175 p->link->timer--; /* Yes, decrement by a tick */
176 else
177 goto done; /* No, we're outta' here */
178
179 while ((n = p->link) &&
180 (n->timer == 0)) /* Next guy needs to be serviced */
181 {
182 p->link = n->link; /* Unlink us */
183 n->onQ = 0;
184
185 ATENABLE(s, adsptmr_lock);
186 DoTimerElem(n);
187 ATDISABLE(s, adsptmr_lock);
188
189 p = (TimerElemPtr)qhead;
190 } /* while */
191
192done:
193 ATENABLE(s, adsptmr_lock);
194}