Update mutex debug code to be somewhat compatible with multi-threaded
authorKen Raeburn <raeburn@mit.edu>
Sat, 1 May 2004 01:29:44 +0000 (01:29 +0000)
committerKen Raeburn <raeburn@mit.edu>
Sat, 1 May 2004 01:29:44 +0000 (01:29 +0000)
execution.  It won't be as useful in single-threaded programs for detecting
bugs in the mutex handling for now, though.

* k5-thread.h (k5_mutex_debug_check_init, k5_mutex_debug_update_loc): New
macros.
(k5_mutex_debug_lock, k5_mutex_debug_unlock): Use them.
(k5_mutex_lock, k5_mutex_unlock) [ENABLE_THREADS && DEBUG_THREADS]: Use them
instead of k5_mutex_debug_lock and k5_mutex_debug_unlock.
(enum k5_mutex_debug_states): New enum.
(K5_MUTEX_DEBUG_LOCKED, K5_MUTEX_DEBUG_UNLOCKED): Change to enumerator values.
(k5_mutex_debug_info): Use the enum type.  Reorder fields.
(K5_MUTEX_DEBUG_INITIALIZER): Update for new field order.

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@16293 dc483132-0cff-0310-8789-dd5450dbe970

src/include/ChangeLog
src/include/k5-thread.h

index 3e75c77f67f40368b813f8101068e49fd6984786..147ad22a378d773113422d11cb748990e86a3fe0 100644 (file)
@@ -1,5 +1,17 @@
 2004-04-30  Ken Raeburn  <raeburn@mit.edu>
 
+       * k5-thread.h (k5_mutex_debug_check_init,
+       k5_mutex_debug_update_loc): New macros.
+       (k5_mutex_debug_lock, k5_mutex_debug_unlock): Use them.
+       (k5_mutex_lock, k5_mutex_unlock) [ENABLE_THREADS &&
+       DEBUG_THREADS]: Use them instead of k5_mutex_debug_lock and
+       k5_mutex_debug_unlock.
+       (enum k5_mutex_debug_states): New enum.
+       (K5_MUTEX_DEBUG_LOCKED, K5_MUTEX_DEBUG_UNLOCKED): Change to
+       enumerator values.
+       (k5_mutex_debug_info): Use the enum type.  Reorder fields.
+       (K5_MUTEX_DEBUG_INITIALIZER): Update for new field order.
+
        * k5-int.h: Include k5-thread.h.
        (struct krb5_rc_st): Add a mutex.
 
index d596e482728e1cab80e70b07a6250ea1c09e9ab3..592b94fe68df4cc9cb107bc0f264001d09ddd5bd 100644 (file)
 #define DEBUG_THREADS
 
 #include <assert.h>
+enum k5_mutex_debug_states {
+    K5_MUTEX_DEBUG_UNLOCKED = 34,
+    K5_MUTEX_DEBUG_LOCKED = 47
+};
 typedef struct {
-    /* We've got some bits to spare; using more than one bit decreases
-       the likelihood that random storage will contain the right
-       values.  */
-    unsigned int initialized : 3;
-    unsigned int locked : 3;
+    const char *filename;
     /* No source file in this tree gets anywhere near 32K lines.  */
     short lineno;
-    const char *filename;
+    short initialized;
+    enum k5_mutex_debug_states locked;
 } k5_mutex_debug_info;
-#define K5_MUTEX_DEBUG_INITIALIZER     { 2, K5_MUTEX_DEBUG_UNLOCKED, 0, 0 }
-#define K5_MUTEX_DEBUG_LOCKED          4
-#define K5_MUTEX_DEBUG_UNLOCKED                3
+#define K5_MUTEX_DEBUG_INITIALIZER     { 0, 0, 2, K5_MUTEX_DEBUG_UNLOCKED }
 #define k5_mutex_debug_finish_init(M)          \
        (assert((M)->initialized == 2), (M)->initialized = 1, 0)
 #define k5_mutex_debug_init(M)                 \
@@ -161,21 +160,18 @@ typedef struct {
        (assert((M)->initialized == 1                           \
                && (M)->locked == K5_MUTEX_DEBUG_UNLOCKED),     \
         (M)->initialized = 0)
+#define k5_mutex_debug_check_init(M)           \
+       (assert((M)->initialized != 2),         \
+        assert((M)->initialized != 0),         \
+        assert((M)->initialized == 1), 0)
+#define k5_mutex_debug_update_loc(M)                           \
+       ((M)->lineno = __LINE__, (M)->filename = __FILE__)
 #define k5_mutex_debug_lock(M)                                 \
-       (assert((M)->initialized != 2),                         \
-        assert((M)->initialized != 0),                         \
-        assert((M)->initialized == 1),                         \
-        assert((M)->locked != 0),                              \
-        assert((M)->locked != K5_MUTEX_DEBUG_LOCKED),          \
-        assert((M)->locked == K5_MUTEX_DEBUG_UNLOCKED),        \
-        (M)->locked = K5_MUTEX_DEBUG_LOCKED,                   \
-        (M)->lineno = __LINE__, (M)->filename = __FILE__, 0)
+       (k5_mutex_debug_check_init(M),                          \
+        k5_mutex_debug_update_loc(M), 0)
 #define k5_mutex_debug_unlock(M)                               \
-       (assert((M)->initialized == 1                           \
-               && (M)->locked == K5_MUTEX_DEBUG_LOCKED),       \
-        (M)->locked = K5_MUTEX_DEBUG_UNLOCKED,                 \
-        (M)->lineno = __LINE__, (M)->filename = __FILE__, 0)
-
+       (k5_mutex_debug_check_init(M),                          \
+        k5_mutex_debug_update_loc(M), 0)
 
 typedef enum {
     K5_KEY_COM_ERR,
@@ -217,10 +213,18 @@ typedef struct {
                                 0)
 #define k5_mutex_destroy(M)    (k5_mutex_debug_init(&(M)->debug),            \
                                 assert(0==pthread_mutex_destroy(&(M)->lock)))
-#define k5_mutex_lock(M)       (k5_mutex_debug_lock(&(M)->debug),          \
+#define k5_mutex_lock(M)       (k5_mutex_debug_check_init(&(M)->debug),    \
                                 assert(0==pthread_mutex_lock(&(M)->lock)), \
+                                assert((M)->debug.locked                   \
+                                       == K5_MUTEX_DEBUG_UNLOCKED),        \
+                                k5_mutex_debug_update_loc(&(M)->debug),    \
+                                (M)->debug.locked = K5_MUTEX_DEBUG_LOCKED, \
                                 0)
-#define k5_mutex_unlock(M)     (k5_mutex_debug_unlock(&(M)->debug),          \
+#define k5_mutex_unlock(M)     (k5_mutex_debug_check_init(&(M)->debug),      \
+                                assert((M)->debug.locked                     \
+                                       == K5_MUTEX_DEBUG_LOCKED),            \
+                                k5_mutex_debug_update_loc(&(M)->debug),      \
+                                (M)->debug.locked = K5_MUTEX_DEBUG_UNLOCKED, \
                                 assert(0==pthread_mutex_unlock(&(M)->lock)), \
                                 0)