xref: /xnu-11215/iokit/Kernel/IOEventSource.cpp (revision a5e72196)
1 /*
2  * Copyright (c) 1998-2000, 2009 Apple 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 /*
29  *  Copyright (c) 1998 Apple Computer, Inc.  All rights reserved.
30  *
31  *  HISTORY
32  *   1998-7-13	Godfrey van der Linden(gvdl)
33  *       Created.
34  *  ]*/
35 #include <IOKit/IOLib.h>
36 
37 #include <IOKit/IOEventSource.h>
38 #include <IOKit/IOWorkLoop.h>
39 #include <libkern/Block.h>
40 
41 #define super OSObject
42 
43 OSDefineMetaClassAndAbstractStructors(IOEventSource, OSObject)
44 
45 OSMetaClassDefineReservedUnused(IOEventSource, 0);
46 OSMetaClassDefineReservedUnused(IOEventSource, 1);
47 OSMetaClassDefineReservedUnused(IOEventSource, 2);
48 OSMetaClassDefineReservedUnused(IOEventSource, 3);
49 OSMetaClassDefineReservedUnused(IOEventSource, 4);
50 OSMetaClassDefineReservedUnused(IOEventSource, 5);
51 OSMetaClassDefineReservedUnused(IOEventSource, 6);
52 OSMetaClassDefineReservedUnused(IOEventSource, 7);
53 
54 bool
55 IOEventSource::checkForWork()
56 {
57 	return false;
58 }
59 
60 /* inline function implementations */
61 
62 #if IOKITSTATS
63 
64 #define IOStatisticsRegisterCounter() \
65 do { \
66 	reserved->counter = IOStatistics::registerEventSource(inOwner); \
67 } while (0)
68 
69 #define IOStatisticsUnregisterCounter() \
70 do { \
71 	if (reserved) \
72 	        IOStatistics::unregisterEventSource(reserved->counter); \
73 } while (0)
74 
75 #define IOStatisticsOpenGate() \
76 do { \
77 	IOStatistics::countOpenGate(reserved->counter); \
78 } while (0)
79 
80 #define IOStatisticsCloseGate() \
81 do { \
82 	IOStatistics::countCloseGate(reserved->counter); \
83 } while (0)
84 
85 #else
86 
87 #define IOStatisticsRegisterCounter()
88 #define IOStatisticsUnregisterCounter()
89 #define IOStatisticsOpenGate()
90 #define IOStatisticsCloseGate()
91 
92 #endif /* IOKITSTATS */
93 
94 void
95 IOEventSource::signalWorkAvailable()
96 {
97 	workLoop->signalWorkAvailable();
98 }
99 
100 void
101 IOEventSource::openGate()
102 {
103 	IOStatisticsOpenGate();
104 	workLoop->openGate();
105 }
106 
107 void
108 IOEventSource::closeGate()
109 {
110 	workLoop->closeGate();
111 	IOStatisticsCloseGate();
112 }
113 
114 bool
115 IOEventSource::tryCloseGate()
116 {
117 	bool res;
118 	if ((res = workLoop->tryCloseGate())) {
119 		IOStatisticsCloseGate();
120 	}
121 	return res;
122 }
123 
124 int
125 IOEventSource::sleepGate(void *event, UInt32 type)
126 {
127 	int res;
128 	IOStatisticsOpenGate();
129 	res = workLoop->sleepGate(event, type);
130 	IOStatisticsCloseGate();
131 	return res;
132 }
133 
134 int
135 IOEventSource::sleepGate(void *event, AbsoluteTime deadline, UInt32 type)
136 {
137 	int res;
138 	IOStatisticsOpenGate();
139 	res = workLoop->sleepGate(event, deadline, type);
140 	IOStatisticsCloseGate();
141 	return res;
142 }
143 
144 void
145 IOEventSource::wakeupGate(void *event, bool oneThread)
146 {
147 	workLoop->wakeupGate(event, oneThread);
148 }
149 
150 
151 bool
152 IOEventSource::init(OSObject *inOwner,
153     Action inAction)
154 {
155 	if (!inOwner) {
156 		return false;
157 	}
158 
159 	owner = inOwner;
160 
161 	if (!super::init()) {
162 		return false;
163 	}
164 
165 	(void) setAction(inAction);
166 	enabled = true;
167 
168 	if (!reserved) {
169 		reserved = IONew(ExpansionData, 1);
170 		if (!reserved) {
171 			return false;
172 		}
173 	}
174 
175 	IOStatisticsRegisterCounter();
176 
177 	return true;
178 }
179 
180 void
181 IOEventSource::free( void )
182 {
183 	IOStatisticsUnregisterCounter();
184 
185 	if ((kActionBlock & flags) && actionBlock) {
186 		Block_release(actionBlock);
187 	}
188 
189 	if (reserved) {
190 		IODelete(reserved, ExpansionData, 1);
191 	}
192 
193 	super::free();
194 }
195 
196 void
197 IOEventSource::setRefcon(void *newrefcon)
198 {
199 	refcon = newrefcon;
200 }
201 
202 void *
203 IOEventSource::getRefcon() const
204 {
205 	return refcon;
206 }
207 
208 IOEventSource::Action
209 IOEventSource::getAction() const
210 {
211 	if (kActionBlock & flags) {
212 		return NULL;
213 	}
214 	return action;
215 }
216 
217 IOEventSource::ActionBlock
218 IOEventSource::getActionBlock(ActionBlock) const
219 {
220 	if (kActionBlock & flags) {
221 		return actionBlock;
222 	}
223 	return NULL;
224 }
225 
226 void
227 IOEventSource::setAction(Action inAction)
228 {
229 	if ((kActionBlock & flags) && actionBlock) {
230 		Block_release(actionBlock);
231 	}
232 	action = inAction;
233 }
234 
235 void
236 IOEventSource::setActionBlock(ActionBlock block)
237 {
238 	if ((kActionBlock & flags) && actionBlock) {
239 		Block_release(actionBlock);
240 	}
241 	actionBlock = Block_copy(block);
242 	flags |= kActionBlock;
243 }
244 
245 IOEventSource *
246 IOEventSource::getNext() const
247 {
248 	return eventChainNext;
249 };
250 
251 void
252 IOEventSource::setNext(IOEventSource *inNext)
253 {
254 	eventChainNext = inNext;
255 }
256 
257 void
258 IOEventSource::enable()
259 {
260 	enabled = true;
261 	if (workLoop) {
262 		return signalWorkAvailable();
263 	}
264 }
265 
266 void
267 IOEventSource::disable()
268 {
269 	enabled = false;
270 }
271 
272 bool
273 IOEventSource::isEnabled() const
274 {
275 	return enabled;
276 }
277 
278 void
279 IOEventSource::setWorkLoop(IOWorkLoop *inWorkLoop)
280 {
281 	if (!inWorkLoop) {
282 		disable();
283 	}
284 	workLoop = inWorkLoop;
285 }
286 
287 IOWorkLoop *
288 IOEventSource::getWorkLoop() const
289 {
290 	return workLoop;
291 }
292 
293 bool
294 IOEventSource::onThread() const
295 {
296 	return (workLoop != NULL) && workLoop->onThread();
297 }
298