xref: /xnu-10063.121.3/iokit/Kernel/IOLocks.cpp (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 1998-2007 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions  *
15*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions  *
18*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions  * limitations under the License.
25*2c2f96dcSApple OSS Distributions  *
26*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions  */
28*2c2f96dcSApple OSS Distributions 
29*2c2f96dcSApple OSS Distributions #include <IOKit/system.h>
30*2c2f96dcSApple OSS Distributions 
31*2c2f96dcSApple OSS Distributions #include <IOKit/IOReturn.h>
32*2c2f96dcSApple OSS Distributions #include <IOKit/IOLib.h>
33*2c2f96dcSApple OSS Distributions #include <IOKit/assert.h>
34*2c2f96dcSApple OSS Distributions 
35*2c2f96dcSApple OSS Distributions #include <IOKit/IOLocksPrivate.h>
36*2c2f96dcSApple OSS Distributions 
37*2c2f96dcSApple OSS Distributions extern "C" {
38*2c2f96dcSApple OSS Distributions #include <kern/locks.h>
39*2c2f96dcSApple OSS Distributions 
40*2c2f96dcSApple OSS Distributions #if defined(__x86_64__)
41*2c2f96dcSApple OSS Distributions /* Synthetic event if none is specified, for backwards compatibility only. */
42*2c2f96dcSApple OSS Distributions static bool IOLockSleep_NO_EVENT __attribute__((used)) = 0;
43*2c2f96dcSApple OSS Distributions #endif
44*2c2f96dcSApple OSS Distributions 
45*2c2f96dcSApple OSS Distributions void
IOLockInitWithState(IOLock * lock,IOLockState state)46*2c2f96dcSApple OSS Distributions IOLockInitWithState( IOLock * lock, IOLockState state)
47*2c2f96dcSApple OSS Distributions {
48*2c2f96dcSApple OSS Distributions 	if (state == kIOLockStateLocked) {
49*2c2f96dcSApple OSS Distributions 		lck_mtx_lock( lock);
50*2c2f96dcSApple OSS Distributions 	}
51*2c2f96dcSApple OSS Distributions }
52*2c2f96dcSApple OSS Distributions 
53*2c2f96dcSApple OSS Distributions IOLock *
IOLockAlloc(void)54*2c2f96dcSApple OSS Distributions IOLockAlloc( void )
55*2c2f96dcSApple OSS Distributions {
56*2c2f96dcSApple OSS Distributions 	return lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL);
57*2c2f96dcSApple OSS Distributions }
58*2c2f96dcSApple OSS Distributions 
59*2c2f96dcSApple OSS Distributions void
IOLockInlineInit(IOLock * lock)60*2c2f96dcSApple OSS Distributions IOLockInlineInit( IOLock *lock )
61*2c2f96dcSApple OSS Distributions {
62*2c2f96dcSApple OSS Distributions 	lck_mtx_init(lock, IOLockGroup, LCK_ATTR_NULL);
63*2c2f96dcSApple OSS Distributions }
64*2c2f96dcSApple OSS Distributions 
65*2c2f96dcSApple OSS Distributions void
IOLockInlineDestroy(IOLock * lock)66*2c2f96dcSApple OSS Distributions IOLockInlineDestroy( IOLock * lock)
67*2c2f96dcSApple OSS Distributions {
68*2c2f96dcSApple OSS Distributions 	lck_mtx_destroy( lock, IOLockGroup);
69*2c2f96dcSApple OSS Distributions }
70*2c2f96dcSApple OSS Distributions 
71*2c2f96dcSApple OSS Distributions void
IOLockFree(IOLock * lock)72*2c2f96dcSApple OSS Distributions IOLockFree( IOLock * lock)
73*2c2f96dcSApple OSS Distributions {
74*2c2f96dcSApple OSS Distributions 	lck_mtx_free( lock, IOLockGroup);
75*2c2f96dcSApple OSS Distributions }
76*2c2f96dcSApple OSS Distributions 
77*2c2f96dcSApple OSS Distributions lck_mtx_t *
IOLockGetMachLock(IOLock * lock)78*2c2f96dcSApple OSS Distributions IOLockGetMachLock( IOLock * lock)
79*2c2f96dcSApple OSS Distributions {
80*2c2f96dcSApple OSS Distributions 	return (lck_mtx_t *)lock;
81*2c2f96dcSApple OSS Distributions }
82*2c2f96dcSApple OSS Distributions 
83*2c2f96dcSApple OSS Distributions int
IOLockSleep(IOLock * lock,void * event,UInt32 interType)84*2c2f96dcSApple OSS Distributions IOLockSleep( IOLock * lock, void *event, UInt32 interType)
85*2c2f96dcSApple OSS Distributions {
86*2c2f96dcSApple OSS Distributions 	return (int) lck_mtx_sleep(lock, LCK_SLEEP_PROMOTED_PRI, (event_t) event, (wait_interrupt_t) interType);
87*2c2f96dcSApple OSS Distributions }
88*2c2f96dcSApple OSS Distributions 
89*2c2f96dcSApple OSS Distributions int
IOLockSleepDeadline(IOLock * lock,void * event,AbsoluteTime deadline,UInt32 interType)90*2c2f96dcSApple OSS Distributions IOLockSleepDeadline( IOLock * lock, void *event,
91*2c2f96dcSApple OSS Distributions     AbsoluteTime deadline, UInt32 interType)
92*2c2f96dcSApple OSS Distributions {
93*2c2f96dcSApple OSS Distributions 	return (int) lck_mtx_sleep_deadline(lock, LCK_SLEEP_PROMOTED_PRI, (event_t) event,
94*2c2f96dcSApple OSS Distributions 	           (wait_interrupt_t) interType, __OSAbsoluteTime(deadline));
95*2c2f96dcSApple OSS Distributions }
96*2c2f96dcSApple OSS Distributions 
97*2c2f96dcSApple OSS Distributions void
IOLockWakeup(IOLock * lock,void * event,bool oneThread)98*2c2f96dcSApple OSS Distributions IOLockWakeup(IOLock * lock, void *event, bool oneThread)
99*2c2f96dcSApple OSS Distributions {
100*2c2f96dcSApple OSS Distributions 	thread_wakeup_prim((event_t) event, oneThread, THREAD_AWAKENED);
101*2c2f96dcSApple OSS Distributions }
102*2c2f96dcSApple OSS Distributions 
103*2c2f96dcSApple OSS Distributions 
104*2c2f96dcSApple OSS Distributions #if defined(__x86_64__)
105*2c2f96dcSApple OSS Distributions /*
106*2c2f96dcSApple OSS Distributions  * For backwards compatibility, kexts built against pre-Darwin 14 headers will bind at runtime to this function,
107*2c2f96dcSApple OSS Distributions  * which supports a NULL event,
108*2c2f96dcSApple OSS Distributions  */
109*2c2f96dcSApple OSS Distributions int     IOLockSleep_legacy_x86_64( IOLock * lock, void *event, UInt32 interType) __asm("_IOLockSleep");
110*2c2f96dcSApple OSS Distributions int     IOLockSleepDeadline_legacy_x86_64( IOLock * lock, void *event,
111*2c2f96dcSApple OSS Distributions     AbsoluteTime deadline, UInt32 interType) __asm("_IOLockSleepDeadline");
112*2c2f96dcSApple OSS Distributions void    IOLockWakeup_legacy_x86_64(IOLock * lock, void *event, bool oneThread) __asm("_IOLockWakeup");
113*2c2f96dcSApple OSS Distributions 
114*2c2f96dcSApple OSS Distributions int
IOLockSleep_legacy_x86_64(IOLock * lock,void * event,UInt32 interType)115*2c2f96dcSApple OSS Distributions IOLockSleep_legacy_x86_64( IOLock * lock, void *event, UInt32 interType)
116*2c2f96dcSApple OSS Distributions {
117*2c2f96dcSApple OSS Distributions 	if (event == NULL) {
118*2c2f96dcSApple OSS Distributions 		event = (void *)&IOLockSleep_NO_EVENT;
119*2c2f96dcSApple OSS Distributions 	}
120*2c2f96dcSApple OSS Distributions 
121*2c2f96dcSApple OSS Distributions 	return IOLockSleep(lock, event, interType);
122*2c2f96dcSApple OSS Distributions }
123*2c2f96dcSApple OSS Distributions 
124*2c2f96dcSApple OSS Distributions int
IOLockSleepDeadline_legacy_x86_64(IOLock * lock,void * event,AbsoluteTime deadline,UInt32 interType)125*2c2f96dcSApple OSS Distributions IOLockSleepDeadline_legacy_x86_64( IOLock * lock, void *event,
126*2c2f96dcSApple OSS Distributions     AbsoluteTime deadline, UInt32 interType)
127*2c2f96dcSApple OSS Distributions {
128*2c2f96dcSApple OSS Distributions 	if (event == NULL) {
129*2c2f96dcSApple OSS Distributions 		event = (void *)&IOLockSleep_NO_EVENT;
130*2c2f96dcSApple OSS Distributions 	}
131*2c2f96dcSApple OSS Distributions 
132*2c2f96dcSApple OSS Distributions 	return IOLockSleepDeadline(lock, event, deadline, interType);
133*2c2f96dcSApple OSS Distributions }
134*2c2f96dcSApple OSS Distributions 
135*2c2f96dcSApple OSS Distributions void
IOLockWakeup_legacy_x86_64(IOLock * lock,void * event,bool oneThread)136*2c2f96dcSApple OSS Distributions IOLockWakeup_legacy_x86_64(IOLock * lock, void *event, bool oneThread)
137*2c2f96dcSApple OSS Distributions {
138*2c2f96dcSApple OSS Distributions 	if (event == NULL) {
139*2c2f96dcSApple OSS Distributions 		event = (void *)&IOLockSleep_NO_EVENT;
140*2c2f96dcSApple OSS Distributions 	}
141*2c2f96dcSApple OSS Distributions 
142*2c2f96dcSApple OSS Distributions 	IOLockWakeup(lock, event, oneThread);
143*2c2f96dcSApple OSS Distributions }
144*2c2f96dcSApple OSS Distributions #endif /* defined(__x86_64__) */
145*2c2f96dcSApple OSS Distributions 
146*2c2f96dcSApple OSS Distributions 
147*2c2f96dcSApple OSS Distributions struct _IORecursiveLock {
148*2c2f96dcSApple OSS Distributions 	lck_mtx_t       mutex;
149*2c2f96dcSApple OSS Distributions 	lck_grp_t       *group;
150*2c2f96dcSApple OSS Distributions 	thread_t        thread;
151*2c2f96dcSApple OSS Distributions 	UInt32          count;
152*2c2f96dcSApple OSS Distributions };
153*2c2f96dcSApple OSS Distributions 
154*2c2f96dcSApple OSS Distributions IORecursiveLock *
IORecursiveLockAllocWithLockGroup(lck_grp_t * lockGroup)155*2c2f96dcSApple OSS Distributions IORecursiveLockAllocWithLockGroup( lck_grp_t * lockGroup )
156*2c2f96dcSApple OSS Distributions {
157*2c2f96dcSApple OSS Distributions 	_IORecursiveLock * lock;
158*2c2f96dcSApple OSS Distributions 
159*2c2f96dcSApple OSS Distributions 	if (lockGroup == NULL) {
160*2c2f96dcSApple OSS Distributions 		return NULL;
161*2c2f96dcSApple OSS Distributions 	}
162*2c2f96dcSApple OSS Distributions 
163*2c2f96dcSApple OSS Distributions 	lock = IOMallocType( _IORecursiveLock );
164*2c2f96dcSApple OSS Distributions 	if (!lock) {
165*2c2f96dcSApple OSS Distributions 		return NULL;
166*2c2f96dcSApple OSS Distributions 	}
167*2c2f96dcSApple OSS Distributions 
168*2c2f96dcSApple OSS Distributions 	lck_mtx_init( &lock->mutex, lockGroup, LCK_ATTR_NULL );
169*2c2f96dcSApple OSS Distributions 	lock->group = lockGroup;
170*2c2f96dcSApple OSS Distributions 	lock->thread = NULL;
171*2c2f96dcSApple OSS Distributions 	lock->count  = 0;
172*2c2f96dcSApple OSS Distributions 
173*2c2f96dcSApple OSS Distributions 	return (IORecursiveLock *) lock;
174*2c2f96dcSApple OSS Distributions }
175*2c2f96dcSApple OSS Distributions 
176*2c2f96dcSApple OSS Distributions 
177*2c2f96dcSApple OSS Distributions IORecursiveLock *
IORecursiveLockAlloc(void)178*2c2f96dcSApple OSS Distributions IORecursiveLockAlloc( void )
179*2c2f96dcSApple OSS Distributions {
180*2c2f96dcSApple OSS Distributions 	return IORecursiveLockAllocWithLockGroup( IOLockGroup );
181*2c2f96dcSApple OSS Distributions }
182*2c2f96dcSApple OSS Distributions 
183*2c2f96dcSApple OSS Distributions void
IORecursiveLockFree(IORecursiveLock * _lock)184*2c2f96dcSApple OSS Distributions IORecursiveLockFree( IORecursiveLock * _lock )
185*2c2f96dcSApple OSS Distributions {
186*2c2f96dcSApple OSS Distributions 	_IORecursiveLock * lock = (_IORecursiveLock *)_lock;
187*2c2f96dcSApple OSS Distributions 
188*2c2f96dcSApple OSS Distributions 	lck_mtx_destroy(&lock->mutex, lock->group);
189*2c2f96dcSApple OSS Distributions 	IOFreeType( lock, _IORecursiveLock );
190*2c2f96dcSApple OSS Distributions }
191*2c2f96dcSApple OSS Distributions 
192*2c2f96dcSApple OSS Distributions lck_mtx_t *
IORecursiveLockGetMachLock(IORecursiveLock * lock)193*2c2f96dcSApple OSS Distributions IORecursiveLockGetMachLock( IORecursiveLock * lock )
194*2c2f96dcSApple OSS Distributions {
195*2c2f96dcSApple OSS Distributions 	return &lock->mutex;
196*2c2f96dcSApple OSS Distributions }
197*2c2f96dcSApple OSS Distributions 
198*2c2f96dcSApple OSS Distributions void
IORecursiveLockLock(IORecursiveLock * _lock)199*2c2f96dcSApple OSS Distributions IORecursiveLockLock( IORecursiveLock * _lock)
200*2c2f96dcSApple OSS Distributions {
201*2c2f96dcSApple OSS Distributions 	_IORecursiveLock * lock = (_IORecursiveLock *)_lock;
202*2c2f96dcSApple OSS Distributions 
203*2c2f96dcSApple OSS Distributions 	if (lock->thread == IOThreadSelf()) {
204*2c2f96dcSApple OSS Distributions 		lock->count++;
205*2c2f96dcSApple OSS Distributions 	} else {
206*2c2f96dcSApple OSS Distributions 		lck_mtx_lock( &lock->mutex );
207*2c2f96dcSApple OSS Distributions 		assert( lock->thread == NULL );
208*2c2f96dcSApple OSS Distributions 		assert( lock->count == 0 );
209*2c2f96dcSApple OSS Distributions 		lock->thread = IOThreadSelf();
210*2c2f96dcSApple OSS Distributions 		lock->count = 1;
211*2c2f96dcSApple OSS Distributions 	}
212*2c2f96dcSApple OSS Distributions }
213*2c2f96dcSApple OSS Distributions 
214*2c2f96dcSApple OSS Distributions boolean_t
IORecursiveLockTryLock(IORecursiveLock * _lock)215*2c2f96dcSApple OSS Distributions IORecursiveLockTryLock( IORecursiveLock * _lock)
216*2c2f96dcSApple OSS Distributions {
217*2c2f96dcSApple OSS Distributions 	_IORecursiveLock * lock = (_IORecursiveLock *)_lock;
218*2c2f96dcSApple OSS Distributions 
219*2c2f96dcSApple OSS Distributions 	if (lock->thread == IOThreadSelf()) {
220*2c2f96dcSApple OSS Distributions 		lock->count++;
221*2c2f96dcSApple OSS Distributions 		return true;
222*2c2f96dcSApple OSS Distributions 	} else {
223*2c2f96dcSApple OSS Distributions 		if (lck_mtx_try_lock( &lock->mutex )) {
224*2c2f96dcSApple OSS Distributions 			assert( lock->thread == NULL );
225*2c2f96dcSApple OSS Distributions 			assert( lock->count == 0 );
226*2c2f96dcSApple OSS Distributions 			lock->thread = IOThreadSelf();
227*2c2f96dcSApple OSS Distributions 			lock->count = 1;
228*2c2f96dcSApple OSS Distributions 			return true;
229*2c2f96dcSApple OSS Distributions 		}
230*2c2f96dcSApple OSS Distributions 	}
231*2c2f96dcSApple OSS Distributions 	return false;
232*2c2f96dcSApple OSS Distributions }
233*2c2f96dcSApple OSS Distributions 
234*2c2f96dcSApple OSS Distributions void
IORecursiveLockUnlock(IORecursiveLock * _lock)235*2c2f96dcSApple OSS Distributions IORecursiveLockUnlock( IORecursiveLock * _lock)
236*2c2f96dcSApple OSS Distributions {
237*2c2f96dcSApple OSS Distributions 	_IORecursiveLock * lock = (_IORecursiveLock *)_lock;
238*2c2f96dcSApple OSS Distributions 
239*2c2f96dcSApple OSS Distributions 	assert( lock->thread == IOThreadSelf());
240*2c2f96dcSApple OSS Distributions 
241*2c2f96dcSApple OSS Distributions 	if (0 == (--lock->count)) {
242*2c2f96dcSApple OSS Distributions 		lock->thread = NULL;
243*2c2f96dcSApple OSS Distributions 		lck_mtx_unlock( &lock->mutex );
244*2c2f96dcSApple OSS Distributions 	}
245*2c2f96dcSApple OSS Distributions }
246*2c2f96dcSApple OSS Distributions 
247*2c2f96dcSApple OSS Distributions boolean_t
IORecursiveLockHaveLock(const IORecursiveLock * _lock)248*2c2f96dcSApple OSS Distributions IORecursiveLockHaveLock( const IORecursiveLock * _lock)
249*2c2f96dcSApple OSS Distributions {
250*2c2f96dcSApple OSS Distributions 	_IORecursiveLock * lock = (_IORecursiveLock *)_lock;
251*2c2f96dcSApple OSS Distributions 
252*2c2f96dcSApple OSS Distributions 	return lock->thread == IOThreadSelf();
253*2c2f96dcSApple OSS Distributions }
254*2c2f96dcSApple OSS Distributions 
255*2c2f96dcSApple OSS Distributions int
IORecursiveLockSleep(IORecursiveLock * _lock,void * event,UInt32 interType)256*2c2f96dcSApple OSS Distributions IORecursiveLockSleep(IORecursiveLock *_lock, void *event, UInt32 interType)
257*2c2f96dcSApple OSS Distributions {
258*2c2f96dcSApple OSS Distributions 	_IORecursiveLock * lock = (_IORecursiveLock *)_lock;
259*2c2f96dcSApple OSS Distributions 	UInt32 count = lock->count;
260*2c2f96dcSApple OSS Distributions 	int res;
261*2c2f96dcSApple OSS Distributions 
262*2c2f96dcSApple OSS Distributions 	assert(lock->thread == IOThreadSelf());
263*2c2f96dcSApple OSS Distributions 
264*2c2f96dcSApple OSS Distributions 	lock->count = 0;
265*2c2f96dcSApple OSS Distributions 	lock->thread = NULL;
266*2c2f96dcSApple OSS Distributions 	res = lck_mtx_sleep(&lock->mutex, LCK_SLEEP_PROMOTED_PRI, (event_t) event, (wait_interrupt_t) interType);
267*2c2f96dcSApple OSS Distributions 
268*2c2f96dcSApple OSS Distributions 	// Must re-establish the recursive lock no matter why we woke up
269*2c2f96dcSApple OSS Distributions 	// otherwise we would potentially leave the return path corrupted.
270*2c2f96dcSApple OSS Distributions 	assert(lock->thread == NULL);
271*2c2f96dcSApple OSS Distributions 	assert(lock->count == 0);
272*2c2f96dcSApple OSS Distributions 	lock->thread = IOThreadSelf();
273*2c2f96dcSApple OSS Distributions 	lock->count = count;
274*2c2f96dcSApple OSS Distributions 	return res;
275*2c2f96dcSApple OSS Distributions }
276*2c2f96dcSApple OSS Distributions 
277*2c2f96dcSApple OSS Distributions int
IORecursiveLockSleepDeadline(IORecursiveLock * _lock,void * event,AbsoluteTime deadline,UInt32 interType)278*2c2f96dcSApple OSS Distributions IORecursiveLockSleepDeadline( IORecursiveLock * _lock, void *event,
279*2c2f96dcSApple OSS Distributions     AbsoluteTime deadline, UInt32 interType)
280*2c2f96dcSApple OSS Distributions {
281*2c2f96dcSApple OSS Distributions 	_IORecursiveLock * lock = (_IORecursiveLock *)_lock;
282*2c2f96dcSApple OSS Distributions 	UInt32 count = lock->count;
283*2c2f96dcSApple OSS Distributions 	int res;
284*2c2f96dcSApple OSS Distributions 
285*2c2f96dcSApple OSS Distributions 	assert(lock->thread == IOThreadSelf());
286*2c2f96dcSApple OSS Distributions 
287*2c2f96dcSApple OSS Distributions 	lock->count = 0;
288*2c2f96dcSApple OSS Distributions 	lock->thread = NULL;
289*2c2f96dcSApple OSS Distributions 	res = lck_mtx_sleep_deadline(&lock->mutex, LCK_SLEEP_PROMOTED_PRI, (event_t) event,
290*2c2f96dcSApple OSS Distributions 	    (wait_interrupt_t) interType, __OSAbsoluteTime(deadline));
291*2c2f96dcSApple OSS Distributions 
292*2c2f96dcSApple OSS Distributions 	// Must re-establish the recursive lock no matter why we woke up
293*2c2f96dcSApple OSS Distributions 	// otherwise we would potentially leave the return path corrupted.
294*2c2f96dcSApple OSS Distributions 	assert(lock->thread == NULL);
295*2c2f96dcSApple OSS Distributions 	assert(lock->count == 0);
296*2c2f96dcSApple OSS Distributions 	lock->thread = IOThreadSelf();
297*2c2f96dcSApple OSS Distributions 	lock->count = count;
298*2c2f96dcSApple OSS Distributions 	return res;
299*2c2f96dcSApple OSS Distributions }
300*2c2f96dcSApple OSS Distributions 
301*2c2f96dcSApple OSS Distributions void
IORecursiveLockWakeup(IORecursiveLock *,void * event,bool oneThread)302*2c2f96dcSApple OSS Distributions IORecursiveLockWakeup(IORecursiveLock *, void *event, bool oneThread)
303*2c2f96dcSApple OSS Distributions {
304*2c2f96dcSApple OSS Distributions 	thread_wakeup_prim((event_t) event, oneThread, THREAD_AWAKENED);
305*2c2f96dcSApple OSS Distributions }
306*2c2f96dcSApple OSS Distributions 
307*2c2f96dcSApple OSS Distributions /*
308*2c2f96dcSApple OSS Distributions  * Complex (read/write) lock operations
309*2c2f96dcSApple OSS Distributions  */
310*2c2f96dcSApple OSS Distributions 
311*2c2f96dcSApple OSS Distributions IORWLock *
IORWLockAlloc(void)312*2c2f96dcSApple OSS Distributions IORWLockAlloc( void )
313*2c2f96dcSApple OSS Distributions {
314*2c2f96dcSApple OSS Distributions 	return lck_rw_alloc_init(IOLockGroup, LCK_ATTR_NULL);
315*2c2f96dcSApple OSS Distributions }
316*2c2f96dcSApple OSS Distributions 
317*2c2f96dcSApple OSS Distributions void
IORWLockInlineInit(IORWLock * lock)318*2c2f96dcSApple OSS Distributions IORWLockInlineInit( IORWLock *lock )
319*2c2f96dcSApple OSS Distributions {
320*2c2f96dcSApple OSS Distributions 	lck_rw_init(lock, IOLockGroup, LCK_ATTR_NULL);
321*2c2f96dcSApple OSS Distributions }
322*2c2f96dcSApple OSS Distributions 
323*2c2f96dcSApple OSS Distributions void
IORWLockInlineDestroy(IORWLock * lock)324*2c2f96dcSApple OSS Distributions IORWLockInlineDestroy( IORWLock * lock)
325*2c2f96dcSApple OSS Distributions {
326*2c2f96dcSApple OSS Distributions 	lck_rw_destroy( lock, IOLockGroup);
327*2c2f96dcSApple OSS Distributions }
328*2c2f96dcSApple OSS Distributions 
329*2c2f96dcSApple OSS Distributions void
IORWLockFree(IORWLock * lock)330*2c2f96dcSApple OSS Distributions IORWLockFree( IORWLock * lock)
331*2c2f96dcSApple OSS Distributions {
332*2c2f96dcSApple OSS Distributions 	lck_rw_free( lock, IOLockGroup);
333*2c2f96dcSApple OSS Distributions }
334*2c2f96dcSApple OSS Distributions 
335*2c2f96dcSApple OSS Distributions lck_rw_t *
IORWLockGetMachLock(IORWLock * lock)336*2c2f96dcSApple OSS Distributions IORWLockGetMachLock( IORWLock * lock)
337*2c2f96dcSApple OSS Distributions {
338*2c2f96dcSApple OSS Distributions 	return (lck_rw_t *)lock;
339*2c2f96dcSApple OSS Distributions }
340*2c2f96dcSApple OSS Distributions 
341*2c2f96dcSApple OSS Distributions 
342*2c2f96dcSApple OSS Distributions /*
343*2c2f96dcSApple OSS Distributions  * Spin locks
344*2c2f96dcSApple OSS Distributions  */
345*2c2f96dcSApple OSS Distributions 
346*2c2f96dcSApple OSS Distributions IOSimpleLock *
IOSimpleLockAlloc(void)347*2c2f96dcSApple OSS Distributions IOSimpleLockAlloc( void )
348*2c2f96dcSApple OSS Distributions {
349*2c2f96dcSApple OSS Distributions 	return lck_spin_alloc_init( IOLockGroup, LCK_ATTR_NULL);
350*2c2f96dcSApple OSS Distributions }
351*2c2f96dcSApple OSS Distributions 
352*2c2f96dcSApple OSS Distributions void
IOSimpleLockInit(IOSimpleLock * lock)353*2c2f96dcSApple OSS Distributions IOSimpleLockInit( IOSimpleLock * lock)
354*2c2f96dcSApple OSS Distributions {
355*2c2f96dcSApple OSS Distributions 	lck_spin_init( lock, IOLockGroup, LCK_ATTR_NULL);
356*2c2f96dcSApple OSS Distributions }
357*2c2f96dcSApple OSS Distributions 
358*2c2f96dcSApple OSS Distributions void
IOSimpleLockDestroy(IOSimpleLock * lock)359*2c2f96dcSApple OSS Distributions IOSimpleLockDestroy( IOSimpleLock * lock )
360*2c2f96dcSApple OSS Distributions {
361*2c2f96dcSApple OSS Distributions 	lck_spin_destroy(lock, IOLockGroup);
362*2c2f96dcSApple OSS Distributions }
363*2c2f96dcSApple OSS Distributions 
364*2c2f96dcSApple OSS Distributions void
IOSimpleLockFree(IOSimpleLock * lock)365*2c2f96dcSApple OSS Distributions IOSimpleLockFree( IOSimpleLock * lock )
366*2c2f96dcSApple OSS Distributions {
367*2c2f96dcSApple OSS Distributions 	lck_spin_free( lock, IOLockGroup);
368*2c2f96dcSApple OSS Distributions }
369*2c2f96dcSApple OSS Distributions 
370*2c2f96dcSApple OSS Distributions lck_spin_t *
IOSimpleLockGetMachLock(IOSimpleLock * lock)371*2c2f96dcSApple OSS Distributions IOSimpleLockGetMachLock( IOSimpleLock * lock)
372*2c2f96dcSApple OSS Distributions {
373*2c2f96dcSApple OSS Distributions 	return (lck_spin_t *)lock;
374*2c2f96dcSApple OSS Distributions }
375*2c2f96dcSApple OSS Distributions 
376*2c2f96dcSApple OSS Distributions #ifndef IOLOCKS_INLINE
377*2c2f96dcSApple OSS Distributions /*
378*2c2f96dcSApple OSS Distributions  * Lock assertions
379*2c2f96dcSApple OSS Distributions  */
380*2c2f96dcSApple OSS Distributions 
381*2c2f96dcSApple OSS Distributions void
IOLockAssert(IOLock * lock,IOLockAssertState type)382*2c2f96dcSApple OSS Distributions IOLockAssert(IOLock * lock, IOLockAssertState type)
383*2c2f96dcSApple OSS Distributions {
384*2c2f96dcSApple OSS Distributions 	LCK_MTX_ASSERT(lock, type);
385*2c2f96dcSApple OSS Distributions }
386*2c2f96dcSApple OSS Distributions 
387*2c2f96dcSApple OSS Distributions void
IORWLockAssert(IORWLock * lock,IORWLockAssertState type)388*2c2f96dcSApple OSS Distributions IORWLockAssert(IORWLock * lock, IORWLockAssertState type)
389*2c2f96dcSApple OSS Distributions {
390*2c2f96dcSApple OSS Distributions 	LCK_RW_ASSERT(lock, type);
391*2c2f96dcSApple OSS Distributions }
392*2c2f96dcSApple OSS Distributions 
393*2c2f96dcSApple OSS Distributions void
IOSimpleLockAssert(IOSimpleLock * lock,IOSimpleLockAssertState type)394*2c2f96dcSApple OSS Distributions IOSimpleLockAssert(IOSimpleLock *lock, IOSimpleLockAssertState type)
395*2c2f96dcSApple OSS Distributions {
396*2c2f96dcSApple OSS Distributions 	LCK_SPIN_ASSERT(l, type);
397*2c2f96dcSApple OSS Distributions }
398*2c2f96dcSApple OSS Distributions #endif /* !IOLOCKS_INLINE */
399*2c2f96dcSApple OSS Distributions } /* extern "C" */
400