]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOPMinformeeList.cpp
xnu-792.25.20.tar.gz
[apple/xnu.git] / iokit / Kernel / IOPMinformeeList.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
6601e61a 4 * @APPLE_LICENSE_HEADER_START@
1c79356b 5 *
6601e61a
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
8f6c56a5 11 *
6601e61a
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
6601e61a
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
8f6c56a5 19 *
6601e61a 20 * @APPLE_LICENSE_HEADER_END@
1c79356b 21 */
1c79356b
A
22#include <IOKit/pwr_mgt/IOPM.h>
23#include <IOKit/pwr_mgt/IOPMinformeeList.h>
24#include <IOKit/pwr_mgt/IOPMinformee.h>
25
26#define super OSObject
27OSDefineMetaClassAndStructors(IOPMinformeeList,OSObject)
28
29//*********************************************************************************
30// init
31//
32//*********************************************************************************
33void IOPMinformeeList::initialize ( void )
34{
35 firstItem = NULL;
36 length = 0;
37}
38
0c530ab8
A
39//******************************************************************************
40// getSharedRecursiveLock
41//
42//******************************************************************************
43IORecursiveLock *IOPMinformeeList::getSharedRecursiveLock( void )
44{
45 static IORecursiveLock *sharedListLock = NULL;
46
47 /* A running system could have 50-60+ instances of IOPMInformeeList.
48 * They'll share this lock, since list insertion and removal is relatively
49 * rare, and generally tied to major events like device discovery.
50 *
51 * getSharedRecursiveLock() is called from IOStartIOKit to initialize
52 * the sharedListLock before any IOPMinformeeLists are instantiated.
53 *
54 * The IOPMinformeeList class will be around for the lifetime of the system,
55 * we don't worry about freeing this lock.
56 */
57
58 if ( NULL == sharedListLock )
59 {
60 sharedListLock = IORecursiveLockAlloc();
61 }
62 return sharedListLock;
63}
64
1c79356b
A
65//*********************************************************************************
66// addToList
67//
68//*********************************************************************************
69
70IOReturn IOPMinformeeList::addToList ( IOPMinformee * newInformee )
71{
72 IOPMinformee * nextInformee;
0c530ab8
A
73 IORecursiveLock *listLock = getSharedRecursiveLock();
74
75 if(!listLock)
76 return kIOReturnError;
77
78 IORecursiveLockLock(listLock);
79 nextInformee = firstItem;
80
81 // Is new object already in the list?
82 while ( nextInformee != NULL )
83 {
84 if ( nextInformee->whatObject == newInformee->whatObject )
85 {
86 // object is present; just exit
87 goto unlock_and_exit;
1c79356b
A
88 }
89 nextInformee = nextInList(nextInformee);
90 }
0c530ab8
A
91
92 // add it to the front of the list
93 newInformee->nextInList = firstItem;
1c79356b 94 firstItem = newInformee;
0c530ab8
A
95 length++;
96
97unlock_and_exit:
98 IORecursiveLockUnlock(listLock);
99 return IOPMNoErr;
100}
101
102
103//*********************************************************************************
104// removeFromList
105//
106// Find the item in the list, unlink it, and free it.
107//*********************************************************************************
108
109IOReturn IOPMinformeeList::removeFromList ( IOService * theItem )
110{
111 IOPMinformee * item = firstItem;
112 IOPMinformee * temp;
113 IORecursiveLock *listLock = getSharedRecursiveLock();
114
115 if ( NULL == item )
116 return IOPMNoErr;
117 if(!listLock)
118 return kIOReturnError;
119
120 IORecursiveLockLock( listLock );
121
122 if ( item->whatObject == theItem )
123 {
124 firstItem = item->nextInList;
125 length--;
126 item->release();
127 goto unlock_and_exit;
128 }
129
130 while ( item->nextInList != NULL )
131 {
132 if ( item->nextInList->whatObject == theItem )
133 {
134 temp = item->nextInList;
135 item->nextInList = temp->nextInList;
136 length--;
137 temp->release();
138 goto unlock_and_exit;
139 }
140 item = item->nextInList;
141 }
142
143unlock_and_exit:
144 IORecursiveLockUnlock(listLock);
1c79356b
A
145 return IOPMNoErr;
146}
147
148
149//*********************************************************************************
150// firstInList
151//
152//*********************************************************************************
153
154IOPMinformee * IOPMinformeeList::firstInList ( void )
155{
156 return firstItem;
157}
158
159//*********************************************************************************
160// nextInList
161//
162//*********************************************************************************
163
164IOPMinformee * IOPMinformeeList::nextInList ( IOPMinformee * currentItem )
165{
166 if ( currentItem != NULL ) {
167 return (currentItem->nextInList);
168 }
169 return NULL;
170}
171
172//*********************************************************************************
173// numberOfItems
174//
175//*********************************************************************************
176
177unsigned long IOPMinformeeList::numberOfItems ( void )
178{
179 return length;
180}
181
182//*********************************************************************************
183// findItem
184//
185// Look through the list for the one which points to the object identified
186// by the parameter. Return a pointer to the list item or NULL.
187//*********************************************************************************
188
189IOPMinformee * IOPMinformeeList::findItem ( IOService * driverOrChild )
190{
191 IOPMinformee * nextObject;
192
193 nextObject = firstInList();
194 while ( nextObject != NULL ) {
195 if ( nextObject->whatObject == driverOrChild ) {
196 return nextObject;
197 }
198 nextObject = nextInList(nextObject);
199 }
200 return NULL;
201}
202
203
1c79356b
A
204
205//*********************************************************************************
206// free
207//
208// Free all items in the list, and then free the list itself
209//*********************************************************************************
210
211void IOPMinformeeList::free (void )
212{
213 IOPMinformee * next = firstItem;
214
215 while ( next != NULL ) {
216 firstItem = next->nextInList;
217 length--;
218 next->release();
219 next = firstItem;
220 }
221super::free();
222}
223