Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59203 - sandbox/chrono/boost/chrono
From: vicente.botet_at_[hidden]
Date: 2010-01-21 15:46:58


Author: viboes
Date: 2010-01-21 15:46:57 EST (Thu, 21 Jan 2010)
New Revision: 59203
URL: http://svn.boost.org/trac/boost/changeset/59203

Log:
Boost.Chrono: Version 0.3.2, Bug fixes
* Bug using duration(0), should use duration::zero().
* Suspend didn't works as partial_ not initialized and elapsed() didn't take care of partial_.
* Bad use of system::error_code & ec; in stopwatch_suspender.
* Added suspendable_clock.hpp
* Use of scoped_suspend stopwatch_reporter to avoid counting while reporting.

Added:
   sandbox/chrono/boost/chrono/suspendable_clock.hpp (contents, props changed)
   sandbox/chrono/boost/chrono/thread_clock.hpp (contents, props changed)
Text files modified:
   sandbox/chrono/boost/chrono/digital_time_formatter.hpp | 3 +--
   sandbox/chrono/boost/chrono/stopwatch.hpp | 23 +++++++++++++++++++----
   sandbox/chrono/boost/chrono/stopwatch_accumulator.hpp | 5 ++++-
   sandbox/chrono/boost/chrono/stopwatch_accumulator_formatter.hpp | 3 +--
   sandbox/chrono/boost/chrono/stopwatch_formatter.hpp | 3 +--
   sandbox/chrono/boost/chrono/stopwatch_reporter.hpp | 2 ++
   sandbox/chrono/boost/chrono/stopwatch_scoped.hpp | 4 ++--
   7 files changed, 30 insertions(+), 13 deletions(-)

