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