[PPL-devel] [GIT] ppl/ppl(master): Weightwatch are now independent from client code.

Abramo Bagnara abramo.bagnara at gmail.com
Sat Jul 11 14:07:52 CEST 2009


Module: ppl/ppl
Branch: master
Commit: a2845d7717669addb44c47793cd92446b51751a0
URL:    http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=a2845d7717669addb44c47793cd92446b51751a0

Author: Abramo Bagnara <abramo.bagnara at gmail.com>
Date:   Sat Jul 11 14:07:36 2009 +0200

Weightwatch are now independent from client code.

---

 Watchdog/src/Weightwatch.cc         |   64 ++++++++++++++++++++++++++--------
 Watchdog/src/Weightwatch.defs.hh    |   21 ++++++++---
 Watchdog/src/Weightwatch.inlines.hh |   22 ------------
 3 files changed, 64 insertions(+), 43 deletions(-)

diff --git a/Watchdog/src/Weightwatch.cc b/Watchdog/src/Weightwatch.cc
index 1da5575..7dae6c9 100644
--- a/Watchdog/src/Weightwatch.cc
+++ b/Watchdog/src/Weightwatch.cc
@@ -29,34 +29,68 @@ site: http://www.cs.unipr.it/ppl/ . */
 #include <string>
 #include <string.h>
 
-namespace PWL = Parma_Watchdog_Library;
+namespace Parma_Watchdog_Library {
 
 // The ordered queue of pending weight thresholds.
-PWL::Weightwatch::WW_Pending_List PWL::Weightwatch::pending;
+Weightwatch::WW_Pending_List Weightwatch::pending;
 
-PWL::Weight PWL::Weightwatch::weight_so_far = 0;
+Weight* Weightwatch::current_weight_ptr = 0;
+void (**Weightwatch::check_hook_ptr)(void) = 0;
 
-PWL::Weightwatch::WW_Pending_List::Iterator
-PWL::Weightwatch::new_weight_threshold(int units,
+#ifndef NDEBUG
+Weight Weightwatch::previous_weight = 0;
+#endif
+
+Weightwatch::WW_Pending_List::Iterator
+Weightwatch::new_weight_threshold(int units,
 				       const Handler& handler,
 				       bool& expired_flag) {
   assert(units > 0);
-  if (weight_so_far == 0)
-    weight_so_far = 1;
-  return pending.insert(weight_so_far + units, handler, expired_flag);
+  assert(current_weight_ptr);
+  *check_hook_ptr = Weightwatch::check;
+  return pending.insert(*current_weight_ptr + units, handler, expired_flag);
 }
 
-void
-PWL::Weightwatch::remove_weight_threshold(WW_Pending_List::Iterator position) {
-  pending.erase(position);
+Weightwatch::WW_Pending_List::Iterator
+Weightwatch::remove_weight_threshold(WW_Pending_List::Iterator position) {
+  Weightwatch::WW_Pending_List::Iterator i =  pending.erase(position);
   if (pending.empty())
-    weight_so_far = 0;
+    *check_hook_ptr = 0;
+  return i;
 }
 
-PWL::Weightwatch::~Weightwatch() {
-  if (!expired) {
+Weightwatch::~Weightwatch() {
+  if (!expired)
     remove_weight_threshold(pending_position);
-  }
   delete &handler;
 }
 
+void
+Weightwatch::check() {
+  assert(current_weight_ptr);
+  WW_Pending_List::Iterator i = pending.begin();
+  assert(i != pending.end());
+  assert(*current_weight_ptr - previous_weight < 1U << (sizeof(Weight)*8-1));
+#ifndef NDEBUG
+  previous_weight = *current_weight_ptr;
+#endif
+  while (*current_weight_ptr - i->deadline() < 1U << (sizeof(Weight)*8-1)) {
+    i->handler().act();
+    i->expired_flag() = true;
+    i = remove_weight_threshold(i);
+    if (i == pending.end())
+      break;
+  }
+}
+
+void
+Weightwatch::initialize(Weight& current_weight, void (*&check_hook)(void)) {
+  current_weight_ptr = &current_weight;
+  check_hook_ptr = &check_hook;
+  check_hook = 0;
+#ifndef NDEBUG
+  previous_weight = current_weight;
+#endif
+}
+
+}
diff --git a/Watchdog/src/Weightwatch.defs.hh b/Watchdog/src/Weightwatch.defs.hh
index dfa4c04..f22de5d 100644
--- a/Watchdog/src/Weightwatch.defs.hh
+++ b/Watchdog/src/Weightwatch.defs.hh
@@ -30,7 +30,7 @@ site: http://www.cs.unipr.it/ppl/ . */
 
 namespace Parma_Watchdog_Library {
 
-typedef unsigned long long Weight;
+typedef unsigned int Weight;
 
 //! A watchdog for computational weight.
 class Weightwatch {
@@ -40,8 +40,7 @@ public:
 
   Weightwatch(int units, void (*function)());
   ~Weightwatch();
-  static void add(unsigned int units, unsigned int iterations);
-  static void check();
+  static void initialize(Weight& current_weight, void (*&check_hook)(void));
 
 private:
   typedef Pending_List<Weight> WW_Pending_List;
@@ -58,8 +57,16 @@ private:
   //! The ordered queue of pending weight thresholds.
   static WW_Pending_List pending;
 
-  //! Current weight.
-  static Weight weight_so_far;
+  //! Pointer to current weight.
+  static Weight *current_weight_ptr;
+
+  //! Pointer to check hook.
+  static void (**check_hook_ptr)(void);
+
+#ifndef NDEBUG
+  //! Weight at previous check.
+  static Weight previous_weight;
+#endif
 
   // Handle the addition of a new weight threshold.
   static WW_Pending_List::Iterator new_weight_threshold(int units,
@@ -67,8 +74,10 @@ private:
 						      bool& expired_flag);
 
   // Handle the removal of a weight threshold.
-  void remove_weight_threshold(WW_Pending_List::Iterator position);
+  static WW_Pending_List::Iterator remove_weight_threshold(WW_Pending_List::Iterator position);
 
+  //! Check weight threshold reaching.
+  static void check();
 };
 
 } // namespace Parma_Watchdog_Library
diff --git a/Watchdog/src/Weightwatch.inlines.hh b/Watchdog/src/Weightwatch.inlines.hh
index 8102799..e792449 100644
--- a/Watchdog/src/Weightwatch.inlines.hh
+++ b/Watchdog/src/Weightwatch.inlines.hh
@@ -48,28 +48,6 @@ Weightwatch::Weightwatch(int units, void (*function)())
   pending_position = new_weight_threshold(units, handler, expired);
 }
 
-inline void
-Weightwatch::add(unsigned int units, unsigned int iterations) {
-  if (weight_so_far == 0)
-    return;
-  weight_so_far += (Weight)units * iterations;
-}
-
-inline void
-Weightwatch::check() {
-  if (weight_so_far == 0)
-    return;
-  WW_Pending_List::Iterator i = pending.begin();
-  assert(i != pending.end());
-  while (weight_so_far >= i->deadline()) {
-    i->handler().act();
-    i->expired_flag() = true;
-    i = pending.erase(i);
-    if (i == pending.end())
-      weight_so_far = 0;
-  }
-}
-
 } // namespace Parma_Watchdog_Library
 
 #endif // !defined(PWL_Weightwatch_inlines_hh)




More information about the PPL-devel mailing list