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