]>
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 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
38 #include <IOKit/system.h>
40 #include <IOKit/IOReturn.h>
41 #include <IOKit/IOLib.h>
42 #include <IOKit/assert.h>
44 #include <IOKit/IOLocksPrivate.h>
47 #include <kern/locks.h>
49 void IOLockInitWithState( IOLock
* lock
, IOLockState state
)
51 if( state
== kIOLockStateLocked
)
55 IOLock
* IOLockAlloc( void )
57 return( lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
) );
60 void IOLockFree( IOLock
* lock
)
62 lck_mtx_free( lock
, IOLockGroup
);
65 lck_mtx_t
* IOLockGetMachLock( IOLock
* lock
)
67 return( (lck_mtx_t
*)lock
);
70 int IOLockSleep( IOLock
* lock
, void *event
, UInt32 interType
)
72 return (int) lck_mtx_sleep(lock
, LCK_SLEEP_DEFAULT
, (event_t
) event
, (wait_interrupt_t
) interType
);
75 int IOLockSleepDeadline( IOLock
* lock
, void *event
,
76 AbsoluteTime deadline
, UInt32 interType
)
78 return (int) lck_mtx_sleep_deadline(lock
, LCK_SLEEP_DEFAULT
, (event_t
) event
,
79 (wait_interrupt_t
) interType
, __OSAbsoluteTime(deadline
));
82 void IOLockWakeup(IOLock
* lock
, void *event
, bool oneThread
)
84 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
88 struct _IORecursiveLock
{
95 IORecursiveLock
* IORecursiveLockAllocWithLockGroup( lck_grp_t
* lockGroup
)
97 _IORecursiveLock
* lock
;
102 lock
= IONew( _IORecursiveLock
, 1 );
106 lock
->mutex
= lck_mtx_alloc_init( lockGroup
, LCK_ATTR_NULL
);
108 lock
->group
= lockGroup
;
112 IODelete( lock
, _IORecursiveLock
, 1 );
116 return( (IORecursiveLock
*) lock
);
120 IORecursiveLock
* IORecursiveLockAlloc( void )
122 return IORecursiveLockAllocWithLockGroup( IOLockGroup
);
125 void IORecursiveLockFree( IORecursiveLock
* _lock
)
127 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
129 lck_mtx_free( lock
->mutex
, lock
->group
);
130 IODelete( lock
, _IORecursiveLock
, 1 );
133 lck_mtx_t
* IORecursiveLockGetMachLock( IORecursiveLock
* lock
)
135 return( lock
->mutex
);
138 void IORecursiveLockLock( IORecursiveLock
* _lock
)
140 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
142 if( lock
->thread
== IOThreadSelf())
145 lck_mtx_lock( lock
->mutex
);
146 assert( lock
->thread
== 0 );
147 assert( lock
->count
== 0 );
148 lock
->thread
= IOThreadSelf();
153 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* _lock
)
155 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
157 if( lock
->thread
== IOThreadSelf()) {
161 if( lck_mtx_try_lock( lock
->mutex
)) {
162 assert( lock
->thread
== 0 );
163 assert( lock
->count
== 0 );
164 lock
->thread
= IOThreadSelf();
172 void IORecursiveLockUnlock( IORecursiveLock
* _lock
)
174 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
176 assert( lock
->thread
== IOThreadSelf() );
178 if( 0 == (--lock
->count
)) {
180 lck_mtx_unlock( lock
->mutex
);
184 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* _lock
)
186 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
188 return( lock
->thread
== IOThreadSelf());
191 int IORecursiveLockSleep(IORecursiveLock
*_lock
, void *event
, UInt32 interType
)
193 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
194 UInt32 count
= lock
->count
;
197 assert(lock
->thread
== IOThreadSelf());
198 assert(lock
->count
== 1 || interType
== THREAD_UNINT
);
202 res
= lck_mtx_sleep(lock
->mutex
, LCK_SLEEP_DEFAULT
, (event_t
) event
, (wait_interrupt_t
) interType
);
204 // Must re-establish the recursive lock no matter why we woke up
205 // otherwise we would potentially leave the return path corrupted.
206 assert(lock
->thread
== 0);
207 assert(lock
->count
== 0);
208 lock
->thread
= IOThreadSelf();
213 void IORecursiveLockWakeup(IORecursiveLock
*, void *event
, bool oneThread
)
215 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
219 * Complex (read/write) lock operations
222 IORWLock
* IORWLockAlloc( void )
224 return( lck_rw_alloc_init(IOLockGroup
, LCK_ATTR_NULL
) );
227 void IORWLockFree( IORWLock
* lock
)
229 lck_rw_free( lock
, IOLockGroup
);
232 lck_rw_t
* IORWLockGetMachLock( IORWLock
* lock
)
234 return( (lck_rw_t
*)lock
);
242 IOSimpleLock
* IOSimpleLockAlloc( void )
244 return( lck_spin_alloc_init( IOLockGroup
, LCK_ATTR_NULL
) );
247 void IOSimpleLockInit( IOSimpleLock
* lock
)
249 lck_spin_init( lock
, IOLockGroup
, LCK_ATTR_NULL
);
252 void IOSimpleLockFree( IOSimpleLock
* lock
)
254 lck_spin_free( lock
, IOLockGroup
);
257 lck_spin_t
* IOSimpleLockGetMachLock( IOSimpleLock
* lock
)
259 return( (lck_spin_t
*)lock
);