RESOURCE_RESET and RESOURCE_RESETALL
authorChristian Thaeter <ct@pipapo.org>
Wed, 14 Jul 2010 02:18:09 +0000 (04:18 +0200)
committerChristian Thaeter <ct@pipapo.org>
Fri, 16 Jul 2010 04:08:07 +0000 (06:08 +0200)
brings resources back into a pristine state forgetting anything
learned so far.

src/nobug.h
src/nobug_resources.c

index e3760fe..8266b8b 100644 (file)
@@ -1292,6 +1292,70 @@ NOTREACHED      abort           abort           nothing
    ))
 
 
+/*
+//resourcemacros PARA RESOURCE_RESETALL; RESOURCE_RESETALL; reset the resource tracker to a pristine state
+//resourcemacros  RESOURCE_RESETALL(flag)
+//resourcemacros  NOBUG_RESOURCE_RESETALL_RAW(flagptr)
+//resourcemacros  NOBUG_RESOURCE_RESETALL_RAW_CTX(flagptr, context)
+//resourcemacros
+//resourcemacros Sometimes the resource tracker can give false positives when it finds a locking order violation
+//resourcemacros while the programmer knows that this will never happen in the real program, because for example
+//resourcemacros this is only used at initialization or shutdown and never overlaps. This macro can then be used
+//resourcemacros to reset all whats learnt about all resources and start over.
+//resourcemacros
+//resourcemacros `flag`::
+//resourcemacros     the NoBug flag which turns logging on for this macro
+//resourcemacros
+*/
+#define NOBUG_RESOURCE_RESETALL(flag, handle) \
+  NOBUG_RESOURCE_RESETALL_RAW_CTX(&NOBUG_FLAG(flag), NOBUG_CONTEXT)
+
+#define NOBUG_RESOURCE_RESETALL_RAW(flag, handle) \
+  NOBUG_RESOURCE_RESETALL_RAW_CTX(flag, NOBUG_CONTEXT)
+
+#define NOBUG_RESOURCE_RESETALL_RAW_CTX(flag, context)                                  \
+  NOBUG_IF_ALPHA (                                                                      \
+                  NOBUG_RESOURCE_ASSERT_CTX(nobug_resource_resetall (),                 \
+                                            "RESOURCE_ASSERT_RESETALL", context, "%s",  \
+                                            nobug_resource_error))
+
+
+
+/*
+//resourcemacros PARA RESOURCE_RESET; RESOURCE_RESET; reset a single resource to a pristine state
+//resourcemacros  RESOURCE_RESET(flag, handle)
+//resourcemacros  NOBUG_RESOURCE_RESETALL_RAW(flagptr, handle)
+//resourcemacros  NOBUG_RESOURCE_RESETALL_RAW_CTX(flagptr, handle, context)
+//resourcemacros
+//resourcemacros Sometimes the resource tracker can give false positives when it finds a locking order violation
+//resourcemacros while the programmer knows that this will never happen in the real program, because for example
+//resourcemacros this is only used at initialization or shutdown and never overlaps. This macro can then be used
+//resourcemacros to reset all whats learnt about a single resources and start over.
+//resourcemacros
+//resourcemacros `flag`::
+//resourcemacros     the NoBug flag which turns logging on for this macro
+//resourcemacros `handle`::
+//resourcemacros     the `NOBUG_RESOURCE_HANDLE` used to track this resource
+//resourcemacros
+*/
+#define NOBUG_RESOURCE_RESET(flag, handle) \
+  NOBUG_RESOURCE_RESET_RAW_CTX(&NOBUG_FLAG(flag), handle, NOBUG_CONTEXT)
+
+#define NOBUG_RESOURCE_RESET_RAW(flag, handle) \
+  NOBUG_RESOURCE_RESET_RAW_CTX(flag, handle, NOBUG_CONTEXT)
+
+#define NOBUG_RESOURCE_RESET_RAW_CTX(flag, handle, context)                             \
+  NOBUG_IF_ALPHA (                                                                      \
+                  NOBUG_RESOURCE_ASSERT_CTX(nobug_resource_reset (handle),              \
+                                            "RESOURCE_ASSERT_RESET", context,           \
+                                            "%s: %s@%p: %s",                            \
+                                            (handle)?(handle)->type:"",                 \
+                                            (handle)?(handle)->hdr.name:"",             \
+                                            (handle)?(handle)->object_id:NULL,          \
+                                            nobug_resource_error))
+
+
+
 /*
 //resourcemacros PARA RESOURCE_ENTER; RESOURCE_ENTER; claim a resource
 //resourcemacros  RESOURCE_ENTER(flag, announced, user, state, handle)
@@ -1973,6 +2037,12 @@ NOBUG_IFNOT(NOBUG_USE_VALGRIND, 0)
 #ifndef RESOURCE_FORGET
 #define RESOURCE_FORGET NOBUG_RESOURCE_FORGET
 #endif
+#ifndef RESOURCE_RESETALL
+#define RESOURCE_RESETALL NOBUG_RESOURCE_RESETALL
+#endif
+#ifndef RESOURCE_RESET
+#define RESOURCE_RESET NOBUG_RESOURCE_RESET
+#endif
 #ifndef RESOURCE_ENTER
 #define RESOURCE_ENTER NOBUG_RESOURCE_ENTER
 #endif
@@ -2443,6 +2513,14 @@ nobug_resource_node_available (void);
 #endif
 
 
+int
+nobug_resource_reset (struct nobug_resource_record* self);
+
+
+int
+nobug_resource_resetall (void);
+
+
 struct nobug_resource_dump_context
 {
   struct nobug_flag* flag;
index 9e4dd27..489f901 100644 (file)
@@ -291,6 +291,60 @@ nobug_resource_forget (struct nobug_resource_record* self)
 
 
 
+int
+nobug_resource_reset (struct nobug_resource_record* self)
+{
+  (void) self;
+#if NOBUG_USE_PTHREAD
+  pthread_mutex_lock (&nobug_resource_mutex);
+
+  if (!llist_find (&nobug_resource_registry, &self->hdr.node, compare_resource_records, NULL))
+    {
+      nobug_resource_error = "not registered";
+      return 0;
+    }
+
+  /*
+  if (!llist_is_empty (&self->users))
+    {
+      nobug_resource_error = "still in use";
+      return 0;
+    }
+  */
+
+  /* destroy all nodes recursively */
+  LLIST_WHILE_HEAD (&self->nodes, n)
+    nobug_resource_node_free ((struct nobug_resource_node*)n);
+
+  pthread_mutex_unlock (&nobug_resource_mutex);
+#endif
+
+  return 1;
+}
+
+
+
+int
+nobug_resource_resetall (void)
+{
+#if NOBUG_USE_PTHREAD
+  pthread_mutex_lock (&nobug_resource_mutex);
+
+  LLIST_FOREACH (&nobug_resource_registry, r)
+    {
+      struct nobug_resource_record* resource = (struct nobug_resource_record*) r;
+      LLIST_WHILE_HEAD (&resource->nodes, n)
+        nobug_resource_node_free ((struct nobug_resource_node*)n);
+    }
+
+  pthread_mutex_unlock (&nobug_resource_mutex);
+#endif
+
+  return 1;
+}
+
+
+
 #if NOBUG_USE_PTHREAD
 static int
 nobug_resource_node_resource_cmpfn (const_LList a, const_LList b, void* extra)