]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOLocks.cpp
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
33 #include <IOKit/system.h>
35 #include <IOKit/IOReturn.h>
36 #include <IOKit/IOLib.h>
37 #include <IOKit/assert.h>
40 #include <kern/simple_lock.h>
41 #include <machine/machine_routines.h>
43 IOLock
* IOLockAlloc( void )
45 return( mutex_alloc(ETAP_IO_AHA
) );
48 void IOLockFree( IOLock
* lock
)
53 void IOLockInitWithState( IOLock
* lock
, IOLockState state
)
55 if( state
== kIOLockStateLocked
)
59 struct _IORecursiveLock
{
65 IORecursiveLock
* IORecursiveLockAlloc( void )
67 _IORecursiveLock
* lock
;
69 lock
= IONew( _IORecursiveLock
, 1);
73 lock
->mutex
= mutex_alloc(ETAP_IO_AHA
);
78 IODelete( lock
, _IORecursiveLock
, 1);
82 return( (IORecursiveLock
*) lock
);
85 void IORecursiveLockFree( IORecursiveLock
* _lock
)
87 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
89 mutex_free( lock
->mutex
);
90 IODelete( lock
, _IORecursiveLock
, 1);
93 void IORecursiveLockLock( IORecursiveLock
* _lock
)
95 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
97 if( lock
->thread
== IOThreadSelf())
100 mutex_lock( lock
->mutex
);
101 assert( lock
->thread
== 0 );
102 assert( lock
->count
== 0 );
103 lock
->thread
= IOThreadSelf();
108 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* _lock
)
110 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
112 if( lock
->thread
== IOThreadSelf()) {
116 if( mutex_try( lock
->mutex
)) {
117 assert( lock
->thread
== 0 );
118 assert( lock
->count
== 0 );
119 lock
->thread
= IOThreadSelf();
127 void IORecursiveLockUnlock( IORecursiveLock
* _lock
)
129 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
131 assert( lock
->thread
== IOThreadSelf() );
133 if( 0 == (--lock
->count
)) {
135 mutex_unlock( lock
->mutex
);
139 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* _lock
)
141 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
143 return( lock
->thread
== IOThreadSelf());
146 int IORecursiveLockSleep(IORecursiveLock
*_lock
, void *event
, UInt32 interType
)
148 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
149 UInt32 count
= lock
->count
;
152 assert(lock
->thread
== IOThreadSelf());
153 assert(lock
->count
== 1 || interType
== THREAD_UNINT
);
157 res
= thread_sleep_mutex((event_t
) event
, lock
->mutex
, (int) interType
);
159 // Must re-establish the recursive lock no matter why we woke up
160 // otherwise we would potentially leave the return path corrupted.
161 assert(lock
->thread
== 0);
162 assert(lock
->count
== 0);
163 lock
->thread
= IOThreadSelf();
168 void IORecursiveLockWakeup(IORecursiveLock
*, void *event
, bool oneThread
)
170 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
174 * Complex (read/write) lock operations
177 IORWLock
* IORWLockAlloc( void )
181 lock
= lock_alloc( true, ETAP_IO_AHA
, ETAP_IO_AHA
);
186 void IORWLockFree( IORWLock
* lock
)
196 IOSimpleLock
* IOSimpleLockAlloc( void )
200 lock
= (IOSimpleLock
*) IOMalloc( sizeof(IOSimpleLock
));
202 IOSimpleLockInit( lock
);
207 void IOSimpleLockInit( IOSimpleLock
* lock
)
209 simple_lock_init( (simple_lock_t
) lock
, ETAP_IO_AHA
);
212 void IOSimpleLockFree( IOSimpleLock
* lock
)
214 IOFree( lock
, sizeof(IOSimpleLock
));