]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOLocks.cpp
9b1d177407084357bfedb46006bf28262348cd31
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@
31 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
40 #include <IOKit/system.h>
42 #include <IOKit/IOReturn.h>
43 #include <IOKit/IOLib.h>
44 #include <IOKit/assert.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
{
94 IORecursiveLock
* IORecursiveLockAlloc( void )
96 _IORecursiveLock
* lock
;
98 lock
= IONew( _IORecursiveLock
, 1);
102 lock
->mutex
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
107 IODelete( lock
, _IORecursiveLock
, 1);
111 return( (IORecursiveLock
*) lock
);
114 void IORecursiveLockFree( IORecursiveLock
* _lock
)
116 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
118 lck_mtx_free( lock
->mutex
, IOLockGroup
);
119 IODelete( lock
, _IORecursiveLock
, 1);
122 lck_mtx_t
* IORecursiveLockGetMachLock( IORecursiveLock
* lock
)
124 return( lock
->mutex
);
127 void IORecursiveLockLock( IORecursiveLock
* _lock
)
129 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
131 if( lock
->thread
== IOThreadSelf())
134 lck_mtx_lock( lock
->mutex
);
135 assert( lock
->thread
== 0 );
136 assert( lock
->count
== 0 );
137 lock
->thread
= IOThreadSelf();
142 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* _lock
)
144 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
146 if( lock
->thread
== IOThreadSelf()) {
150 if( lck_mtx_try_lock( lock
->mutex
)) {
151 assert( lock
->thread
== 0 );
152 assert( lock
->count
== 0 );
153 lock
->thread
= IOThreadSelf();
161 void IORecursiveLockUnlock( IORecursiveLock
* _lock
)
163 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
165 assert( lock
->thread
== IOThreadSelf() );
167 if( 0 == (--lock
->count
)) {
169 lck_mtx_unlock( lock
->mutex
);
173 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* _lock
)
175 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
177 return( lock
->thread
== IOThreadSelf());
180 int IORecursiveLockSleep(IORecursiveLock
*_lock
, void *event
, UInt32 interType
)
182 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
183 UInt32 count
= lock
->count
;
186 assert(lock
->thread
== IOThreadSelf());
187 assert(lock
->count
== 1 || interType
== THREAD_UNINT
);
191 res
= lck_mtx_sleep(lock
->mutex
, LCK_SLEEP_DEFAULT
, (event_t
) event
, (wait_interrupt_t
) interType
);
193 // Must re-establish the recursive lock no matter why we woke up
194 // otherwise we would potentially leave the return path corrupted.
195 assert(lock
->thread
== 0);
196 assert(lock
->count
== 0);
197 lock
->thread
= IOThreadSelf();
202 void IORecursiveLockWakeup(IORecursiveLock
*, void *event
, bool oneThread
)
204 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
208 * Complex (read/write) lock operations
211 IORWLock
* IORWLockAlloc( void )
213 return( lck_rw_alloc_init(IOLockGroup
, LCK_ATTR_NULL
) );
216 void IORWLockFree( IORWLock
* lock
)
218 lck_rw_free( lock
, IOLockGroup
);
221 lck_rw_t
* IORWLockGetMachLock( IORWLock
* lock
)
223 return( (lck_rw_t
*)lock
);
231 IOSimpleLock
* IOSimpleLockAlloc( void )
233 return( lck_spin_alloc_init( IOLockGroup
, LCK_ATTR_NULL
) );
236 void IOSimpleLockInit( IOSimpleLock
* lock
)
238 lck_spin_init( lock
, IOLockGroup
, LCK_ATTR_NULL
);
241 void IOSimpleLockFree( IOSimpleLock
* lock
)
243 lck_spin_free( lock
, IOLockGroup
);
246 lck_spin_t
* IOSimpleLockGetMachLock( IOSimpleLock
* lock
)
248 return( (lck_spin_t
*)lock
);