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>
48 #include <libkern/Block.h>
51 #define super IOEventSource
52 OSDefineMetaClassAndStructors(IOTimerEventSource
, IOEventSource
)
53 OSMetaClassDefineReservedUsed(IOTimerEventSource
, 0);
54 OSMetaClassDefineReservedUsed(IOTimerEventSource
, 1);
55 OSMetaClassDefineReservedUsed(IOTimerEventSource
, 2);
56 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 3);
57 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 4);
58 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 5);
59 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 6);
60 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 7);
64 #define IOStatisticsInitializeCounter() \
66 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsTimerEventSourceCounter); \
69 #define IOStatisticsOpenGate() \
71 IOStatistics::countOpenGate(me->IOEventSource::reserved->counter); \
74 #define IOStatisticsCloseGate() \
76 IOStatistics::countCloseGate(me->IOEventSource::reserved->counter); \
79 #define IOStatisticsTimeout() \
81 IOStatistics::countTimerTimeout(me->IOEventSource::reserved->counter); \
86 #define IOStatisticsInitializeCounter()
87 #define IOStatisticsOpenGate()
88 #define IOStatisticsCloseGate()
89 #define IOStatisticsTimeout()
91 #endif /* IOKITSTATS */
94 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
95 // not a subclassed implementation.
98 // Timeout handler function. This function is called by the kernel when
99 // the timeout interval expires.
103 IOTimerEventSource::invokeAction(IOTimerEventSource::Action _action
, IOTimerEventSource
* ts
,
104 OSObject
* _owner
, IOWorkLoop
* _workLoop
)
106 bool trace
= (gIOKitTrace
& kIOTraceTimers
) ? true : false;
109 IOTimeStampStartConstant(IODBG_TIMES(IOTIMES_ACTION
),
110 VM_KERNEL_ADDRHIDE(_action
), VM_KERNEL_ADDRHIDE(_owner
));
113 if (kActionBlock
& flags
) {
114 ((IOTimerEventSource::ActionBlock
) actionBlock
)(ts
);
116 (*_action
)(_owner
, ts
);
120 DTRACE_TMR3(iotescallout__expire
, Action
, _action
, OSObject
, _owner
, void, _workLoop
);
124 IOTimeStampEndConstant(IODBG_TIMES(IOTIMES_ACTION
),
125 VM_KERNEL_UNSLIDE(_action
), VM_KERNEL_ADDRHIDE(_owner
));
130 IOTimerEventSource::timeout(void *self
)
132 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
134 IOStatisticsTimeout();
136 if (me
->enabled
&& me
->action
) {
142 IOStatisticsCloseGate();
143 doit
= (Action
) me
->action
;
144 if (doit
&& me
->enabled
&& AbsoluteTime_to_scalar(&me
->abstime
)) {
145 me
->invokeAction(doit
, me
, me
->owner
, me
->workLoop
);
147 IOStatisticsOpenGate();
154 IOTimerEventSource::timeoutAndRelease(void * self
, void * c
)
156 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
157 /* The second parameter (a pointer) gets abused to carry an SInt32, so on LP64, "count"
158 * must be cast to "long" before, in order to tell GCC we're not truncating a pointer. */
159 SInt32 count
= (SInt32
) (long) c
;
161 IOStatisticsTimeout();
163 if (me
->enabled
&& me
->action
) {
165 wl
= me
->reserved
->workLoop
;
169 IOStatisticsCloseGate();
170 doit
= (Action
) me
->action
;
171 if (doit
&& (me
->reserved
->calloutGeneration
== count
)) {
172 me
->invokeAction(doit
, me
, me
->owner
, me
->workLoop
);
174 IOStatisticsOpenGate();
179 me
->reserved
->workLoop
->release();
183 // -- work loop delivery
186 IOTimerEventSource::checkForWork()
191 && (reserved
->calloutGenerationSignaled
== reserved
->calloutGeneration
)
192 && enabled
&& (doit
= (Action
) action
)) {
193 reserved
->calloutGenerationSignaled
= ~reserved
->calloutGeneration
;
194 invokeAction(doit
, this, owner
, workLoop
);
201 IOTimerEventSource::timeoutSignaled(void * self
, void * c
)
203 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
205 me
->reserved
->calloutGenerationSignaled
= (SInt32
)(long) c
;
207 me
->signalWorkAvailable();
214 IOTimerEventSource::setTimeoutFunc()
216 thread_call_priority_t pri
;
220 panic("setTimeoutFunc already %p, %p", this, reserved
);
223 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
224 // not a subclassed implementation
225 reserved
= IONew(ExpansionData
, 1);
226 reserved
->calloutGenerationSignaled
= ~reserved
->calloutGeneration
;
230 thread_call_options_t tcoptions
= 0;
231 thread_call_func_t func
= NULL
;
233 switch (kIOTimerEventSourceOptionsPriorityMask
& options
) {
234 case kIOTimerEventSourceOptionsPriorityHigh
:
235 pri
= THREAD_CALL_PRIORITY_HIGH
;
236 func
= &IOTimerEventSource::timeoutAndRelease
;
239 case kIOTimerEventSourceOptionsPriorityKernel
:
240 pri
= THREAD_CALL_PRIORITY_KERNEL
;
241 func
= &IOTimerEventSource::timeoutAndRelease
;
244 case kIOTimerEventSourceOptionsPriorityKernelHigh
:
245 pri
= THREAD_CALL_PRIORITY_KERNEL_HIGH
;
246 func
= &IOTimerEventSource::timeoutAndRelease
;
249 case kIOTimerEventSourceOptionsPriorityUser
:
250 pri
= THREAD_CALL_PRIORITY_USER
;
251 func
= &IOTimerEventSource::timeoutAndRelease
;
254 case kIOTimerEventSourceOptionsPriorityLow
:
255 pri
= THREAD_CALL_PRIORITY_LOW
;
256 func
= &IOTimerEventSource::timeoutAndRelease
;
259 case kIOTimerEventSourceOptionsPriorityWorkLoop
:
260 pri
= THREAD_CALL_PRIORITY_KERNEL
;
261 tcoptions
|= THREAD_CALL_OPTIONS_SIGNAL
;
262 if (kIOTimerEventSourceOptionsAllowReenter
& options
) {
265 func
= &IOTimerEventSource::timeoutSignaled
;
272 assertf(func
, "IOTimerEventSource options 0x%x", options
);
274 return; // init will fail
276 if (THREAD_CALL_OPTIONS_SIGNAL
& tcoptions
) {
282 if (!(kIOTimerEventSourceOptionsAllowReenter
& options
)) {
283 tcoptions
|= THREAD_CALL_OPTIONS_ONCE
;
286 calloutEntry
= (void *) thread_call_allocate_with_options(func
,
287 (thread_call_param_t
) this, pri
, tcoptions
);
288 assert(calloutEntry
);
292 IOTimerEventSource::init(OSObject
*inOwner
, Action inAction
)
294 if (!super::init(inOwner
, (IOEventSource::Action
) inAction
)) {
303 IOStatisticsInitializeCounter();
309 IOTimerEventSource::init(uint32_t options
, OSObject
*inOwner
, Action inAction
)
312 return init(inOwner
, inAction
);
316 IOTimerEventSource::timerEventSource(uint32_t inOptions
, OSObject
*inOwner
, Action inAction
)
318 IOTimerEventSource
*me
= new IOTimerEventSource
;
320 if (me
&& !me
->init(inOptions
, inOwner
, inAction
)) {
329 IOTimerEventSource::timerEventSource(uint32_t options
, OSObject
*inOwner
, ActionBlock _action
)
331 IOTimerEventSource
* tes
;
332 tes
= IOTimerEventSource::timerEventSource(options
, inOwner
, (Action
) NULL
);
334 tes
->setActionBlock((IOEventSource::ActionBlock
) _action
);
340 #define _thread_call_cancel(tc) ((kActive & flags) ? thread_call_cancel_wait((tc)) : thread_call_cancel((tc)))
343 IOTimerEventSource::timerEventSource(OSObject
*inOwner
, Action inAction
)
345 return IOTimerEventSource::timerEventSource(
346 kIOTimerEventSourceOptionsPriorityKernelHigh
,
351 IOTimerEventSource::free()
354 __assert_only
bool freed
;
358 freed
= thread_call_free((thread_call_t
) calloutEntry
);
363 IODelete(reserved
, ExpansionData
, 1);
370 IOTimerEventSource::cancelTimeout()
373 reserved
->calloutGeneration
++;
375 bool active
= _thread_call_cancel((thread_call_t
) calloutEntry
);
376 AbsoluteTime_to_scalar(&abstime
) = 0;
377 if (active
&& reserved
&& (kPassive
& flags
)) {
384 IOTimerEventSource::enable()
387 if (kIOReturnSuccess
!= wakeAtTime(abstime
)) {
388 super::disable(); // Problem re-scheduling timeout ignore enable
393 IOTimerEventSource::disable()
396 reserved
->calloutGeneration
++;
398 bool active
= _thread_call_cancel((thread_call_t
) calloutEntry
);
400 if (active
&& reserved
&& (kPassive
& flags
)) {
407 IOTimerEventSource::setTimeoutTicks(UInt32 ticks
)
409 return setTimeout(ticks
, kTickScale
);
413 IOTimerEventSource::setTimeoutMS(UInt32 ms
)
415 return setTimeout(ms
, kMillisecondScale
);
419 IOTimerEventSource::setTimeoutUS(UInt32 us
)
421 return setTimeout(us
, kMicrosecondScale
);
425 IOTimerEventSource::setTimeout(UInt32 interval
, UInt32 scale_factor
)
429 clock_interval_to_deadline(interval
, scale_factor
, &end
);
430 return wakeAtTime(end
);
433 #if !defined(__LP64__)
435 IOTimerEventSource::setTimeout(mach_timespec_t interval
)
437 AbsoluteTime end
, nsecs
;
439 clock_interval_to_absolutetime_interval
440 (interval
.tv_nsec
, kNanosecondScale
, &nsecs
);
441 clock_interval_to_deadline
442 (interval
.tv_sec
, NSEC_PER_SEC
, &end
);
443 ADD_ABSOLUTETIME(&end
, &nsecs
);
445 return wakeAtTime(end
);
450 IOTimerEventSource::setTimeout(AbsoluteTime interval
)
453 clock_absolutetime_interval_to_deadline(interval
, &end
);
454 return wakeAtTime(end
);
458 IOTimerEventSource::setTimeout(uint32_t options
,
459 AbsoluteTime abstime
, AbsoluteTime leeway
)
462 if (options
& kIOTimeOptionsContinuous
) {
463 clock_continuoustime_interval_to_deadline(abstime
, &end
);
465 clock_absolutetime_interval_to_deadline(abstime
, &end
);
468 return wakeAtTime(options
, end
, leeway
);
472 IOTimerEventSource::wakeAtTimeTicks(UInt32 ticks
)
474 return wakeAtTime(ticks
, kTickScale
);
478 IOTimerEventSource::wakeAtTimeMS(UInt32 ms
)
480 return wakeAtTime(ms
, kMillisecondScale
);
484 IOTimerEventSource::wakeAtTimeUS(UInt32 us
)
486 return wakeAtTime(us
, kMicrosecondScale
);
490 IOTimerEventSource::wakeAtTime(UInt32 inAbstime
, UInt32 scale_factor
)
493 clock_interval_to_absolutetime_interval(inAbstime
, scale_factor
, &end
);
495 return wakeAtTime(end
);
498 #if !defined(__LP64__)
500 IOTimerEventSource::wakeAtTime(mach_timespec_t inAbstime
)
502 AbsoluteTime end
, nsecs
;
504 clock_interval_to_absolutetime_interval
505 (inAbstime
.tv_nsec
, kNanosecondScale
, &nsecs
);
506 clock_interval_to_absolutetime_interval
507 (inAbstime
.tv_sec
, kSecondScale
, &end
);
508 ADD_ABSOLUTETIME(&end
, &nsecs
);
510 return wakeAtTime(end
);
515 IOTimerEventSource::setWorkLoop(IOWorkLoop
*inWorkLoop
)
517 super::setWorkLoop(inWorkLoop
);
518 if (enabled
&& AbsoluteTime_to_scalar(&abstime
) && workLoop
) {
524 IOTimerEventSource::wakeAtTime(AbsoluteTime inAbstime
)
526 return wakeAtTime(0, inAbstime
, 0);
530 IOTimerEventSource::wakeAtTime(uint32_t options
, AbsoluteTime inAbstime
, AbsoluteTime leeway
)
533 return kIOReturnNoResources
;
537 if (enabled
&& AbsoluteTime_to_scalar(&inAbstime
) && AbsoluteTime_to_scalar(&abstime
) && workLoop
) {
538 uint32_t tcoptions
= 0;
540 if (kIOTimeOptionsWithLeeway
& options
) {
541 tcoptions
|= THREAD_CALL_DELAY_LEEWAY
;
543 if (kIOTimeOptionsContinuous
& options
) {
544 tcoptions
|= THREAD_CALL_CONTINUOUS
;
548 if (kPassive
& flags
) {
552 reserved
->workLoop
= workLoop
;
553 reserved
->calloutGeneration
++;
554 if (thread_call_enter_delayed_with_leeway((thread_call_t
) calloutEntry
,
555 (void *)(uintptr_t) reserved
->calloutGeneration
, inAbstime
, leeway
, tcoptions
)
556 && (kPassive
& flags
)) {
561 thread_call_enter_delayed_with_leeway((thread_call_t
) calloutEntry
,
562 NULL
, inAbstime
, leeway
, tcoptions
);
566 return kIOReturnSuccess
;