1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 18:45:10 +00:00

Simplify timed_thread time calculations with some c++0x magic

This commit is contained in:
Pavel Labath 2010-06-10 19:13:15 +02:00
parent 6642004d61
commit 0f213c89f2
8 changed files with 22 additions and 44 deletions

View File

@ -89,9 +89,8 @@ int create_audacious_thread(void)
{
if (!info.audacious.p_timed_thread) {
info.audacious.p_timed_thread =
timed_thread::create(std::bind(audacious_thread_func,
std::placeholders::_1), info.music_player_interval *
1000000);
timed_thread::create(std::bind(audacious_thread_func, std::placeholders::_1),
std::chrono::microseconds(long(info.music_player_interval * 1000000)));
}
if (!info.audacious.p_timed_thread) {

View File

@ -144,7 +144,7 @@ void ccurl_init_thread(ccurl_location_ptr curloc, int interval)
assert(curloc->result);
#endif /* DEBUG */
curloc->p_timed_thread = timed_thread::create(std::bind(ccurl_thread,
std::placeholders::_1, curloc), interval * 1000000);
std::placeholders::_1, curloc), std::chrono::microseconds(long(interval * 1000000)));
if (!curloc->p_timed_thread) {
NORM_ERR("curl thread: error creating timed thread");

View File

@ -323,7 +323,8 @@ void print_execi(struct text_object *obj, char *p, int p_max_size)
* note that we don't register this thread with the
* timed_thread list, because we destroy it manually
*/
ed->p_timed_thread = timed_thread::create(std::bind(threaded_exec, std::placeholders::_1, obj), ed->interval * 1000000, false);
ed->p_timed_thread = timed_thread::create(std::bind(threaded_exec, std::placeholders::_1, obj),
std::chrono::microseconds(long(ed->interval * 1000000)), false);
if (!ed->p_timed_thread) {
NORM_ERR("Error creating texeci timed thread");
}

View File

@ -643,7 +643,8 @@ static void ensure_mail_thread(struct mail_s *mail,
if (mail->p_timed_thread)
return;
mail->p_timed_thread = timed_thread::create(std::bind(func, std::placeholders::_1, mail), mail->interval * 1000000);
mail->p_timed_thread = timed_thread::create(std::bind(func, std::placeholders::_1, mail),
std::chrono::microseconds(long(mail->interval * 1000000)));
if (!mail->p_timed_thread) {
NORM_ERR("Error creating %s timed thread", text);
}

View File

@ -126,7 +126,7 @@ static void update_moc_loop(thread_handle &handle)
/* never reached */
}
static int run_moc_thread(double interval)
static int run_moc_thread(std::chrono::microseconds interval)
{
if (moc_thread)
return 0;
@ -141,7 +141,7 @@ static int run_moc_thread(double interval)
int update_moc(void)
{
run_moc_thread(info.music_player_interval * 100000);
run_moc_thread(std::chrono::microseconds(long(info.music_player_interval * 100000)));
return 0;
}

View File

@ -127,14 +127,13 @@ static void update_mpd_thread(thread_handle &handle);
int update_mpd(void)
{
int interval;
static timed_thread_ptr thread;
if (thread)
return 0;
interval = info.music_player_interval * 1000000;
thread = timed_thread::create(std::bind(update_mpd_thread, std::placeholders::_1), interval);
thread = timed_thread::create(std::bind(update_mpd_thread, std::placeholders::_1),
std::chrono::microseconds(long(info.music_player_interval * 1000000)) );
if (!thread) {
NORM_ERR("Failed to create MPD timed thread");
return 0;

View File

@ -30,7 +30,6 @@
#include <thread>
#include <list>
#include <chrono>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
@ -52,10 +51,10 @@ inline bool cv_status_to_bool(bool s)
{ return s; }
#endif
/* Abstraction layer for timed threads */
typedef struct std::chrono::system_clock clk;
using std::chrono::duration_cast;
/* private */
struct _timed_thread {
@ -73,8 +72,8 @@ typedef std::list<timed_thread_ptr> thread_list_t;
thread_list_t thread_list;
/* create a timed thread (object creation only) */
timed_thread::timed_thread(const std::function<void(thread_handle &)> &start_routine, unsigned
int interval_usecs) :
timed_thread::timed_thread(const std::function<void(thread_handle &)> &start_routine,
std::chrono::microseconds interval_usecs) :
p_timed_thread(new _timed_thread), p_thread_handle(this),
interval_usecs(interval_usecs), running(false)
{
@ -177,16 +176,7 @@ int timed_thread::test(int override_wait_time)
#endif /* DEBUG */
bool rc = false;
/* determine when to wait until */
#ifdef _GLIBCXX_USE_CLOCK_REALTIME
clk::time_point wait_time = p_timed_thread->last_time +
clk::duration(interval_usecs * 1000);
#elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
clk::time_point wait_time = p_timed_thread->last_time +
clk::duration(interval_usecs);
#else
clk::time_point wait_time = p_timed_thread->last_time +
clk::duration(interval_usecs / 1000000);
#endif
clk::time_point wait_time = p_timed_thread->last_time + duration_cast<clk::duration>(interval_usecs);
/* acquire runnable_cond mutex */
{
@ -206,22 +196,9 @@ int timed_thread::test(int override_wait_time)
}
p_timed_thread->last_time = clk::now();
#ifdef _GLIBCXX_USE_CLOCK_REALTIME
if (wait_time + clk::duration(interval_usecs * 1000) >
p_timed_thread->last_time) {
if (wait_time + duration_cast<clk::duration>(interval_usecs) > p_timed_thread->last_time) {
p_timed_thread->last_time = wait_time;
}
#elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
if (wait_time + clk::duration(interval_usecs) >
p_timed_thread->last_time) {
p_timed_thread->last_time = wait_time;
}
#else
if (wait_time + clk::duration(interval_usecs / 1000000) >
p_timed_thread->last_time) {
p_timed_thread->last_time = wait_time;
}
#endif
/* if runnable_cond was signaled, tell caller to exit thread */
return rc;

View File

@ -26,6 +26,7 @@
#define _TIMED_THREAD_H_
#include <stdlib.h>
#include <chrono>
#include <functional>
#include <memory>
@ -59,8 +60,8 @@ class thread_handle {
class timed_thread {
public:
/* create a timed thread (object creation only) */
static timed_thread_ptr create(const std::function<void(thread_handle &)> &start_routine, const unsigned int
interval_usecs, bool register_for_destruction = true) {
static timed_thread_ptr create(const std::function<void(thread_handle &)> &start_routine,
std::chrono::microseconds interval_usecs, bool register_for_destruction = true) {
timed_thread_ptr ptr(new timed_thread(start_routine, interval_usecs));
if (register_for_destruction) {
register_(ptr);
@ -92,8 +93,8 @@ class timed_thread {
private:
/* create a timed thread (object creation only) */
timed_thread(const std::function<void(thread_handle &)> &start_routine, unsigned int
interval_usecs);
timed_thread(const std::function<void(thread_handle &)> &start_routine,
std::chrono::microseconds interval_usecs);
/* waits required interval (unless override_wait_time is non-zero) for
* termination signal returns 1 if received, 0 otherwise. should also return 1
@ -112,7 +113,7 @@ class timed_thread {
/* private internal data */
std::auto_ptr<_timed_thread> p_timed_thread;
thread_handle p_thread_handle;
unsigned int interval_usecs;
std::chrono::microseconds interval_usecs;
bool running;
friend class thread_handle;
};