]>
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_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1997 Apple Computer, Inc. All rights reserved.
31 * Copyright (c) 1994-1996 NeXT Software, Inc. All rights reserved.
33 * AppleIOPSSafeCondLock.m. Lock object with exported condition variable,
38 * 01-Aug-91 Doug Mitchell at NeXT
42 #include <IOKit/IOConditionLock.h>
44 #define super OSObject
45 OSDefineMetaClassAndStructors(IOConditionLock
, OSObject
)
47 bool 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
;
63 IOConditionLock
*IOConditionLock::withCondition(int condition
, bool intr
)
65 IOConditionLock
*me
= new IOConditionLock
;
67 if (me
&& !me
->initWithCondition(condition
, intr
)) {
74 void IOConditionLock::free()
77 IOLockFree(cond_interlock
);
79 IOLockFree(sleep_interlock
);
83 bool IOConditionLock::getInterruptible() const
88 int IOConditionLock:: getCondition() const
93 int IOConditionLock:: setCondition(int inCondition
)
97 condition
= inCondition
;
98 thread_wakeup_one((void *) &condition
);
103 void IOConditionLock::unlock()
105 IOTakeLock(sleep_interlock
);
107 thread_wakeup_one((void *) &condition
);
112 IOLockWakeup(sleep_interlock
, this, /* one-thread */ false); // Wakeup everybody
115 IOUnlock(sleep_interlock
);
118 void IOConditionLock::unlockWith(int inCondition
)
120 IOTakeLock(sleep_interlock
);
121 IOTakeLock(cond_interlock
);
123 condition
= inCondition
;
125 IOUnlock(cond_interlock
);
126 IOUnlock(sleep_interlock
);
131 bool IOConditionLock::tryLock()
135 IOTakeLock(sleep_interlock
);
141 IOUnlock(sleep_interlock
);
146 int IOConditionLock::lock()
148 int thread_res
= THREAD_AWAKENED
;
150 IOTakeLock(sleep_interlock
);
152 /* Try to acquire the want_lock bit. */
153 while (want_lock
&& thread_res
== THREAD_AWAKENED
)
156 thread_res
= IOLockSleep(sleep_interlock
, (void *) this, interruptible
);
158 if (thread_res
== THREAD_AWAKENED
)
161 IOUnlock(sleep_interlock
);
166 int IOConditionLock::lockWhen(int inCondition
)
172 /* First get the actual lock */
174 if (thread_res
!= THREAD_AWAKENED
)
175 break; // Failed to acquire lock
177 if (inCondition
== condition
)
178 break; // Hold lock and condition is expected value
181 * Need to hold a IOTakeLock when we call thread_sleep().
182 * Both _cond_interlock and want_lock must be held to
185 IOTakeLock(cond_interlock
);
186 unlock(); // Release lock and sleep
189 * this is the critical section on a multi in which
190 * another thread could hold _sleep_interlock, but they
191 * can't change _condition. Holding _cond_interlock here
192 * (until after assert_wait() is called from
193 * thread_sleep()) ensures that we'll be notified
194 * of changes in _condition.
196 assert_wait((void *) &condition
, interruptible
); /* assert event */
197 IOUnlock(cond_interlock
); /* release the lock */
198 thread_res
= thread_block(THREAD_CONTINUE_NULL
); /* block ourselves */
199 } while (thread_res
== THREAD_AWAKENED
);