]>
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 void IOLockInitWithState( IOLock
* lock
, IOLockState state
)
42 if( state
== kIOLockStateLocked
)
46 IOLock
* IOLockAlloc( void )
48 return( lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
) );
51 void IOLockFree( IOLock
* lock
)
53 lck_mtx_free( lock
, IOLockGroup
);
56 lck_mtx_t
* IOLockGetMachLock( IOLock
* lock
)
58 return( (lck_mtx_t
*)lock
);
61 int IOLockSleep( IOLock
* lock
, void *event
, UInt32 interType
)
63 return (int) lck_mtx_sleep(lock
, LCK_SLEEP_DEFAULT
, (event_t
) event
, (wait_interrupt_t
) interType
);
66 int IOLockSleepDeadline( IOLock
* lock
, void *event
,
67 AbsoluteTime deadline
, UInt32 interType
)
69 return (int) lck_mtx_sleep_deadline(lock
, LCK_SLEEP_DEFAULT
, (event_t
) event
,
70 (wait_interrupt_t
) interType
, __OSAbsoluteTime(deadline
));
73 void IOLockWakeup(IOLock
* lock
, void *event
, bool oneThread
)
75 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
79 struct _IORecursiveLock
{
86 IORecursiveLock
* IORecursiveLockAllocWithLockGroup( lck_grp_t
* lockGroup
)
88 _IORecursiveLock
* lock
;
93 lock
= IONew( _IORecursiveLock
, 1 );
97 lock
->mutex
= lck_mtx_alloc_init( lockGroup
, LCK_ATTR_NULL
);
99 lock
->group
= lockGroup
;
103 IODelete( lock
, _IORecursiveLock
, 1 );
107 return( (IORecursiveLock
*) lock
);
111 IORecursiveLock
* IORecursiveLockAlloc( void )
113 return IORecursiveLockAllocWithLockGroup( IOLockGroup
);
116 void IORecursiveLockFree( IORecursiveLock
* _lock
)
118 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
120 lck_mtx_free( lock
->mutex
, lock
->group
);
121 IODelete( lock
, _IORecursiveLock
, 1 );
124 lck_mtx_t
* IORecursiveLockGetMachLock( IORecursiveLock
* lock
)
126 return( lock
->mutex
);
129 void IORecursiveLockLock( IORecursiveLock
* _lock
)
131 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
133 if( lock
->thread
== IOThreadSelf())
136 lck_mtx_lock( lock
->mutex
);
137 assert( lock
->thread
== 0 );
138 assert( lock
->count
== 0 );
139 lock
->thread
= IOThreadSelf();
144 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* _lock
)
146 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
148 if( lock
->thread
== IOThreadSelf()) {
152 if( lck_mtx_try_lock( lock
->mutex
)) {
153 assert( lock
->thread
== 0 );
154 assert( lock
->count
== 0 );
155 lock
->thread
= IOThreadSelf();
163 void IORecursiveLockUnlock( IORecursiveLock
* _lock
)
165 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
167 assert( lock
->thread
== IOThreadSelf() );
169 if( 0 == (--lock
->count
)) {
171 lck_mtx_unlock( lock
->mutex
);
175 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* _lock
)
177 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
179 return( lock
->thread
== IOThreadSelf());
182 int IORecursiveLockSleep(IORecursiveLock
*_lock
, void *event
, UInt32 interType
)
184 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
185 UInt32 count
= lock
->count
;
188 assert(lock
->thread
== IOThreadSelf());
192 res
= lck_mtx_sleep(lock
->mutex
, LCK_SLEEP_DEFAULT
, (event_t
) event
, (wait_interrupt_t
) interType
);
194 // Must re-establish the recursive lock no matter why we woke up
195 // otherwise we would potentially leave the return path corrupted.
196 assert(lock
->thread
== 0);
197 assert(lock
->count
== 0);
198 lock
->thread
= IOThreadSelf();
203 int IORecursiveLockSleepDeadline( IORecursiveLock
* _lock
, void *event
,
204 AbsoluteTime deadline
, UInt32 interType
)
206 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
207 UInt32 count
= lock
->count
;
210 assert(lock
->thread
== IOThreadSelf());
214 res
= lck_mtx_sleep_deadline(lock
->mutex
, LCK_SLEEP_DEFAULT
, (event_t
) event
,
215 (wait_interrupt_t
) interType
, __OSAbsoluteTime(deadline
));
217 // Must re-establish the recursive lock no matter why we woke up
218 // otherwise we would potentially leave the return path corrupted.
219 assert(lock
->thread
== 0);
220 assert(lock
->count
== 0);
221 lock
->thread
= IOThreadSelf();
226 void IORecursiveLockWakeup(IORecursiveLock
*, void *event
, bool oneThread
)
228 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
232 * Complex (read/write) lock operations
235 IORWLock
* IORWLockAlloc( void )
237 return( lck_rw_alloc_init(IOLockGroup
, LCK_ATTR_NULL
) );
240 void IORWLockFree( IORWLock
* lock
)
242 lck_rw_free( lock
, IOLockGroup
);
245 lck_rw_t
* IORWLockGetMachLock( IORWLock
* lock
)
247 return( (lck_rw_t
*)lock
);
255 IOSimpleLock
* IOSimpleLockAlloc( void )
257 return( lck_spin_alloc_init( IOLockGroup
, LCK_ATTR_NULL
) );
260 void IOSimpleLockInit( IOSimpleLock
* lock
)
262 lck_spin_init( lock
, IOLockGroup
, LCK_ATTR_NULL
);
265 void IOSimpleLockFree( IOSimpleLock
* lock
)
267 lck_spin_free( lock
, IOLockGroup
);
270 lck_spin_t
* IOSimpleLockGetMachLock( IOSimpleLock
* lock
)
272 return( (lck_spin_t
*)lock
);