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
46OSDefineMetaClassAndAbstractStructors(IOEventSource, OSObject)
47
48OSMetaClassDefineReservedUnused(IOEventSource, 0);
49OSMetaClassDefineReservedUnused(IOEventSource, 1);
50OSMetaClassDefineReservedUnused(IOEventSource, 2);
51OSMetaClassDefineReservedUnused(IOEventSource, 3);
52OSMetaClassDefineReservedUnused(IOEventSource, 4);
53OSMetaClassDefineReservedUnused(IOEventSource, 5);
54OSMetaClassDefineReservedUnused(IOEventSource, 6);
55OSMetaClassDefineReservedUnused(IOEventSource, 7);
56
57bool
58IOEventSource::checkForWork()
59{
60 return false;
61}
62
63/* inline function implementations */
64
65#if IOKITSTATS
66
67#define IOStatisticsRegisterCounter() \
68do { \
69 reserved->counter = IOStatistics::registerEventSource(inOwner); \
70} while (0)
71
72#define IOStatisticsUnregisterCounter() \
73do { \
74 if (reserved) \
75 IOStatistics::unregisterEventSource(reserved->counter); \
76} while (0)
77
78#define IOStatisticsOpenGate() \
79do { \
80 IOStatistics::countOpenGate(reserved->counter); \
81} while (0)
82
83#define IOStatisticsCloseGate() \
84do { \
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
97void
98IOEventSource::signalWorkAvailable()
99{
100 workLoop->signalWorkAvailable();
101}
102
103void
104IOEventSource::openGate()
105{
106 IOStatisticsOpenGate();
107 workLoop->openGate();
108}
109
110void
111IOEventSource::closeGate()
112{
113 workLoop->closeGate();
114 IOStatisticsCloseGate();
115}
116
117bool
118IOEventSource::tryCloseGate()
119{
120 bool res;
121 if ((res = workLoop->tryCloseGate())) {
122 IOStatisticsCloseGate();
123 }
124 return res;
125}
126
127int
128IOEventSource::sleepGate(void *event, UInt32 type)
129{
130 int res;
131 IOStatisticsOpenGate();
132 res = workLoop->sleepGate(event, interuptibleType: type);
133 IOStatisticsCloseGate();
134 return res;
135}
136
137int
138IOEventSource::sleepGate(void *event, AbsoluteTime deadline, UInt32 type)
139{
140 int res;
141 IOStatisticsOpenGate();
142 res = workLoop->sleepGate(event, deadline, interuptibleType: type);
143 IOStatisticsCloseGate();
144 return res;
145}
146
147void
148IOEventSource::wakeupGate(void *event, bool oneThread)
149{
150 workLoop->wakeupGate(event, oneThread);
151}
152
153
154bool
155IOEventSource::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
180void
181IOEventSource::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
196void
197IOEventSource::setRefcon(void *newrefcon)
198{
199 refcon = newrefcon;
200}
201
202void *
203IOEventSource::getRefcon() const
204{
205 return refcon;
206}
207
208IOEventSource::Action
209IOEventSource::getAction() const
210{
211 if (kActionBlock & flags) {
212 return NULL;
213 }
214 return action;
215}
216
217IOEventSource::ActionBlock
218IOEventSource::getActionBlock(ActionBlock) const
219{
220 if (kActionBlock & flags) {
221 return actionBlock;
222 }
223 return NULL;
224}
225
226void
227IOEventSource::setAction(Action inAction)
228{
229 if ((kActionBlock & flags) && actionBlock) {
230 Block_release(actionBlock);
231 }
232 action = inAction;
233 flags &= ~kActionBlock;
234}
235
236void
237IOEventSource::setActionBlock(ActionBlock block)
238{
239 if ((kActionBlock & flags) && actionBlock) {
240 Block_release(actionBlock);
241 }
242 actionBlock = Block_copy(block);
243 flags |= kActionBlock;
244}
245
246IOEventSource *
247IOEventSource::getNext() const
248{
249 return eventChainNext;
250};
251
252void
253IOEventSource::setNext(IOEventSource *inNext)
254{
255 eventChainNext = inNext;
256}
257
258void
259IOEventSource::enable()
260{
261 enabled = true;
262 if (workLoop) {
263 return signalWorkAvailable();
264 }
265}
266
267void
268IOEventSource::disable()
269{
270 enabled = false;
271}
272
273bool
274IOEventSource::isEnabled() const
275{
276 return enabled;
277}
278
279void
280IOEventSource::setWorkLoop(IOWorkLoop *inWorkLoop)
281{
282 if (!inWorkLoop) {
283 disable();
284 }
285 workLoop = inWorkLoop;
286}
287
288IOWorkLoop *
289IOEventSource::getWorkLoop() const
290{
291 return workLoop;
292}
293
294bool
295IOEventSource::onThread() const
296{
297 return (workLoop != NULL) && workLoop->onThread();
298}
299