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/*
29Copyright (c) 1998 Apple Computer, Inc.  All rights reserved.
30
31HISTORY
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
40#define super OSObject
41
42OSDefineMetaClassAndAbstractStructors(IOEventSource, OSObject)
43
44OSMetaClassDefineReservedUnused(IOEventSource, 0);
45OSMetaClassDefineReservedUnused(IOEventSource, 1);
46OSMetaClassDefineReservedUnused(IOEventSource, 2);
47OSMetaClassDefineReservedUnused(IOEventSource, 3);
48OSMetaClassDefineReservedUnused(IOEventSource, 4);
49OSMetaClassDefineReservedUnused(IOEventSource, 5);
50OSMetaClassDefineReservedUnused(IOEventSource, 6);
51OSMetaClassDefineReservedUnused(IOEventSource, 7);
52
53bool IOEventSource::checkForWork() { return false; }
54
55/* inline function implementations */
56
57#if IOKITSTATS
58
59#define IOStatisticsRegisterCounter() \
60do { \
61	reserved->counter = IOStatistics::registerEventSource(inOwner); \
62} while (0)
63
64#define IOStatisticsUnregisterCounter() \
65do { \
66	if (reserved) \
67		IOStatistics::unregisterEventSource(reserved->counter); \
68} while (0)
69
70#define IOStatisticsOpenGate() \
71do { \
72	IOStatistics::countOpenGate(reserved->counter); \
73} while (0)
74
75#define IOStatisticsCloseGate() \
76do { \
77	IOStatistics::countCloseGate(reserved->counter); \
78} while (0)
79
80#else
81
82#define IOStatisticsRegisterCounter()
83#define IOStatisticsUnregisterCounter()
84#define IOStatisticsOpenGate()
85#define IOStatisticsCloseGate()
86
87#endif /* IOKITSTATS */
88
89void IOEventSource::signalWorkAvailable()
90{
91	workLoop->signalWorkAvailable();
92}
93
94void IOEventSource::openGate()
95{
96	IOStatisticsOpenGate();
97	workLoop->openGate();
98}
99
100void IOEventSource::closeGate()
101{
102	workLoop->closeGate();
103	IOStatisticsCloseGate();
104}
105
106bool IOEventSource::tryCloseGate()
107{
108	bool res;
109	if ((res = workLoop->tryCloseGate())) {
110		IOStatisticsCloseGate();
111	}
112	return res;
113}
114
115int IOEventSource::sleepGate(void *event, UInt32 type)
116{
117	bool res;
118	IOStatisticsOpenGate();
119	res = workLoop->sleepGate(event, type);
120	IOStatisticsCloseGate();
121	return res;
122}
123
124int IOEventSource::sleepGate(void *event, AbsoluteTime deadline, UInt32 type)
125{
126	bool res;
127	IOStatisticsOpenGate();
128	res = workLoop->sleepGate(event, deadline, type);
129	IOStatisticsCloseGate();
130	return res;
131}
132
133void IOEventSource::wakeupGate(void *event, bool oneThread) { workLoop->wakeupGate(event, oneThread); }
134
135
136bool IOEventSource::init(OSObject *inOwner,
137                         Action inAction)
138{
139    if (!inOwner)
140        return false;
141
142    owner = inOwner;
143
144    if ( !super::init() )
145        return false;
146
147    (void) setAction(inAction);
148    enabled = true;
149
150    if(!reserved) {
151        reserved = IONew(ExpansionData, 1);
152        if (!reserved) {
153            return false;
154        }
155    }
156
157    IOStatisticsRegisterCounter();
158
159    return true;
160}
161
162void IOEventSource::free( void )
163{
164    IOStatisticsUnregisterCounter();
165
166    if (reserved)
167		IODelete(reserved, ExpansionData, 1);
168
169    super::free();
170}
171
172IOEventSource::Action IOEventSource::getAction () const { return action; };
173
174void IOEventSource::setAction(Action inAction)
175{
176    action = inAction;
177}
178
179IOEventSource *IOEventSource::getNext() const { return eventChainNext; };
180
181void IOEventSource::setNext(IOEventSource *inNext)
182{
183    eventChainNext = inNext;
184}
185
186void IOEventSource::enable()
187{
188    enabled = true;
189    if (workLoop)
190        return signalWorkAvailable();
191}
192
193void IOEventSource::disable()
194{
195    enabled = false;
196}
197
198bool IOEventSource::isEnabled() const
199{
200    return enabled;
201}
202
203void IOEventSource::setWorkLoop(IOWorkLoop *inWorkLoop)
204{
205    if ( !inWorkLoop )
206        disable();
207    workLoop = inWorkLoop;
208}
209
210IOWorkLoop *IOEventSource::getWorkLoop() const
211{
212    return workLoop;
213}
214
215bool IOEventSource::onThread() const
216{
217    return (workLoop != 0) && workLoop->onThread();
218}
219