mirror of
https://github.com/Llewellynvdm/conky.git
synced 2025-01-30 18:49:31 +00:00
a new update callback system
it should replace both timed_thread and run_update_callback() systems it features: - automatic removal of callbacks which are not used - ability to run callback less frequent than the update_interval - avoidance of running the same callback multiple times
This commit is contained in:
parent
29ef75083a
commit
c5fe259ae0
@ -38,7 +38,7 @@ set(conky_sources colours.cc combine.cc common.cc conky.cc core.cc
|
|||||||
diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.cc
|
diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.cc
|
||||||
mboxscan.cc read_tcpip.cc scroll.cc specials.cc tailhead.cc
|
mboxscan.cc read_tcpip.cc scroll.cc specials.cc tailhead.cc
|
||||||
temphelper.cc text_object.cc timeinfo.cc top.cc algebra.cc prioqueue.cc proc.cc
|
temphelper.cc text_object.cc timeinfo.cc top.cc algebra.cc prioqueue.cc proc.cc
|
||||||
user.cc luamm.cc data-source.cc lua-config.cc setting.cc llua.cc)
|
user.cc luamm.cc data-source.cc lua-config.cc setting.cc llua.cc update-cb.cc)
|
||||||
|
|
||||||
# add timed thread library
|
# add timed thread library
|
||||||
add_library(timed-thread timed-thread.cc)
|
add_library(timed-thread timed-thread.cc)
|
||||||
|
74
src/semaphore.hh
Normal file
74
src/semaphore.hh
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
||||||
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
||||||
|
*
|
||||||
|
* Conky, a system monitor, based on torsmo
|
||||||
|
*
|
||||||
|
* Please see COPYING for details
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Pavel Labath et al.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SEMAPHORE_HH
|
||||||
|
#define SEMAPHORE_HH
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
#include <cstring>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include <semaphore.h>
|
||||||
|
|
||||||
|
class semaphore {
|
||||||
|
sem_t sem;
|
||||||
|
|
||||||
|
semaphore(const semaphore &) = delete;
|
||||||
|
semaphore& operator=(const semaphore &) = delete;
|
||||||
|
public:
|
||||||
|
semaphore(unsigned int value = 0) throw(std::logic_error)
|
||||||
|
{
|
||||||
|
if(sem_init(&sem, 0, value))
|
||||||
|
throw std::logic_error(strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
~semaphore() throw()
|
||||||
|
{ sem_destroy(&sem); }
|
||||||
|
|
||||||
|
void post() throw(std::overflow_error)
|
||||||
|
{
|
||||||
|
if(sem_post(&sem))
|
||||||
|
throw std::overflow_error(strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
void wait() throw()
|
||||||
|
{
|
||||||
|
while(sem_wait(&sem)) {
|
||||||
|
if(errno != EINTR)
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool trywait() throw()
|
||||||
|
{
|
||||||
|
while(sem_trywait(&sem)) {
|
||||||
|
if(errno == EAGAIN)
|
||||||
|
return false;
|
||||||
|
else if(errno != EINTR)
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
126
src/update-cb.cc
Normal file
126
src/update-cb.cc
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
||||||
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
||||||
|
*
|
||||||
|
* Conky, a system monitor, based on torsmo
|
||||||
|
*
|
||||||
|
* Please see COPYING for details
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Pavel Labath et al.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include "update-cb.hh"
|
||||||
|
|
||||||
|
#include <typeinfo>
|
||||||
|
|
||||||
|
namespace conky {
|
||||||
|
namespace {
|
||||||
|
semaphore sem_wait;
|
||||||
|
enum {UNUSED_MAX = 5};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace priv {
|
||||||
|
callback_base::~callback_base()
|
||||||
|
{
|
||||||
|
if(thread) {
|
||||||
|
done = true;
|
||||||
|
sem_start.post();
|
||||||
|
thread->join();
|
||||||
|
delete thread;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline size_t callback_base::get_hash(const handle &h)
|
||||||
|
{ return h->hash; }
|
||||||
|
|
||||||
|
inline bool callback_base::is_equal(const handle &a, const handle &b)
|
||||||
|
{
|
||||||
|
if(a->hash != b->hash)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(typeid(*a) != typeid(*b))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return *a == *b;
|
||||||
|
}
|
||||||
|
|
||||||
|
callback_base::handle callback_base::do_register_cb(const handle &h)
|
||||||
|
{
|
||||||
|
const handle &ret = *callbacks.insert(h).first;
|
||||||
|
|
||||||
|
if(h->period < ret->period) {
|
||||||
|
ret->period = h->period;
|
||||||
|
ret->remaining = 0;
|
||||||
|
}
|
||||||
|
assert(ret->wait == h->wait);
|
||||||
|
ret->unused = 0;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void callback_base::run()
|
||||||
|
{
|
||||||
|
if(not thread)
|
||||||
|
thread = new std::thread(&callback_base::start_routine, this);
|
||||||
|
|
||||||
|
sem_start.post();
|
||||||
|
}
|
||||||
|
|
||||||
|
void callback_base::start_routine()
|
||||||
|
{
|
||||||
|
for(;;) {
|
||||||
|
sem_start.wait();
|
||||||
|
if(done)
|
||||||
|
return;
|
||||||
|
work();
|
||||||
|
if(wait)
|
||||||
|
sem_wait.post();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
callback_base::Callbacks callback_base::callbacks(1, get_hash, is_equal);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void run_all_callbacks()
|
||||||
|
{
|
||||||
|
using priv::callback_base;
|
||||||
|
|
||||||
|
size_t wait = 0;
|
||||||
|
for(auto i = callback_base::callbacks.begin(); i != callback_base::callbacks.end(); ) {
|
||||||
|
callback_base &cb = **i;
|
||||||
|
|
||||||
|
if(cb.remaining-- == 0) {
|
||||||
|
if(!i->unique() || ++cb.unused < UNUSED_MAX) {
|
||||||
|
cb.remaining = cb.period-1;
|
||||||
|
cb.run();
|
||||||
|
if(cb.wait)
|
||||||
|
++wait;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(cb.unused == UNUSED_MAX) {
|
||||||
|
auto t = i;
|
||||||
|
++i;
|
||||||
|
callback_base::callbacks.erase(t);
|
||||||
|
} else
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(wait-- > 0)
|
||||||
|
sem_wait.wait();
|
||||||
|
}
|
||||||
|
}
|
216
src/update-cb.hh
Normal file
216
src/update-cb.hh
Normal file
@ -0,0 +1,216 @@
|
|||||||
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
||||||
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
||||||
|
*
|
||||||
|
* Conky, a system monitor, based on torsmo
|
||||||
|
*
|
||||||
|
* Please see COPYING for details
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Pavel Labath et al.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef UPDATE_CB_HH
|
||||||
|
#define UPDATE_CB_HH
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
|
#include <tuple>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "semaphore.hh"
|
||||||
|
|
||||||
|
namespace conky {
|
||||||
|
// forward declarations
|
||||||
|
template<typename Callback>
|
||||||
|
class callback_handle;
|
||||||
|
void run_all_callbacks();
|
||||||
|
template<typename Callback, typename... Params>
|
||||||
|
callback_handle<Callback> register_cb(uint32_t period, Params&&... params);
|
||||||
|
|
||||||
|
namespace priv {
|
||||||
|
class callback_base {
|
||||||
|
typedef callback_handle<callback_base> handle;
|
||||||
|
typedef std::unordered_set<handle, size_t (*)(const handle &),
|
||||||
|
bool (*)(const handle &, const handle &)>
|
||||||
|
Callbacks;
|
||||||
|
|
||||||
|
|
||||||
|
semaphore sem_start;
|
||||||
|
std::thread *thread;
|
||||||
|
const size_t hash;
|
||||||
|
uint32_t period;
|
||||||
|
uint32_t remaining;
|
||||||
|
const bool wait;
|
||||||
|
bool done;
|
||||||
|
uint8_t unused;
|
||||||
|
|
||||||
|
callback_base(const callback_base &) = delete;
|
||||||
|
callback_base& operator=(const callback_base &) = delete;
|
||||||
|
|
||||||
|
virtual bool operator==(const callback_base &) = 0;
|
||||||
|
|
||||||
|
void run();
|
||||||
|
void start_routine();
|
||||||
|
|
||||||
|
// a list of registered callbacks
|
||||||
|
static Callbacks callbacks;
|
||||||
|
|
||||||
|
// used by the callbacks list
|
||||||
|
static inline size_t get_hash(const handle &h);
|
||||||
|
static inline bool is_equal(const handle &a, const handle &b);
|
||||||
|
|
||||||
|
static handle do_register_cb(const handle &h);
|
||||||
|
|
||||||
|
template<typename Callback, typename... Params>
|
||||||
|
friend callback_handle<Callback>
|
||||||
|
conky::register_cb(uint32_t period, Params&&... params);
|
||||||
|
|
||||||
|
friend void conky::run_all_callbacks();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
callback_base(size_t hash_, uint32_t period_, bool wait_)
|
||||||
|
: thread(NULL), hash(hash_), period(period_), remaining(0), wait(wait_),
|
||||||
|
done(false), unused(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// to be implemented by descendant classes
|
||||||
|
virtual void work() = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::mutex result_mutex;
|
||||||
|
|
||||||
|
virtual ~callback_base();
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
class callback_handle: private std::shared_ptr<Callback> {
|
||||||
|
typedef std::shared_ptr<Callback> Base;
|
||||||
|
|
||||||
|
callback_handle(Callback *ptr)
|
||||||
|
: Base(ptr)
|
||||||
|
{}
|
||||||
|
|
||||||
|
callback_handle(Base &&ptr)
|
||||||
|
: Base(std::move(ptr))
|
||||||
|
{}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
using Base::operator->;
|
||||||
|
using Base::operator*;
|
||||||
|
|
||||||
|
friend void conky::run_all_callbacks();
|
||||||
|
template<typename Callback_, typename... Params>
|
||||||
|
friend callback_handle<Callback_> register_cb(uint32_t period, Params&&... params);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Callback, typename... Params>
|
||||||
|
callback_handle<Callback> register_cb(uint32_t period, Params&&... params)
|
||||||
|
{
|
||||||
|
return std::dynamic_pointer_cast<Callback>(priv::callback_base::do_register_cb(
|
||||||
|
priv::callback_base::handle(
|
||||||
|
new Callback(period, std::forward<Params>(params)...)
|
||||||
|
)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace priv {
|
||||||
|
template<size_t pos, typename... Elements>
|
||||||
|
struct hash_tuple {
|
||||||
|
typedef std::tuple<Elements...> Tuple;
|
||||||
|
typedef typename std::tuple_element<pos-1, Tuple>::type Element;
|
||||||
|
|
||||||
|
static inline size_t hash(const Tuple &tuple)
|
||||||
|
{
|
||||||
|
return std::hash<Element>()(std::get<pos-1>(tuple))
|
||||||
|
+ 47 * hash_tuple<pos-1, Elements...>::hash(tuple);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename... Elements>
|
||||||
|
struct hash_tuple<0, Elements...> {
|
||||||
|
static inline size_t hash(const std::tuple<Elements...> &)
|
||||||
|
{ return 0; }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* To create a callback, inherit from this class. The Result template parameter should be the
|
||||||
|
* type of your output, so that your users can retrieve it with the get_result* functions.
|
||||||
|
*
|
||||||
|
* get_result() returns a reference to the internal variable. It can be used without locking
|
||||||
|
* if the object has wait set to true (wait=true means that the run_all_callbacks() waits for
|
||||||
|
* the callback to finish work()ing before returning). If object has wait=false then the user
|
||||||
|
* must first lock the result_mutex.
|
||||||
|
*
|
||||||
|
* get_result_copy() returns a copy of the result object and it handles the necessary
|
||||||
|
* locking. Don't call it if you hold a lock on the result_mutex.
|
||||||
|
*
|
||||||
|
* You should implement the work() function to do the actual updating and store the result in
|
||||||
|
* the result variable (lock the mutex while you are doing it, especially if you have
|
||||||
|
* wait=false).
|
||||||
|
*
|
||||||
|
* The Keys... template parameters are parameters for your work function. E.g., a curl
|
||||||
|
* callback can have one parameter - the url to retrieve,. hddtemp may have two - host and
|
||||||
|
* port number of the hddtemp server, etc. The register_cb() function make sure that there
|
||||||
|
* exists only one object (of the same type) with the same values for all the keys.
|
||||||
|
*
|
||||||
|
* Callbacks are registered with the register_cb() function. You pass the class name as the
|
||||||
|
* template parameter, and any additional parameters to the constructor as function
|
||||||
|
* parameters. The period parameter specifies how often the callback will run. It should be
|
||||||
|
* left for the user to decide that. register_cb() returns a pointer to the newly created
|
||||||
|
* object. As long as someone holds a pointer to the object, the callback will be run.
|
||||||
|
*
|
||||||
|
* run_all_callbacks() runs the registered callbacks (with the specified periodicity). It
|
||||||
|
* should be called from somewhere inside the main loop, according to the update_interval
|
||||||
|
* setting. It waits for the callbacks which have wait=true. It leaves the rest to run in
|
||||||
|
* background.
|
||||||
|
*/
|
||||||
|
template<typename Result, typename... Keys>
|
||||||
|
class callback: public priv::callback_base {
|
||||||
|
virtual bool operator==(const callback_base &other)
|
||||||
|
{ return tuple == dynamic_cast<const callback &>(other).tuple; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
typedef std::tuple<Keys...> Tuple;
|
||||||
|
|
||||||
|
const Tuple tuple;
|
||||||
|
Result result;
|
||||||
|
|
||||||
|
public:
|
||||||
|
callback(uint32_t period_, bool wait_, const Tuple &tuple_)
|
||||||
|
: callback_base(priv::hash_tuple<sizeof...(Keys), Keys...>::hash(tuple_),
|
||||||
|
period_, wait_),
|
||||||
|
tuple(tuple_)
|
||||||
|
{}
|
||||||
|
|
||||||
|
const Result& get_result()
|
||||||
|
{ return result; }
|
||||||
|
|
||||||
|
Result get_result_copy()
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(result_mutex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* LUA_CONFIG_HH */
|
Loading…
x
Reference in New Issue
Block a user