/*
- * Copyright (c) [2012-2014] Novell, Inc.
+ * Copyright (c) [2012-2015] Novell, Inc.
*
* All Rights Reserved.
*
#include <unistd.h>
#include <poll.h>
-#include <time.h>
#include "DBusMainLoop.h"
}
}
- 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();
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;
}
}
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());
}
}
}
-
- time_t
- DBus::MainLoop::monotonic_clock()
- {
- struct timespec tmp;
- clock_gettime(CLOCK_MONOTONIC, &tmp);
- return tmp.tv_sec;
- }
-
}
/*
- * Copyright (c) [2012-2014] Novell, Inc.
+ * Copyright (c) [2012-2015] Novell, Inc.
*
* All Rights Reserved.
*
#include <dbus/dbus.h>
+#include <chrono>
#include "DBusConnection.h"
namespace DBus
{
+ using namespace std::chrono;
+
+
class MainLoop : public Connection
{
public:
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);
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:
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;
};
+-------------------------------------------------------------------
+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
{
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();
}
/*
- * Copyright (c) [2012-2014] Novell, Inc.
+ * Copyright (c) [2012-2015] Novell, Inc.
*
* All Rights Reserved.
*
RefCounter::RefCounter()
- : counter(0), last_used(monotonic_time())
+ : counter(0), last_used(steady_clock::now())
{
}
assert(counter > 0);
if (--counter == 0)
- last_used = monotonic_time();
+ last_used = steady_clock::now();
return counter;
}
{
boost::lock_guard<boost::mutex> lock(mutex);
- last_used = monotonic_time();
+ last_used = steady_clock::now();
}
}
-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);
}
/*
- * 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;
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;
};
/*
- * Copyright (c) 2012 Novell, Inc.
+ * Copyright (c) [2012-2015] Novell, Inc.
*
* All Rights Reserved.
*
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;
void signal(DBus::Message& message);
void client_disconnected(const string& name);
void periodic();
- int periodic_timeout();
+ milliseconds periodic_timeout();
};
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);
}
-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);
}
/*
- * Copyright (c) [2004-2014] Novell, Inc.
+ * Copyright (c) [2004-2015] Novell, Inc.
*
* All Rights Reserved.
*
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";
/*
- * Copyright (c) [2004-2014] Novell, Inc.
+ * Copyright (c) [2004-2015] Novell, Inc.
*
* All Rights Reserved.
*
#include <map>
#include <vector>
#include <stdexcept>
+#include <chrono>
namespace snapper
protected:
- struct timeval start_tv;
+ std::chrono::steady_clock::time_point start_time;
};