2 * Copyright (c) 1998-2000, 2009-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/cdefs.h>
32 #include <kern/thread_call.h>
35 #include <IOKit/assert.h>
36 #include <IOKit/system.h>
38 #include <IOKit/IOLib.h>
39 #include <IOKit/IOTimerEventSource.h>
40 #include <IOKit/IOWorkLoop.h>
42 #include <IOKit/IOTimeStamp.h>
43 #include <IOKit/IOKitDebug.h>
45 #define super IOEventSource
46 OSDefineMetaClassAndStructors(IOTimerEventSource
, IOEventSource
)
47 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 0);
48 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 1);
49 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 2);
50 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 3);
51 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 4);
52 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 5);
53 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 6);
54 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 7);
58 #define IOStatisticsInitializeCounter() \
60 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsTimerEventSourceCounter); \
63 #define IOStatisticsOpenGate() \
65 IOStatistics::countOpenGate(me->IOEventSource::reserved->counter); \
68 #define IOStatisticsCloseGate() \
70 IOStatistics::countCloseGate(me->IOEventSource::reserved->counter); \
73 #define IOStatisticsTimeout() \
75 IOStatistics::countTimerTimeout(me->IOEventSource::reserved->counter); \
80 #define IOStatisticsInitializeCounter()
81 #define IOStatisticsOpenGate()
82 #define IOStatisticsCloseGate()
83 #define IOStatisticsTimeout()
85 #endif /* IOKITSTATS */
88 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
89 // not a subclassed implementation.
92 // Timeout handler function. This function is called by the kernel when
93 // the timeout interval expires.
95 void IOTimerEventSource::timeout(void *self
)
97 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
99 IOStatisticsTimeout();
101 if (me
->enabled
&& me
->action
)
109 IOStatisticsCloseGate();
110 doit
= (Action
) me
->action
;
111 if (doit
&& me
->enabled
&& AbsoluteTime_to_scalar(&me
->abstime
))
113 bool trace
= (gIOKitTrace
& kIOTraceTimers
) ? true : false;
116 IOTimeStampStartConstant(IODBG_TIMES(IOTIMES_ACTION
),
117 (uintptr_t) doit
, (uintptr_t) me
->owner
);
119 (*doit
)(me
->owner
, me
);
122 IOTimeStampEndConstant(IODBG_TIMES(IOTIMES_ACTION
),
123 (uintptr_t) doit
, (uintptr_t) me
->owner
);
125 IOStatisticsOpenGate();
131 void IOTimerEventSource::timeoutAndRelease(void * self
, void * c
)
133 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
134 /* The second parameter (a pointer) gets abused to carry an SInt32, so on LP64, "count"
135 must be cast to "long" before, in order to tell GCC we're not truncating a pointer. */
136 SInt32 count
= (SInt32
) (long) c
;
138 IOStatisticsTimeout();
140 if (me
->enabled
&& me
->action
)
143 wl
= me
->reserved
->workLoop
;
148 IOStatisticsCloseGate();
149 doit
= (Action
) me
->action
;
150 if (doit
&& (me
->reserved
->calloutGeneration
== count
))
152 bool trace
= (gIOKitTrace
& kIOTraceTimers
) ? true : false;
155 IOTimeStampStartConstant(IODBG_TIMES(IOTIMES_ACTION
),
156 (uintptr_t) doit
, (uintptr_t) me
->owner
);
158 (*doit
)(me
->owner
, me
);
161 IOTimeStampEndConstant(IODBG_TIMES(IOTIMES_ACTION
),
162 (uintptr_t) doit
, (uintptr_t) me
->owner
);
164 IOStatisticsOpenGate();
169 me
->reserved
->workLoop
->release();
173 void IOTimerEventSource::setTimeoutFunc()
175 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
176 // not a subclassed implementation
177 reserved
= IONew(ExpansionData
, 1);
178 calloutEntry
= (void *) thread_call_allocate((thread_call_func_t
) &IOTimerEventSource::timeoutAndRelease
,
179 (thread_call_param_t
) this);
182 bool IOTimerEventSource::init(OSObject
*inOwner
, Action inAction
)
184 if (!super::init(inOwner
, (IOEventSource::Action
) inAction
) )
191 IOStatisticsInitializeCounter();
197 IOTimerEventSource::timerEventSource(OSObject
*inOwner
, Action inAction
)
199 IOTimerEventSource
*me
= new IOTimerEventSource
;
201 if (me
&& !me
->init(inOwner
, inAction
)) {
209 void IOTimerEventSource::free()
213 thread_call_free((thread_call_t
) calloutEntry
);
217 IODelete(reserved
, ExpansionData
, 1);
222 void IOTimerEventSource::cancelTimeout()
225 reserved
->calloutGeneration
++;
226 bool active
= thread_call_cancel((thread_call_t
) calloutEntry
);
227 AbsoluteTime_to_scalar(&abstime
) = 0;
228 if (active
&& reserved
)
235 void IOTimerEventSource::enable()
238 if (kIOReturnSuccess
!= wakeAtTime(abstime
))
239 super::disable(); // Problem re-scheduling timeout ignore enable
242 void IOTimerEventSource::disable()
245 reserved
->calloutGeneration
++;
246 bool active
= thread_call_cancel((thread_call_t
) calloutEntry
);
248 if (active
&& reserved
)
255 IOReturn
IOTimerEventSource::setTimeoutTicks(UInt32 ticks
)
257 return setTimeout(ticks
, kTickScale
);
260 IOReturn
IOTimerEventSource::setTimeoutMS(UInt32 ms
)
262 return setTimeout(ms
, kMillisecondScale
);
265 IOReturn
IOTimerEventSource::setTimeoutUS(UInt32 us
)
267 return setTimeout(us
, kMicrosecondScale
);
270 IOReturn
IOTimerEventSource::setTimeout(UInt32 interval
, UInt32 scale_factor
)
274 clock_interval_to_deadline(interval
, scale_factor
, &end
);
275 return wakeAtTime(end
);
278 #if !defined(__LP64__)
279 IOReturn
IOTimerEventSource::setTimeout(mach_timespec_t interval
)
281 AbsoluteTime end
, nsecs
;
283 clock_interval_to_absolutetime_interval
284 (interval
.tv_nsec
, kNanosecondScale
, &nsecs
);
285 clock_interval_to_deadline
286 (interval
.tv_sec
, NSEC_PER_SEC
, &end
);
287 ADD_ABSOLUTETIME(&end
, &nsecs
);
289 return wakeAtTime(end
);
293 IOReturn
IOTimerEventSource::setTimeout(AbsoluteTime interval
)
297 clock_get_uptime(&end
);
298 ADD_ABSOLUTETIME(&end
, &interval
);
300 return wakeAtTime(end
);
303 IOReturn
IOTimerEventSource::wakeAtTimeTicks(UInt32 ticks
)
305 return wakeAtTime(ticks
, kTickScale
);
308 IOReturn
IOTimerEventSource::wakeAtTimeMS(UInt32 ms
)
310 return wakeAtTime(ms
, kMillisecondScale
);
313 IOReturn
IOTimerEventSource::wakeAtTimeUS(UInt32 us
)
315 return wakeAtTime(us
, kMicrosecondScale
);
318 IOReturn
IOTimerEventSource::wakeAtTime(UInt32 inAbstime
, UInt32 scale_factor
)
321 clock_interval_to_absolutetime_interval(inAbstime
, scale_factor
, &end
);
323 return wakeAtTime(end
);
326 #if !defined(__LP64__)
327 IOReturn
IOTimerEventSource::wakeAtTime(mach_timespec_t inAbstime
)
329 AbsoluteTime end
, nsecs
;
331 clock_interval_to_absolutetime_interval
332 (inAbstime
.tv_nsec
, kNanosecondScale
, &nsecs
);
333 clock_interval_to_absolutetime_interval
334 (inAbstime
.tv_sec
, kSecondScale
, &end
);
335 ADD_ABSOLUTETIME(&end
, &nsecs
);
337 return wakeAtTime(end
);
341 void IOTimerEventSource::setWorkLoop(IOWorkLoop
*inWorkLoop
)
343 super::setWorkLoop(inWorkLoop
);
344 if ( enabled
&& AbsoluteTime_to_scalar(&abstime
) && workLoop
)
348 IOReturn
IOTimerEventSource::wakeAtTime(AbsoluteTime inAbstime
)
351 return kIOReturnNoResources
;
354 if ( enabled
&& AbsoluteTime_to_scalar(&inAbstime
) && AbsoluteTime_to_scalar(&abstime
) && workLoop
)
360 reserved
->workLoop
= workLoop
;
361 reserved
->calloutGeneration
++;
362 if (thread_call_enter1_delayed((thread_call_t
) calloutEntry
,
363 (void *) reserved
->calloutGeneration
, inAbstime
))
370 thread_call_enter_delayed((thread_call_t
) calloutEntry
, inAbstime
);
373 return kIOReturnSuccess
;