2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
28 * IOTimerEventSource.cpp
31 * 2-Feb-1999 Joe Liu (jliu) created.
32 * 1999-10-14 Godfrey van der Linden(gvdl)
33 * Revamped to use thread_call APIs
37 #include <sys/cdefs.h>
40 #include <kern/thread_call.h>
43 #include <IOKit/assert.h>
44 #include <IOKit/system.h>
46 #include <IOKit/IOLib.h>
47 #include <IOKit/IOTimerEventSource.h>
48 #include <IOKit/IOWorkLoop.h>
50 #include <IOKit/IOTimeStamp.h>
52 #define super IOEventSource
53 OSDefineMetaClassAndStructors(IOTimerEventSource
, IOEventSource
)
54 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 0);
55 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 1);
56 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 2);
57 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 3);
58 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 4);
59 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 5);
60 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 6);
61 OSMetaClassDefineReservedUnused(IOTimerEventSource
, 7);
63 bool IOTimerEventSource::checkForWork() { return false; }
65 // Timeout handler function. This function is called by the kernel when
66 // the timeout interval expires.
68 void IOTimerEventSource::timeout(void *self
)
70 IOTimerEventSource
*me
= (IOTimerEventSource
*) self
;
73 Action doit
= (Action
) me
->action
;
76 IOTimeStampConstant(IODBG_TIMES(IOTIMES_ACTION
),
77 (unsigned int) doit
, (unsigned int) me
->owner
);
79 (*doit
)(me
->owner
, me
);
85 void IOTimerEventSource::setTimeoutFunc()
87 calloutEntry
= (void *) thread_call_allocate((thread_call_func_t
) timeout
,
88 (thread_call_param_t
) this);
91 bool IOTimerEventSource::init(OSObject
*inOwner
, Action inAction
)
93 if (!super::init(inOwner
, (IOEventSource::Action
) inAction
) )
104 IOTimerEventSource::timerEventSource(OSObject
*inOwner
, Action inAction
)
106 IOTimerEventSource
*me
= new IOTimerEventSource
;
108 if (me
&& !me
->init(inOwner
, inAction
)) {
116 void IOTimerEventSource::free()
120 thread_call_free((thread_call_t
) calloutEntry
);
126 void IOTimerEventSource::cancelTimeout()
128 thread_call_cancel((thread_call_t
) calloutEntry
);
129 AbsoluteTime_to_scalar(&abstime
) = 0;
132 void IOTimerEventSource::enable()
135 if (kIOReturnSuccess
!= wakeAtTime(abstime
))
136 super::disable(); // Problem re-scheduling timeout ignore enable
139 void IOTimerEventSource::disable()
141 thread_call_cancel((thread_call_t
) calloutEntry
);
145 IOReturn
IOTimerEventSource::setTimeoutTicks(UInt32 ticks
)
147 return setTimeout(ticks
, NSEC_PER_SEC
/hz
);
150 IOReturn
IOTimerEventSource::setTimeoutMS(UInt32 ms
)
152 return setTimeout(ms
, kMillisecondScale
);
155 IOReturn
IOTimerEventSource::setTimeoutUS(UInt32 us
)
157 return setTimeout(us
, kMicrosecondScale
);
160 IOReturn
IOTimerEventSource::setTimeout(UInt32 interval
, UInt32 scale_factor
)
164 clock_interval_to_deadline(interval
, scale_factor
, &end
);
165 return wakeAtTime(end
);
168 IOReturn
IOTimerEventSource::setTimeout(mach_timespec_t interval
)
170 AbsoluteTime end
, nsecs
;
172 clock_interval_to_absolutetime_interval
173 (interval
.tv_nsec
, kNanosecondScale
, &nsecs
);
174 clock_interval_to_deadline
175 (interval
.tv_sec
, NSEC_PER_SEC
, &end
);
176 ADD_ABSOLUTETIME(&end
, &nsecs
);
178 return wakeAtTime(end
);
181 IOReturn
IOTimerEventSource::setTimeout(AbsoluteTime interval
)
185 clock_get_uptime(&end
);
186 ADD_ABSOLUTETIME(&end
, &interval
);
188 return wakeAtTime(end
);
191 IOReturn
IOTimerEventSource::wakeAtTimeTicks(UInt32 ticks
)
193 return wakeAtTime(ticks
, NSEC_PER_SEC
/hz
);
196 IOReturn
IOTimerEventSource::wakeAtTimeMS(UInt32 ms
)
198 return wakeAtTime(ms
, kMillisecondScale
);
201 IOReturn
IOTimerEventSource::wakeAtTimeUS(UInt32 us
)
203 return wakeAtTime(us
, kMicrosecondScale
);
206 IOReturn
IOTimerEventSource::wakeAtTime(UInt32 abstime
, UInt32 scale_factor
)
209 clock_interval_to_absolutetime_interval(abstime
, scale_factor
, &end
);
211 return wakeAtTime(end
);
214 IOReturn
IOTimerEventSource::wakeAtTime(mach_timespec_t abstime
)
216 AbsoluteTime end
, nsecs
;
218 clock_interval_to_absolutetime_interval
219 (abstime
.tv_nsec
, kNanosecondScale
, &nsecs
);
220 clock_interval_to_absolutetime_interval
221 (abstime
.tv_sec
, kSecondScale
, &end
);
222 ADD_ABSOLUTETIME(&end
, &nsecs
);
224 return wakeAtTime(end
);
227 IOReturn
IOTimerEventSource::wakeAtTime(AbsoluteTime inAbstime
)
230 return kIOReturnNoResources
;
233 if ( enabled
&& AbsoluteTime_to_scalar(&abstime
) )
234 thread_call_enter_delayed((thread_call_t
) calloutEntry
, abstime
);
236 return kIOReturnSuccess
;