]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- use C++11 chrono::steady_clock 144/head
authorArvin Schnell <aschnell@suse.de>
Tue, 27 Jan 2015 11:34:16 +0000 (12:34 +0100)
committerArvin Schnell <aschnell@suse.de>
Tue, 27 Jan 2015 11:34:16 +0000 (12:34 +0100)
dbus/DBusMainLoop.cc
dbus/DBusMainLoop.h
package/snapper.changes
server/Client.cc
server/MetaSnapper.cc
server/MetaSnapper.h
server/snapperd.cc
snapper/AppUtil.cc
snapper/AppUtil.h

index 08c0eb492a2b6361f4761c6ccafa7c18acffa9a0..67d4f0b7f6a17705c8245ac85470ea44b45d8b7d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) [2012-2014] Novell, Inc.
+ * Copyright (c) [2012-2015] Novell, Inc.
  *
  * All Rights Reserved.
  *
@@ -22,7 +22,6 @@
 
 #include <unistd.h>
 #include <poll.h>
-#include <time.h>
 
 #include "DBusMainLoop.h"
 
@@ -106,17 +105,16 @@ namespace DBus
                }
            }
 
-           int timeout = periodic_timeout();
+           milliseconds timeout = periodic_timeout();
 
-           if (idle_timeout >= 0)
+           if (idle_timeout.count() >= 0)
            {
-               int time_left = last_action - monotonic_clock() + idle_timeout;
-
-               if (timeout > time_left * 1000 || timeout == -1)
-                   timeout = time_left * 1000;
+               steady_clock::duration time_left = idle_for() + idle_timeout;
+               if (timeout > time_left || timeout.count() < 0)
+                   timeout = duration_cast<milliseconds>(time_left);
            }
 
-           int r = poll(&pollfds[0], pollfds.size(), timeout);
+           int r = poll(&pollfds[0], pollfds.size(), timeout.count());
            if (r == -1)
                throw FatalException();
 
@@ -165,11 +163,10 @@ namespace DBus
                dispatch_incoming(msg);
            }
 
-           if (idle_timeout >= 0)
+           if (idle_timeout.count() >= 0)
            {
-               int time_left = last_action - monotonic_clock() + idle_timeout;
-
-               if (time_left <= 0)
+               steady_clock::duration time_left = idle_for() + idle_timeout;
+               if (time_left.count() <= 0)
                    break;
            }
        }
@@ -177,16 +174,23 @@ namespace DBus
 
 
     void
-    MainLoop::set_idle_timeout(int s)
+    MainLoop::set_idle_timeout(milliseconds idle_timeout)
     {
-       idle_timeout = s;
+       MainLoop::idle_timeout = idle_timeout;
     }
 
 
     void
     MainLoop::reset_idle_count()
     {
-       last_action = monotonic_clock();
+       last_action = steady_clock::now();
+    }
+
+
+    milliseconds
+    MainLoop::idle_for() const
+    {
+       return duration_cast<milliseconds>(last_action - steady_clock::now());
     }
 
 
@@ -341,13 +345,4 @@ namespace DBus
        }
     }
 
-
-    time_t
-    DBus::MainLoop::monotonic_clock()
-    {
-       struct timespec tmp;
-       clock_gettime(CLOCK_MONOTONIC, &tmp);
-       return tmp.tv_sec;
-    }
-
 }
index d170de58c329fa042bac0d965808df3867578939..15786578c8bae7b80e8026e919c460ce45c2c000 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) [2012-2014] Novell, Inc.
+ * Copyright (c) [2012-2015] Novell, Inc.
  *
  * All Rights Reserved.
  *
@@ -25,6 +25,7 @@
 
 
 #include <dbus/dbus.h>
+#include <chrono>
 
 #include "DBusConnection.h"
 
