]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOLocks.cpp
xnu-3789.60.24.tar.gz
[apple/xnu.git] / iokit / Kernel / IOLocks.cpp
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 1998-2007 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
91447636 28
1c79356b
A
29#include <IOKit/system.h>
30
31#include <IOKit/IOReturn.h>
32#include <IOKit/IOLib.h>
33#include <IOKit/assert.h>
34
2d21ac55
A
35#include <IOKit/IOLocksPrivate.h>
36
1c79356b 37extern "C" {
91447636
A
38#include <kern/locks.h>
39
fe8ab488
A
40#if defined(__x86_64__)
41/* Synthetic event if none is specified, for backwards compatibility only. */
42static bool IOLockSleep_NO_EVENT __attribute__((used)) = 0;
43#endif
44
91447636
A
45void IOLockInitWithState( IOLock * lock, IOLockState state)
46{
47 if( state == kIOLockStateLocked)
48 lck_mtx_lock( lock);
49}
1c79356b
A
50
51IOLock * IOLockAlloc( void )
52{
91447636 53 return( lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL) );
1c79356b
A
54}
55
56void IOLockFree( IOLock * lock)
57{
91447636 58 lck_mtx_free( lock, IOLockGroup);
1c79356b
A
59}
60
91447636 61lck_mtx_t * IOLockGetMachLock( IOLock * lock)
1c79356b 62{
91447636 63 return( (lck_mtx_t *)lock);
1c79356b
A
64}
65
91447636
A
66int IOLockSleep( IOLock * lock, void *event, UInt32 interType)
67{
fe8ab488 68 return (int) lck_mtx_sleep(lock, LCK_SLEEP_PROMOTED_PRI, (event_t) event, (wait_interrupt_t) interType);
91447636
A
69}
70
71int IOLockSleepDeadline( IOLock * lock, void *event,
72 AbsoluteTime deadline, UInt32 interType)
73{
fe8ab488 74 return (int) lck_mtx_sleep_deadline(lock, LCK_SLEEP_PROMOTED_PRI, (event_t) event,
91447636
A
75 (wait_interrupt_t) interType, __OSAbsoluteTime(deadline));
76}
77
78void IOLockWakeup(IOLock * lock, void *event, bool oneThread)
79{
80 thread_wakeup_prim((event_t) event, oneThread, THREAD_AWAKENED);
81}
82
fe8ab488
A
83#if defined(__x86_64__)
84/*
85 * For backwards compatibility, kexts built against pre-Darwin 14 headers will bind at runtime to this function,
86 * which supports a NULL event,
87 */
88int IOLockSleep_legacy_x86_64( IOLock * lock, void *event, UInt32 interType) __asm("_IOLockSleep");
89int IOLockSleepDeadline_legacy_x86_64( IOLock * lock, void *event,
90 AbsoluteTime deadline, UInt32 interType) __asm("_IOLockSleepDeadline");
91void IOLockWakeup_legacy_x86_64(IOLock * lock, void *event, bool oneThread) __asm("_IOLockWakeup");
92
93int IOLockSleep_legacy_x86_64( IOLock * lock, void *event, UInt32 interType)
94{
95 if (event == NULL)
96 event = (void *)&IOLockSleep_NO_EVENT;
97
98 return IOLockSleep(lock, event, interType);
99}
100
101int IOLockSleepDeadline_legacy_x86_64( IOLock * lock, void *event,
102 AbsoluteTime deadline, UInt32 interType)
103{
104 if (event == NULL)
105 event = (void *)&IOLockSleep_NO_EVENT;
106
107 return IOLockSleepDeadline(lock, event, deadline, interType);
108}
109
110void IOLockWakeup_legacy_x86_64(IOLock * lock, void *event, bool oneThread)
111{
112 if (event == NULL)
113 event = (void *)&IOLockSleep_NO_EVENT;
114
115 IOLockWakeup(lock, event, oneThread);
116}
117#endif /* defined(__x86_64__) */
118
91447636 119
1c79356b 120struct _IORecursiveLock {
39037602 121 lck_mtx_t mutex;
2d21ac55
A
122 lck_grp_t *group;
123 thread_t thread;
124 UInt32 count;
1c79356b
A
125};
126
2d21ac55 127IORecursiveLock * IORecursiveLockAllocWithLockGroup( lck_grp_t * lockGroup )
1c79356b
A
128{
129 _IORecursiveLock * lock;
130
2d21ac55 131 if( lockGroup == 0 )
1c79356b
A
132 return( 0 );
133
2d21ac55
A
134 lock = IONew( _IORecursiveLock, 1 );
135 if( !lock )
136 return( 0 );
137
39037602
A
138 lck_mtx_init( &lock->mutex, lockGroup, LCK_ATTR_NULL );
139 lock->group = lockGroup;
140 lock->thread = 0;
141 lock->count = 0;
1c79356b
A
142
143 return( (IORecursiveLock *) lock );
144}
145
2d21ac55
A
146
147IORecursiveLock * IORecursiveLockAlloc( void )
148{
149 return IORecursiveLockAllocWithLockGroup( IOLockGroup );
150}
151
1c79356b
A
152void IORecursiveLockFree( IORecursiveLock * _lock )
153{
154 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
2d21ac55 155
39037602 156 lck_mtx_destroy(&lock->mutex, lock->group);
2d21ac55 157 IODelete( lock, _IORecursiveLock, 1 );
1c79356b
A
158}
159
2d21ac55 160lck_mtx_t * IORecursiveLockGetMachLock( IORecursiveLock * lock )
91447636 161{
39037602 162 return( &lock->mutex );
91447636
A
163}
164
1c79356b
A
165void IORecursiveLockLock( IORecursiveLock * _lock)
166{
167 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
168
169 if( lock->thread == IOThreadSelf())
170 lock->count++;
171 else {
39037602 172 lck_mtx_lock( &lock->mutex );
1c79356b
A
173 assert( lock->thread == 0 );
174 assert( lock->count == 0 );
175 lock->thread = IOThreadSelf();
176 lock->count = 1;
177 }
178}
179
180boolean_t IORecursiveLockTryLock( IORecursiveLock * _lock)
181{
182 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
183
184 if( lock->thread == IOThreadSelf()) {
185 lock->count++;
186 return( true );
187 } else {
39037602 188 if( lck_mtx_try_lock( &lock->mutex )) {
1c79356b
A
189 assert( lock->thread == 0 );
190 assert( lock->count == 0 );
191 lock->thread = IOThreadSelf();
192 lock->count = 1;
193 return( true );
194 }
195 }
196 return( false );
197}
198
199void IORecursiveLockUnlock( IORecursiveLock * _lock)
200{
201 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
202
203 assert( lock->thread == IOThreadSelf() );
204
205 if( 0 == (--lock->count)) {
206 lock->thread = 0;
39037602 207 lck_mtx_unlock( &lock->mutex );
1c79356b
A
208 }
209}
210
211boolean_t IORecursiveLockHaveLock( const IORecursiveLock * _lock)
212{
213 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
214
215 return( lock->thread == IOThreadSelf());
216}
217
218int IORecursiveLockSleep(IORecursiveLock *_lock, void *event, UInt32 interType)
219{
220 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
221 UInt32 count = lock->count;
222 int res;
223
224 assert(lock->thread == IOThreadSelf());
1c79356b 225
1c79356b
A
226 lock->count = 0;
227 lock->thread = 0;
39037602 228 res = lck_mtx_sleep(&lock->mutex, LCK_SLEEP_PROMOTED_PRI, (event_t) event, (wait_interrupt_t) interType);
1c79356b 229
7b1edb79
A
230 // Must re-establish the recursive lock no matter why we woke up
231 // otherwise we would potentially leave the return path corrupted.
7b1edb79 232 assert(lock->thread == 0);
b0d623f7
A
233 assert(lock->count == 0);
234 lock->thread = IOThreadSelf();
235 lock->count = count;
236 return res;
237}
238
239int IORecursiveLockSleepDeadline( IORecursiveLock * _lock, void *event,
240 AbsoluteTime deadline, UInt32 interType)
241{
242 _IORecursiveLock * lock = (_IORecursiveLock *)_lock;
243 UInt32 count = lock->count;
244 int res;
245
246 assert(lock->thread == IOThreadSelf());
247
248 lock->count = 0;
249 lock->thread = 0;
39037602 250 res = lck_mtx_sleep_deadline(&lock->mutex, LCK_SLEEP_PROMOTED_PRI, (event_t) event,
fe8ab488 251 (wait_interrupt_t) interType, __OSAbsoluteTime(deadline));
b0d623f7
A
252
253 // Must re-establish the recursive lock no matter why we woke up
254 // otherwise we would potentially leave the return path corrupted.
255 assert(lock->thread == 0);
7b1edb79
A
256 assert(lock->count == 0);
257 lock->thread = IOThreadSelf();
258 lock->count = count;
1c79356b
A
259 return res;
260}
261
262void IORecursiveLockWakeup(IORecursiveLock *, void *event, bool oneThread)
263{
264 thread_wakeup_prim((event_t) event, oneThread, THREAD_AWAKENED);
265}
266
267/*
268 * Complex (read/write) lock operations
269 */
270
271IORWLock * IORWLockAlloc( void )
272{
91447636 273 return( lck_rw_alloc_init(IOLockGroup, LCK_ATTR_NULL) );
1c79356b
A
274}
275
276void IORWLockFree( IORWLock * lock)
277{
91447636
A
278 lck_rw_free( lock, IOLockGroup);
279}
280
281lck_rw_t * IORWLockGetMachLock( IORWLock * lock)
282{
283 return( (lck_rw_t *)lock);
1c79356b
A
284}
285
286
287/*
288 * Spin locks
289 */
290
291IOSimpleLock * IOSimpleLockAlloc( void )
292{
91447636 293 return( lck_spin_alloc_init( IOLockGroup, LCK_ATTR_NULL) );
1c79356b
A
294}
295
296void IOSimpleLockInit( IOSimpleLock * lock)
297{
91447636 298 lck_spin_init( lock, IOLockGroup, LCK_ATTR_NULL);
1c79356b
A
299}
300
301void IOSimpleLockFree( IOSimpleLock * lock )
302{
91447636
A
303 lck_spin_free( lock, IOLockGroup);
304}
305
306lck_spin_t * IOSimpleLockGetMachLock( IOSimpleLock * lock)
307{
308 return( (lck_spin_t *)lock);
1c79356b
A
309}
310
311} /* extern "C" */
312
313