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