1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions * Copyright (c) 1998-2000, 2009 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions *
4*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions *
6*1b191cb5SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions *
15*1b191cb5SApple OSS Distributions * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions *
18*1b191cb5SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions * limitations under the License.
25*1b191cb5SApple OSS Distributions *
26*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions */
28*1b191cb5SApple OSS Distributions /*
29*1b191cb5SApple OSS Distributions * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
30*1b191cb5SApple OSS Distributions *
31*1b191cb5SApple OSS Distributions * HISTORY
32*1b191cb5SApple OSS Distributions * 1998-7-13 Godfrey van der Linden(gvdl)
33*1b191cb5SApple OSS Distributions * Created.
34*1b191cb5SApple OSS Distributions * ]*/
35*1b191cb5SApple OSS Distributions
36*1b191cb5SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
37*1b191cb5SApple OSS Distributions
38*1b191cb5SApple OSS Distributions #include <IOKit/IOLib.h>
39*1b191cb5SApple OSS Distributions
40*1b191cb5SApple OSS Distributions #include <IOKit/IOEventSource.h>
41*1b191cb5SApple OSS Distributions #include <IOKit/IOWorkLoop.h>
42*1b191cb5SApple OSS Distributions #include <libkern/Block.h>
43*1b191cb5SApple OSS Distributions
44*1b191cb5SApple OSS Distributions #define super OSObject
45*1b191cb5SApple OSS Distributions
46*1b191cb5SApple OSS Distributions OSDefineMetaClassAndAbstractStructors(IOEventSource, OSObject)
47*1b191cb5SApple OSS Distributions
48*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOEventSource, 0);
49*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOEventSource, 1);
50*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOEventSource, 2);
51*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOEventSource, 3);
52*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOEventSource, 4);
53*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOEventSource, 5);
54*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOEventSource, 6);
55*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOEventSource, 7);
56*1b191cb5SApple OSS Distributions
57*1b191cb5SApple OSS Distributions bool
checkForWork()58*1b191cb5SApple OSS Distributions IOEventSource::checkForWork()
59*1b191cb5SApple OSS Distributions {
60*1b191cb5SApple OSS Distributions return false;
61*1b191cb5SApple OSS Distributions }
62*1b191cb5SApple OSS Distributions
63*1b191cb5SApple OSS Distributions /* inline function implementations */
64*1b191cb5SApple OSS Distributions
65*1b191cb5SApple OSS Distributions #if IOKITSTATS
66*1b191cb5SApple OSS Distributions
67*1b191cb5SApple OSS Distributions #define IOStatisticsRegisterCounter() \
68*1b191cb5SApple OSS Distributions do { \
69*1b191cb5SApple OSS Distributions reserved->counter = IOStatistics::registerEventSource(inOwner); \
70*1b191cb5SApple OSS Distributions } while (0)
71*1b191cb5SApple OSS Distributions
72*1b191cb5SApple OSS Distributions #define IOStatisticsUnregisterCounter() \
73*1b191cb5SApple OSS Distributions do { \
74*1b191cb5SApple OSS Distributions if (reserved) \
75*1b191cb5SApple OSS Distributions IOStatistics::unregisterEventSource(reserved->counter); \
76*1b191cb5SApple OSS Distributions } while (0)
77*1b191cb5SApple OSS Distributions
78*1b191cb5SApple OSS Distributions #define IOStatisticsOpenGate() \
79*1b191cb5SApple OSS Distributions do { \
80*1b191cb5SApple OSS Distributions IOStatistics::countOpenGate(reserved->counter); \
81*1b191cb5SApple OSS Distributions } while (0)
82*1b191cb5SApple OSS Distributions
83*1b191cb5SApple OSS Distributions #define IOStatisticsCloseGate() \
84*1b191cb5SApple OSS Distributions do { \
85*1b191cb5SApple OSS Distributions IOStatistics::countCloseGate(reserved->counter); \
86*1b191cb5SApple OSS Distributions } while (0)
87*1b191cb5SApple OSS Distributions
88*1b191cb5SApple OSS Distributions #else
89*1b191cb5SApple OSS Distributions
90*1b191cb5SApple OSS Distributions #define IOStatisticsRegisterCounter()
91*1b191cb5SApple OSS Distributions #define IOStatisticsUnregisterCounter()
92*1b191cb5SApple OSS Distributions #define IOStatisticsOpenGate()
93*1b191cb5SApple OSS Distributions #define IOStatisticsCloseGate()
94*1b191cb5SApple OSS Distributions
95*1b191cb5SApple OSS Distributions #endif /* IOKITSTATS */
96*1b191cb5SApple OSS Distributions
97*1b191cb5SApple OSS Distributions void
signalWorkAvailable()98*1b191cb5SApple OSS Distributions IOEventSource::signalWorkAvailable()
99*1b191cb5SApple OSS Distributions {
100*1b191cb5SApple OSS Distributions workLoop->signalWorkAvailable();
101*1b191cb5SApple OSS Distributions }
102*1b191cb5SApple OSS Distributions
103*1b191cb5SApple OSS Distributions void
openGate()104*1b191cb5SApple OSS Distributions IOEventSource::openGate()
105*1b191cb5SApple OSS Distributions {
106*1b191cb5SApple OSS Distributions IOStatisticsOpenGate();
107*1b191cb5SApple OSS Distributions workLoop->openGate();
108*1b191cb5SApple OSS Distributions }
109*1b191cb5SApple OSS Distributions
110*1b191cb5SApple OSS Distributions void
closeGate()111*1b191cb5SApple OSS Distributions IOEventSource::closeGate()
112*1b191cb5SApple OSS Distributions {
113*1b191cb5SApple OSS Distributions workLoop->closeGate();
114*1b191cb5SApple OSS Distributions IOStatisticsCloseGate();
115*1b191cb5SApple OSS Distributions }
116*1b191cb5SApple OSS Distributions
117*1b191cb5SApple OSS Distributions bool
tryCloseGate()118*1b191cb5SApple OSS Distributions IOEventSource::tryCloseGate()
119*1b191cb5SApple OSS Distributions {
120*1b191cb5SApple OSS Distributions bool res;
121*1b191cb5SApple OSS Distributions if ((res = workLoop->tryCloseGate())) {
122*1b191cb5SApple OSS Distributions IOStatisticsCloseGate();
123*1b191cb5SApple OSS Distributions }
124*1b191cb5SApple OSS Distributions return res;
125*1b191cb5SApple OSS Distributions }
126*1b191cb5SApple OSS Distributions
127*1b191cb5SApple OSS Distributions int
sleepGate(void * event,UInt32 type)128*1b191cb5SApple OSS Distributions IOEventSource::sleepGate(void *event, UInt32 type)
129*1b191cb5SApple OSS Distributions {
130*1b191cb5SApple OSS Distributions int res;
131*1b191cb5SApple OSS Distributions IOStatisticsOpenGate();
132*1b191cb5SApple OSS Distributions res = workLoop->sleepGate(event, type);
133*1b191cb5SApple OSS Distributions IOStatisticsCloseGate();
134*1b191cb5SApple OSS Distributions return res;
135*1b191cb5SApple OSS Distributions }
136*1b191cb5SApple OSS Distributions
137*1b191cb5SApple OSS Distributions int
sleepGate(void * event,AbsoluteTime deadline,UInt32 type)138*1b191cb5SApple OSS Distributions IOEventSource::sleepGate(void *event, AbsoluteTime deadline, UInt32 type)
139*1b191cb5SApple OSS Distributions {
140*1b191cb5SApple OSS Distributions int res;
141*1b191cb5SApple OSS Distributions IOStatisticsOpenGate();
142*1b191cb5SApple OSS Distributions res = workLoop->sleepGate(event, deadline, type);
143*1b191cb5SApple OSS Distributions IOStatisticsCloseGate();
144*1b191cb5SApple OSS Distributions return res;
145*1b191cb5SApple OSS Distributions }
146*1b191cb5SApple OSS Distributions
147*1b191cb5SApple OSS Distributions void
wakeupGate(void * event,bool oneThread)148*1b191cb5SApple OSS Distributions IOEventSource::wakeupGate(void *event, bool oneThread)
149*1b191cb5SApple OSS Distributions {
150*1b191cb5SApple OSS Distributions workLoop->wakeupGate(event, oneThread);
151*1b191cb5SApple OSS Distributions }
152*1b191cb5SApple OSS Distributions
153*1b191cb5SApple OSS Distributions
154*1b191cb5SApple OSS Distributions bool
init(OSObject * inOwner,Action inAction)155*1b191cb5SApple OSS Distributions IOEventSource::init(OSObject *inOwner,
156*1b191cb5SApple OSS Distributions Action inAction)
157*1b191cb5SApple OSS Distributions {
158*1b191cb5SApple OSS Distributions if (!inOwner) {
159*1b191cb5SApple OSS Distributions return false;
160*1b191cb5SApple OSS Distributions }
161*1b191cb5SApple OSS Distributions
162*1b191cb5SApple OSS Distributions owner = inOwner;
163*1b191cb5SApple OSS Distributions
164*1b191cb5SApple OSS Distributions if (!super::init()) {
165*1b191cb5SApple OSS Distributions return false;
166*1b191cb5SApple OSS Distributions }
167*1b191cb5SApple OSS Distributions
168*1b191cb5SApple OSS Distributions (void) setAction(inAction);
169*1b191cb5SApple OSS Distributions enabled = true;
170*1b191cb5SApple OSS Distributions
171*1b191cb5SApple OSS Distributions if (!reserved) {
172*1b191cb5SApple OSS Distributions reserved = IOMallocType(ExpansionData);
173*1b191cb5SApple OSS Distributions }
174*1b191cb5SApple OSS Distributions
175*1b191cb5SApple OSS Distributions IOStatisticsRegisterCounter();
176*1b191cb5SApple OSS Distributions
177*1b191cb5SApple OSS Distributions return true;
178*1b191cb5SApple OSS Distributions }
179*1b191cb5SApple OSS Distributions
180*1b191cb5SApple OSS Distributions void
free(void)181*1b191cb5SApple OSS Distributions IOEventSource::free( void )
182*1b191cb5SApple OSS Distributions {
183*1b191cb5SApple OSS Distributions IOStatisticsUnregisterCounter();
184*1b191cb5SApple OSS Distributions
185*1b191cb5SApple OSS Distributions if ((kActionBlock & flags) && actionBlock) {
186*1b191cb5SApple OSS Distributions Block_release(actionBlock);
187*1b191cb5SApple OSS Distributions }
188*1b191cb5SApple OSS Distributions
189*1b191cb5SApple OSS Distributions if (reserved) {
190*1b191cb5SApple OSS Distributions IOFreeType(reserved, ExpansionData);
191*1b191cb5SApple OSS Distributions }
192*1b191cb5SApple OSS Distributions
193*1b191cb5SApple OSS Distributions super::free();
194*1b191cb5SApple OSS Distributions }
195*1b191cb5SApple OSS Distributions
196*1b191cb5SApple OSS Distributions void
setRefcon(void * newrefcon)197*1b191cb5SApple OSS Distributions IOEventSource::setRefcon(void *newrefcon)
198*1b191cb5SApple OSS Distributions {
199*1b191cb5SApple OSS Distributions refcon = newrefcon;
200*1b191cb5SApple OSS Distributions }
201*1b191cb5SApple OSS Distributions
202*1b191cb5SApple OSS Distributions void *
getRefcon() const203*1b191cb5SApple OSS Distributions IOEventSource::getRefcon() const
204*1b191cb5SApple OSS Distributions {
205*1b191cb5SApple OSS Distributions return refcon;
206*1b191cb5SApple OSS Distributions }
207*1b191cb5SApple OSS Distributions
208*1b191cb5SApple OSS Distributions IOEventSource::Action
getAction() const209*1b191cb5SApple OSS Distributions IOEventSource::getAction() const
210*1b191cb5SApple OSS Distributions {
211*1b191cb5SApple OSS Distributions if (kActionBlock & flags) {
212*1b191cb5SApple OSS Distributions return NULL;
213*1b191cb5SApple OSS Distributions }
214*1b191cb5SApple OSS Distributions return action;
215*1b191cb5SApple OSS Distributions }
216*1b191cb5SApple OSS Distributions
217*1b191cb5SApple OSS Distributions IOEventSource::ActionBlock
getActionBlock(ActionBlock) const218*1b191cb5SApple OSS Distributions IOEventSource::getActionBlock(ActionBlock) const
219*1b191cb5SApple OSS Distributions {
220*1b191cb5SApple OSS Distributions if (kActionBlock & flags) {
221*1b191cb5SApple OSS Distributions return actionBlock;
222*1b191cb5SApple OSS Distributions }
223*1b191cb5SApple OSS Distributions return NULL;
224*1b191cb5SApple OSS Distributions }
225*1b191cb5SApple OSS Distributions
226*1b191cb5SApple OSS Distributions void
setAction(Action inAction)227*1b191cb5SApple OSS Distributions IOEventSource::setAction(Action inAction)
228*1b191cb5SApple OSS Distributions {
229*1b191cb5SApple OSS Distributions if ((kActionBlock & flags) && actionBlock) {
230*1b191cb5SApple OSS Distributions Block_release(actionBlock);
231*1b191cb5SApple OSS Distributions }
232*1b191cb5SApple OSS Distributions action = inAction;
233*1b191cb5SApple OSS Distributions flags &= ~kActionBlock;
234*1b191cb5SApple OSS Distributions }
235*1b191cb5SApple OSS Distributions
236*1b191cb5SApple OSS Distributions void
setActionBlock(ActionBlock block)237*1b191cb5SApple OSS Distributions IOEventSource::setActionBlock(ActionBlock block)
238*1b191cb5SApple OSS Distributions {
239*1b191cb5SApple OSS Distributions if ((kActionBlock & flags) && actionBlock) {
240*1b191cb5SApple OSS Distributions Block_release(actionBlock);
241*1b191cb5SApple OSS Distributions }
242*1b191cb5SApple OSS Distributions actionBlock = Block_copy(block);
243*1b191cb5SApple OSS Distributions flags |= kActionBlock;
244*1b191cb5SApple OSS Distributions }
245*1b191cb5SApple OSS Distributions
246*1b191cb5SApple OSS Distributions IOEventSource *
getNext() const247*1b191cb5SApple OSS Distributions IOEventSource::getNext() const
248*1b191cb5SApple OSS Distributions {
249*1b191cb5SApple OSS Distributions return eventChainNext;
250*1b191cb5SApple OSS Distributions };
251*1b191cb5SApple OSS Distributions
252*1b191cb5SApple OSS Distributions void
setNext(IOEventSource * inNext)253*1b191cb5SApple OSS Distributions IOEventSource::setNext(IOEventSource *inNext)
254*1b191cb5SApple OSS Distributions {
255*1b191cb5SApple OSS Distributions eventChainNext = inNext;
256*1b191cb5SApple OSS Distributions }
257*1b191cb5SApple OSS Distributions
258*1b191cb5SApple OSS Distributions void
enable()259*1b191cb5SApple OSS Distributions IOEventSource::enable()
260*1b191cb5SApple OSS Distributions {
261*1b191cb5SApple OSS Distributions enabled = true;
262*1b191cb5SApple OSS Distributions if (workLoop) {
263*1b191cb5SApple OSS Distributions return signalWorkAvailable();
264*1b191cb5SApple OSS Distributions }
265*1b191cb5SApple OSS Distributions }
266*1b191cb5SApple OSS Distributions
267*1b191cb5SApple OSS Distributions void
disable()268*1b191cb5SApple OSS Distributions IOEventSource::disable()
269*1b191cb5SApple OSS Distributions {
270*1b191cb5SApple OSS Distributions enabled = false;
271*1b191cb5SApple OSS Distributions }
272*1b191cb5SApple OSS Distributions
273*1b191cb5SApple OSS Distributions bool
isEnabled() const274*1b191cb5SApple OSS Distributions IOEventSource::isEnabled() const
275*1b191cb5SApple OSS Distributions {
276*1b191cb5SApple OSS Distributions return enabled;
277*1b191cb5SApple OSS Distributions }
278*1b191cb5SApple OSS Distributions
279*1b191cb5SApple OSS Distributions void
setWorkLoop(IOWorkLoop * inWorkLoop)280*1b191cb5SApple OSS Distributions IOEventSource::setWorkLoop(IOWorkLoop *inWorkLoop)
281*1b191cb5SApple OSS Distributions {
282*1b191cb5SApple OSS Distributions if (!inWorkLoop) {
283*1b191cb5SApple OSS Distributions disable();
284*1b191cb5SApple OSS Distributions }
285*1b191cb5SApple OSS Distributions workLoop = inWorkLoop;
286*1b191cb5SApple OSS Distributions }
287*1b191cb5SApple OSS Distributions
288*1b191cb5SApple OSS Distributions IOWorkLoop *
getWorkLoop() const289*1b191cb5SApple OSS Distributions IOEventSource::getWorkLoop() const
290*1b191cb5SApple OSS Distributions {
291*1b191cb5SApple OSS Distributions return workLoop;
292*1b191cb5SApple OSS Distributions }
293*1b191cb5SApple OSS Distributions
294*1b191cb5SApple OSS Distributions bool
onThread() const295*1b191cb5SApple OSS Distributions IOEventSource::onThread() const
296*1b191cb5SApple OSS Distributions {
297*1b191cb5SApple OSS Distributions return (workLoop != NULL) && workLoop->onThread();
298*1b191cb5SApple OSS Distributions }
299