]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOLocks.cpp
xnu-792.10.96.tar.gz
[apple/xnu.git] / iokit / Kernel / IOLocks.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
24 *
25 * HISTORY
26 *
27 */
28
29
91447636
A
30#define IOLOCKS_CPP 1
31
1c79356b
A
32#include <IOKit/system.h>
33
34#include <IOKit/IOReturn.h>
35#include <IOKit/IOLib.h>
36#include <IOKit/assert.h>
37
38extern "C" {
91447636
A
39#include <kern/locks.h>
40
41void IOLockInitWithState( IOLock * lock, IOLockState state)
42{
43 if( state == kIOLockStateLocked)
44 lck_mtx_lock( lock);
45}
1c79356b
A
46
47IOLock * IOLockAlloc( void )
48{
91447636 49 return( lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL) );
1c79356b
A
50}
51
52void IOLockFree( IOLock * lock)
53{
91447636 54 lck_mtx_free( lock, IOLockGroup);
1c79356b
A
55}
56
91447636 57lck_mtx_t * IOLockGetMachLock( IOLock * lock)
1c79356b 58{
91447636 59 return( (lck_mtx_t *)lock);
1c79356b
A
60}
61
91447636
A
62int IOLockSleep( IOLock * lock, void *event, UInt32 interType)
63{
64 return (int) lck_mtx_sleep(lock, LCK_SLEEP_DEFAULT, (event_t) event, (wait_interrupt_t) interType);
65}
66
67int IOLockSleepDeadline( IOLock * lock, void *event,
68 AbsoluteTime deadline, UInt32 interType)
69{
70 return (int) lck_mtx_sleep_deadline(lock, LCK_SLEEP_DEFAULT, (event_t) event,
71 (wait_interrupt_t) interType, __OSAbsoluteTime(deadline));
72}
73
74void IOLockWakeup(IOLock * lock, void *event, bool oneThread)
75{
76 thread_wakeup_prim((event_t) event, oneThread, THREAD_AWAKENED);
77}
78
79
1c79356b 80struct _IORecursiveLock {
91447636 81 lck_mtx_t *mutex;
1c79356b
A
82 thread_t thread;
83 UInt32 count;
84};
85
86IORecursiveLock * IORecursiveLockAlloc( void )
87{
88 _IORecursiveLock * lock;
89
90 lock = IONew( _IORecursiveLock, 1);
91 if( !lock)
92 return( 0 );
93
91447636 94 lock->mutex = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL);
1c79356b
A
95 if( lock->mutex) {
96 lock->thread = 0;
97 lock->count = 0;
98 } else {
99 IODelete( lock, _IORecursiveLock, 1);
100 lock = 0;
101 }
102
103 return( (IORecursiveLock *) lock );
104}
105
106void IORecursiveLockFree( IORecursiveLock * _lock )
107{
108 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
109
91447636 110 lck_mtx_free( lock->mutex , IOLockGroup);
1c79356b
A
111 IODelete( lock, _IORecursiveLock, 1);
112}
113
91447636
A
114lck_mtx_t * IORecursiveLockGetMachLock( IORecursiveLock * lock)
115{
116 return( lock->mutex);
117}
118
1c79356b
A
119void IORecursiveLockLock( IORecursiveLock * _lock)
120{
121 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
122
123 if( lock->thread == IOThreadSelf())
124 lock->count++;
125 else {
91447636 126 lck_mtx_lock( lock->mutex );
1c79356b
A
127 assert( lock->thread == 0 );
128 assert( lock->count == 0 );
129 lock->thread = IOThreadSelf();
130 lock->count = 1;
131 }
132}
133
134boolean_t IORecursiveLockTryLock( IORecursiveLock * _lock)
135{
136 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
137
138 if( lock->thread == IOThreadSelf()) {
139 lock->count++;
140 return( true );
141 } else {
91447636 142 if( lck_mtx_try_lock( lock->mutex )) {
1c79356b
A
143 assert( lock->thread == 0 );
144 assert( lock->count == 0 );
145 lock->thread = IOThreadSelf();
146 lock->count = 1;
147 return( true );
148 }
149 }
150 return( false );
151}
152
153void IORecursiveLockUnlock( IORecursiveLock * _lock)
154{
155 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
156
157 assert( lock->thread == IOThreadSelf() );
158
159 if( 0 == (--lock->count)) {
160 lock->thread = 0;
91447636 161 lck_mtx_unlock( lock->mutex );
1c79356b
A
162 }
163}
164
165boolean_t IORecursiveLockHaveLock( const IORecursiveLock * _lock)
166{
167 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
168
169 return( lock->thread == IOThreadSelf());
170}
171
172int IORecursiveLockSleep(IORecursiveLock *_lock, void *event, UInt32 interType)
173{
174 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
175 UInt32 count = lock->count;
176 int res;
177
178 assert(lock->thread == IOThreadSelf());
179 assert(lock->count == 1 || interType == THREAD_UNINT);
180
1c79356b
A
181 lock->count = 0;
182 lock->thread = 0;
91447636 183 res = lck_mtx_sleep(lock->mutex, LCK_SLEEP_DEFAULT, (event_t) event, (wait_interrupt_t) interType);
1c79356b 184
7b1edb79
A
185 // Must re-establish the recursive lock no matter why we woke up
186 // otherwise we would potentially leave the return path corrupted.
7b1edb79
A
187 assert(lock->thread == 0);
188 assert(lock->count == 0);
189 lock->thread = IOThreadSelf();
190 lock->count = count;
1c79356b
A
191 return res;
192}
193
194void IORecursiveLockWakeup(IORecursiveLock *, void *event, bool oneThread)
195{
196 thread_wakeup_prim((event_t) event, oneThread, THREAD_AWAKENED);
197}
198
199/*
200 * Complex (read/write) lock operations
201 */
202
203IORWLock * IORWLockAlloc( void )
204{
91447636 205 return( lck_rw_alloc_init(IOLockGroup, LCK_ATTR_NULL) );
1c79356b
A
206}
207
208void IORWLockFree( IORWLock * lock)
209{
91447636
A
210 lck_rw_free( lock, IOLockGroup);
211}
212
213lck_rw_t * IORWLockGetMachLock( IORWLock * lock)
214{
215 return( (lck_rw_t *)lock);
1c79356b
A
216}
217
218
219/*
220 * Spin locks
221 */
222
223IOSimpleLock * IOSimpleLockAlloc( void )
224{
91447636 225 return( lck_spin_alloc_init( IOLockGroup, LCK_ATTR_NULL) );
1c79356b
A
226}
227
228void IOSimpleLockInit( IOSimpleLock * lock)
229{
91447636 230 lck_spin_init( lock, IOLockGroup, LCK_ATTR_NULL);
1c79356b
A
231}
232
233void IOSimpleLockFree( IOSimpleLock * lock )
234{
91447636
A
235 lck_spin_free( lock, IOLockGroup);
236}
237
238lck_spin_t * IOSimpleLockGetMachLock( IOSimpleLock * lock)
239{
240 return( (lck_spin_t *)lock);
1c79356b
A
241}
242
243} /* extern "C" */
244
245