@@ -32,6 +33,9 @@
 namespace DBus
 {
 
+    using namespace std::chrono;
+
+
     class MainLoop : public Connection
     {
     public:
@@ -41,7 +45,7 @@ namespace DBus
 
        void run();
 
-       void set_idle_timeout(int s);
+       void set_idle_timeout(milliseconds idle_timeout);
        void reset_idle_count();
 
        void add_client_match(const string& name);
@@ -50,7 +54,7 @@ namespace DBus
        virtual void method_call(Message& message) = 0;
        virtual void signal(Message& message) = 0;
        virtual void client_disconnected(const string& name) = 0;
-       virtual int periodic_timeout() = 0;
+       virtual milliseconds periodic_timeout() = 0;
        virtual void periodic() = 0;
 
     private:
@@ -94,10 +98,10 @@ namespace DBus
 
        void dispatch_incoming(Message& message);
 
-       int idle_timeout;
-       time_t last_action;
+       milliseconds idle_for() const;
 
-       static time_t monotonic_clock();
+       milliseconds idle_timeout;
+       steady_clock::time_point last_action;
 
     };
 
index 42472c84cd17ffe289eccaf17b25ed745eeec8d9..dd5f5180734592dcae44f0ef4f79bd7d4abeca7e 100644 (file)
@@ -1,3 +1,8 @@
+-------------------------------------------------------------------
+Tue Jan 27 12:31:45 CET 2015 - aschnell@suse.de
+
+- use C++11 chrono::steady_clock
+
 -------------------------------------------------------------------
 Mon Jan 19 12:42:13 CET 2015 - aschnell@suse.de
 
index 8521a444a0ac501f98f1e6d912185da11f68fa04..1c1bfc41e84cb492c8e70505612a992ae94bd7e7 100644 (file)
@@ -1352,7 +1352,7 @@ Client::debug(DBus::Connection& conn, DBus::Message& msg) const
        {
            s << ", loaded";
            if (it->use_count() == 0)
-               s << ", unused for " << it->unused_for() << "s";
+               s << ", unused for " << duration_cast<milliseconds>(it->unused_for()).count() << "ms";
            else
                s << ", use count " << it->use_count();
        }
index 48ed4b11aea66df086d437a2c4260408b0dcdbb1..18a89f8c98fb453b3d5ef180da8aae288a55cf3a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) [2012-2014] Novell, Inc.
+ * Copyright (c) [2012-2015] Novell, Inc.
  *
  * All Rights Reserved.
  *
@@ -35,7 +35,7 @@ MetaSnappers meta_snappers;
 
 
 RefCounter::RefCounter()
-    : counter(0), last_used(monotonic_time())
+    : counter(0), last_used(steady_clock::now())
 {
 }
 
@@ -57,7 +57,7 @@ RefCounter::dec_use_count()
     assert(counter > 0);
 
     if (--counter == 0)
-       last_used = monotonic_time();
+       last_used = steady_clock::now();
 
     return counter;
 }
@@ -68,7 +68,7 @@ RefCounter::update_use_time()
 {
     boost::lock_guard<boost::mutex> lock(mutex);
 
-    last_used = monotonic_time();
+    last_used = steady_clock::now();
 }
 
 
@@ -81,27 +81,15 @@ RefCounter::use_count() const
 }
 
 
-int
+milliseconds
 RefCounter::unused_for() const
 {
     boost::lock_guard<boost::mutex> lock(mutex);
 
     if (counter != 0)
-       return 0;
-
-    struct timespec tmp;
-    clock_gettime(CLOCK_MONOTONIC, &tmp);
-
-    return tmp.tv_sec - last_used;
-}
-
+       return milliseconds(0);
 
-time_t
-RefCounter::monotonic_time()
-{
-    struct timespec tmp;
-    clock_gettime(CLOCK_MONOTONIC, &tmp);
-    return tmp.tv_sec;
+    return duration_cast<milliseconds>(steady_clock::now() - last_used);
 }
 
 
index e570a5d28d96906ad97ef8e6a1ebb3d532d440ca..eec66977bf257d61859ebdcca79883b4a70a6a78 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) [2012-2013] Novell, Inc.
+ * Copyright (c) [2012-2015] Novell, Inc.
  *
  * All Rights Reserved.
  *
 #define SNAPPER_META_SNAPPER_H
 
 
+#include <chrono>
 #include <boost/thread.hpp>
 
 #include <snapper/Snapper.h>
 
 
 using namespace std;
+using namespace std::chrono;
 using namespace snapper;
 
 
