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 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
31 * IOTimerEventSource.cpp
34 * 2-Feb-1999 Joe Liu (jliu) created.
35 * 1999-10-14 Godfrey van der Linden(gvdl)
36 * Revamped to use thread_call APIs
40 #include <sys/cdefs.h>
43 #include <kern/thread_call.h>
46 #include <IOKit/assert.h>
47 #include <IOKit/system.h>
49 #include <IOKit/IOLib.h>
50 #include <IOKit/IOTimerEventSource.h>
51 #include <IOKit/IOWorkLoop.h>
53 #include <IOKit/IOTimeStamp.h>
55 #define super IOEventSource
56 OSDefineMetaClassAndStructors(IOTimerEventSource
, IOEventSource
)
57 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 0);
58 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 1);
59 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 2);
60 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 3);
61 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 4);
62 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 5);
63 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 6);
64 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 7);
67 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
68 // not a subclassed implementation.
71 bool IOTimerEventSource::checkForWork() { return false; }
73 // Timeout handler function. This function is called by the kernel when
74 // the timeout interval expires.
76 void IOTimerEventSource::timeout(void *self
)
78 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
80 if (me
->enabled
&& me
->action
)
88 doit
= (Action
) me
->action
;
89 if (doit
&& me
->enabled
&& AbsoluteTime_to_scalar(&me
->abstime
))
91 IOTimeStampConstant(IODBG_TIMES(IOTIMES_ACTION
),
92 (uintptr_t) doit
, (uintptr_t) me
->owner
);
93 (*doit
)(me
->owner
, me
);
100 void IOTimerEventSource::timeoutAndRelease(void * self
, void * c
)
102 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
103 /* The second parameter (a pointer) gets abused to carry an SInt32, so on LP64, "count"
104 must be cast to "long" before, in order to tell GCC we're not truncating a pointer. */
105 SInt32 count
= (SInt32
) (long) c
;
107 if (me
->enabled
&& me
->action
)
110 wl
= me
->reserved
->workLoop
;
115 doit
= (Action
) me
->action
;
116 if (doit
&& (me
->reserved
->calloutGeneration
== count
))
118 IOTimeStampConstant(IODBG_TIMES(IOTIMES_ACTION
),
119 (uintptr_t) doit
, (uintptr_t) me
->owner
);
120 (*doit
)(me
->owner
, me
);
126 me
->reserved
->workLoop
->release();
130 void IOTimerEventSource::setTimeoutFunc()
132 // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
133 // not a subclassed implementation
134 reserved
= IONew(ExpansionData
, 1);
135 calloutEntry
= (void *) thread_call_allocate((thread_call_func_t
) &IOTimerEventSource::timeoutAndRelease
,
136 (thread_call_param_t
) this);
139 bool IOTimerEventSource::init(OSObject
*inOwner
, Action inAction
)
141 if (!super::init(inOwner
, (IOEventSource::Action
) inAction
) )
152 IOTimerEventSource::timerEventSource(OSObject
*inOwner
, Action inAction
)
154 IOTimerEventSource
*me
= new IOTimerEventSource
;
156 if (me
&& !me
->init(inOwner
, inAction
)) {
164 void IOTimerEventSource::free()
168 thread_call_free((thread_call_t
) calloutEntry
);
172 IODelete(reserved
, ExpansionData
, 1);
177 void IOTimerEventSource::cancelTimeout()
180 reserved
->calloutGeneration
++;
181 bool active
= thread_call_cancel((thread_call_t
) calloutEntry
);
182 AbsoluteTime_to_scalar(&abstime
) = 0;
183 if (active
&& reserved
)
190 void IOTimerEventSource::enable()
193 if (kIOReturnSuccess
!= wakeAtTime(abstime
))
194 super::disable(); // Problem re-scheduling timeout ignore enable
197 void IOTimerEventSource::disable()
200 reserved
->calloutGeneration
++;
201 bool active
= thread_call_cancel((thread_call_t
) calloutEntry
);
203 if (active
&& reserved
)
210 IOReturn
IOTimerEventSource::setTimeoutTicks(UInt32 ticks
)
212 return setTimeout(ticks
, kTickScale
);
215 IOReturn
IOTimerEventSource::setTimeoutMS(UInt32 ms
)
217 return setTimeout(ms
, kMillisecondScale
);
220 IOReturn
IOTimerEventSource::setTimeoutUS(UInt32 us
)
222 return setTimeout(us
, kMicrosecondScale
);
225 IOReturn
IOTimerEventSource::setTimeout(UInt32 interval
, UInt32 scale_factor
)
229 clock_interval_to_deadline(interval
, scale_factor
, &end
);
230 return wakeAtTime(end
);
233 #if !defined(__LP64__)
234 IOReturn
IOTimerEventSource::setTimeout(mach_timespec_t interval
)
236 AbsoluteTime end
, nsecs
;
238 clock_interval_to_absolutetime_interval
239 (interval
.tv_nsec
, kNanosecondScale
, &nsecs
);
240 clock_interval_to_deadline
241 (interval
.tv_sec
, NSEC_PER_SEC
, &end
);
242 ADD_ABSOLUTETIME(&end
, &nsecs
);
244 return wakeAtTime(end
);
248 IOReturn
IOTimerEventSource::setTimeout(AbsoluteTime interval
)
252 clock_get_uptime(&end
);
253 ADD_ABSOLUTETIME(&end
, &interval
);
255 return wakeAtTime(end
);
258 IOReturn
IOTimerEventSource::wakeAtTimeTicks(UInt32 ticks
)
260 return wakeAtTime(ticks
, kTickScale
);
263 IOReturn
IOTimerEventSource::wakeAtTimeMS(UInt32 ms
)
265 return wakeAtTime(ms
, kMillisecondScale
);
268 IOReturn
IOTimerEventSource::wakeAtTimeUS(UInt32 us
)
270 return wakeAtTime(us
, kMicrosecondScale
);
273 IOReturn
IOTimerEventSource::wakeAtTime(UInt32 inAbstime
, UInt32 scale_factor
)
276 clock_interval_to_absolutetime_interval(inAbstime
, scale_factor
, &end
);
278 return wakeAtTime(end
);
281 #if !defined(__LP64__)
282 IOReturn
IOTimerEventSource::wakeAtTime(mach_timespec_t inAbstime
)
284 AbsoluteTime end
, nsecs
;
286 clock_interval_to_absolutetime_interval
287 (inAbstime
.tv_nsec
, kNanosecondScale
, &nsecs
);
288 clock_interval_to_absolutetime_interval
289 (inAbstime
.tv_sec
, kSecondScale
, &end
);
290 ADD_ABSOLUTETIME(&end
, &nsecs
);
292 return wakeAtTime(end
);
296 void IOTimerEventSource::setWorkLoop(IOWorkLoop
*inWorkLoop
)
298 super::setWorkLoop(inWorkLoop
);
299 if ( enabled
&& AbsoluteTime_to_scalar(&abstime
) && workLoop
)
303 IOReturn
IOTimerEventSource::wakeAtTime(AbsoluteTime inAbstime
)
306 return kIOReturnNoResources
;
309 if ( enabled
&& AbsoluteTime_to_scalar(&inAbstime
) && AbsoluteTime_to_scalar(&abstime
) && workLoop
)
315 reserved
->workLoop
= workLoop
;
316 reserved
->calloutGeneration
++;
317 if (thread_call_enter1_delayed((thread_call_t
) calloutEntry
,
318 (void *) reserved
->calloutGeneration
, inAbstime
))
325 thread_call_enter_delayed((thread_call_t
) calloutEntry
, inAbstime
);
328 return kIOReturnSuccess
;