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 #define super IOEventSource
49 OSDefineMetaClassAndStructors(IOTimerEventSource
, IOEventSource
)
50 OSMetaClassDefineReservedUsed(IOTimerEventSource
, 0);
51 OSMetaClassDefineReservedUsed(IOTimerEventSource
, 1);
52 OSMetaClassDefineReservedUsed(IOTimerEventSource
, 2);
53 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 3);
54 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 4);
55 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 5);
56 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 6);
57 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 7);
61 #define IOStatisticsInitializeCounter() \
63 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsTimerEventSourceCounter); \
66 #define IOStatisticsOpenGate() \
68 IOStatistics::countOpenGate(me->IOEventSource::reserved->counter); \
71 #define IOStatisticsCloseGate() \
73 IOStatistics::countCloseGate(me->IOEventSource::reserved->counter); \
76 #define IOStatisticsTimeout() \
78 IOStatistics::countTimerTimeout(me->IOEventSource::reserved->counter); \
83 #define IOStatisticsInitializeCounter()
84 #define IOStatisticsOpenGate()
85 #define IOStatisticsCloseGate()
86 #define IOStatisticsTimeout()
88 #endif /* IOKITSTATS */
91 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
92 // not a subclassed implementation.
95 // Timeout handler function. This function is called by the kernel when
96 // the timeout interval expires.
99 static __inline__
void
100 InvokeAction(IOTimerEventSource::Action action
, IOTimerEventSource
* ts
,
101 OSObject
* owner
, IOWorkLoop
* workLoop
)
103 bool trace
= (gIOKitTrace
& kIOTraceTimers
) ? true : false;
106 IOTimeStampStartConstant(IODBG_TIMES(IOTIMES_ACTION
),
107 VM_KERNEL_ADDRHIDE(action
), VM_KERNEL_ADDRHIDE(owner
));
109 (*action
)(owner
, ts
);
112 DTRACE_TMR3(iotescallout__expire
, Action
, action
, OSObject
, owner
, void, workLoop
);
116 IOTimeStampEndConstant(IODBG_TIMES(IOTIMES_ACTION
),
117 VM_KERNEL_UNSLIDE(action
), VM_KERNEL_ADDRHIDE(owner
));
120 void IOTimerEventSource::timeout(void *self
)
122 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
124 IOStatisticsTimeout();
126 if (me
->enabled
&& me
->action
)
134 IOStatisticsCloseGate();
135 doit
= (Action
) me
->action
;
136 if (doit
&& me
->enabled
&& AbsoluteTime_to_scalar(&me
->abstime
))
138 InvokeAction(doit
, me
, me
->owner
, me
->workLoop
);
140 IOStatisticsOpenGate();
146 void IOTimerEventSource::timeoutAndRelease(void * self
, void * c
)
148 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
149 /* The second parameter (a pointer) gets abused to carry an SInt32, so on LP64, "count"
150 must be cast to "long" before, in order to tell GCC we're not truncating a pointer. */
151 SInt32 count
= (SInt32
) (long) c
;
153 IOStatisticsTimeout();
155 if (me
->enabled
&& me
->action
)
158 wl
= me
->reserved
->workLoop
;
163 IOStatisticsCloseGate();
164 doit
= (Action
) me
->action
;
165 if (doit
&& (me
->reserved
->calloutGeneration
== count
))
167 InvokeAction(doit
, me
, me
->owner
, me
->workLoop
);
169 IOStatisticsOpenGate();
174 me
->reserved
->workLoop
->release();
178 // -- work loop delivery
180 bool IOTimerEventSource::checkForWork()
185 && (reserved
->calloutGenerationSignaled
== reserved
->calloutGeneration
)
186 && enabled
&& (doit
= (Action
) action
))
188 reserved
->calloutGenerationSignaled
= ~reserved
->calloutGeneration
;
189 InvokeAction(doit
, this, owner
, workLoop
);
195 void IOTimerEventSource::timeoutSignaled(void * self
, void * c
)
197 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
199 me
->reserved
->calloutGenerationSignaled
= (SInt32
)(long) c
;
200 if (me
->enabled
) me
->signalWorkAvailable();
205 void IOTimerEventSource::setTimeoutFunc()
207 thread_call_priority_t pri
;
210 if (reserved
) panic("setTimeoutFunc already %p, %p", this, reserved
);
212 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
213 // not a subclassed implementation
214 reserved
= IONew(ExpansionData
, 1);
215 reserved
->calloutGenerationSignaled
= ~reserved
->calloutGeneration
;
219 thread_call_options_t tcoptions
= 0;
220 thread_call_func_t func
= NULL
;
222 switch (kIOTimerEventSourceOptionsPriorityMask
& options
)
224 case kIOTimerEventSourceOptionsPriorityHigh
:
225 pri
= THREAD_CALL_PRIORITY_HIGH
;
226 func
= &IOTimerEventSource::timeoutAndRelease
;
229 case kIOTimerEventSourceOptionsPriorityKernel
:
230 pri
= THREAD_CALL_PRIORITY_KERNEL
;
231 func
= &IOTimerEventSource::timeoutAndRelease
;
234 case kIOTimerEventSourceOptionsPriorityKernelHigh
:
235 pri
= THREAD_CALL_PRIORITY_KERNEL_HIGH
;
236 func
= &IOTimerEventSource::timeoutAndRelease
;
239 case kIOTimerEventSourceOptionsPriorityUser
:
240 pri
= THREAD_CALL_PRIORITY_USER
;
241 func
= &IOTimerEventSource::timeoutAndRelease
;
244 case kIOTimerEventSourceOptionsPriorityLow
:
245 pri
= THREAD_CALL_PRIORITY_LOW
;
246 func
= &IOTimerEventSource::timeoutAndRelease
;
249 case kIOTimerEventSourceOptionsPriorityWorkLoop
:
250 pri
= THREAD_CALL_PRIORITY_KERNEL
;
251 tcoptions
|= THREAD_CALL_OPTIONS_SIGNAL
;
252 if (kIOTimerEventSourceOptionsAllowReenter
& options
) break;
253 func
= &IOTimerEventSource::timeoutSignaled
;
260 assertf(func
, "IOTimerEventSource options 0x%x", options
);
261 if (!func
) return; // init will fail
263 if (THREAD_CALL_OPTIONS_SIGNAL
& tcoptions
) flags
|= kActive
;
264 else flags
|= kPassive
;
266 if (!(kIOTimerEventSourceOptionsAllowReenter
& options
)) tcoptions
|= THREAD_CALL_OPTIONS_ONCE
;
268 calloutEntry
= (void *) thread_call_allocate_with_options(func
,
269 (thread_call_param_t
) this, pri
, tcoptions
);
270 assert(calloutEntry
);
273 bool IOTimerEventSource::init(OSObject
*inOwner
, Action inAction
)
275 if (!super::init(inOwner
, (IOEventSource::Action
) inAction
) )
282 IOStatisticsInitializeCounter();
287 bool IOTimerEventSource::init(uint32_t options
, OSObject
*inOwner
, Action inAction
)
290 return (init(inOwner
, inAction
));
294 IOTimerEventSource::timerEventSource(uint32_t inOptions
, OSObject
*inOwner
, Action inAction
)
296 IOTimerEventSource
*me
= new IOTimerEventSource
;
298 if (me
&& !me
->init(inOptions
, inOwner
, inAction
)) {
306 #define _thread_call_cancel(tc) ((kActive & flags) ? thread_call_cancel_wait((tc)) : thread_call_cancel((tc)))
309 IOTimerEventSource::timerEventSource(OSObject
*inOwner
, Action inAction
)
311 return (IOTimerEventSource::timerEventSource(
312 kIOTimerEventSourceOptionsPriorityKernelHigh
,
316 void IOTimerEventSource::free()
319 __assert_only
bool freed
;
323 freed
= thread_call_free((thread_call_t
) calloutEntry
);
328 IODelete(reserved
, ExpansionData
, 1);
333 void IOTimerEventSource::cancelTimeout()
336 reserved
->calloutGeneration
++;
337 bool active
= _thread_call_cancel((thread_call_t
) calloutEntry
);
338 AbsoluteTime_to_scalar(&abstime
) = 0;
339 if (active
&& reserved
&& (kPassive
& flags
))
346 void IOTimerEventSource::enable()
349 if (kIOReturnSuccess
!= wakeAtTime(abstime
))
350 super::disable(); // Problem re-scheduling timeout ignore enable
353 void IOTimerEventSource::disable()
356 reserved
->calloutGeneration
++;
357 bool active
= _thread_call_cancel((thread_call_t
) calloutEntry
);
359 if (active
&& reserved
&& (kPassive
& flags
))
366 IOReturn
IOTimerEventSource::setTimeoutTicks(UInt32 ticks
)
368 return setTimeout(ticks
, kTickScale
);
371 IOReturn
IOTimerEventSource::setTimeoutMS(UInt32 ms
)
373 return setTimeout(ms
, kMillisecondScale
);
376 IOReturn
IOTimerEventSource::setTimeoutUS(UInt32 us
)
378 return setTimeout(us
, kMicrosecondScale
);
381 IOReturn
IOTimerEventSource::setTimeout(UInt32 interval
, UInt32 scale_factor
)
385 clock_interval_to_deadline(interval
, scale_factor
, &end
);
386 return wakeAtTime(end
);
389 #if !defined(__LP64__)
390 IOReturn
IOTimerEventSource::setTimeout(mach_timespec_t interval
)
392 AbsoluteTime end
, nsecs
;
394 clock_interval_to_absolutetime_interval
395 (interval
.tv_nsec
, kNanosecondScale
, &nsecs
);
396 clock_interval_to_deadline
397 (interval
.tv_sec
, NSEC_PER_SEC
, &end
);
398 ADD_ABSOLUTETIME(&end
, &nsecs
);
400 return wakeAtTime(end
);
404 IOReturn
IOTimerEventSource::setTimeout(AbsoluteTime interval
)
407 clock_absolutetime_interval_to_deadline(interval
, &end
);
408 return wakeAtTime(end
);
411 IOReturn
IOTimerEventSource::setTimeout(uint32_t options
,
412 AbsoluteTime abstime
, AbsoluteTime leeway
)
415 if (options
& kIOTimeOptionsContinuous
)
416 clock_continuoustime_interval_to_deadline(abstime
, &end
);
418 clock_absolutetime_interval_to_deadline(abstime
, &end
);
420 return wakeAtTime(options
, end
, leeway
);
423 IOReturn
IOTimerEventSource::wakeAtTimeTicks(UInt32 ticks
)
425 return wakeAtTime(ticks
, kTickScale
);
428 IOReturn
IOTimerEventSource::wakeAtTimeMS(UInt32 ms
)
430 return wakeAtTime(ms
, kMillisecondScale
);
433 IOReturn
IOTimerEventSource::wakeAtTimeUS(UInt32 us
)
435 return wakeAtTime(us
, kMicrosecondScale
);
438 IOReturn
IOTimerEventSource::wakeAtTime(UInt32 inAbstime
, UInt32 scale_factor
)
441 clock_interval_to_absolutetime_interval(inAbstime
, scale_factor
, &end
);
443 return wakeAtTime(end
);
446 #if !defined(__LP64__)
447 IOReturn
IOTimerEventSource::wakeAtTime(mach_timespec_t inAbstime
)
449 AbsoluteTime end
, nsecs
;
451 clock_interval_to_absolutetime_interval
452 (inAbstime
.tv_nsec
, kNanosecondScale
, &nsecs
);
453 clock_interval_to_absolutetime_interval
454 (inAbstime
.tv_sec
, kSecondScale
, &end
);
455 ADD_ABSOLUTETIME(&end
, &nsecs
);
457 return wakeAtTime(end
);
461 void IOTimerEventSource::setWorkLoop(IOWorkLoop
*inWorkLoop
)
463 super::setWorkLoop(inWorkLoop
);
464 if ( enabled
&& AbsoluteTime_to_scalar(&abstime
) && workLoop
)
468 IOReturn
IOTimerEventSource::wakeAtTime(AbsoluteTime inAbstime
)
470 return wakeAtTime(0, inAbstime
, 0);
473 IOReturn
IOTimerEventSource::wakeAtTime(uint32_t options
, AbsoluteTime inAbstime
, AbsoluteTime leeway
)
476 return kIOReturnNoResources
;
479 if ( enabled
&& AbsoluteTime_to_scalar(&inAbstime
) && AbsoluteTime_to_scalar(&abstime
) && workLoop
)
481 uint32_t tcoptions
= 0;
483 if (kIOTimeOptionsWithLeeway
& options
) tcoptions
|= THREAD_CALL_DELAY_LEEWAY
;
484 if (kIOTimeOptionsContinuous
& options
) tcoptions
|= THREAD_CALL_CONTINUOUS
;
488 if (kPassive
& flags
)
493 reserved
->workLoop
= workLoop
;
494 reserved
->calloutGeneration
++;
495 if (thread_call_enter_delayed_with_leeway((thread_call_t
) calloutEntry
,
496 (void *)(uintptr_t) reserved
->calloutGeneration
, inAbstime
, leeway
, tcoptions
)
497 && (kPassive
& flags
))
505 thread_call_enter_delayed_with_leeway((thread_call_t
) calloutEntry
,
506 NULL
, inAbstime
, leeway
, tcoptions
);
510 return kIOReturnSuccess
;