@@ -44,17 +46,15 @@ public:
     void update_use_time();
 
     int use_count() const;
-    int unused_for() const;
+    milliseconds unused_for() const;
 
 private:
 
-    static time_t monotonic_time();
-
     mutable boost::mutex mutex;
 
     int counter;
 
-    time_t last_used;
+    steady_clock::time_point last_used;
 
 };
 
index 6e6152dbd630e0bc1334e6ab00f77765652263b5..7e0c34030950f205c35a83e94d0636e0ebae39a2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012 Novell, Inc.
+ * Copyright (c) [2012-2015] Novell, Inc.
  *
  * All Rights Reserved.
  *
@@ -37,8 +37,8 @@
 using namespace std;
 
 
-const int idle_time = 60;
-const int snapper_cleanup_time = 30;
+const seconds idle_time(60);
+const seconds snapper_cleanup_time(30);
 
 bool log_stdout = false;
 bool log_debug = false;
@@ -55,7 +55,7 @@ public:
     void signal(DBus::Message& message);
     void client_disconnected(const string& name);
     void periodic();
-    int periodic_timeout();
+    milliseconds periodic_timeout();
 
 };
 
@@ -93,7 +93,7 @@ MyMainLoop::method_call(DBus::Message& msg)
            y2deb("client connected invisible '" << msg.get_sender() << "'");
            add_client_match(msg.get_sender());
            client = clients.add(msg.get_sender());
-           set_idle_timeout(-1);
+           set_idle_timeout(seconds(-1));
        }
 
        client->add_task(*this, msg);
@@ -146,22 +146,22 @@ MyMainLoop::periodic()
 }
 
 
-int
+milliseconds
 MyMainLoop::periodic_timeout()
 {
     boost::unique_lock<boost::shared_mutex> lock(big_mutex);
 
     if (clients.has_zombies())
-       return 1000;
+       return seconds(1);
 
     if (!backgrounds.empty())
-       return 1000;
+       return seconds(1);
 
     for (MetaSnappers::const_iterator it = meta_snappers.begin(); it != meta_snappers.end(); ++it)
        if (it->is_loaded() && it->use_count() == 0)
-           return 1000;
+           return seconds(1);
 
-    return -1;
+    return seconds(-1);
 }
 
 
index 2db7269b51013aaf9b7e40f322946c8bc3467e68..ed76005e264e710439573f4d5ea9b949de0c5a2f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) [2004-2014] Novell, Inc.
+ * Copyright (c) [2004-2015] Novell, Inc.
  *
  * All Rights Reserved.
  *
@@ -357,25 +357,22 @@ namespace snapper
 
 
     StopWatch::StopWatch()
+       : start_time(chrono::steady_clock::now())
     {
-       gettimeofday(&start_tv, NULL);
     }
 
 
     double
     StopWatch::read() const
     {
-       struct timeval stop_tv;
-       gettimeofday(&stop_tv, NULL);
-
-       struct timeval tv;
-       timersub(&stop_tv, &start_tv, &tv);
-
-       return double(tv.tv_sec) + (double)(tv.tv_usec) / 1000000.0;
+       chrono::steady_clock::time_point stop_time = chrono::steady_clock::now();
+       chrono::steady_clock::duration duration = stop_time - start_time;
+       return chrono::duration<double>(duration).count();
     }
 
 
-    std::ostream& operator<<(std::ostream& s, const StopWatch& sw)
+    std::ostream&
+    operator<<(std::ostream& s, const StopWatch& sw)
     {
        boost::io::ios_all_saver ias(s);
        return s << fixed << sw.read() << "s";
index 452b48e111bbe5de11b49772a4fdb5f52ac8e93b..1b0b0d828c104db626c79ff56873115a770d8a78 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) [2004-2014] Novell, Inc.
+ * Copyright (c) [2004-2015] Novell, Inc.
  *
  * All Rights Reserved.
  *
@@ -34,6 +34,7 @@
 #include <map>
 #include <vector>
 #include <stdexcept>
+#include <chrono>
 
 
 namespace snapper
@@ -103,7 +104,7 @@ namespace snapper
 
     protected:
 
-       struct timeval start_tv;
+       std::chrono::steady_clock::time_point start_time;
 
     };