]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-lockdebug.mm
5423acda55507869f5d4eb92a1c5917c330f672c
[apple/objc4.git] / runtime / objc-lockdebug.mm
1 /*
2 * Copyright (c) 2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /***********************************************************************
25 * objc-lock.m
26 * Error-checking locks for debugging.
27 **********************************************************************/
28
29 #include "objc-private.h"
30
31 #if DEBUG && !TARGET_OS_WIN32
32
33 /***********************************************************************
34 * Recording - per-thread list of mutexes and monitors held
35 **********************************************************************/
36
37 typedef struct {
38 void *l; // the lock itself
39 int k; // the kind of lock it is (MUTEX, MONITOR, etc)
40 int i; // the lock's nest count
41 } lockcount;
42
43 #define MUTEX 1
44 #define MONITOR 2
45 #define RDLOCK 3
46 #define WRLOCK 4
47 #define RECURSIVE 5
48
49 typedef struct _objc_lock_list {
50 int allocated;
51 int used;
52 lockcount list[0];
53 } _objc_lock_list;
54
55 static tls_key_t lock_tls;
56
57 static void
58 destroyLocks(void *value)
59 {
60 _objc_lock_list *locks = (_objc_lock_list *)value;
61 // fixme complain about any still-held locks?
62 if (locks) free(locks);
63 }
64
65 static struct _objc_lock_list *
66 getLocks(BOOL create)
67 {
68 _objc_lock_list *locks;
69
70 // Use a dedicated tls key to prevent differences vs non-debug in
71 // usage of objc's other tls keys (required for some unit tests).
72 INIT_ONCE_PTR(lock_tls, tls_create(&destroyLocks), (void)0);
73
74 locks = (_objc_lock_list *)tls_get(lock_tls);
75 if (!locks) {
76 if (!create) {
77 return NULL;
78 } else {
79 locks = (_objc_lock_list *)calloc(1, sizeof(_objc_lock_list) + sizeof(lockcount) * 16);
80 locks->allocated = 16;
81 locks->used = 0;
82 tls_set(lock_tls, locks);
83 }
84 }
85
86 if (locks->allocated == locks->used) {
87 if (!create) {
88 return locks;
89 } else {
90 _objc_lock_list *oldlocks = locks;
91 locks = (_objc_lock_list *)calloc(1, sizeof(_objc_lock_list) + 2 * oldlocks->used * sizeof(lockcount));
92 locks->used = oldlocks->used;
93 locks->allocated = oldlocks->used * 2;
94 memcpy(locks->list, oldlocks->list, locks->used * sizeof(lockcount));
95 tls_set(lock_tls, locks);
96 free(oldlocks);
97 }
98 }
99
100 return locks;
101 }
102
103 static BOOL
104 hasLock(_objc_lock_list *locks, void *lock, int kind)
105 {
106 int i;
107 if (!locks) return NO;
108
109 for (i = 0; i < locks->used; i++) {
110 if (locks->list[i].l == lock && locks->list[i].k == kind) return YES;
111 }
112 return NO;
113 }
114
115
116 static void
117 setLock(_objc_lock_list *locks, void *lock, int kind)
118 {
119 int i;
120 for (i = 0; i < locks->used; i++) {
121 if (locks->list[i].l == lock && locks->list[i].k == kind) {
122 locks->list[i].i++;
123 return;
124 }
125 }
126
127 locks->list[locks->used].l = lock;
128 locks->list[locks->used].i = 1;
129 locks->list[locks->used].k = kind;
130 locks->used++;
131 }
132
133 static void
134 clearLock(_objc_lock_list *locks, void *lock, int kind)
135 {
136 int i;
137 for (i = 0; i < locks->used; i++) {
138 if (locks->list[i].l == lock && locks->list[i].k == kind) {
139 if (--locks->list[i].i == 0) {
140 locks->list[i].l = NULL;
141 locks->list[i] = locks->list[--locks->used];
142 }
143 return;
144 }
145 }
146
147 _objc_fatal("lock not found!");
148 }
149
150
151 /***********************************************************************
152 * Mutex checking
153 **********************************************************************/
154
155 void
156 lockdebug_mutex_lock(mutex_t *lock)
157 {
158 _objc_lock_list *locks = getLocks(YES);
159
160 if (hasLock(locks, lock, MUTEX)) {
161 _objc_fatal("deadlock: relocking mutex");
162 }
163 setLock(locks, lock, MUTEX);
164 }
165
166 // try-lock success is the only case with lockdebug effects.
167 // try-lock when already locked is OK (will fail)
168 // try-lock failure does nothing.
169 void
170 lockdebug_mutex_try_lock_success(mutex_t *lock)
171 {
172 _objc_lock_list *locks = getLocks(YES);
173 setLock(locks, lock, MUTEX);
174 }
175
176 void
177 lockdebug_mutex_unlock(mutex_t *lock)
178 {
179 _objc_lock_list *locks = getLocks(NO);
180
181 if (!hasLock(locks, lock, MUTEX)) {
182 _objc_fatal("unlocking unowned mutex");
183 }
184 clearLock(locks, lock, MUTEX);
185 }
186
187
188 void
189 lockdebug_mutex_assert_locked(mutex_t *lock)
190 {
191 _objc_lock_list *locks = getLocks(NO);
192
193 if (!hasLock(locks, lock, MUTEX)) {
194 _objc_fatal("mutex incorrectly not locked");
195 }
196 }
197
198 void
199 lockdebug_mutex_assert_unlocked(mutex_t *lock)
200 {
201 _objc_lock_list *locks = getLocks(NO);
202
203 if (hasLock(locks, lock, MUTEX)) {
204 _objc_fatal("mutex incorrectly locked");
205 }
206 }
207
208
209 /***********************************************************************
210 * Recursive mutex checking
211 **********************************************************************/
212
213 void
214 lockdebug_recursive_mutex_lock(recursive_mutex_tt<true> *lock)
215 {
216 _objc_lock_list *locks = getLocks(YES);
217 setLock(locks, lock, RECURSIVE);
218 }
219
220 void
221 lockdebug_recursive_mutex_unlock(recursive_mutex_tt<true> *lock)
222 {
223 _objc_lock_list *locks = getLocks(NO);
224
225 if (!hasLock(locks, lock, RECURSIVE)) {
226 _objc_fatal("unlocking unowned recursive mutex");
227 }
228 clearLock(locks, lock, RECURSIVE);
229 }
230
231
232 void
233 lockdebug_recursive_mutex_assert_locked(recursive_mutex_tt<true> *lock)
234 {
235 _objc_lock_list *locks = getLocks(NO);
236
237 if (!hasLock(locks, lock, RECURSIVE)) {
238 _objc_fatal("recursive mutex incorrectly not locked");
239 }
240 }
241
242 void
243 lockdebug_recursive_mutex_assert_unlocked(recursive_mutex_tt<true> *lock)
244 {
245 _objc_lock_list *locks = getLocks(NO);
246
247 if (hasLock(locks, lock, RECURSIVE)) {
248 _objc_fatal("recursive mutex incorrectly locked");
249 }
250 }
251
252
253 /***********************************************************************
254 * Monitor checking
255 **********************************************************************/
256
257 void
258 lockdebug_monitor_enter(monitor_t *lock)
259 {
260 _objc_lock_list *locks = getLocks(YES);
261
262 if (hasLock(locks, lock, MONITOR)) {
263 _objc_fatal("deadlock: relocking monitor");
264 }
265 setLock(locks, lock, MONITOR);
266 }
267
268 void
269 lockdebug_monitor_leave(monitor_t *lock)
270 {
271 _objc_lock_list *locks = getLocks(NO);
272
273 if (!hasLock(locks, lock, MONITOR)) {
274 _objc_fatal("unlocking unowned monitor");
275 }
276 clearLock(locks, lock, MONITOR);
277 }
278
279 void
280 lockdebug_monitor_wait(monitor_t *lock)
281 {
282 _objc_lock_list *locks = getLocks(NO);
283
284 if (!hasLock(locks, lock, MONITOR)) {
285 _objc_fatal("waiting in unowned monitor");
286 }
287 }
288
289
290 void
291 lockdebug_monitor_assert_locked(monitor_t *lock)
292 {
293 _objc_lock_list *locks = getLocks(NO);
294
295 if (!hasLock(locks, lock, MONITOR)) {
296 _objc_fatal("monitor incorrectly not locked");
297 }
298 }
299
300 void
301 lockdebug_monitor_assert_unlocked(monitor_t *lock)
302 {
303 _objc_lock_list *locks = getLocks(NO);
304
305 if (hasLock(locks, lock, MONITOR)) {
306 _objc_fatal("monitor incorrectly held");
307 }
308 }
309
310
311 /***********************************************************************
312 * rwlock checking
313 **********************************************************************/
314
315 void
316 lockdebug_rwlock_read(rwlock_tt<true> *lock)
317 {
318 _objc_lock_list *locks = getLocks(YES);
319
320 if (hasLock(locks, lock, RDLOCK)) {
321 // Recursive rwlock read is bad (may deadlock vs pending writer)
322 _objc_fatal("recursive rwlock read");
323 }
324 if (hasLock(locks, lock, WRLOCK)) {
325 _objc_fatal("deadlock: read after write for rwlock");
326 }
327 setLock(locks, lock, RDLOCK);
328 }
329
330 // try-read success is the only case with lockdebug effects.
331 // try-read when already reading is OK (won't deadlock)
332 // try-read when already writing is OK (will fail)
333 // try-read failure does nothing.
334 void
335 lockdebug_rwlock_try_read_success(rwlock_tt<true> *lock)
336 {
337 _objc_lock_list *locks = getLocks(YES);
338 setLock(locks, lock, RDLOCK);
339 }
340
341 void
342 lockdebug_rwlock_unlock_read(rwlock_tt<true> *lock)
343 {
344 _objc_lock_list *locks = getLocks(NO);
345
346 if (!hasLock(locks, lock, RDLOCK)) {
347 _objc_fatal("un-reading unowned rwlock");
348 }
349 clearLock(locks, lock, RDLOCK);
350 }
351
352
353 void
354 lockdebug_rwlock_write(rwlock_tt<true> *lock)
355 {
356 _objc_lock_list *locks = getLocks(YES);
357
358 if (hasLock(locks, lock, RDLOCK)) {
359 // Lock promotion not allowed (may deadlock)
360 _objc_fatal("deadlock: write after read for rwlock");
361 }
362 if (hasLock(locks, lock, WRLOCK)) {
363 _objc_fatal("recursive rwlock write");
364 }
365 setLock(locks, lock, WRLOCK);
366 }
367
368 // try-write success is the only case with lockdebug effects.
369 // try-write when already reading is OK (will fail)
370 // try-write when already writing is OK (will fail)
371 // try-write failure does nothing.
372 void
373 lockdebug_rwlock_try_write_success(rwlock_tt<true> *lock)
374 {
375 _objc_lock_list *locks = getLocks(YES);
376 setLock(locks, lock, WRLOCK);
377 }
378
379 void
380 lockdebug_rwlock_unlock_write(rwlock_tt<true> *lock)
381 {
382 _objc_lock_list *locks = getLocks(NO);
383
384 if (!hasLock(locks, lock, WRLOCK)) {
385 _objc_fatal("un-writing unowned rwlock");
386 }
387 clearLock(locks, lock, WRLOCK);
388 }
389
390
391 void
392 lockdebug_rwlock_assert_reading(rwlock_tt<true> *lock)
393 {
394 _objc_lock_list *locks = getLocks(NO);
395
396 if (!hasLock(locks, lock, RDLOCK)) {
397 _objc_fatal("rwlock incorrectly not reading");
398 }
399 }
400
401 void
402 lockdebug_rwlock_assert_writing(rwlock_tt<true> *lock)
403 {
404 _objc_lock_list *locks = getLocks(NO);
405
406 if (!hasLock(locks, lock, WRLOCK)) {
407 _objc_fatal("rwlock incorrectly not writing");
408 }
409 }
410
411 void
412 lockdebug_rwlock_assert_locked(rwlock_tt<true> *lock)
413 {
414 _objc_lock_list *locks = getLocks(NO);
415
416 if (!hasLock(locks, lock, RDLOCK) && !hasLock(locks, lock, WRLOCK)) {
417 _objc_fatal("rwlock incorrectly neither reading nor writing");
418 }
419 }
420
421 void
422 lockdebug_rwlock_assert_unlocked(rwlock_tt<true> *lock)
423 {
424 _objc_lock_list *locks = getLocks(NO);
425
426 if (hasLock(locks, lock, RDLOCK) || hasLock(locks, lock, WRLOCK)) {
427 _objc_fatal("rwlock incorrectly not unlocked");
428 }
429 }
430
431
432 #endif