]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-lockdebug.mm
9976b7127086586f63d870254c35034764944043
[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 !defined(NDEBUG) && !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_internal(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_internal(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_internal(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_internal(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 int
156 _mutex_lock_debug(mutex_t *lock, const char *name)
157 {
158 _objc_lock_list *locks = getLocks(YES);
159
160 if (hasLock(locks, lock, MUTEX)) {
161 _objc_fatal("deadlock: relocking mutex %s\n", name+1);
162 }
163 setLock(locks, lock, MUTEX);
164
165 return _mutex_lock_nodebug(lock);
166 }
167
168 int
169 _mutex_try_lock_debug(mutex_t *lock, const char *name)
170 {
171 _objc_lock_list *locks = getLocks(YES);
172
173 // attempting to relock in try_lock is OK
174 int result = _mutex_try_lock_nodebug(lock);
175
176 if (result) {
177 setLock(locks, lock, MUTEX);
178 }
179
180 return result;
181 }
182
183 int
184 _mutex_unlock_debug(mutex_t *lock, const char *name)
185 {
186 _objc_lock_list *locks = getLocks(NO);
187
188 if (!hasLock(locks, lock, MUTEX)) {
189 _objc_fatal("unlocking unowned mutex %s\n", name+1);
190 }
191 clearLock(locks, lock, MUTEX);
192
193 return _mutex_unlock_nodebug(lock);
194 }
195
196 void
197 _mutex_assert_locked_debug(mutex_t *lock, const char *name)
198 {
199 _objc_lock_list *locks = getLocks(NO);
200
201 if (!hasLock(locks, lock, MUTEX)) {
202 _objc_fatal("mutex %s incorrectly not held\n",name+1);
203 }
204 }
205
206
207 void
208 _mutex_assert_unlocked_debug(mutex_t *lock, const char *name)
209 {
210 _objc_lock_list *locks = getLocks(NO);
211
212 if (hasLock(locks, lock, MUTEX)) {
213 _objc_fatal("mutex %s incorrectly held\n", name+1);
214 }
215 }
216
217
218 /***********************************************************************
219 * Recursive mutex checking
220 **********************************************************************/
221
222 int
223 _recursive_mutex_lock_debug(recursive_mutex_t *lock, const char *name)
224 {
225 _objc_lock_list *locks = getLocks(YES);
226
227 setLock(locks, lock, RECURSIVE);
228
229 return _recursive_mutex_lock_nodebug(lock);
230 }
231
232 int
233 _recursive_mutex_try_lock_debug(recursive_mutex_t *lock, const char *name)
234 {
235 _objc_lock_list *locks = getLocks(YES);
236
237 int result = _recursive_mutex_try_lock_nodebug(lock);
238
239 if (result) {
240 setLock(locks, lock, RECURSIVE);
241 }
242
243 return result;
244 }
245
246 int
247 _recursive_mutex_unlock_debug(recursive_mutex_t *lock, const char *name)
248 {
249 _objc_lock_list *locks = getLocks(NO);
250
251 if (!hasLock(locks, lock, RECURSIVE)) {
252 _objc_fatal("unlocking unowned recursive mutex %s\n", name+1);
253 }
254 clearLock(locks, lock, RECURSIVE);
255
256 return _recursive_mutex_unlock_nodebug(lock);
257 }
258
259 void
260 _recursive_mutex_assert_locked_debug(recursive_mutex_t *lock, const char *name)
261 {
262 _objc_lock_list *locks = getLocks(NO);
263
264 if (!hasLock(locks, lock, RECURSIVE)) {
265 _objc_fatal("recursive mutex %s incorrectly not held\n",name+1);
266 }
267 }
268
269
270 void
271 _recursive_mutex_assert_unlocked_debug(recursive_mutex_t *lock, const char *name)
272 {
273 _objc_lock_list *locks = getLocks(NO);
274
275 if (hasLock(locks, lock, RECURSIVE)) {
276 _objc_fatal("recursive mutex %s incorrectly held\n", name+1);
277 }
278 }
279
280
281 /***********************************************************************
282 * Monitor checking
283 **********************************************************************/
284
285 int
286 _monitor_enter_debug(monitor_t *lock, const char *name)
287 {
288 _objc_lock_list *locks = getLocks(YES);
289
290 if (hasLock(locks, lock, MONITOR)) {
291 _objc_fatal("deadlock: relocking monitor %s\n", name+1);
292 }
293 setLock(locks, lock, MONITOR);
294
295 return _monitor_enter_nodebug(lock);
296 }
297
298 int
299 _monitor_exit_debug(monitor_t *lock, const char *name)
300 {
301 _objc_lock_list *locks = getLocks(NO);
302
303 if (!hasLock(locks, lock, MONITOR)) {
304 _objc_fatal("unlocking unowned monitor%s\n", name+1);
305 }
306 clearLock(locks, lock, MONITOR);
307
308 return _monitor_exit_nodebug(lock);
309 }
310
311 int
312 _monitor_wait_debug(monitor_t *lock, const char *name)
313 {
314 _objc_lock_list *locks = getLocks(NO);
315
316 if (!hasLock(locks, lock, MONITOR)) {
317 _objc_fatal("waiting in unowned monitor%s\n", name+1);
318 }
319
320 return _monitor_wait_nodebug(lock);
321 }
322
323 void
324 _monitor_assert_locked_debug(monitor_t *lock, const char *name)
325 {
326 _objc_lock_list *locks = getLocks(NO);
327
328 if (!hasLock(locks, lock, MONITOR)) {
329 _objc_fatal("monitor %s incorrectly not held\n",name+1);
330 }
331 }
332
333 void
334 _monitor_assert_unlocked_debug(monitor_t *lock, const char *name)
335 {
336 _objc_lock_list *locks = getLocks(NO);
337
338 if (hasLock(locks, lock, MONITOR)) {
339 _objc_fatal("monitor %s incorrectly held\n", name+1);
340 }
341 }
342
343
344 /***********************************************************************
345 * rwlock checking
346 **********************************************************************/
347
348 void
349 _rwlock_read_debug(rwlock_t *lock, const char *name)
350 {
351 _objc_lock_list *locks = getLocks(YES);
352
353 if (hasLock(locks, lock, RDLOCK)) {
354 // Recursive rwlock read is bad (may deadlock vs pending writer)
355 _objc_fatal("recursive rwlock read %s\n", name+1);
356 }
357 if (hasLock(locks, lock, WRLOCK)) {
358 _objc_fatal("deadlock: read after write for rwlock %s\n", name+1);
359 }
360 setLock(locks, lock, RDLOCK);
361
362 _rwlock_read_nodebug(lock);
363 }
364
365 int
366 _rwlock_try_read_debug(rwlock_t *lock, const char *name)
367 {
368 _objc_lock_list *locks = getLocks(YES);
369
370 // try-read when already reading is OK (won't deadlock against writer)
371 // try-read when already writing is OK (will fail)
372 int result = _rwlock_try_read_nodebug(lock);
373
374 if (result) {
375 setLock(locks, lock, RDLOCK);
376 }
377
378 return result;
379 }
380
381 void
382 _rwlock_unlock_read_debug(rwlock_t *lock, const char *name)
383 {
384 _objc_lock_list *locks = getLocks(NO);
385
386 if (!hasLock(locks, lock, RDLOCK)) {
387 _objc_fatal("un-reading unowned rwlock %s\n", name+1);
388 }
389 clearLock(locks, lock, RDLOCK);
390
391 _rwlock_unlock_read_nodebug(lock);
392 }
393
394 void
395 _rwlock_write_debug(rwlock_t *lock, const char *name)
396 {
397 _objc_lock_list *locks = getLocks(YES);
398
399 if (hasLock(locks, lock, RDLOCK)) {
400 // Lock promotion not allowed (may deadlock)
401 _objc_fatal("deadlock: write after read for rwlock %s\n", name+1);
402 }
403 if (hasLock(locks, lock, WRLOCK)) {
404 _objc_fatal("recursive rwlock write %s\n", name+1);
405 }
406 setLock(locks, lock, WRLOCK);
407
408 _rwlock_write_nodebug(lock);
409 }
410
411
412 int
413 _rwlock_try_write_debug(rwlock_t *lock, const char *name)
414 {
415 _objc_lock_list *locks = getLocks(YES);
416
417 // try-write when already reading is OK (will fail)
418 // try-write when already writing is OK (will fail)
419 int result = _rwlock_try_write_nodebug(lock);
420
421 if (result) {
422 setLock(locks, lock, WRLOCK);
423 }
424
425 return result;
426 }
427
428 void
429 _rwlock_unlock_write_debug(rwlock_t *lock, const char *name)
430 {
431 _objc_lock_list *locks = getLocks(NO);
432
433 if (!hasLock(locks, lock, WRLOCK)) {
434 _objc_fatal("un-writing unowned rwlock %s\n", name+1);
435 }
436 clearLock(locks, lock, WRLOCK);
437
438 _rwlock_unlock_write_nodebug(lock);
439 }
440
441
442 void
443 _rwlock_assert_reading_debug(rwlock_t *lock, const char *name)
444 {
445 _objc_lock_list *locks = getLocks(NO);
446
447 if (!hasLock(locks, lock, RDLOCK)) {
448 _objc_fatal("rwlock %s incorrectly not reading\n", name+1);
449 }
450 }
451
452 void
453 _rwlock_assert_writing_debug(rwlock_t *lock, const char *name)
454 {
455 _objc_lock_list *locks = getLocks(NO);
456
457 if (!hasLock(locks, lock, WRLOCK)) {
458 _objc_fatal("rwlock %s incorrectly not writing\n", name+1);
459 }
460 }
461
462 void
463 _rwlock_assert_locked_debug(rwlock_t *lock, const char *name)
464 {
465 _objc_lock_list *locks = getLocks(NO);
466
467 if (!hasLock(locks, lock, RDLOCK) && !hasLock(locks, lock, WRLOCK)) {
468 _objc_fatal("rwlock %s incorrectly neither reading nor writing\n",
469 name+1);
470 }
471 }
472
473 void
474 _rwlock_assert_unlocked_debug(rwlock_t *lock, const char *name)
475 {
476 _objc_lock_list *locks = getLocks(NO);
477
478 if (hasLock(locks, lock, RDLOCK) || hasLock(locks, lock, WRLOCK)) {
479 _objc_fatal("rwlock %s incorrectly not unlocked\n", name+1);
480 }
481 }
482
483
484 #endif