]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOSyncer.cpp
2a9cf5465c6780d569915dffe4456db76e91eb10
[apple/xnu.git] / iokit / Kernel / IOSyncer.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* IOSyncer.cpp created by wgulland on 2000-02-02 */
26
27 #include <IOKit/IOLib.h>
28 #include <IOKit/IOSyncer.h>
29
30 OSDefineMetaClassAndStructors(IOSyncer, OSObject)
31
32 IOSyncer * IOSyncer::create(bool twoRetains)
33 {
34 IOSyncer * me = new IOSyncer;
35
36 if (me && !me->init(twoRetains)) {
37 me->release();
38 return 0;
39 }
40
41 return me;
42 }
43
44 bool IOSyncer::init(bool twoRetains)
45 {
46 if (!OSObject::init())
47 return false;
48
49 if (!(guardLock = IOSimpleLockAlloc()) )
50 return false;
51
52 IOSimpleLockInit(guardLock);
53
54 if(twoRetains)
55 retain();
56
57 fResult = kIOReturnSuccess;
58
59 reinit();
60
61 return true;
62 }
63
64 void IOSyncer::reinit()
65 {
66 IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock);
67 threadMustStop = true;
68 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
69 }
70
71 void IOSyncer::free()
72 {
73 // just in case a thread is blocked here:
74 privateSignal();
75
76 if (guardLock != NULL)
77 IOSimpleLockFree(guardLock);
78
79 OSObject::free();
80 }
81
82 IOReturn IOSyncer::wait(bool autoRelease = true)
83 {
84 IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock);
85
86 if (threadMustStop) {
87 assert_wait((void *) &threadMustStop, false);
88 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
89 thread_block(THREAD_CONTINUE_NULL);
90 }
91 else
92 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
93
94 IOReturn result = fResult; // Pick up before auto deleting!
95
96 if(autoRelease)
97 release();
98
99 return result;
100 }
101
102 void IOSyncer::signal(IOReturn res = kIOReturnSuccess,
103 bool autoRelease = true)
104 {
105 fResult = res;
106 privateSignal();
107 if(autoRelease)
108 release();
109 }
110
111 void IOSyncer::privateSignal()
112 {
113 if (threadMustStop) {
114 IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock);
115 threadMustStop = false;
116 thread_wakeup_one((void *) &threadMustStop);
117 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
118 }
119 }