[PPL-devel] [GIT] ppl/ppl(master): Added Weightwatch_Traits class (from Abramo). Modified maybe_abandon().

Enea Zaffanella zaffanella at cs.unipr.it
Sun Jul 12 09:47:03 CEST 2009


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

Author: Enea Zaffanella <zaffanella at cs.unipr.it>
Date:   Sun Jul 12 09:14:52 2009 +0200

Added Weightwatch_Traits class (from Abramo). Modified maybe_abandon().

---

 src/globals.cc         |    3 +++
 src/globals.defs.hh    |   38 ++++++++++++++++++++++++++++++++++++++
 src/globals.inlines.hh |   19 +++++++++++++++++++
 3 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/src/globals.cc b/src/globals.cc
index 9325c4e..7511d17 100644
--- a/src/globals.cc
+++ b/src/globals.cc
@@ -30,3 +30,6 @@ namespace PPL = Parma_Polyhedra_Library;
 
 const PPL::Throwable* volatile PPL::abandon_expensive_computations = 0;
 
+// Initialize Weightwatch_Traits static data members.
+PPL::Weightwatch_Traits::Threshold PPL::Weightwatch_Traits::weight = 0;
+void (*PPL::Weightwatch_Traits::check_function)(void) = 0;
diff --git a/src/globals.defs.hh b/src/globals.defs.hh
index a6e943c..6bfe173 100644
--- a/src/globals.defs.hh
+++ b/src/globals.defs.hh
@@ -84,6 +84,44 @@ dimension_type
 compute_capacity(dimension_type requested_size,
 		 dimension_type maximum_size);
 
+
+#ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
+//! Traits class for the deterministic timeout mechanism.
+/*! \ingroup PPL_CXX_interface
+  This abstract base class should be instantiated by those users
+  willing to provide a polynomial upper bound to the time spent
+  by any invocation of a library operator.
+*/
+#endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
+struct Weightwatch_Traits {
+  //! The type used to specify thresholds for computational weight.
+  typedef unsigned int Threshold;
+
+  //! The type used to specify increments of computational weight.
+  typedef unsigned int Delta;
+
+  //! Returns the current computational weight.
+  static const Threshold& get();
+
+  //! Compares the two weights \p a and \p b.
+  static bool less_than(const Threshold& a, const Threshold& b);
+
+  //! Sets \p threshold to be \p delta units bigger than the current weigth.
+  static void from_delta(Threshold& threshold, const Delta& delta);
+
+  //! The current computational weight.
+  static Threshold weight;
+
+  /*! \brief
+    A pointer to the function that has to be called when checking
+    the reaching of thresholds.
+
+    The pointer can be null if no threshold are set.
+  */
+  static void (*check_function)(void);
+};
+
+
 //! User objects the PPL can throw.
 /*! \ingroup PPL_CXX_interface
   This abstract base class should be instantiated by those users
diff --git a/src/globals.inlines.hh b/src/globals.inlines.hh
index f35672a..75d104e 100644
--- a/src/globals.inlines.hh
+++ b/src/globals.inlines.hh
@@ -33,12 +33,31 @@ not_a_dimension() {
   return std::numeric_limits<dimension_type>::max();
 }
 
+inline const Weightwatch_Traits::Threshold&
+Weightwatch_Traits::get() {
+  return weight;
+}
+
+inline bool
+Weightwatch_Traits::less_than(const Threshold& a, const Threshold& b) {
+  return b - a < 1U << (sizeof(Threshold)*8-1);
+}
+
+inline void
+Weightwatch_Traits::from_delta(Threshold& threshold, const Delta& delta) {
+  threshold = weight + delta;
+}
+
 inline
 Throwable::~Throwable() {
 }
 
 inline void
 maybe_abandon() {
+  if (Weightwatch_Traits::check_function) {
+    ++Weightwatch_Traits::weight;
+    Weightwatch_Traits::check_function();
+  }
   if (const Throwable* p = abandon_expensive_computations)
     p->throw_me();
 }




More information about the PPL-devel mailing list