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