Modified: sandbox/chrono/boost/chrono/digital_time_formatter.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/digital_time_formatter.hpp (original)
+++ sandbox/chrono/boost/chrono/digital_time_formatter.hpp 2010-01-21 15:46:57 EST (Thu, 21 Jan 2010)
@@ -65,9 +65,8 @@
         {
             typedef typename Stopwatch::duration duration_t;
             duration_t d = stopwatch_.elapsed( ec );
- duration_t d0((0));
 
- if ( d < d0 ) return;
+ if ( d < duration_t::zero() ) return;
 
             boost::io::ios_flags_saver ifs( os );
             os.setf( std::ios_base::fixed, std::ios_base::floatfield );

Modified: sandbox/chrono/boost/chrono/stopwatch.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch.hpp 2010-01-21 15:46:57 EST (Thu, 21 Jan 2010)
@@ -56,16 +56,23 @@
         typedef Clock clock;
         typedef typename Clock::duration duration;
         typedef typename Clock::time_point time_point;
+ typedef typename Clock::rep rep;
+ typedef typename Clock::period period;
 
         explicit stopwatch( system::error_code & ec = system::throws )
- : running_(false), start_(), level_(0)
+ : running_(false), suspended_(false), start_(), level_(0), partial_(duration::zero()), suspend_level_(0)
         {
+ //duration d0((0));
+ //partial_=d0;
             start(ec);
         }
 
         explicit stopwatch( const dont_start_t& t )
- : running_(false), start_(), level_(0)
- { }
+ : running_(false), suspended_(false), start_(), level_(0), partial_(duration::zero()), suspend_level_(0)
+ {
+ //duration d0((0));
+ //partial_=d0;
+ }
 
         time_point start( system::error_code & ec = system::throws ) {
             ++level_;
@@ -141,7 +148,15 @@
 
         duration elapsed( system::error_code & ec = system::throws )
         {
- return clock::now( ec ) - start_;
+ if (running_)
+ return clock::now( ec ) - start_ + partial_;
+ else
+ return partial_;
+ }
+
+ time_point now( system::error_code & ec = system::throws )
+ {
+ return time_point(elapsed( ec ));
         }
 
         void reset( system::error_code & ec = system::throws ) {

Modified: sandbox/chrono/boost/chrono/stopwatch_accumulator.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch_accumulator.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch_accumulator.hpp 2010-01-21 15:46:57 EST (Thu, 21 Jan 2010)
@@ -74,8 +74,11 @@
         typedef Accumulator accumulator;
 
         stopwatch_accumulator( )
- : running_(false), partial_(0)
+ : running_(false), suspended_(false), accumulated_(),
+ partial_(), start_(duration::zero()), level_(0), suspend_level_(0)
         {
+ //duration d0((0));
+ //partial_=d0;
         }
 
         std::pair<duration, time_point> restart( system::error_code & ec = system::throws ) {

Modified: sandbox/chrono/boost/chrono/stopwatch_accumulator_formatter.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch_accumulator_formatter.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch_accumulator_formatter.hpp 2010-01-21 15:46:57 EST (Thu, 21 Jan 2010)
@@ -68,9 +68,8 @@
             typedef typename Stopwatch::accumulator accumulator;
             typedef typename Stopwatch::duration duration_t;
             accumulator& acc = stopwatch_.accumulated();
- //duration_t d0((0));
 
- //if ( d < d0 ) return;
+ //if ( d < duration_t::zero() ) return;
             if ( places > 9 )
                 places = 9; // sanity check
             else if ( places < 0 )

Modified: sandbox/chrono/boost/chrono/stopwatch_formatter.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch_formatter.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch_formatter.hpp 2010-01-21 15:46:57 EST (Thu, 21 Jan 2010)
@@ -65,9 +65,8 @@
         {
             typedef typename Stopwatch::duration duration_t;
             duration_t d = stopwatch_.elapsed( ec );
- duration_t d0((0));
 
- if ( d < d0 ) return;
+ if ( d < duration_t::zero() ) return;
             if ( places > 9 )
                 places = 9; // sanity check
             else if ( places < 0 )

Modified: sandbox/chrono/boost/chrono/stopwatch_reporter.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch_reporter.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch_reporter.hpp 2010-01-21 15:46:57 EST (Thu, 21 Jan 2010)
@@ -13,6 +13,7 @@
 #include <boost/chrono/chrono.hpp>
 #include <boost/chrono/stopwatch_scoped.hpp>
 #include <boost/system/error_code.hpp>
+#include <boost/chrono/suspendable_clock.hpp>
 #include <boost/cstdint.hpp>
 #include <string>
 #include <iostream>
@@ -158,6 +159,7 @@
 
     template <class Stopwatch, class Formatter>
     void stopwatch_reporter<Stopwatch, Formatter>::report( system::error_code & ec ) {
+ chrono::scoped_suspend<typename Stopwatch::clock> _(ec);
         m_reported = true;
         if ( m_format.empty() ) m_format = Formatter::default_format();
 

Modified: sandbox/chrono/boost/chrono/stopwatch_scoped.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch_scoped.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch_scoped.hpp 2010-01-21 15:46:57 EST (Thu, 21 Jan 2010)
@@ -55,7 +55,7 @@
             stopwatch_.suspend(ec);
         }
         ~stopwatch_suspender() {
- system::error_code & ec;
+ system::error_code ec;
             stopwatch_.resume(ec);
         }
     private:
@@ -74,7 +74,7 @@
             stopwatch_.resume(ec);
         }
         ~stopwatch_resumer() {
- system::error_code & ec;
+ system::error_code ec;
             stopwatch_.suspend(ec);
         }
     private:

Added: sandbox/chrono/boost/chrono/suspendable_clock.hpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/boost/chrono/suspendable_clock.hpp 2010-01-21 15:46:57 EST (Thu, 21 Jan 2010)
@@ -0,0 +1,148 @@
+// boost/chrono/process_cpu_clocks.hpp -----------------------------------------------------------//
+
+// Copyright 2009-2010 Vicente J. Botet Escriba
+
+// Distributed under the Boost Software License, Version 1.0.
+// See http://www.boost.org/LICENSE_1_0.txt
+
+// See http://www.boost.org/libs/system for documentation.
+
+#ifndef BOOST_CHRONO_SUSPENDABLE_CLOCK_HPP
+#define BOOST_CHRONO_SUSPENDABLE_CLOCK_HPP
+
+#include <boost/chrono/chrono.hpp>
+#include <boost/system/error_code.hpp>
+#include <boost/thread/tss.hpp>
+#include <memory>
+
+#include <boost/config/abi_prefix.hpp> // must be the last #include
+
+namespace boost { namespace chrono {
+
+ template < class Clock >
+ class suspendable_clock {
+ public:
+ typedef typename Clock::duration duration;
+ typedef typename Clock::rep rep;
+ typedef typename Clock::period period;
+ typedef chrono::time_point<suspendable_clock<Clock> > time_point;
+ static const bool is_monotonic = true;
+
+ private:
+ struct thread_specific_context {
+ bool suspended_;
+ time_point suspended_time_;
+ duration suspended_duration_;
+ std::size_t suspend_level_;
+
+ void suspend( system::error_code & ec = system::throws ) {
+ if (!suspended_) {
+ time_point tmp=Clock::now( ec );
+ if (ec) return;
+ ++suspend_level_;
+ suspended_time_ = tmp;
+ suspended_=true;
+ } else {
+ ++suspend_level_;
+ }
+ }
+ void resume( system::error_code & ec = system::throws ) {
+ if (suspended_&&(--suspend_level_==0)) {
+ time_point tmp = Clock::now( ec );
+ if (ec) return;
+ suspended_duration_ += tmp - suspended_time_;
+ suspended_=false;
+ }
+ }
+
+ };
+ static thread_specific_context* instance(system::error_code & ec) {
+ thread_specific_context* ptr= ptr_.get();
+ if (ptr==0) {
+ if (ec == system::throws) {
+ std::auto_ptr<thread_specific_context> ptr2(new thread_specific_context());
+ ptr_.reset(ptr2.get());
+ ptr = ptr2.release();
+ } else {
+ ptr=(new(std::nothrow) thread_specific_context());
+ if (ptr==0) {
+ //ec=...
+ return 0;
+ }
+ try {
+ ptr_.reset(ptr);
+ } catch (...) {
+ //ec=...
+ return 0;
+ }
+ }
+ }
+ return ptr;
+ }
+ duration suspended(system::error_code & ec = system::throws)
+ {
+ thread_specific_context* ptr= instance(ec);
+ if (ptr!=0) return ptr->suspended_duration_;
+ else return duration(0);
+ }
+
+ static thread_specific_ptr<thread_specific_context> ptr_;
+ public:
+ static time_point now( system::error_code & ec = system::throws ) {
+ return Clock::now(ec)-suspended(ec);
+ }
+
+ static void suspend( system::error_code & ec = system::throws ) {
+ thread_specific_context* ptr= instance(ec);
+ if (ptr!=0) ptr->suspend(ec);
+ }
+ void resume( system::error_code & ec = system::throws ) {
+ thread_specific_context* ptr= instance(ec);
+ if (ptr!=0) ptr->resume(ec);
+ }
+ class scoped_suspend {
+ public:
+ scoped_suspend(system::error_code & ec = system::throws)
+ : ptr_(instance(ec))
+ {
+ ptr_->suspend(ec);
+ }
+ ~scoped_suspend() {
+ system::error_code ec;
+ ptr_->resume(ec);
+ }
+ private:
+ thread_specific_context* ptr_;
+ scoped_suspend(const scoped_suspend&); // = delete;
+ scoped_suspend& operator=(const scoped_suspend&); // = delete;
+ };
+
+ };
+
+ template <class Clock>
+ struct is_suspendable : mpl:: false_ {};
+
+ template <class Clock>
+ struct is_suspendable<suspendable_clock<Clock> > : mpl:: true_ {};
+
+
+ template <class Clock>
+ class scoped_suspend {
+ public:
+ scoped_suspend(system::error_code & ec = system::throws) {}
+ ~scoped_suspend() {}
+ };
+
+ template <class Clock>
+ class scoped_suspend<suspendable_clock<Clock> >
+ : public suspendable_clock<Clock>::scoped_suspend {
+ public:
+ scoped_suspend(system::error_code & ec = system::throws) : Clock::scoped_suspend(ec) {}
+ };
+
+} // namespace chrono
+} // namespace boost
+
+#include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
+
+#endif // BOOST_CHRONO_PROCESS_CPU_CLOCKS_HPP

Added: sandbox/chrono/boost/chrono/thread_clock.hpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/boost/chrono/thread_clock.hpp 2010-01-21 15:46:57 EST (Thu, 21 Jan 2010)
@@ -0,0 +1,37 @@
+// boost/chrono/process_cpu_clocks.hpp -----------------------------------------------------------//
+
+// Copyright 2009-2010 Vicente J. Botet Escriba
+
+// Distributed under the Boost Software License, Version 1.0.
+// See http://www.boost.org/LICENSE_1_0.txt
+
+// See http://www.boost.org/libs/system for documentation.
+
+#ifndef BOOST_CHRONO_THREAD_CLOCK_HPP
+#define BOOST_CHRONO_THREAD_CLOCK_HPP
+
+#include <boost/chrono/chrono.hpp>
+#include <boost/system/error_code.hpp>
+#include <boost/operators.hpp>
+
+#include <boost/config/abi_prefix.hpp> // must be the last #include
+
+namespace boost { namespace chrono {
+
+ class BOOST_CHRONO_DECL thread_clock {
+ public:
+ typedef nanoseconds duration;
+ typedef duration::rep rep;
+ typedef duration::period period;
+ typedef chrono::time_point<process_real_cpu_clock> time_point;
+ static const bool is_monotonic = true;
+
+ static time_point now( system::error_code & ec = system::throws );
+ };
+
+} // namespace chrono
+} // namespace boost
+
+#include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
+
+#endif // BOOST_CHRONO_PROCESS_CPU_CLOCKS_HPP


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk