]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOLocks.cpp
edd23bea95f7d9db26f727e2147839a1e89418c5
2 * Copyright (c) 1998-2000 Apple Computer, 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>
45 #include <kern/locks.h>
47 void IOLockInitWithState( IOLock
* lock
, IOLockState state
)
49 if( state
== kIOLockStateLocked
)
53 IOLock
* IOLockAlloc( void )
55 return( lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
) );
58 void IOLockFree( IOLock
* lock
)
60 lck_mtx_free( lock
, IOLockGroup
);
63 lck_mtx_t
* IOLockGetMachLock( IOLock
* lock
)
65 return( (lck_mtx_t
*)lock
);
68 int IOLockSleep( IOLock
* lock
, void *event
, UInt32 interType
)
70 return (int) lck_mtx_sleep(lock
, LCK_SLEEP_DEFAULT
, (event_t
) event
, (wait_interrupt_t
) interType
);
73 int IOLockSleepDeadline( IOLock
* lock
, void *event
,
74 AbsoluteTime deadline
, UInt32 interType
)
76 return (int) lck_mtx_sleep_deadline(lock
, LCK_SLEEP_DEFAULT
, (event_t
) event
,
77 (wait_interrupt_t
) interType
, __OSAbsoluteTime(deadline
));
80 void IOLockWakeup(IOLock
* lock
, void *event
, bool oneThread
)
82 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
86 struct _IORecursiveLock
{
92 IORecursiveLock
* IORecursiveLockAlloc( void )
94 _IORecursiveLock
* lock
;
96 lock
= IONew( _IORecursiveLock
, 1);
100 lock
->mutex
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
105 IODelete( lock
, _IORecursiveLock
, 1);
109 return( (IORecursiveLock
*) lock
);
112 void IORecursiveLockFree( IORecursiveLock
* _lock
)
114 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
116 lck_mtx_free( lock
->mutex
, IOLockGroup
);
117 IODelete( lock
, _IORecursiveLock
, 1);
120 lck_mtx_t
* IORecursiveLockGetMachLock( IORecursiveLock
* lock
)
122 return( lock
->mutex
);
125 void IORecursiveLockLock( IORecursiveLock
* _lock
)
127 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
129 if( lock
->thread
== IOThreadSelf())
132 lck_mtx_lock( lock
->mutex
);
133 assert( lock
->thread
== 0 );
134 assert( lock
->count
== 0 );
135 lock
->thread
= IOThreadSelf();
140 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* _lock
)
142 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
144 if( lock
->thread
== IOThreadSelf()) {
148 if( lck_mtx_try_lock( lock
->mutex
)) {
149 assert( lock
->thread
== 0 );
150 assert( lock
->count
== 0 );
151 lock
->thread
= IOThreadSelf();
159 void IORecursiveLockUnlock( IORecursiveLock
* _lock
)
161 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
163 assert( lock
->thread
== IOThreadSelf() );
165 if( 0 == (--lock
->count
)) {
167 lck_mtx_unlock( lock
->mutex
);
171 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* _lock
)
173 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
175 return( lock
->thread
== IOThreadSelf());
178 int IORecursiveLockSleep(IORecursiveLock
*_lock
, void *event
, UInt32 interType
)
180 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
181 UInt32 count
= lock
->count
;
184 assert(lock
->thread
== IOThreadSelf());
185 assert(lock
->count
== 1 || interType
== THREAD_UNINT
);
189 res
= lck_mtx_sleep(lock
->mutex
, LCK_SLEEP_DEFAULT
, (event_t
) event
, (wait_interrupt_t
) interType
);
191 // Must re-establish the recursive lock no matter why we woke up
192 // otherwise we would potentially leave the return path corrupted.
193 assert(lock
->thread
== 0);
194 assert(lock
->count
== 0);
195 lock
->thread
= IOThreadSelf();
200 void IORecursiveLockWakeup(IORecursiveLock
*, void *event
, bool oneThread
)
202 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
206 * Complex (read/write) lock operations
209 IORWLock
* IORWLockAlloc( void )
211 return( lck_rw_alloc_init(IOLockGroup
, LCK_ATTR_NULL
) );
214 void IORWLockFree( IORWLock
* lock
)
216 lck_rw_free( lock
, IOLockGroup
);
219 lck_rw_t
* IORWLockGetMachLock( IORWLock
* lock
)
221 return( (lck_rw_t
*)lock
);
229 IOSimpleLock
* IOSimpleLockAlloc( void )
231 return( lck_spin_alloc_init( IOLockGroup
, LCK_ATTR_NULL
) );
234 void IOSimpleLockInit( IOSimpleLock
* lock
)
236 lck_spin_init( lock
, IOLockGroup
, LCK_ATTR_NULL
);
239 void IOSimpleLockFree( IOSimpleLock
* lock
)
241 lck_spin_free( lock
, IOLockGroup
);
244 lck_spin_t
* IOSimpleLockGetMachLock( IOSimpleLock
* lock
)
246 return( (lck_spin_t
*)lock
);