]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
8f6c56a5 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. | |
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 | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1998 Apple Computer, Inc. All rights reserved. | |
30 | * | |
31 | * HISTORY | |
32 | * | |
33 | */ | |
34 | ||
35 | ||
91447636 A |
36 | #define IOLOCKS_CPP 1 |
37 | ||
1c79356b A |
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" { | |
91447636 A |
45 | #include <kern/locks.h> |
46 | ||
47 | void IOLockInitWithState( IOLock * lock, IOLockState state) | |
48 | { | |
49 | if( state == kIOLockStateLocked) | |
50 | lck_mtx_lock( lock); | |
51 | } | |
1c79356b A |
52 | |
53 | IOLock * IOLockAlloc( void ) | |
54 | { | |
91447636 | 55 | return( lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL) ); |
1c79356b A |
56 | } |
57 | ||
58 | void IOLockFree( IOLock * lock) | |
59 | { | |
91447636 | 60 | lck_mtx_free( lock, IOLockGroup); |
1c79356b A |
61 | } |
62 | ||
91447636 | 63 | lck_mtx_t * IOLockGetMachLock( IOLock * lock) |
1c79356b | 64 | { |
91447636 | 65 | return( (lck_mtx_t *)lock); |
1c79356b A |
66 | } |
67 | ||
91447636 A |
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 | ||
1c79356b | 86 | struct _IORecursiveLock { |
91447636 | 87 | lck_mtx_t *mutex; |
1c79356b A |
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 | ||
91447636 | 100 | lock->mutex = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL); |
1c79356b A |
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 | ||
91447636 | 116 | lck_mtx_free( lock->mutex , IOLockGroup); |
1c79356b A |
117 | IODelete( lock, _IORecursiveLock, 1); |
118 | } | |
119 | ||
91447636 A |
120 | lck_mtx_t * IORecursiveLockGetMachLock( IORecursiveLock * lock) |
121 | { | |
122 | return( lock->mutex); | |
123 | } | |
124 | ||
1c79356b A |
125 | void IORecursiveLockLock( IORecursiveLock * _lock) |
126 | { | |
127 | _IORecursiveLock * lock = (_IORecursiveLock *)_lock; | |
128 | ||
129 | if( lock->thread == IOThreadSelf()) | |
130 | lock->count++; | |
131 | else { | |
91447636 | 132 | lck_mtx_lock( lock->mutex ); |
1c79356b A |
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 { | |
91447636 | 148 | if( lck_mtx_try_lock( lock->mutex )) { |
1c79356b A |
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; | |
91447636 | 167 | lck_mtx_unlock( lock->mutex ); |
1c79356b A |
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 | ||
1c79356b A |
187 | lock->count = 0; |
188 | lock->thread = 0; | |
91447636 | 189 | res = lck_mtx_sleep(lock->mutex, LCK_SLEEP_DEFAULT, (event_t) event, (wait_interrupt_t) interType); |
1c79356b | 190 | |
7b1edb79 A |
191 | // Must re-establish the recursive lock no matter why we woke up |
192 | // otherwise we would potentially leave the return path corrupted. | |
7b1edb79 A |
193 | assert(lock->thread == 0); |
194 | assert(lock->count == 0); | |
195 | lock->thread = IOThreadSelf(); | |
196 | lock->count = count; | |
1c79356b A |
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 | { | |
91447636 | 211 | return( lck_rw_alloc_init(IOLockGroup, LCK_ATTR_NULL) ); |
1c79356b A |
212 | } |
213 | ||
214 | void IORWLockFree( IORWLock * lock) | |
215 | { | |
91447636 A |
216 | lck_rw_free( lock, IOLockGroup); |
217 | } | |
218 | ||
219 | lck_rw_t * IORWLockGetMachLock( IORWLock * lock) | |
220 | { | |
221 | return( (lck_rw_t *)lock); | |
1c79356b A |
222 | } |
223 | ||
224 | ||
225 | /* | |
226 | * Spin locks | |
227 | */ | |
228 | ||
229 | IOSimpleLock * IOSimpleLockAlloc( void ) | |
230 | { | |
91447636 | 231 | return( lck_spin_alloc_init( IOLockGroup, LCK_ATTR_NULL) ); |
1c79356b A |
232 | } |
233 | ||
234 | void IOSimpleLockInit( IOSimpleLock * lock) | |
235 | { | |
91447636 | 236 | lck_spin_init( lock, IOLockGroup, LCK_ATTR_NULL); |
1c79356b A |
237 | } |
238 | ||
239 | void IOSimpleLockFree( IOSimpleLock * lock ) | |
240 | { | |
91447636 A |
241 | lck_spin_free( lock, IOLockGroup); |
242 | } | |
243 | ||
244 | lck_spin_t * IOSimpleLockGetMachLock( IOSimpleLock * lock) | |
245 | { | |
246 | return( (lck_spin_t *)lock); | |
1c79356b A |
247 | } |
248 | ||
249 | } /* extern "C" */ | |
250 | ||
251 |