]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOConditionLock.cpp
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@
28 /* Copyright (c) 1997 Apple Computer, Inc. All rights reserved.
29 * Copyright (c) 1994-1996 NeXT Software, Inc. All rights reserved.
31 * AppleIOPSSafeCondLock.m. Lock object with exported condition variable,
36 * 01-Aug-91 Doug Mitchell at NeXT
40 #include <IOKit/IOConditionLock.h>
42 #define super OSObject
43 OSDefineMetaClassAndStructors(IOConditionLock
, OSObject
)
46 IOConditionLock::initWithCondition(int inCondition
, bool inIntr
)
52 cond_interlock
= IOLockAlloc();
53 sleep_interlock
= IOLockAlloc();
55 condition
= inCondition
;
58 interruptible
= (inIntr
) ? THREAD_INTERRUPTIBLE
: THREAD_UNINT
;
60 return cond_interlock
&& sleep_interlock
;
64 IOConditionLock::withCondition(int condition
, bool intr
)
66 IOConditionLock
*me
= new IOConditionLock
;
68 if (me
&& !me
->initWithCondition(condition
, intr
)) {
76 IOConditionLock::free()
79 IOLockFree(cond_interlock
);
81 if (sleep_interlock
) {
82 IOLockFree(sleep_interlock
);
88 IOConditionLock::getInterruptible() const
94 IOConditionLock:: getCondition() const
100 IOConditionLock:: setCondition(int inCondition
)
104 condition
= inCondition
;
105 thread_wakeup_one((void *) &condition
);
111 IOConditionLock::unlock()
113 IOTakeLock(sleep_interlock
);
115 thread_wakeup_one((void *) &condition
);
120 IOLockWakeup(sleep_interlock
, this, /* one-thread */ false); // Wakeup everybody
123 IOUnlock(sleep_interlock
);
127 IOConditionLock::unlockWith(int inCondition
)
129 IOTakeLock(sleep_interlock
);
130 IOTakeLock(cond_interlock
);
132 condition
= inCondition
;
134 IOUnlock(cond_interlock
);
135 IOUnlock(sleep_interlock
);
141 IOConditionLock::tryLock()
145 IOTakeLock(sleep_interlock
);
152 IOUnlock(sleep_interlock
);
158 IOConditionLock::lock()
160 int thread_res
= THREAD_AWAKENED
;
162 IOTakeLock(sleep_interlock
);
164 /* Try to acquire the want_lock bit. */
165 while (want_lock
&& thread_res
== THREAD_AWAKENED
) {
167 thread_res
= IOLockSleep(sleep_interlock
, (void *) this, interruptible
);
169 if (thread_res
== THREAD_AWAKENED
) {
173 IOUnlock(sleep_interlock
);
179 IOConditionLock::lockWhen(int inCondition
)
184 /* First get the actual lock */
186 if (thread_res
!= THREAD_AWAKENED
) {
187 break; // Failed to acquire lock
189 if (inCondition
== condition
) {
190 break; // Hold lock and condition is expected value
193 * Need to hold a IOTakeLock when we call thread_sleep().
194 * Both _cond_interlock and want_lock must be held to
197 IOTakeLock(cond_interlock
);
198 unlock(); // Release lock and sleep
201 * this is the critical section on a multi in which
202 * another thread could hold _sleep_interlock, but they
203 * can't change _condition. Holding _cond_interlock here
204 * (until after assert_wait() is called from
205 * thread_sleep()) ensures that we'll be notified
206 * of changes in _condition.
208 assert_wait((void *) &condition
, interruptible
); /* assert event */
209 IOUnlock(cond_interlock
); /* release the lock */
210 thread_res
= thread_block(THREAD_CONTINUE_NULL
); /* block ourselves */
211 } while (thread_res
== THREAD_AWAKENED
);