From e6c6e15b03639e98df68df35ba0f639bdbc1dc51 Mon Sep 17 00:00:00 2001 From: Justus Winter <4winter@informatik.uni-hamburg.de> Date: Sat, 15 Aug 2015 18:30:28 +0200 Subject: kern: keep track of the writer when debugging locks * configfrag.ac (MACH_LDEBUG): Adjust comment, we use it to sanity check all locks now. * kern/lock.c (lock_write): Keep track of the writer thread. (lock_done): Clear writer. (lock_read_to_write): Keep track of the writer thread. (lock_write_to_read): Assert that the current thread holds the lock. Clear writer. (lock_try_write): Keep track of the writer thread. (lock_try_read_to_write): Likewise. (lock_set_recursive): Assert that the current thread holds the lock. * kern/lock.h (struct lock): New field `writer'. (have_read_lock, have_write_lock, have_lock): New macros that can be used to assert that the current thread holds the given lock. If MACH_LDEBUG is not set, they evaluate to true. --- kern/lock.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'kern/lock.h') diff --git a/kern/lock.h b/kern/lock.h index e88e1824..2781a48a 100644 --- a/kern/lock.h +++ b/kern/lock.h @@ -174,6 +174,9 @@ struct lock { /* boolean_t */ can_sleep:1, /* Can attempts to lock go to sleep? */ recursion_depth:12, /* Depth of recursion */ :0; +#if MACH_LDEBUG + struct thread *writer; +#endif /* MACH_LDEBUG */ decl_simple_lock_data(,interlock) /* Hardware interlock field. Last in the structure so that @@ -203,6 +206,17 @@ extern boolean_t lock_try_read_to_write(lock_t); extern void lock_set_recursive(lock_t); extern void lock_clear_recursive(lock_t); +/* Lock debugging support. */ +#if ! MACH_LDEBUG +#define have_read_lock(l) 1 +#define have_write_lock(l) 1 +#else /* MACH_LDEBUG */ +/* XXX: We don't keep track of readers, so this is an approximation. */ +#define have_read_lock(l) ((l)->read_count > 0) +#define have_write_lock(l) ((l)->writer == current_thread()) +#endif /* MACH_LDEBUG */ +#define have_lock(l) (have_read_lock(l) || have_write_lock(l)) + void db_show_all_slocks(void); #endif /* _KERN_LOCK_H_ */ -- cgit v1.2.3