1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions * Copyright (c) 1998-2000, 2009-2010 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions *
4*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions *
6*1b191cb5SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions *
15*1b191cb5SApple OSS Distributions * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions *
18*1b191cb5SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions * limitations under the License.
25*1b191cb5SApple OSS Distributions *
26*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions */
28*1b191cb5SApple OSS Distributions
29*1b191cb5SApple OSS Distributions #include <ptrauth.h>
30*1b191cb5SApple OSS Distributions #include <sys/cdefs.h>
31*1b191cb5SApple OSS Distributions
32*1b191cb5SApple OSS Distributions __BEGIN_DECLS
33*1b191cb5SApple OSS Distributions #include <kern/thread_call.h>
34*1b191cb5SApple OSS Distributions __END_DECLS
35*1b191cb5SApple OSS Distributions
36*1b191cb5SApple OSS Distributions #include <IOKit/assert.h>
37*1b191cb5SApple OSS Distributions #include <IOKit/system.h>
38*1b191cb5SApple OSS Distributions
39*1b191cb5SApple OSS Distributions #include <IOKit/IOLib.h>
40*1b191cb5SApple OSS Distributions #include <IOKit/IOTimerEventSource.h>
41*1b191cb5SApple OSS Distributions #include <IOKit/IOWorkLoop.h>
42*1b191cb5SApple OSS Distributions
43*1b191cb5SApple OSS Distributions #include <IOKit/IOTimeStamp.h>
44*1b191cb5SApple OSS Distributions #include <IOKit/IOKitDebug.h>
45*1b191cb5SApple OSS Distributions #if CONFIG_DTRACE
46*1b191cb5SApple OSS Distributions #include <mach/sdt.h>
47*1b191cb5SApple OSS Distributions #endif
48*1b191cb5SApple OSS Distributions
49*1b191cb5SApple OSS Distributions #include <libkern/Block.h>
50*1b191cb5SApple OSS Distributions #include <libkern/Block_private.h>
51*1b191cb5SApple OSS Distributions
52*1b191cb5SApple OSS Distributions
53*1b191cb5SApple OSS Distributions #define super IOEventSource
54*1b191cb5SApple OSS Distributions OSDefineMetaClassAndStructors(IOTimerEventSource, IOEventSource)
55*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOTimerEventSource, 0);
56*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOTimerEventSource, 1);
57*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOTimerEventSource, 2);
58*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOTimerEventSource, 3);
59*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOTimerEventSource, 4);
60*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOTimerEventSource, 5);
61*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOTimerEventSource, 6);
62*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOTimerEventSource, 7);
63*1b191cb5SApple OSS Distributions
64*1b191cb5SApple OSS Distributions #if IOKITSTATS
65*1b191cb5SApple OSS Distributions
66*1b191cb5SApple OSS Distributions #define IOStatisticsInitializeCounter() \
67*1b191cb5SApple OSS Distributions do { \
68*1b191cb5SApple OSS Distributions IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsTimerEventSourceCounter); \
69*1b191cb5SApple OSS Distributions } while (0)
70*1b191cb5SApple OSS Distributions
71*1b191cb5SApple OSS Distributions #define IOStatisticsOpenGate() \
72*1b191cb5SApple OSS Distributions do { \
73*1b191cb5SApple OSS Distributions IOStatistics::countOpenGate(me->IOEventSource::reserved->counter); \
74*1b191cb5SApple OSS Distributions } while (0)
75*1b191cb5SApple OSS Distributions
76*1b191cb5SApple OSS Distributions #define IOStatisticsCloseGate() \
77*1b191cb5SApple OSS Distributions do { \
78*1b191cb5SApple OSS Distributions IOStatistics::countCloseGate(me->IOEventSource::reserved->counter); \
79*1b191cb5SApple OSS Distributions } while (0)
80*1b191cb5SApple OSS Distributions
81*1b191cb5SApple OSS Distributions #define IOStatisticsTimeout() \
82*1b191cb5SApple OSS Distributions do { \
83*1b191cb5SApple OSS Distributions IOStatistics::countTimerTimeout(me->IOEventSource::reserved->counter); \
84*1b191cb5SApple OSS Distributions } while (0)
85*1b191cb5SApple OSS Distributions
86*1b191cb5SApple OSS Distributions #else
87*1b191cb5SApple OSS Distributions
88*1b191cb5SApple OSS Distributions #define IOStatisticsInitializeCounter()
89*1b191cb5SApple OSS Distributions #define IOStatisticsOpenGate()
90*1b191cb5SApple OSS Distributions #define IOStatisticsCloseGate()
91*1b191cb5SApple OSS Distributions #define IOStatisticsTimeout()
92*1b191cb5SApple OSS Distributions
93*1b191cb5SApple OSS Distributions #endif /* IOKITSTATS */
94*1b191cb5SApple OSS Distributions
95*1b191cb5SApple OSS Distributions //
96*1b191cb5SApple OSS Distributions // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
97*1b191cb5SApple OSS Distributions // not a subclassed implementation.
98*1b191cb5SApple OSS Distributions //
99*1b191cb5SApple OSS Distributions
100*1b191cb5SApple OSS Distributions // Timeout handler function. This function is called by the kernel when
101*1b191cb5SApple OSS Distributions // the timeout interval expires.
102*1b191cb5SApple OSS Distributions //
103*1b191cb5SApple OSS Distributions
104*1b191cb5SApple OSS Distributions __inline__ void
invokeAction(IOEventSource::Action _action,IOTimerEventSource * ts,OSObject * _owner,IOWorkLoop * _workLoop)105*1b191cb5SApple OSS Distributions IOTimerEventSource::invokeAction(IOEventSource::Action _action, IOTimerEventSource * ts,
106*1b191cb5SApple OSS Distributions OSObject * _owner, IOWorkLoop * _workLoop)
107*1b191cb5SApple OSS Distributions {
108*1b191cb5SApple OSS Distributions bool trace = (gIOKitTrace & kIOTraceTimers) ? true : false;
109*1b191cb5SApple OSS Distributions void * address;
110*1b191cb5SApple OSS Distributions
111*1b191cb5SApple OSS Distributions if (kActionBlock & flags) {
112*1b191cb5SApple OSS Distributions address = ptrauth_nop_cast(void *, _Block_get_invoke_fn((struct Block_layout *) actionBlock));
113*1b191cb5SApple OSS Distributions } else {
114*1b191cb5SApple OSS Distributions address = ptrauth_nop_cast(void *, _action);
115*1b191cb5SApple OSS Distributions }
116*1b191cb5SApple OSS Distributions
117*1b191cb5SApple OSS Distributions if (trace) {
118*1b191cb5SApple OSS Distributions IOTimeStampStartConstant(IODBG_TIMES(IOTIMES_ACTION),
119*1b191cb5SApple OSS Distributions VM_KERNEL_ADDRHIDE(address),
120*1b191cb5SApple OSS Distributions VM_KERNEL_ADDRHIDE(_owner));
121*1b191cb5SApple OSS Distributions }
122*1b191cb5SApple OSS Distributions
123*1b191cb5SApple OSS Distributions if (kActionBlock & flags) {
124*1b191cb5SApple OSS Distributions ((IOTimerEventSource::ActionBlock) actionBlock)(ts);
125*1b191cb5SApple OSS Distributions } else {
126*1b191cb5SApple OSS Distributions ((IOTimerEventSource::Action)_action)(_owner, ts);
127*1b191cb5SApple OSS Distributions }
128*1b191cb5SApple OSS Distributions
129*1b191cb5SApple OSS Distributions #if CONFIG_DTRACE
130*1b191cb5SApple OSS Distributions DTRACE_TMR3(iotescallout__expire, Action, address, OSObject, _owner, void, _workLoop);
131*1b191cb5SApple OSS Distributions #endif
132*1b191cb5SApple OSS Distributions
133*1b191cb5SApple OSS Distributions if (trace) {
134*1b191cb5SApple OSS Distributions IOTimeStampEndConstant(IODBG_TIMES(IOTIMES_ACTION),
135*1b191cb5SApple OSS Distributions VM_KERNEL_UNSLIDE(address),
136*1b191cb5SApple OSS Distributions VM_KERNEL_ADDRHIDE(_owner));
137*1b191cb5SApple OSS Distributions }
138*1b191cb5SApple OSS Distributions }
139*1b191cb5SApple OSS Distributions
140*1b191cb5SApple OSS Distributions void
timeout(void * self)141*1b191cb5SApple OSS Distributions IOTimerEventSource::timeout(void *self)
142*1b191cb5SApple OSS Distributions {
143*1b191cb5SApple OSS Distributions IOTimerEventSource *me = (IOTimerEventSource *) self;
144*1b191cb5SApple OSS Distributions
145*1b191cb5SApple OSS Distributions IOStatisticsTimeout();
146*1b191cb5SApple OSS Distributions
147*1b191cb5SApple OSS Distributions if (me->enabled && me->action) {
148*1b191cb5SApple OSS Distributions IOWorkLoop *
149*1b191cb5SApple OSS Distributions wl = me->workLoop;
150*1b191cb5SApple OSS Distributions if (wl) {
151*1b191cb5SApple OSS Distributions IOEventSource::Action doit;
152*1b191cb5SApple OSS Distributions wl->closeGate();
153*1b191cb5SApple OSS Distributions IOStatisticsCloseGate();
154*1b191cb5SApple OSS Distributions doit = me->action;
155*1b191cb5SApple OSS Distributions if (doit && me->enabled && AbsoluteTime_to_scalar(&me->abstime)) {
156*1b191cb5SApple OSS Distributions me->invokeAction(doit, me, me->owner, me->workLoop);
157*1b191cb5SApple OSS Distributions }
158*1b191cb5SApple OSS Distributions IOStatisticsOpenGate();
159*1b191cb5SApple OSS Distributions wl->openGate();
160*1b191cb5SApple OSS Distributions }
161*1b191cb5SApple OSS Distributions }
162*1b191cb5SApple OSS Distributions }
163*1b191cb5SApple OSS Distributions
164*1b191cb5SApple OSS Distributions void
timeoutAndRelease(void * self,void * c)165*1b191cb5SApple OSS Distributions IOTimerEventSource::timeoutAndRelease(void * self, void * c)
166*1b191cb5SApple OSS Distributions {
167*1b191cb5SApple OSS Distributions IOTimerEventSource *me = (IOTimerEventSource *) self;
168*1b191cb5SApple OSS Distributions /* The second parameter (a pointer) gets abused to carry an SInt32, so on LP64, "count"
169*1b191cb5SApple OSS Distributions * must be cast to "long" before, in order to tell GCC we're not truncating a pointer. */
170*1b191cb5SApple OSS Distributions SInt32 count = (SInt32) (long) c;
171*1b191cb5SApple OSS Distributions
172*1b191cb5SApple OSS Distributions IOStatisticsTimeout();
173*1b191cb5SApple OSS Distributions
174*1b191cb5SApple OSS Distributions if (me->enabled && me->action) {
175*1b191cb5SApple OSS Distributions IOWorkLoop *
176*1b191cb5SApple OSS Distributions wl = me->reserved->workLoop;
177*1b191cb5SApple OSS Distributions if (wl) {
178*1b191cb5SApple OSS Distributions IOEventSource::Action doit;
179*1b191cb5SApple OSS Distributions wl->closeGate();
180*1b191cb5SApple OSS Distributions IOStatisticsCloseGate();
181*1b191cb5SApple OSS Distributions doit = me->action;
182*1b191cb5SApple OSS Distributions if (doit && (me->reserved->calloutGeneration == count)) {
183*1b191cb5SApple OSS Distributions thread_call_start_iotes_invocation((thread_call_t)me->calloutEntry);
184*1b191cb5SApple OSS Distributions me->invokeAction(doit, me, me->owner, me->workLoop);
185*1b191cb5SApple OSS Distributions }
186*1b191cb5SApple OSS Distributions IOStatisticsOpenGate();
187*1b191cb5SApple OSS Distributions wl->openGate();
188*1b191cb5SApple OSS Distributions }
189*1b191cb5SApple OSS Distributions }
190*1b191cb5SApple OSS Distributions if (me->reserved->workLoop) {
191*1b191cb5SApple OSS Distributions me->reserved->workLoop->release();
192*1b191cb5SApple OSS Distributions }
193*1b191cb5SApple OSS Distributions me->release();
194*1b191cb5SApple OSS Distributions }
195*1b191cb5SApple OSS Distributions
196*1b191cb5SApple OSS Distributions // -- work loop delivery
197*1b191cb5SApple OSS Distributions
198*1b191cb5SApple OSS Distributions bool
checkForWork()199*1b191cb5SApple OSS Distributions IOTimerEventSource::checkForWork()
200*1b191cb5SApple OSS Distributions {
201*1b191cb5SApple OSS Distributions IOEventSource::Action doit;
202*1b191cb5SApple OSS Distributions
203*1b191cb5SApple OSS Distributions if (reserved
204*1b191cb5SApple OSS Distributions && (reserved->calloutGenerationSignaled == reserved->calloutGeneration)
205*1b191cb5SApple OSS Distributions && enabled && (doit = action)) {
206*1b191cb5SApple OSS Distributions reserved->calloutGenerationSignaled = ~reserved->calloutGeneration;
207*1b191cb5SApple OSS Distributions invokeAction(doit, this, owner, workLoop);
208*1b191cb5SApple OSS Distributions }
209*1b191cb5SApple OSS Distributions
210*1b191cb5SApple OSS Distributions return false;
211*1b191cb5SApple OSS Distributions }
212*1b191cb5SApple OSS Distributions
213*1b191cb5SApple OSS Distributions void
timeoutSignaled(void * self,void * c)214*1b191cb5SApple OSS Distributions IOTimerEventSource::timeoutSignaled(void * self, void * c)
215*1b191cb5SApple OSS Distributions {
216*1b191cb5SApple OSS Distributions IOTimerEventSource *me = (IOTimerEventSource *) self;
217*1b191cb5SApple OSS Distributions
218*1b191cb5SApple OSS Distributions me->reserved->calloutGenerationSignaled = (SInt32)(long) c;
219*1b191cb5SApple OSS Distributions if (me->enabled) {
220*1b191cb5SApple OSS Distributions me->signalWorkAvailable();
221*1b191cb5SApple OSS Distributions }
222*1b191cb5SApple OSS Distributions }
223*1b191cb5SApple OSS Distributions
224*1b191cb5SApple OSS Distributions // --
225*1b191cb5SApple OSS Distributions
226*1b191cb5SApple OSS Distributions void
setTimeoutFunc()227*1b191cb5SApple OSS Distributions IOTimerEventSource::setTimeoutFunc()
228*1b191cb5SApple OSS Distributions {
229*1b191cb5SApple OSS Distributions thread_call_priority_t pri;
230*1b191cb5SApple OSS Distributions uint32_t options;
231*1b191cb5SApple OSS Distributions
232*1b191cb5SApple OSS Distributions if (reserved) {
233*1b191cb5SApple OSS Distributions panic("setTimeoutFunc already %p, %p", this, reserved);
234*1b191cb5SApple OSS Distributions }
235*1b191cb5SApple OSS Distributions
236*1b191cb5SApple OSS Distributions // reserved != 0 means IOTimerEventSource::timeoutAndRelease is being used,
237*1b191cb5SApple OSS Distributions // not a subclassed implementation
238*1b191cb5SApple OSS Distributions reserved = IOMallocType(ExpansionData);
239*1b191cb5SApple OSS Distributions
240*1b191cb5SApple OSS Distributions reserved->calloutGenerationSignaled = ~reserved->calloutGeneration;
241*1b191cb5SApple OSS Distributions // make use of an existing ivar for parameter passing
242*1b191cb5SApple OSS Distributions options = (uint32_t) abstime;
243*1b191cb5SApple OSS Distributions abstime = 0;
244*1b191cb5SApple OSS Distributions
245*1b191cb5SApple OSS Distributions thread_call_options_t tcoptions = 0;
246*1b191cb5SApple OSS Distributions thread_call_func_t func = NULL;
247*1b191cb5SApple OSS Distributions
248*1b191cb5SApple OSS Distributions switch (kIOTimerEventSourceOptionsPriorityMask & options) {
249*1b191cb5SApple OSS Distributions case kIOTimerEventSourceOptionsPriorityHigh:
250*1b191cb5SApple OSS Distributions pri = THREAD_CALL_PRIORITY_HIGH;
251*1b191cb5SApple OSS Distributions func = &IOTimerEventSource::timeoutAndRelease;
252*1b191cb5SApple OSS Distributions break;
253*1b191cb5SApple OSS Distributions
254*1b191cb5SApple OSS Distributions case kIOTimerEventSourceOptionsPriorityKernel:
255*1b191cb5SApple OSS Distributions pri = THREAD_CALL_PRIORITY_KERNEL;
256*1b191cb5SApple OSS Distributions func = &IOTimerEventSource::timeoutAndRelease;
257*1b191cb5SApple OSS Distributions break;
258*1b191cb5SApple OSS Distributions
259*1b191cb5SApple OSS Distributions case kIOTimerEventSourceOptionsPriorityKernelHigh:
260*1b191cb5SApple OSS Distributions pri = THREAD_CALL_PRIORITY_KERNEL_HIGH;
261*1b191cb5SApple OSS Distributions func = &IOTimerEventSource::timeoutAndRelease;
262*1b191cb5SApple OSS Distributions break;
263*1b191cb5SApple OSS Distributions
264*1b191cb5SApple OSS Distributions case kIOTimerEventSourceOptionsPriorityUser:
265*1b191cb5SApple OSS Distributions pri = THREAD_CALL_PRIORITY_USER;
266*1b191cb5SApple OSS Distributions func = &IOTimerEventSource::timeoutAndRelease;
267*1b191cb5SApple OSS Distributions break;
268*1b191cb5SApple OSS Distributions
269*1b191cb5SApple OSS Distributions case kIOTimerEventSourceOptionsPriorityLow:
270*1b191cb5SApple OSS Distributions pri = THREAD_CALL_PRIORITY_LOW;
271*1b191cb5SApple OSS Distributions func = &IOTimerEventSource::timeoutAndRelease;
272*1b191cb5SApple OSS Distributions break;
273*1b191cb5SApple OSS Distributions
274*1b191cb5SApple OSS Distributions case kIOTimerEventSourceOptionsPriorityWorkLoop:
275*1b191cb5SApple OSS Distributions pri = THREAD_CALL_PRIORITY_KERNEL;
276*1b191cb5SApple OSS Distributions tcoptions |= THREAD_CALL_OPTIONS_SIGNAL;
277*1b191cb5SApple OSS Distributions if (kIOTimerEventSourceOptionsAllowReenter & options) {
278*1b191cb5SApple OSS Distributions break;
279*1b191cb5SApple OSS Distributions }
280*1b191cb5SApple OSS Distributions func = &IOTimerEventSource::timeoutSignaled;
281*1b191cb5SApple OSS Distributions break;
282*1b191cb5SApple OSS Distributions
283*1b191cb5SApple OSS Distributions default:
284*1b191cb5SApple OSS Distributions break;
285*1b191cb5SApple OSS Distributions }
286*1b191cb5SApple OSS Distributions
287*1b191cb5SApple OSS Distributions assertf(func, "IOTimerEventSource options 0x%x", options);
288*1b191cb5SApple OSS Distributions if (!func) {
289*1b191cb5SApple OSS Distributions return; // init will fail
290*1b191cb5SApple OSS Distributions }
291*1b191cb5SApple OSS Distributions if (THREAD_CALL_OPTIONS_SIGNAL & tcoptions) {
292*1b191cb5SApple OSS Distributions flags |= kActive;
293*1b191cb5SApple OSS Distributions } else {
294*1b191cb5SApple OSS Distributions flags |= kPassive;
295*1b191cb5SApple OSS Distributions }
296*1b191cb5SApple OSS Distributions
297*1b191cb5SApple OSS Distributions if (!(kIOTimerEventSourceOptionsAllowReenter & options)) {
298*1b191cb5SApple OSS Distributions tcoptions |= THREAD_CALL_OPTIONS_ONCE;
299*1b191cb5SApple OSS Distributions }
300*1b191cb5SApple OSS Distributions
301*1b191cb5SApple OSS Distributions calloutEntry = (void *) thread_call_allocate_with_options(func,
302*1b191cb5SApple OSS Distributions (thread_call_param_t) this, pri, tcoptions);
303*1b191cb5SApple OSS Distributions assert(calloutEntry);
304*1b191cb5SApple OSS Distributions }
305*1b191cb5SApple OSS Distributions
306*1b191cb5SApple OSS Distributions bool
init(OSObject * inOwner,Action inAction)307*1b191cb5SApple OSS Distributions IOTimerEventSource::init(OSObject *inOwner, Action inAction)
308*1b191cb5SApple OSS Distributions {
309*1b191cb5SApple OSS Distributions if (!super::init(inOwner, (IOEventSource::Action) inAction)) {
310*1b191cb5SApple OSS Distributions return false;
311*1b191cb5SApple OSS Distributions }
312*1b191cb5SApple OSS Distributions
313*1b191cb5SApple OSS Distributions setTimeoutFunc();
314*1b191cb5SApple OSS Distributions if (!calloutEntry) {
315*1b191cb5SApple OSS Distributions return false;
316*1b191cb5SApple OSS Distributions }
317*1b191cb5SApple OSS Distributions
318*1b191cb5SApple OSS Distributions IOStatisticsInitializeCounter();
319*1b191cb5SApple OSS Distributions
320*1b191cb5SApple OSS Distributions return true;
321*1b191cb5SApple OSS Distributions }
322*1b191cb5SApple OSS Distributions
323*1b191cb5SApple OSS Distributions bool
init(uint32_t options,OSObject * inOwner,Action inAction)324*1b191cb5SApple OSS Distributions IOTimerEventSource::init(uint32_t options, OSObject *inOwner, Action inAction)
325*1b191cb5SApple OSS Distributions {
326*1b191cb5SApple OSS Distributions // make use of an existing ivar for parameter passing
327*1b191cb5SApple OSS Distributions abstime = options;
328*1b191cb5SApple OSS Distributions return init(inOwner, inAction);
329*1b191cb5SApple OSS Distributions }
330*1b191cb5SApple OSS Distributions
331*1b191cb5SApple OSS Distributions IOTimerEventSource *
timerEventSource(uint32_t inOptions,OSObject * inOwner,Action inAction)332*1b191cb5SApple OSS Distributions IOTimerEventSource::timerEventSource(uint32_t inOptions, OSObject *inOwner, Action inAction)
333*1b191cb5SApple OSS Distributions {
334*1b191cb5SApple OSS Distributions IOTimerEventSource *me = new IOTimerEventSource;
335*1b191cb5SApple OSS Distributions
336*1b191cb5SApple OSS Distributions if (me && !me->init(inOptions, inOwner, inAction)) {
337*1b191cb5SApple OSS Distributions me->release();
338*1b191cb5SApple OSS Distributions return NULL;
339*1b191cb5SApple OSS Distributions }
340*1b191cb5SApple OSS Distributions
341*1b191cb5SApple OSS Distributions return me;
342*1b191cb5SApple OSS Distributions }
343*1b191cb5SApple OSS Distributions
344*1b191cb5SApple OSS Distributions IOTimerEventSource *
timerEventSource(uint32_t options,OSObject * inOwner,ActionBlock _action)345*1b191cb5SApple OSS Distributions IOTimerEventSource::timerEventSource(uint32_t options, OSObject *inOwner, ActionBlock _action)
346*1b191cb5SApple OSS Distributions {
347*1b191cb5SApple OSS Distributions IOTimerEventSource * tes;
348*1b191cb5SApple OSS Distributions tes = IOTimerEventSource::timerEventSource(options, inOwner, (Action) NULL);
349*1b191cb5SApple OSS Distributions if (tes) {
350*1b191cb5SApple OSS Distributions tes->setActionBlock((IOEventSource::ActionBlock) _action);
351*1b191cb5SApple OSS Distributions }
352*1b191cb5SApple OSS Distributions
353*1b191cb5SApple OSS Distributions return tes;
354*1b191cb5SApple OSS Distributions }
355*1b191cb5SApple OSS Distributions
356*1b191cb5SApple OSS Distributions #define _thread_call_cancel(tc) ((kActive & flags) ? thread_call_cancel_wait((tc)) : thread_call_cancel((tc)))
357*1b191cb5SApple OSS Distributions
358*1b191cb5SApple OSS Distributions IOTimerEventSource *
timerEventSource(OSObject * inOwner,Action inAction)359*1b191cb5SApple OSS Distributions IOTimerEventSource::timerEventSource(OSObject *inOwner, Action inAction)
360*1b191cb5SApple OSS Distributions {
361*1b191cb5SApple OSS Distributions return IOTimerEventSource::timerEventSource(
362*1b191cb5SApple OSS Distributions kIOTimerEventSourceOptionsPriorityKernelHigh,
363*1b191cb5SApple OSS Distributions inOwner, inAction);
364*1b191cb5SApple OSS Distributions }
365*1b191cb5SApple OSS Distributions
366*1b191cb5SApple OSS Distributions void
free()367*1b191cb5SApple OSS Distributions IOTimerEventSource::free()
368*1b191cb5SApple OSS Distributions {
369*1b191cb5SApple OSS Distributions if (calloutEntry) {
370*1b191cb5SApple OSS Distributions __assert_only bool freed;
371*1b191cb5SApple OSS Distributions
372*1b191cb5SApple OSS Distributions cancelTimeout();
373*1b191cb5SApple OSS Distributions
374*1b191cb5SApple OSS Distributions freed = thread_call_free((thread_call_t) calloutEntry);
375*1b191cb5SApple OSS Distributions assert(freed);
376*1b191cb5SApple OSS Distributions }
377*1b191cb5SApple OSS Distributions
378*1b191cb5SApple OSS Distributions if (reserved) {
379*1b191cb5SApple OSS Distributions IOFreeType(reserved, ExpansionData);
380*1b191cb5SApple OSS Distributions }
381*1b191cb5SApple OSS Distributions
382*1b191cb5SApple OSS Distributions super::free();
383*1b191cb5SApple OSS Distributions }
384*1b191cb5SApple OSS Distributions
385*1b191cb5SApple OSS Distributions void
cancelTimeout()386*1b191cb5SApple OSS Distributions IOTimerEventSource::cancelTimeout()
387*1b191cb5SApple OSS Distributions {
388*1b191cb5SApple OSS Distributions if (reserved) {
389*1b191cb5SApple OSS Distributions reserved->calloutGeneration++;
390*1b191cb5SApple OSS Distributions }
391*1b191cb5SApple OSS Distributions bool active = _thread_call_cancel((thread_call_t) calloutEntry);
392*1b191cb5SApple OSS Distributions AbsoluteTime_to_scalar(&abstime) = 0;
393*1b191cb5SApple OSS Distributions if (active && reserved && (kPassive & flags)) {
394*1b191cb5SApple OSS Distributions release();
395*1b191cb5SApple OSS Distributions workLoop->release();
396*1b191cb5SApple OSS Distributions }
397*1b191cb5SApple OSS Distributions }
398*1b191cb5SApple OSS Distributions
399*1b191cb5SApple OSS Distributions void
enable()400*1b191cb5SApple OSS Distributions IOTimerEventSource::enable()
401*1b191cb5SApple OSS Distributions {
402*1b191cb5SApple OSS Distributions super::enable();
403*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != wakeAtTime(abstime)) {
404*1b191cb5SApple OSS Distributions super::disable(); // Problem re-scheduling timeout ignore enable
405*1b191cb5SApple OSS Distributions }
406*1b191cb5SApple OSS Distributions }
407*1b191cb5SApple OSS Distributions
408*1b191cb5SApple OSS Distributions void
disable()409*1b191cb5SApple OSS Distributions IOTimerEventSource::disable()
410*1b191cb5SApple OSS Distributions {
411*1b191cb5SApple OSS Distributions if (reserved) {
412*1b191cb5SApple OSS Distributions reserved->calloutGeneration++;
413*1b191cb5SApple OSS Distributions }
414*1b191cb5SApple OSS Distributions bool active = _thread_call_cancel((thread_call_t) calloutEntry);
415*1b191cb5SApple OSS Distributions super::disable();
416*1b191cb5SApple OSS Distributions if (active && reserved && (kPassive & flags)) {
417*1b191cb5SApple OSS Distributions release();
418*1b191cb5SApple OSS Distributions workLoop->release();
419*1b191cb5SApple OSS Distributions }
420*1b191cb5SApple OSS Distributions }
421*1b191cb5SApple OSS Distributions
422*1b191cb5SApple OSS Distributions IOReturn
setTimeoutTicks(UInt32 ticks)423*1b191cb5SApple OSS Distributions IOTimerEventSource::setTimeoutTicks(UInt32 ticks)
424*1b191cb5SApple OSS Distributions {
425*1b191cb5SApple OSS Distributions return setTimeout(ticks, kTickScale);
426*1b191cb5SApple OSS Distributions }
427*1b191cb5SApple OSS Distributions
428*1b191cb5SApple OSS Distributions IOReturn
setTimeoutMS(UInt32 ms)429*1b191cb5SApple OSS Distributions IOTimerEventSource::setTimeoutMS(UInt32 ms)
430*1b191cb5SApple OSS Distributions {
431*1b191cb5SApple OSS Distributions return setTimeout(ms, kMillisecondScale);
432*1b191cb5SApple OSS Distributions }
433*1b191cb5SApple OSS Distributions
434*1b191cb5SApple OSS Distributions IOReturn
setTimeoutUS(UInt32 us)435*1b191cb5SApple OSS Distributions IOTimerEventSource::setTimeoutUS(UInt32 us)
436*1b191cb5SApple OSS Distributions {
437*1b191cb5SApple OSS Distributions return setTimeout(us, kMicrosecondScale);
438*1b191cb5SApple OSS Distributions }
439*1b191cb5SApple OSS Distributions
440*1b191cb5SApple OSS Distributions IOReturn
setTimeout(UInt32 interval,UInt32 scale_factor)441*1b191cb5SApple OSS Distributions IOTimerEventSource::setTimeout(UInt32 interval, UInt32 scale_factor)
442*1b191cb5SApple OSS Distributions {
443*1b191cb5SApple OSS Distributions AbsoluteTime end;
444*1b191cb5SApple OSS Distributions
445*1b191cb5SApple OSS Distributions clock_interval_to_deadline(interval, scale_factor, &end);
446*1b191cb5SApple OSS Distributions return wakeAtTime(end);
447*1b191cb5SApple OSS Distributions }
448*1b191cb5SApple OSS Distributions
449*1b191cb5SApple OSS Distributions #if !defined(__LP64__)
450*1b191cb5SApple OSS Distributions IOReturn
setTimeout(mach_timespec_t interval)451*1b191cb5SApple OSS Distributions IOTimerEventSource::setTimeout(mach_timespec_t interval)
452*1b191cb5SApple OSS Distributions {
453*1b191cb5SApple OSS Distributions AbsoluteTime end, nsecs;
454*1b191cb5SApple OSS Distributions
455*1b191cb5SApple OSS Distributions clock_interval_to_absolutetime_interval
456*1b191cb5SApple OSS Distributions (interval.tv_nsec, kNanosecondScale, &nsecs);
457*1b191cb5SApple OSS Distributions clock_interval_to_deadline
458*1b191cb5SApple OSS Distributions (interval.tv_sec, NSEC_PER_SEC, &end);
459*1b191cb5SApple OSS Distributions ADD_ABSOLUTETIME(&end, &nsecs);
460*1b191cb5SApple OSS Distributions
461*1b191cb5SApple OSS Distributions return wakeAtTime(end);
462*1b191cb5SApple OSS Distributions }
463*1b191cb5SApple OSS Distributions #endif
464*1b191cb5SApple OSS Distributions
465*1b191cb5SApple OSS Distributions IOReturn
setTimeout(AbsoluteTime interval)466*1b191cb5SApple OSS Distributions IOTimerEventSource::setTimeout(AbsoluteTime interval)
467*1b191cb5SApple OSS Distributions {
468*1b191cb5SApple OSS Distributions AbsoluteTime end;
469*1b191cb5SApple OSS Distributions clock_absolutetime_interval_to_deadline(interval, &end);
470*1b191cb5SApple OSS Distributions return wakeAtTime(end);
471*1b191cb5SApple OSS Distributions }
472*1b191cb5SApple OSS Distributions
473*1b191cb5SApple OSS Distributions IOReturn
setTimeout(uint32_t options,AbsoluteTime abstime,AbsoluteTime leeway)474*1b191cb5SApple OSS Distributions IOTimerEventSource::setTimeout(uint32_t options,
475*1b191cb5SApple OSS Distributions AbsoluteTime abstime, AbsoluteTime leeway)
476*1b191cb5SApple OSS Distributions {
477*1b191cb5SApple OSS Distributions AbsoluteTime end;
478*1b191cb5SApple OSS Distributions if (options & kIOTimeOptionsContinuous) {
479*1b191cb5SApple OSS Distributions clock_continuoustime_interval_to_deadline(abstime, &end);
480*1b191cb5SApple OSS Distributions } else {
481*1b191cb5SApple OSS Distributions clock_absolutetime_interval_to_deadline(abstime, &end);
482*1b191cb5SApple OSS Distributions }
483*1b191cb5SApple OSS Distributions
484*1b191cb5SApple OSS Distributions return wakeAtTime(options, end, leeway);
485*1b191cb5SApple OSS Distributions }
486*1b191cb5SApple OSS Distributions
487*1b191cb5SApple OSS Distributions IOReturn
wakeAtTimeTicks(UInt32 ticks)488*1b191cb5SApple OSS Distributions IOTimerEventSource::wakeAtTimeTicks(UInt32 ticks)
489*1b191cb5SApple OSS Distributions {
490*1b191cb5SApple OSS Distributions return wakeAtTime(ticks, kTickScale);
491*1b191cb5SApple OSS Distributions }
492*1b191cb5SApple OSS Distributions
493*1b191cb5SApple OSS Distributions IOReturn
wakeAtTimeMS(UInt32 ms)494*1b191cb5SApple OSS Distributions IOTimerEventSource::wakeAtTimeMS(UInt32 ms)
495*1b191cb5SApple OSS Distributions {
496*1b191cb5SApple OSS Distributions return wakeAtTime(ms, kMillisecondScale);
497*1b191cb5SApple OSS Distributions }
498*1b191cb5SApple OSS Distributions
499*1b191cb5SApple OSS Distributions IOReturn
wakeAtTimeUS(UInt32 us)500*1b191cb5SApple OSS Distributions IOTimerEventSource::wakeAtTimeUS(UInt32 us)
501*1b191cb5SApple OSS Distributions {
502*1b191cb5SApple OSS Distributions return wakeAtTime(us, kMicrosecondScale);
503*1b191cb5SApple OSS Distributions }
504*1b191cb5SApple OSS Distributions
505*1b191cb5SApple OSS Distributions IOReturn
wakeAtTime(UInt32 inAbstime,UInt32 scale_factor)506*1b191cb5SApple OSS Distributions IOTimerEventSource::wakeAtTime(UInt32 inAbstime, UInt32 scale_factor)
507*1b191cb5SApple OSS Distributions {
508*1b191cb5SApple OSS Distributions AbsoluteTime end;
509*1b191cb5SApple OSS Distributions clock_interval_to_absolutetime_interval(inAbstime, scale_factor, &end);
510*1b191cb5SApple OSS Distributions
511*1b191cb5SApple OSS Distributions return wakeAtTime(end);
512*1b191cb5SApple OSS Distributions }
513*1b191cb5SApple OSS Distributions
514*1b191cb5SApple OSS Distributions #if !defined(__LP64__)
515*1b191cb5SApple OSS Distributions IOReturn
wakeAtTime(mach_timespec_t inAbstime)516*1b191cb5SApple OSS Distributions IOTimerEventSource::wakeAtTime(mach_timespec_t inAbstime)
517*1b191cb5SApple OSS Distributions {
518*1b191cb5SApple OSS Distributions AbsoluteTime end, nsecs;
519*1b191cb5SApple OSS Distributions
520*1b191cb5SApple OSS Distributions clock_interval_to_absolutetime_interval
521*1b191cb5SApple OSS Distributions (inAbstime.tv_nsec, kNanosecondScale, &nsecs);
522*1b191cb5SApple OSS Distributions clock_interval_to_absolutetime_interval
523*1b191cb5SApple OSS Distributions (inAbstime.tv_sec, kSecondScale, &end);
524*1b191cb5SApple OSS Distributions ADD_ABSOLUTETIME(&end, &nsecs);
525*1b191cb5SApple OSS Distributions
526*1b191cb5SApple OSS Distributions return wakeAtTime(end);
527*1b191cb5SApple OSS Distributions }
528*1b191cb5SApple OSS Distributions #endif
529*1b191cb5SApple OSS Distributions
530*1b191cb5SApple OSS Distributions void
setWorkLoop(IOWorkLoop * inWorkLoop)531*1b191cb5SApple OSS Distributions IOTimerEventSource::setWorkLoop(IOWorkLoop *inWorkLoop)
532*1b191cb5SApple OSS Distributions {
533*1b191cb5SApple OSS Distributions super::setWorkLoop(inWorkLoop);
534*1b191cb5SApple OSS Distributions if (enabled && AbsoluteTime_to_scalar(&abstime) && workLoop) {
535*1b191cb5SApple OSS Distributions wakeAtTime(abstime);
536*1b191cb5SApple OSS Distributions }
537*1b191cb5SApple OSS Distributions }
538*1b191cb5SApple OSS Distributions
539*1b191cb5SApple OSS Distributions IOReturn
wakeAtTime(AbsoluteTime inAbstime)540*1b191cb5SApple OSS Distributions IOTimerEventSource::wakeAtTime(AbsoluteTime inAbstime)
541*1b191cb5SApple OSS Distributions {
542*1b191cb5SApple OSS Distributions return wakeAtTime(0, inAbstime, 0);
543*1b191cb5SApple OSS Distributions }
544*1b191cb5SApple OSS Distributions
545*1b191cb5SApple OSS Distributions IOReturn
wakeAtTime(uint32_t options,AbsoluteTime inAbstime,AbsoluteTime leeway)546*1b191cb5SApple OSS Distributions IOTimerEventSource::wakeAtTime(uint32_t options, AbsoluteTime inAbstime, AbsoluteTime leeway)
547*1b191cb5SApple OSS Distributions {
548*1b191cb5SApple OSS Distributions if (!action) {
549*1b191cb5SApple OSS Distributions return kIOReturnNoResources;
550*1b191cb5SApple OSS Distributions }
551*1b191cb5SApple OSS Distributions
552*1b191cb5SApple OSS Distributions abstime = inAbstime;
553*1b191cb5SApple OSS Distributions if (enabled && AbsoluteTime_to_scalar(&inAbstime) && AbsoluteTime_to_scalar(&abstime) && workLoop) {
554*1b191cb5SApple OSS Distributions uint32_t tcoptions = 0;
555*1b191cb5SApple OSS Distributions
556*1b191cb5SApple OSS Distributions if (kIOTimeOptionsWithLeeway & options) {
557*1b191cb5SApple OSS Distributions tcoptions |= THREAD_CALL_DELAY_LEEWAY;
558*1b191cb5SApple OSS Distributions }
559*1b191cb5SApple OSS Distributions if (kIOTimeOptionsContinuous & options) {
560*1b191cb5SApple OSS Distributions tcoptions |= THREAD_CALL_CONTINUOUS;
561*1b191cb5SApple OSS Distributions }
562*1b191cb5SApple OSS Distributions
563*1b191cb5SApple OSS Distributions if (reserved) {
564*1b191cb5SApple OSS Distributions if (kPassive & flags) {
565*1b191cb5SApple OSS Distributions retain();
566*1b191cb5SApple OSS Distributions workLoop->retain();
567*1b191cb5SApple OSS Distributions }
568*1b191cb5SApple OSS Distributions reserved->workLoop = workLoop;
569*1b191cb5SApple OSS Distributions reserved->calloutGeneration++;
570*1b191cb5SApple OSS Distributions if (thread_call_enter_delayed_with_leeway((thread_call_t) calloutEntry,
571*1b191cb5SApple OSS Distributions (void *)(uintptr_t) reserved->calloutGeneration, inAbstime, leeway, tcoptions)
572*1b191cb5SApple OSS Distributions && (kPassive & flags)) {
573*1b191cb5SApple OSS Distributions release();
574*1b191cb5SApple OSS Distributions workLoop->release();
575*1b191cb5SApple OSS Distributions }
576*1b191cb5SApple OSS Distributions } else {
577*1b191cb5SApple OSS Distributions thread_call_enter_delayed_with_leeway((thread_call_t) calloutEntry,
578*1b191cb5SApple OSS Distributions NULL, inAbstime, leeway, tcoptions);
579*1b191cb5SApple OSS Distributions }
580*1b191cb5SApple OSS Distributions }
581*1b191cb5SApple OSS Distributions
582*1b191cb5SApple OSS Distributions return kIOReturnSuccess;
583*1b191cb5SApple OSS Distributions }
584