]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOPMinformeeList.cpp
57ac0bb7f46b0aeef69352c955bed94e7965a247
[apple/xnu.git] / iokit / Kernel / IOPMinformeeList.cpp
1 /*
2 * Copyright (c) 1998-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 #include <IOKit/pwr_mgt/IOPM.h>
29 #include <IOKit/pwr_mgt/IOPMinformeeList.h>
30 #include <IOKit/pwr_mgt/IOPMinformee.h>
31
32 #define super OSObject
33 OSDefineMetaClassAndStructors(IOPMinformeeList,OSObject)
34
35 //*********************************************************************************
36 // init
37 //
38 //*********************************************************************************
39 void IOPMinformeeList::initialize ( void )
40 {
41 firstItem = NULL;
42 length = 0;
43 }
44
45 //******************************************************************************
46 // getSharedRecursiveLock
47 //
48 //******************************************************************************
49 IORecursiveLock *IOPMinformeeList::getSharedRecursiveLock( void )
50 {
51 static IORecursiveLock *sharedListLock = NULL;
52
53 /* A running system could have 50-60+ instances of IOPMInformeeList.
54 * They'll share this lock, since list insertion and removal is relatively
55 * rare, and generally tied to major events like device discovery.
56 *
57 * getSharedRecursiveLock() is called from IOStartIOKit to initialize
58 * the sharedListLock before any IOPMinformeeLists are instantiated.
59 *
60 * The IOPMinformeeList class will be around for the lifetime of the system,
61 * we don't worry about freeing this lock.
62 */
63
64 if ( NULL == sharedListLock )
65 {
66 sharedListLock = IORecursiveLockAlloc();
67 }
68 return sharedListLock;
69 }
70
71 //*********************************************************************************
72 // addToList
73 //
74 //*********************************************************************************
75
76 IOReturn IOPMinformeeList::addToList ( IOPMinformee * newInformee )
77 {
78 IOPMinformee * nextInformee;
79 IORecursiveLock *listLock = getSharedRecursiveLock();
80
81 if(!listLock)
82 return kIOReturnError;
83
84 IORecursiveLockLock(listLock);
85 nextInformee = firstItem;
86
87 // Is new object already in the list?
88 while ( nextInformee != NULL )
89 {
90 if ( nextInformee->whatObject == newInformee->whatObject )
91 {
92 // object is present; just exit
93 goto unlock_and_exit;
94 }
95 nextInformee = nextInList(nextInformee);
96 }
97
98 // add it to the front of the list
99 newInformee->nextInList = firstItem;
100 firstItem = newInformee;
101 length++;
102
103 unlock_and_exit:
104 IORecursiveLockUnlock(listLock);
105 return IOPMNoErr;
106 }
107
108
109 //*********************************************************************************
110 // removeFromList
111 //
112 // Find the item in the list, unlink it, and free it.
113 //*********************************************************************************
114
115 IOReturn IOPMinformeeList::removeFromList ( IOService * theItem )
116 {
117 IOPMinformee * item = firstItem;
118 IOPMinformee * temp;
119 IORecursiveLock *listLock = getSharedRecursiveLock();
120
121 if ( NULL == item )
122 return IOPMNoErr;
123 if(!listLock)
124 return kIOReturnError;
125
126 IORecursiveLockLock( listLock );
127
128 if ( item->whatObject == theItem )
129 {
130 firstItem = item->nextInList;
131 length--;
132 item->release();
133 goto unlock_and_exit;
134 }
135
136 while ( item->nextInList != NULL )
137 {
138 if ( item->nextInList->whatObject == theItem )
139 {
140 temp = item->nextInList;
141 item->nextInList = temp->nextInList;
142 length--;
143 temp->release();
144 goto unlock_and_exit;
145 }
146 item = item->nextInList;
147 }
148
149 unlock_and_exit:
150 IORecursiveLockUnlock(listLock);
151 return IOPMNoErr;
152 }
153
154
155 //*********************************************************************************
156 // firstInList
157 //
158 //*********************************************************************************
159
160 IOPMinformee * IOPMinformeeList::firstInList ( void )
161 {
162 return firstItem;
163 }
164
165 //*********************************************************************************
166 // nextInList
167 //
168 //*********************************************************************************
169
170 IOPMinformee * IOPMinformeeList::nextInList ( IOPMinformee * currentItem )
171 {
172 if ( currentItem != NULL ) {
173 return (currentItem->nextInList);
174 }
175 return NULL;
176 }
177
178 //*********************************************************************************
179 // numberOfItems
180 //
181 //*********************************************************************************
182
183 unsigned long IOPMinformeeList::numberOfItems ( void )
184 {
185 return length;
186 }
187
188 //*********************************************************************************
189 // findItem
190 //
191 // Look through the list for the one which points to the object identified
192 // by the parameter. Return a pointer to the list item or NULL.
193 //*********************************************************************************
194
195 IOPMinformee * IOPMinformeeList::findItem ( IOService * driverOrChild )
196 {
197 IOPMinformee * nextObject;
198
199 nextObject = firstInList();
200 while ( nextObject != NULL ) {
201 if ( nextObject->whatObject == driverOrChild ) {
202 return nextObject;
203 }
204 nextObject = nextInList(nextObject);
205 }
206 return NULL;
207 }
208
209
210
211 //*********************************************************************************
212 // free
213 //
214 // Free all items in the list, and then free the list itself
215 //*********************************************************************************
216
217 void IOPMinformeeList::free (void )
218 {
219 IOPMinformee * next = firstItem;
220
221 while ( next != NULL ) {
222 firstItem = next->nextInList;
223 length--;
224 next->release();
225 next = firstItem;
226 }
227 super::free();
228 }
229