]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
30 #include <IOKit/system.h>
32 #include <IOKit/IOReturn.h>
33 #include <IOKit/IOLib.h>
34 #include <IOKit/assert.h>
37 #include <kern/simple_lock.h>
38 #include <machine/machine_routines.h>
40 IOLock
* IOLockAlloc( void )
42 return( mutex_alloc(ETAP_IO_AHA
) );
45 void IOLockFree( IOLock
* lock
)
50 void IOLockInitWithState( IOLock
* lock
, IOLockState state
)
52 if( state
== kIOLockStateLocked
)
56 struct _IORecursiveLock
{
62 IORecursiveLock
* IORecursiveLockAlloc( void )
64 _IORecursiveLock
* lock
;
66 lock
= IONew( _IORecursiveLock
, 1);
70 lock
->mutex
= mutex_alloc(ETAP_IO_AHA
);
75 IODelete( lock
, _IORecursiveLock
, 1);
79 return( (IORecursiveLock
*) lock
);
82 void IORecursiveLockFree( IORecursiveLock
* _lock
)
84 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
86 mutex_free( lock
->mutex
);
87 IODelete( lock
, _IORecursiveLock
, 1);
90 void IORecursiveLockLock( IORecursiveLock
* _lock
)
92 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
94 if( lock
->thread
== IOThreadSelf())
97 mutex_lock( lock
->mutex
);
98 assert( lock
->thread
== 0 );
99 assert( lock
->count
== 0 );
100 lock
->thread
= IOThreadSelf();
105 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* _lock
)
107 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
109 if( lock
->thread
== IOThreadSelf()) {
113 if( mutex_try( lock
->mutex
)) {
114 assert( lock
->thread
== 0 );
115 assert( lock
->count
== 0 );
116 lock
->thread
= IOThreadSelf();
124 void IORecursiveLockUnlock( IORecursiveLock
* _lock
)
126 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
128 assert( lock
->thread
== IOThreadSelf() );
130 if( 0 == (--lock
->count
)) {
132 mutex_unlock( lock
->mutex
);
136 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* _lock
)
138 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
140 return( lock
->thread
== IOThreadSelf());
143 int IORecursiveLockSleep(IORecursiveLock
*_lock
, void *event
, UInt32 interType
)
145 _IORecursiveLock
* lock
= (_IORecursiveLock
*)_lock
;
146 UInt32 count
= lock
->count
;
149 assert(lock
->thread
== IOThreadSelf());
150 assert(lock
->count
== 1 || interType
== THREAD_UNINT
);
154 res
= thread_sleep_mutex((event_t
) event
, lock
->mutex
, (int) interType
);
156 // Must re-establish the recursive lock no matter why we woke up
157 // otherwise we would potentially leave the return path corrupted.
158 assert(lock
->thread
== 0);
159 assert(lock
->count
== 0);
160 lock
->thread
= IOThreadSelf();
165 void IORecursiveLockWakeup(IORecursiveLock
*, void *event
, bool oneThread
)
167 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
171 * Complex (read/write) lock operations
174 IORWLock
* IORWLockAlloc( void )
178 lock
= lock_alloc( true, ETAP_IO_AHA
, ETAP_IO_AHA
);
183 void IORWLockFree( IORWLock
* lock
)
193 IOSimpleLock
* IOSimpleLockAlloc( void )
197 lock
= (IOSimpleLock
*) IOMalloc( sizeof(IOSimpleLock
));
199 IOSimpleLockInit( lock
);
204 void IOSimpleLockInit( IOSimpleLock
* lock
)
206 simple_lock_init( (simple_lock_t
) lock
, ETAP_IO_AHA
);
209 void IOSimpleLockFree( IOSimpleLock
* lock
)
211 IOFree( lock
, sizeof(IOSimpleLock
));