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>
49 #define super IOEventSource
50 OSDefineMetaClassAndStructors(IOTimerEventSource
, IOEventSource
)
51 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 0);
52 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 1);
53 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 2);
54 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 3);
55 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 4);
56 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 5);
57 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 6);
58 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 7);
62 #define IOStatisticsInitializeCounter() \
64 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsTimerEventSourceCounter); \
67 #define IOStatisticsOpenGate() \
69 IOStatistics::countOpenGate(me->IOEventSource::reserved->counter); \
72 #define IOStatisticsCloseGate() \
74 IOStatistics::countCloseGate(me->IOEventSource::reserved->counter); \
77 #define IOStatisticsTimeout() \
79 IOStatistics::countTimerTimeout(me->IOEventSource::reserved->counter); \
84 #define IOStatisticsInitializeCounter()
85 #define IOStatisticsOpenGate()
86 #define IOStatisticsCloseGate()
87 #define IOStatisticsTimeout()
89 #endif /* IOKITSTATS */
92 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
93 // not a subclassed implementation.
96 // Timeout handler function. This function is called by the kernel when
97 // the timeout interval expires.
99 void IOTimerEventSource::timeout(void *self
)
101 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
103 IOStatisticsTimeout();
105 if (me
->enabled
&& me
->action
)
113 IOStatisticsCloseGate();
114 doit
= (Action
) me
->action
;
115 if (doit
&& me
->enabled
&& AbsoluteTime_to_scalar(&me
->abstime
))
117 bool trace
= (gIOKitTrace
& kIOTraceTimers
) ? true : false;
120 IOTimeStampStartConstant(IODBG_TIMES(IOTIMES_ACTION
),
121 (uintptr_t) doit
, (uintptr_t) me
->owner
);
123 (*doit
)(me
->owner
, me
);
125 DTRACE_TMR3(iotescallout__expire
, Action
, doit
, OSObject
, me
->owner
, void, me
->workLoop
);
129 IOTimeStampEndConstant(IODBG_TIMES(IOTIMES_ACTION
),
130 (uintptr_t) doit
, (uintptr_t) me
->owner
);
132 IOStatisticsOpenGate();
138 void IOTimerEventSource::timeoutAndRelease(void * self
, void * c
)
140 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
141 /* The second parameter (a pointer) gets abused to carry an SInt32, so on LP64, "count"
142 must be cast to "long" before, in order to tell GCC we're not truncating a pointer. */
143 SInt32 count
= (SInt32
) (long) c
;
145 IOStatisticsTimeout();
147 if (me
->enabled
&& me
->action
)
150 wl
= me
->reserved
->workLoop
;
155 IOStatisticsCloseGate();
156 doit
= (Action
) me
->action
;
157 if (doit
&& (me
->reserved
->calloutGeneration
== count
))
159 bool trace
= (gIOKitTrace
& kIOTraceTimers
) ? true : false;
162 IOTimeStampStartConstant(IODBG_TIMES(IOTIMES_ACTION
),
163 (uintptr_t) doit
, (uintptr_t) me
->owner
);
165 (*doit
)(me
->owner
, me
);
167 DTRACE_TMR3(iotescallout__expire
, Action
, doit
, OSObject
, me
->owner
, void, me
->workLoop
);
171 IOTimeStampEndConstant(IODBG_TIMES(IOTIMES_ACTION
),
172 (uintptr_t) doit
, (uintptr_t) me
->owner
);
174 IOStatisticsOpenGate();
179 me
->reserved
->workLoop
->release();
183 void IOTimerEventSource::setTimeoutFunc()
185 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
186 // not a subclassed implementation
187 reserved
= IONew(ExpansionData
, 1);
188 calloutEntry
= (void *) thread_call_allocate((thread_call_func_t
) &IOTimerEventSource::timeoutAndRelease
,
189 (thread_call_param_t
) this);
192 bool IOTimerEventSource::init(OSObject
*inOwner
, Action inAction
)
194 if (!super::init(inOwner
, (IOEventSource::Action
) inAction
) )
201 IOStatisticsInitializeCounter();
207 IOTimerEventSource::timerEventSource(OSObject
*inOwner
, Action inAction
)
209 IOTimerEventSource
*me
= new IOTimerEventSource
;
211 if (me
&& !me
->init(inOwner
, inAction
)) {
219 void IOTimerEventSource::free()
223 thread_call_free((thread_call_t
) calloutEntry
);
227 IODelete(reserved
, ExpansionData
, 1);
232 void IOTimerEventSource::cancelTimeout()
235 reserved
->calloutGeneration
++;
236 bool active
= thread_call_cancel((thread_call_t
) calloutEntry
);
237 AbsoluteTime_to_scalar(&abstime
) = 0;
238 if (active
&& reserved
)
245 void IOTimerEventSource::enable()
248 if (kIOReturnSuccess
!= wakeAtTime(abstime
))
249 super::disable(); // Problem re-scheduling timeout ignore enable
252 void IOTimerEventSource::disable()
255 reserved
->calloutGeneration
++;
256 bool active
= thread_call_cancel((thread_call_t
) calloutEntry
);
258 if (active
&& reserved
)
265 IOReturn
IOTimerEventSource::setTimeoutTicks(UInt32 ticks
)
267 return setTimeout(ticks
, kTickScale
);
270 IOReturn
IOTimerEventSource::setTimeoutMS(UInt32 ms
)
272 return setTimeout(ms
, kMillisecondScale
);
275 IOReturn
IOTimerEventSource::setTimeoutUS(UInt32 us
)
277 return setTimeout(us
, kMicrosecondScale
);
280 IOReturn
IOTimerEventSource::setTimeout(UInt32 interval
, UInt32 scale_factor
)
284 clock_interval_to_deadline(interval
, scale_factor
, &end
);
285 return wakeAtTime(end
);
288 #if !defined(__LP64__)
289 IOReturn
IOTimerEventSource::setTimeout(mach_timespec_t interval
)
291 AbsoluteTime end
, nsecs
;
293 clock_interval_to_absolutetime_interval
294 (interval
.tv_nsec
, kNanosecondScale
, &nsecs
);
295 clock_interval_to_deadline
296 (interval
.tv_sec
, NSEC_PER_SEC
, &end
);
297 ADD_ABSOLUTETIME(&end
, &nsecs
);
299 return wakeAtTime(end
);
303 IOReturn
IOTimerEventSource::setTimeout(AbsoluteTime interval
)
307 clock_get_uptime(&end
);
308 ADD_ABSOLUTETIME(&end
, &interval
);
310 return wakeAtTime(end
);
313 IOReturn
IOTimerEventSource::wakeAtTimeTicks(UInt32 ticks
)
315 return wakeAtTime(ticks
, kTickScale
);
318 IOReturn
IOTimerEventSource::wakeAtTimeMS(UInt32 ms
)
320 return wakeAtTime(ms
, kMillisecondScale
);
323 IOReturn
IOTimerEventSource::wakeAtTimeUS(UInt32 us
)
325 return wakeAtTime(us
, kMicrosecondScale
);
328 IOReturn
IOTimerEventSource::wakeAtTime(UInt32 inAbstime
, UInt32 scale_factor
)
331 clock_interval_to_absolutetime_interval(inAbstime
, scale_factor
, &end
);
333 return wakeAtTime(end
);
336 #if !defined(__LP64__)
337 IOReturn
IOTimerEventSource::wakeAtTime(mach_timespec_t inAbstime
)
339 AbsoluteTime end
, nsecs
;
341 clock_interval_to_absolutetime_interval
342 (inAbstime
.tv_nsec
, kNanosecondScale
, &nsecs
);
343 clock_interval_to_absolutetime_interval
344 (inAbstime
.tv_sec
, kSecondScale
, &end
);
345 ADD_ABSOLUTETIME(&end
, &nsecs
);
347 return wakeAtTime(end
);
351 void IOTimerEventSource::setWorkLoop(IOWorkLoop
*inWorkLoop
)
353 super::setWorkLoop(inWorkLoop
);
354 if ( enabled
&& AbsoluteTime_to_scalar(&abstime
) && workLoop
)
358 IOReturn
IOTimerEventSource::wakeAtTime(AbsoluteTime inAbstime
)
361 return kIOReturnNoResources
;
364 if ( enabled
&& AbsoluteTime_to_scalar(&inAbstime
) && AbsoluteTime_to_scalar(&abstime
) && workLoop
)
370 reserved
->workLoop
= workLoop
;
371 reserved
->calloutGeneration
++;
372 if (thread_call_enter1_delayed((thread_call_t
) calloutEntry
,
373 (void *) reserved
->calloutGeneration
, inAbstime
))
380 thread_call_enter_delayed((thread_call_t
) calloutEntry
, inAbstime
);
383 return kIOReturnSuccess
;