2 * Copyright (c) 2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 /***********************************************************************
26 * Error-checking locks for debugging.
27 **********************************************************************/
29 #include "objc-private.h"
31 #if DEBUG && !TARGET_OS_WIN32
33 /***********************************************************************
34 * Recording - per-thread list of mutexes and monitors held
35 **********************************************************************/
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
49 typedef struct _objc_lock_list {
55 static tls_key_t lock_tls;
58 destroyLocks(void *value)
60 _objc_lock_list *locks = (_objc_lock_list *)value;
61 // fixme complain about any still-held locks?
62 if (locks) free(locks);
65 static struct _objc_lock_list *
68 _objc_lock_list *locks;
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);
74 locks = (_objc_lock_list *)tls_get(lock_tls);
79 locks = (_objc_lock_list *)calloc(1, sizeof(_objc_lock_list) + sizeof(lockcount) * 16);
80 locks->allocated = 16;
82 tls_set(lock_tls, locks);
86 if (locks->allocated == locks->used) {
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);
104 hasLock(_objc_lock_list *locks, void *lock, int kind)
107 if (!locks) return NO;
109 for (i = 0; i < locks->used; i++) {
110 if (locks->list[i].l == lock && locks->list[i].k == kind) return YES;
117 setLock(_objc_lock_list *locks, void *lock, int kind)
120 for (i = 0; i < locks->used; i++) {
121 if (locks->list[i].l == lock && locks->list[i].k == kind) {
127 locks->list[locks->used].l = lock;
128 locks->list[locks->used].i = 1;
129 locks->list[locks->used].k = kind;
134 clearLock(_objc_lock_list *locks, void *lock, int kind)
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];
147 _objc_fatal("lock not found!");
151 /***********************************************************************
153 **********************************************************************/
155 #if !TARGET_OS_SIMULATOR
156 // Non-simulator platforms have lock debugging built into os_unfair_lock.
160 lockdebug_mutex_lock(mutex_t *lock)
166 lockdebug_mutex_unlock(mutex_t *lock)
172 lockdebug_mutex_assert_locked(mutex_t *lock)
174 os_unfair_lock_assert_owner((os_unfair_lock *)lock);
178 lockdebug_mutex_assert_unlocked(mutex_t *lock)
180 os_unfair_lock_assert_not_owner((os_unfair_lock *)lock);
184 // !TARGET_OS_SIMULATOR
186 // TARGET_OS_SIMULATOR
188 // Simulator platforms have no built-in lock debugging in os_unfair_lock.
192 lockdebug_mutex_lock(mutex_t *lock)
194 _objc_lock_list *locks = getLocks(YES);
196 if (hasLock(locks, lock, MUTEX)) {
197 _objc_fatal("deadlock: relocking mutex");
199 setLock(locks, lock, MUTEX);
202 // try-lock success is the only case with lockdebug effects.
203 // try-lock when already locked is OK (will fail)
204 // try-lock failure does nothing.
206 lockdebug_mutex_try_lock_success(mutex_t *lock)
208 _objc_lock_list *locks = getLocks(YES);
209 setLock(locks, lock, MUTEX);
213 lockdebug_mutex_unlock(mutex_t *lock)
215 _objc_lock_list *locks = getLocks(NO);
217 if (!hasLock(locks, lock, MUTEX)) {
218 _objc_fatal("unlocking unowned mutex");
220 clearLock(locks, lock, MUTEX);
225 lockdebug_mutex_assert_locked(mutex_t *lock)
227 _objc_lock_list *locks = getLocks(NO);
229 if (!hasLock(locks, lock, MUTEX)) {
230 _objc_fatal("mutex incorrectly not locked");
235 lockdebug_mutex_assert_unlocked(mutex_t *lock)
237 _objc_lock_list *locks = getLocks(NO);
239 if (hasLock(locks, lock, MUTEX)) {
240 _objc_fatal("mutex incorrectly locked");
245 // TARGET_OS_SIMULATOR
248 /***********************************************************************
249 * Recursive mutex checking
250 **********************************************************************/
253 lockdebug_recursive_mutex_lock(recursive_mutex_tt<true> *lock)
255 _objc_lock_list *locks = getLocks(YES);
256 setLock(locks, lock, RECURSIVE);
260 lockdebug_recursive_mutex_unlock(recursive_mutex_tt<true> *lock)
262 _objc_lock_list *locks = getLocks(NO);
264 if (!hasLock(locks, lock, RECURSIVE)) {
265 _objc_fatal("unlocking unowned recursive mutex");
267 clearLock(locks, lock, RECURSIVE);
272 lockdebug_recursive_mutex_assert_locked(recursive_mutex_tt<true> *lock)
274 _objc_lock_list *locks = getLocks(NO);
276 if (!hasLock(locks, lock, RECURSIVE)) {
277 _objc_fatal("recursive mutex incorrectly not locked");
282 lockdebug_recursive_mutex_assert_unlocked(recursive_mutex_tt<true> *lock)
284 _objc_lock_list *locks = getLocks(NO);
286 if (hasLock(locks, lock, RECURSIVE)) {
287 _objc_fatal("recursive mutex incorrectly locked");
292 /***********************************************************************
294 **********************************************************************/
297 lockdebug_monitor_enter(monitor_t *lock)
299 _objc_lock_list *locks = getLocks(YES);
301 if (hasLock(locks, lock, MONITOR)) {
302 _objc_fatal("deadlock: relocking monitor");
304 setLock(locks, lock, MONITOR);
308 lockdebug_monitor_leave(monitor_t *lock)
310 _objc_lock_list *locks = getLocks(NO);
312 if (!hasLock(locks, lock, MONITOR)) {
313 _objc_fatal("unlocking unowned monitor");
315 clearLock(locks, lock, MONITOR);
319 lockdebug_monitor_wait(monitor_t *lock)
321 _objc_lock_list *locks = getLocks(NO);
323 if (!hasLock(locks, lock, MONITOR)) {
324 _objc_fatal("waiting in unowned monitor");
330 lockdebug_monitor_assert_locked(monitor_t *lock)
332 _objc_lock_list *locks = getLocks(NO);
334 if (!hasLock(locks, lock, MONITOR)) {
335 _objc_fatal("monitor incorrectly not locked");
340 lockdebug_monitor_assert_unlocked(monitor_t *lock)
342 _objc_lock_list *locks = getLocks(NO);
344 if (hasLock(locks, lock, MONITOR)) {
345 _objc_fatal("monitor incorrectly held");
350 /***********************************************************************
352 **********************************************************************/
355 lockdebug_rwlock_read(rwlock_tt<true> *lock)
357 _objc_lock_list *locks = getLocks(YES);
359 if (hasLock(locks, lock, RDLOCK)) {
360 // Recursive rwlock read is bad (may deadlock vs pending writer)
361 _objc_fatal("recursive rwlock read");
363 if (hasLock(locks, lock, WRLOCK)) {
364 _objc_fatal("deadlock: read after write for rwlock");
366 setLock(locks, lock, RDLOCK);
369 // try-read success is the only case with lockdebug effects.
370 // try-read when already reading is OK (won't deadlock)
371 // try-read when already writing is OK (will fail)
372 // try-read failure does nothing.
374 lockdebug_rwlock_try_read_success(rwlock_tt<true> *lock)
376 _objc_lock_list *locks = getLocks(YES);
377 setLock(locks, lock, RDLOCK);
381 lockdebug_rwlock_unlock_read(rwlock_tt<true> *lock)
383 _objc_lock_list *locks = getLocks(NO);
385 if (!hasLock(locks, lock, RDLOCK)) {
386 _objc_fatal("un-reading unowned rwlock");
388 clearLock(locks, lock, RDLOCK);
393 lockdebug_rwlock_write(rwlock_tt<true> *lock)
395 _objc_lock_list *locks = getLocks(YES);
397 if (hasLock(locks, lock, RDLOCK)) {
398 // Lock promotion not allowed (may deadlock)
399 _objc_fatal("deadlock: write after read for rwlock");
401 if (hasLock(locks, lock, WRLOCK)) {
402 _objc_fatal("recursive rwlock write");
404 setLock(locks, lock, WRLOCK);
407 // try-write success is the only case with lockdebug effects.
408 // try-write when already reading is OK (will fail)
409 // try-write when already writing is OK (will fail)
410 // try-write failure does nothing.
412 lockdebug_rwlock_try_write_success(rwlock_tt<true> *lock)
414 _objc_lock_list *locks = getLocks(YES);
415 setLock(locks, lock, WRLOCK);
419 lockdebug_rwlock_unlock_write(rwlock_tt<true> *lock)
421 _objc_lock_list *locks = getLocks(NO);
423 if (!hasLock(locks, lock, WRLOCK)) {
424 _objc_fatal("un-writing unowned rwlock");
426 clearLock(locks, lock, WRLOCK);
431 lockdebug_rwlock_assert_reading(rwlock_tt<true> *lock)
433 _objc_lock_list *locks = getLocks(NO);
435 if (!hasLock(locks, lock, RDLOCK)) {
436 _objc_fatal("rwlock incorrectly not reading");
441 lockdebug_rwlock_assert_writing(rwlock_tt<true> *lock)
443 _objc_lock_list *locks = getLocks(NO);
445 if (!hasLock(locks, lock, WRLOCK)) {
446 _objc_fatal("rwlock incorrectly not writing");
451 lockdebug_rwlock_assert_locked(rwlock_tt<true> *lock)
453 _objc_lock_list *locks = getLocks(NO);
455 if (!hasLock(locks, lock, RDLOCK) && !hasLock(locks, lock, WRLOCK)) {
456 _objc_fatal("rwlock incorrectly neither reading nor writing");
461 lockdebug_rwlock_assert_unlocked(rwlock_tt<true> *lock)
463 _objc_lock_list *locks = getLocks(NO);
465 if (hasLock(locks, lock, RDLOCK) || hasLock(locks, lock, WRLOCK)) {
466 _objc_fatal("rwlock incorrectly not unlocked");