]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOLocks.cpp
2 * Copyright (c) 1998-2007 Apple 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@
29 #include <IOKit/system.h>
31 #include <IOKit/IOReturn.h>
32 #include <IOKit/IOLib.h>
33 #include <IOKit/assert.h>
35 #include <IOKit/IOLocksPrivate.h>
38 #include <kern/locks.h>
40 #if defined(__x86_64__)
41 /* Synthetic event if none is specified, for backwards compatibility only. */
42 static bool IOLockSleep_NO_EVENT
__attribute__((used
)) = 0;
45 void IOLockInitWithState( IOLock
* lock
, IOLockState state
)
47 if( state
== kIOLockStateLocked
)
51 IOLock
* IOLockAlloc( void )
53 return( lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
) );
56 void IOLockFree( IOLock
* lock
)
58 lck_mtx_free( lock
, IOLockGroup
);
61 lck_mtx_t
* IOLockGetMachLock( IOLock
* lock
)
63 return( (lck_mtx_t
*)lock
);
66 int IOLockSleep( IOLock
* lock
, void *event
, UInt32 interType
)
68 return (int) lck_mtx_sleep(lock
, LCK_SLEEP_PROMOTED_PRI
, (event_t
) event
, (wait_interrupt_t
) interType
);
71 int IOLockSleepDeadline( IOLock
* lock
, void *event
,
72 AbsoluteTime deadline
, UInt32 interType
)
74 return (int) lck_mtx_sleep_deadline(lock
, LCK_SLEEP_PROMOTED_PRI
, (event_t
) event
,
75 (wait_interrupt_t
) interType
, __OSAbsoluteTime(deadline
));
78 void IOLockWakeup(IOLock
* lock
, void *event
, bool oneThread
)
80 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
83 #if defined(__x86_64__)
85 * For backwards compatibility, kexts built against pre-Darwin 14 headers will bind at runtime to this function,
86 * which supports a NULL event,
88 int IOLockSleep_legacy_x86_64( IOLock
* lock
, void *event
, UInt32 interType
) __asm("_IOLockSleep");
89 int IOLockSleepDeadline_legacy_x86_64( IOLock
* lock
, void *event
,
90 AbsoluteTime deadline
, UInt32 interType
) __asm("_IOLockSleepDeadline");
91 void IOLockWakeup_legacy_x86_64(IOLock
* lock
, void *event
, bool oneThread
) __asm("_IOLockWakeup");
93 int IOLockSleep_legacy_x86_64( IOLock
* lock
, void *event
, UInt32 interType
)
96 event
= (void *)&IOLockSleep_NO_EVENT
;
98 return IOLockSleep(lock
, event
, interType
);
101 int IOLockSleepDeadline_legacy_x86_64( IOLock
* lock
, void *event
,
102 AbsoluteTime deadline
, UInt32 interType
)
105 event
= (void *)&IOLockSleep_NO_EVENT
;
107 return IOLockSleepDeadline(lock
, event
, deadline
, interType
);
110 void IOLockWakeup_legacy_x86_64(IOLock
* lock
, void *event
, bool oneThread
)
113 event
= (void *)&IOLockSleep_NO_EVENT
;
115 IOLockWakeup(lock
, event
, oneThread
);
117 #endif /* defined(__x86_64__) */
120 struct _IORecursiveLock
{
127 IORecursiveLock
* IORecursiveLockAllocWithLockGroup( lck_grp_t
* lockGroup
)
129 _IORecursiveLock
* lock
;
134 lock
= IONew( _IORecursiveLock
, 1 );
138 lck_mtx_init( &lock
->mutex
, lockGroup
, LCK_ATTR_NULL
);
139 lock
->group
= lockGroup
;
143 return( (IORecursiveLock
*) lock
);
147 IORecursiveLock
* IORecursiveLockAlloc( void )
149 return IORecursiveLockAllocWithLockGroup( IOLockGroup
);
152 void IORecursiveLockFree( IORecursiveLock
* _lock
)
154 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
156 lck_mtx_destroy(&lock
->mutex
, lock
->group
);
157 IODelete( lock
, _IORecursiveLock
, 1 );
160 lck_mtx_t
* IORecursiveLockGetMachLock( IORecursiveLock
* lock
)
162 return( &lock
->mutex
);
165 void IORecursiveLockLock( IORecursiveLock
* _lock
)
167 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
169 if( lock
->thread
== IOThreadSelf())
172 lck_mtx_lock( &lock
->mutex
);
173 assert( lock
->thread
== 0 );
174 assert( lock
->count
== 0 );
175 lock
->thread
= IOThreadSelf();
180 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* _lock
)
182 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
184 if( lock
->thread
== IOThreadSelf()) {
188 if( lck_mtx_try_lock( &lock
->mutex
)) {
189 assert( lock
->thread
== 0 );
190 assert( lock
->count
== 0 );
191 lock
->thread
= IOThreadSelf();
199 void IORecursiveLockUnlock( IORecursiveLock
* _lock
)
201 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
203 assert( lock
->thread
== IOThreadSelf() );
205 if( 0 == (--lock
->count
)) {
207 lck_mtx_unlock( &lock
->mutex
);
211 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* _lock
)
213 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
215 return( lock
->thread
== IOThreadSelf());
218 int IORecursiveLockSleep(IORecursiveLock
*_lock
, void *event
, UInt32 interType
)
220 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
221 UInt32 count
= lock
->count
;
224 assert(lock
->thread
== IOThreadSelf());
228 res
= lck_mtx_sleep(&lock
->mutex
, LCK_SLEEP_PROMOTED_PRI
, (event_t
) event
, (wait_interrupt_t
) interType
);
230 // Must re-establish the recursive lock no matter why we woke up
231 // otherwise we would potentially leave the return path corrupted.
232 assert(lock
->thread
== 0);
233 assert(lock
->count
== 0);
234 lock
->thread
= IOThreadSelf();
239 int IORecursiveLockSleepDeadline( IORecursiveLock
* _lock
, void *event
,
240 AbsoluteTime deadline
, UInt32 interType
)
242 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
243 UInt32 count
= lock
->count
;
246 assert(lock
->thread
== IOThreadSelf());
250 res
= lck_mtx_sleep_deadline(&lock
->mutex
, LCK_SLEEP_PROMOTED_PRI
, (event_t
) event
,
251 (wait_interrupt_t
) interType
, __OSAbsoluteTime(deadline
));
253 // Must re-establish the recursive lock no matter why we woke up
254 // otherwise we would potentially leave the return path corrupted.
255 assert(lock
->thread
== 0);
256 assert(lock
->count
== 0);
257 lock
->thread
= IOThreadSelf();
262 void IORecursiveLockWakeup(IORecursiveLock
*, void *event
, bool oneThread
)
264 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
268 * Complex (read/write) lock operations
271 IORWLock
* IORWLockAlloc( void )
273 return( lck_rw_alloc_init(IOLockGroup
, LCK_ATTR_NULL
) );
276 void IORWLockFree( IORWLock
* lock
)
278 lck_rw_free( lock
, IOLockGroup
);
281 lck_rw_t
* IORWLockGetMachLock( IORWLock
* lock
)
283 return( (lck_rw_t
*)lock
);
291 IOSimpleLock
* IOSimpleLockAlloc( void )
293 return( lck_spin_alloc_init( IOLockGroup
, LCK_ATTR_NULL
) );
296 void IOSimpleLockInit( IOSimpleLock
* lock
)
298 lck_spin_init( lock
, IOLockGroup
, LCK_ATTR_NULL
);
301 void IOSimpleLockFree( IOSimpleLock
* lock
)
303 lck_spin_free( lock
, IOLockGroup
);
306 lck_spin_t
* IOSimpleLockGetMachLock( IOSimpleLock
* lock
)
308 return( (lck_spin_t
*)lock
);