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