2 * Copyright (c) 1998-2000 Apple Computer, 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);
57 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
58 // not a subclassed implementation.
61 bool IOTimerEventSource::checkForWork() { return false; }
63 // Timeout handler function. This function is called by the kernel when
64 // the timeout interval expires.
66 void IOTimerEventSource::timeout(void *self
)
68 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
70 if (me
->enabled
&& me
->action
)
78 doit
= (Action
) me
->action
;
79 if (doit
&& me
->enabled
&& AbsoluteTime_to_scalar(&me
->abstime
))
81 bool trace
= (gIOKitTrace
& kIOTraceTimers
) ? true : false;
84 IOTimeStampStartConstant(IODBG_TIMES(IOTIMES_ACTION
),
85 (uintptr_t) doit
, (uintptr_t) me
->owner
);
87 (*doit
)(me
->owner
, me
);
90 IOTimeStampEndConstant(IODBG_TIMES(IOTIMES_ACTION
),
91 (uintptr_t) doit
, (uintptr_t) me
->owner
);
98 void IOTimerEventSource::timeoutAndRelease(void * self
, void * c
)
100 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
101 /* The second parameter (a pointer) gets abused to carry an SInt32, so on LP64, "count"
102 must be cast to "long" before, in order to tell GCC we're not truncating a pointer. */
103 SInt32 count
= (SInt32
) (long) c
;
105 if (me
->enabled
&& me
->action
)
108 wl
= me
->reserved
->workLoop
;
113 doit
= (Action
) me
->action
;
114 if (doit
&& (me
->reserved
->calloutGeneration
== count
))
116 bool trace
= (gIOKitTrace
& kIOTraceTimers
) ? true : false;
119 IOTimeStampStartConstant(IODBG_TIMES(IOTIMES_ACTION
),
120 (uintptr_t) doit
, (uintptr_t) me
->owner
);
122 (*doit
)(me
->owner
, me
);
125 IOTimeStampEndConstant(IODBG_TIMES(IOTIMES_ACTION
),
126 (uintptr_t) doit
, (uintptr_t) me
->owner
);
132 me
->reserved
->workLoop
->release();
136 void IOTimerEventSource::setTimeoutFunc()
138 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
139 // not a subclassed implementation
140 reserved
= IONew(ExpansionData
, 1);
141 calloutEntry
= (void *) thread_call_allocate((thread_call_func_t
) &IOTimerEventSource::timeoutAndRelease
,
142 (thread_call_param_t
) this);
145 bool IOTimerEventSource::init(OSObject
*inOwner
, Action inAction
)
147 if (!super::init(inOwner
, (IOEventSource::Action
) inAction
) )
158 IOTimerEventSource::timerEventSource(OSObject
*inOwner
, Action inAction
)
160 IOTimerEventSource
*me
= new IOTimerEventSource
;
162 if (me
&& !me
->init(inOwner
, inAction
)) {
170 void IOTimerEventSource::free()
174 thread_call_free((thread_call_t
) calloutEntry
);
178 IODelete(reserved
, ExpansionData
, 1);
183 void IOTimerEventSource::cancelTimeout()
186 reserved
->calloutGeneration
++;
187 bool active
= thread_call_cancel((thread_call_t
) calloutEntry
);
188 AbsoluteTime_to_scalar(&abstime
) = 0;
189 if (active
&& reserved
)
196 void IOTimerEventSource::enable()
199 if (kIOReturnSuccess
!= wakeAtTime(abstime
))
200 super::disable(); // Problem re-scheduling timeout ignore enable
203 void IOTimerEventSource::disable()
206 reserved
->calloutGeneration
++;
207 bool active
= thread_call_cancel((thread_call_t
) calloutEntry
);
209 if (active
&& reserved
)
216 IOReturn
IOTimerEventSource::setTimeoutTicks(UInt32 ticks
)
218 return setTimeout(ticks
, kTickScale
);
221 IOReturn
IOTimerEventSource::setTimeoutMS(UInt32 ms
)
223 return setTimeout(ms
, kMillisecondScale
);
226 IOReturn
IOTimerEventSource::setTimeoutUS(UInt32 us
)
228 return setTimeout(us
, kMicrosecondScale
);
231 IOReturn
IOTimerEventSource::setTimeout(UInt32 interval
, UInt32 scale_factor
)
235 clock_interval_to_deadline(interval
, scale_factor
, &end
);
236 return wakeAtTime(end
);
239 #if !defined(__LP64__)
240 IOReturn
IOTimerEventSource::setTimeout(mach_timespec_t interval
)
242 AbsoluteTime end
, nsecs
;
244 clock_interval_to_absolutetime_interval
245 (interval
.tv_nsec
, kNanosecondScale
, &nsecs
);
246 clock_interval_to_deadline
247 (interval
.tv_sec
, NSEC_PER_SEC
, &end
);
248 ADD_ABSOLUTETIME(&end
, &nsecs
);
250 return wakeAtTime(end
);
254 IOReturn
IOTimerEventSource::setTimeout(AbsoluteTime interval
)
258 clock_get_uptime(&end
);
259 ADD_ABSOLUTETIME(&end
, &interval
);
261 return wakeAtTime(end
);
264 IOReturn
IOTimerEventSource::wakeAtTimeTicks(UInt32 ticks
)
266 return wakeAtTime(ticks
, kTickScale
);
269 IOReturn
IOTimerEventSource::wakeAtTimeMS(UInt32 ms
)
271 return wakeAtTime(ms
, kMillisecondScale
);
274 IOReturn
IOTimerEventSource::wakeAtTimeUS(UInt32 us
)
276 return wakeAtTime(us
, kMicrosecondScale
);
279 IOReturn
IOTimerEventSource::wakeAtTime(UInt32 inAbstime
, UInt32 scale_factor
)
282 clock_interval_to_absolutetime_interval(inAbstime
, scale_factor
, &end
);
284 return wakeAtTime(end
);
287 #if !defined(__LP64__)
288 IOReturn
IOTimerEventSource::wakeAtTime(mach_timespec_t inAbstime
)
290 AbsoluteTime end
, nsecs
;
292 clock_interval_to_absolutetime_interval
293 (inAbstime
.tv_nsec
, kNanosecondScale
, &nsecs
);
294 clock_interval_to_absolutetime_interval
295 (inAbstime
.tv_sec
, kSecondScale
, &end
);
296 ADD_ABSOLUTETIME(&end
, &nsecs
);
298 return wakeAtTime(end
);
302 void IOTimerEventSource::setWorkLoop(IOWorkLoop
*inWorkLoop
)
304 super::setWorkLoop(inWorkLoop
);
305 if ( enabled
&& AbsoluteTime_to_scalar(&abstime
) && workLoop
)
309 IOReturn
IOTimerEventSource::wakeAtTime(AbsoluteTime inAbstime
)
312 return kIOReturnNoResources
;
315 if ( enabled
&& AbsoluteTime_to_scalar(&inAbstime
) && AbsoluteTime_to_scalar(&abstime
) && workLoop
)
321 reserved
->workLoop
= workLoop
;
322 reserved
->calloutGeneration
++;
323 if (thread_call_enter1_delayed((thread_call_t
) calloutEntry
,
324 (void *) reserved
->calloutGeneration
, inAbstime
))
331 thread_call_enter_delayed((thread_call_t
) calloutEntry
, inAbstime
);
334 return kIOReturnSuccess
;