]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
c5f757160e3d1bdc7c8981807f7e3151ad2bdcc0
[apple/xnu.git] / iokit / Kernel / IOWorkLoop.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
30
31 HISTORY
32 1998-7-13 Godfrey van der Linden(gvdl)
33 Created.
34 */
35 #include <IOKit/IOWorkLoop.h>
36 #include <IOKit/IOEventSource.h>
37 #include <IOKit/IOInterruptEventSource.h>
38 #include <IOKit/IOCommandGate.h>
39 #include <IOKit/IOTimeStamp.h>
40
41 #define super OSObject
42
43 OSDefineMetaClassAndStructors(IOWorkLoop, OSObject);
44
45 // Block of unused functions intended for future use
46 OSMetaClassDefineReservedUsed(IOWorkLoop, 0);
47 OSMetaClassDefineReservedUsed(IOWorkLoop, 1);
48
49 OSMetaClassDefineReservedUnused(IOWorkLoop, 2);
50 OSMetaClassDefineReservedUnused(IOWorkLoop, 3);
51 OSMetaClassDefineReservedUnused(IOWorkLoop, 4);
52 OSMetaClassDefineReservedUnused(IOWorkLoop, 5);
53 OSMetaClassDefineReservedUnused(IOWorkLoop, 6);
54 OSMetaClassDefineReservedUnused(IOWorkLoop, 7);
55
56 enum IOWorkLoopState { kLoopRestart = 0x1, kLoopTerminate = 0x2 };
57 static inline void SETP(void *addr, unsigned int flag)
58 { unsigned int *num = (unsigned int *) addr; *num |= flag; }
59 static inline void CLRP(void *addr, unsigned int flag)
60 { unsigned int *num = (unsigned int *) addr; *num &= ~flag; }
61 static inline bool ISSETP(void *addr, unsigned int flag)
62 { unsigned int *num = (unsigned int *) addr; return (*num & flag) != 0; }
63
64 #define fFlags loopRestart
65
66 bool IOWorkLoop::init()
67 {
68 // The super init and gateLock allocation MUST be done first
69 if ( !super::init() )
70 return false;
71
72 if ( !(gateLock = IORecursiveLockAlloc()) )
73 return false;
74
75 if ( !(workToDoLock = IOSimpleLockAlloc()) )
76 return false;
77
78 controlG = IOCommandGate::
79 commandGate(this, OSMemberFunctionCast(IOCommandGate::Action,
80 this, &IOWorkLoop::_maintRequest));
81 if ( !controlG )
82 return false;
83
84 IOSimpleLockInit(workToDoLock);
85 workToDo = false;
86
87 // Point the controlGate at the workLoop. Usually addEventSource
88 // does this automatically. The problem is in this case addEventSource
89 // uses the control gate and it has to be bootstrapped.
90 controlG->setWorkLoop(this);
91 if (addEventSource(controlG) != kIOReturnSuccess)
92 return false;
93
94 IOThreadFunc cptr =
95 OSMemberFunctionCast(IOThreadFunc, this, &IOWorkLoop::threadMain);
96 workThread = IOCreateThread(cptr, this);
97 if (!workThread)
98 return false;
99
100 return true;
101 }
102
103 IOWorkLoop *
104 IOWorkLoop::workLoop()
105 {
106 IOWorkLoop *me = new IOWorkLoop;
107
108 if (me && !me->init()) {
109 me->release();
110 return 0;
111 }
112
113 return me;
114 }
115
116 // Free is called twice:
117 // First when the atomic retainCount transitions from 1 -> 0
118 // Secondly when the work loop itself is commiting hari kari
119 // Hence the each leg of the free must be single threaded.
120 void IOWorkLoop::free()
121 {
122 if (workThread) {
123 IOInterruptState is;
124
125 // If we are here then we must be trying to shut down this work loop
126 // in this case disable all of the event source, mark the loop for
127 // as terminating and wakeup the work thread itself and return
128 // Note: we hold the gate across the entire operation mainly for the
129 // benefit of our event sources so we can disable them cleanly.
130 closeGate();
131
132 disableAllEventSources();
133
134 is = IOSimpleLockLockDisableInterrupt(workToDoLock);
135 SETP(&fFlags, kLoopTerminate);
136 thread_wakeup_one((void *) &workToDo);
137 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
138
139 openGate();
140 }
141 else /* !workThread */ {
142 IOEventSource *event, *next;
143
144 for (event = eventChain; event; event = next) {
145 next = event->getNext();
146 event->setWorkLoop(0);
147 event->setNext(0);
148 event->release();
149 }
150 eventChain = 0;
151
152 // Either we have a partial initialisation to clean up
153 // or we the workThread itself is performing hari-kari.
154 // either way clean up all of our resources and return.
155
156 if (controlG) {
157 controlG->release();
158 controlG = 0;
159 }
160
161 if (workToDoLock) {
162 IOSimpleLockFree(workToDoLock);
163 workToDoLock = 0;
164 }
165
166 if (gateLock) {
167 IORecursiveLockFree(gateLock);
168 gateLock = 0;
169 }
170
171 super::free();
172 }
173 }
174
175 IOReturn IOWorkLoop::addEventSource(IOEventSource *newEvent)
176 {
177 return controlG->runCommand((void *) mAddEvent, (void *) newEvent);
178 }
179
180 IOReturn IOWorkLoop::removeEventSource(IOEventSource *toRemove)
181 {
182 return controlG->runCommand((void *) mRemoveEvent, (void *) toRemove);
183 }
184
185 void IOWorkLoop::enableAllEventSources() const
186 {
187 IOEventSource *event;
188
189 for (event = eventChain; event; event = event->getNext())
190 event->enable();
191 }
192
193 void IOWorkLoop::disableAllEventSources() const
194 {
195 IOEventSource *event;
196
197 for (event = eventChain; event; event = event->getNext())
198 if (event != controlG) // Don't disable the control gate
199 event->disable();
200 }
201
202 void IOWorkLoop::enableAllInterrupts() const
203 {
204 IOEventSource *event;
205
206 for (event = eventChain; event; event = event->getNext())
207 if (OSDynamicCast(IOInterruptEventSource, event))
208 event->enable();
209 }
210
211 void IOWorkLoop::disableAllInterrupts() const
212 {
213 IOEventSource *event;
214
215 for (event = eventChain; event; event = event->getNext())
216 if (OSDynamicCast(IOInterruptEventSource, event))
217 event->disable();
218 }
219
220 #if KDEBUG
221 #define IOTimeClientS() \
222 do { \
223 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
224 (unsigned int) this, (unsigned int) event); \
225 } while(0)
226
227 #define IOTimeClientE() \
228 do { \
229 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
230 (unsigned int) this, (unsigned int) event); \
231 } while(0)
232
233 #define IOTimeWorkS() \
234 do { \
235 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
236 } while(0)
237
238 #define IOTimeWorkE() \
239 do { \
240 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
241 } while(0)
242
243 #else /* !KDEBUG */
244
245 #define IOTimeClientS()
246 #define IOTimeClientE()
247 #define IOTimeWorkS()
248 #define IOTimeWorkE()
249
250 #endif /* KDEBUG */
251
252 /* virtual */ bool IOWorkLoop::runEventSources()
253 {
254 bool res = false;
255 closeGate();
256 if (ISSETP(&fFlags, kLoopTerminate))
257 goto abort;
258
259 IOTimeWorkS();
260 bool more;
261 do {
262 CLRP(&fFlags, kLoopRestart);
263 workToDo = more = false;
264 for (IOEventSource *evnt = eventChain; evnt; evnt = evnt->getNext()) {
265
266 IOTimeClientS();
267 more |= evnt->checkForWork();
268 IOTimeClientE();
269
270 if (ISSETP(&fFlags, kLoopTerminate))
271 goto abort;
272 else if (fFlags & kLoopRestart) {
273 more = true;
274 break;
275 }
276 }
277 } while (more);
278
279 res = true;
280 IOTimeWorkE();
281
282 abort:
283 openGate();
284 return res;
285 }
286
287 /* virtual */ void IOWorkLoop::threadMain()
288 {
289 do {
290 if ( !runEventSources() )
291 goto exitThread;
292
293 IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock);
294 if ( !ISSETP(&fFlags, kLoopTerminate) && !workToDo) {
295 assert_wait((void *) &workToDo, false);
296 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
297
298 thread_continue_t cptr = OSMemberFunctionCast(
299 thread_continue_t, this, &IOWorkLoop::threadMain);
300 thread_block_parameter(cptr, this);
301 /* NOTREACHED */
302 }
303
304 // At this point we either have work to do or we need
305 // to commit suicide. But no matter
306 // Clear the simple lock and retore the interrupt state
307 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
308 } while(workToDo);
309
310 exitThread:
311 workThread = 0; // Say we don't have a loop and free ourselves
312 free();
313 IOExitThread();
314 }
315
316 IOThread IOWorkLoop::getThread() const
317 {
318 return workThread;
319 }
320
321 bool IOWorkLoop::onThread() const
322 {
323 return (IOThreadSelf() == workThread);
324 }
325
326 bool IOWorkLoop::inGate() const
327 {
328 return IORecursiveLockHaveLock(gateLock);
329 }
330
331 // Internal APIs used by event sources to control the thread
332 void IOWorkLoop::signalWorkAvailable()
333 {
334 if (workToDoLock) {
335 IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock);
336 workToDo = true;
337 thread_wakeup_one((void *) &workToDo);
338 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
339 }
340 }
341
342 void IOWorkLoop::openGate()
343 {
344 IORecursiveLockUnlock(gateLock);
345 }
346
347 void IOWorkLoop::closeGate()
348 {
349 IORecursiveLockLock(gateLock);
350 }
351
352 bool IOWorkLoop::tryCloseGate()
353 {
354 return IORecursiveLockTryLock(gateLock) != 0;
355 }
356
357 int IOWorkLoop::sleepGate(void *event, UInt32 interuptibleType)
358 {
359 return IORecursiveLockSleep(gateLock, event, interuptibleType);
360 }
361
362 void IOWorkLoop::wakeupGate(void *event, bool oneThread)
363 {
364 IORecursiveLockWakeup(gateLock, event, oneThread);
365 }
366
367 IOReturn IOWorkLoop::runAction(Action inAction, OSObject *target,
368 void *arg0, void *arg1,
369 void *arg2, void *arg3)
370 {
371 IOReturn res;
372
373 // closeGate is recursive so don't worry if we already hold the lock.
374 closeGate();
375 res = (*inAction)(target, arg0, arg1, arg2, arg3);
376 openGate();
377
378 return res;
379 }
380
381 IOReturn IOWorkLoop::_maintRequest(void *inC, void *inD, void *, void *)
382 {
383 maintCommandEnum command = (maintCommandEnum) (vm_address_t) inC;
384 IOEventSource *inEvent = (IOEventSource *) inD;
385 IOReturn res = kIOReturnSuccess;
386
387 switch (command)
388 {
389 case mAddEvent:
390 if (!inEvent->getWorkLoop()) {
391 SETP(&fFlags, kLoopRestart);
392
393 inEvent->retain();
394 inEvent->setWorkLoop(this);
395 inEvent->setNext(0);
396
397 if (!eventChain)
398 eventChain = inEvent;
399 else {
400 IOEventSource *event, *next;
401
402 for (event = eventChain; (next = event->getNext()); event = next)
403 ;
404 event->setNext(inEvent);
405 }
406 }
407 break;
408
409 case mRemoveEvent:
410 if (inEvent->getWorkLoop()) {
411 if (eventChain == inEvent)
412 eventChain = inEvent->getNext();
413 else {
414 IOEventSource *event, *next;
415
416 event = eventChain;
417 while ((next = event->getNext()) && next != inEvent)
418 event = next;
419
420 if (!next) {
421 res = kIOReturnBadArgument;
422 break;
423 }
424 event->setNext(inEvent->getNext());
425 }
426
427 inEvent->setWorkLoop(0);
428 inEvent->setNext(0);
429 inEvent->release();
430 SETP(&fFlags, kLoopRestart);
431 }
432 break;
433
434 default:
435 return kIOReturnUnsupported;
436 }
437
438 return res;
439 }