2010-12-24 14:03:54 +00:00
|
|
|
/* -*- 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"
|
|
|
|
|
2012-04-11 16:06:47 +00:00
|
|
|
#include <unistd.h>
|
2010-12-24 14:03:54 +00:00
|
|
|
#include <typeinfo>
|
|
|
|
|
|
|
|
namespace conky {
|
|
|
|
namespace {
|
|
|
|
semaphore sem_wait;
|
|
|
|
enum {UNUSED_MAX = 5};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace priv {
|
|
|
|
callback_base::~callback_base()
|
2011-09-28 16:31:05 +00:00
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void callback_base::stop()
|
2010-12-24 14:03:54 +00:00
|
|
|
{
|
|
|
|
if(thread) {
|
|
|
|
done = true;
|
|
|
|
sem_start.post();
|
2011-02-26 23:17:53 +00:00
|
|
|
if(pipefd.second >= 0)
|
|
|
|
write(pipefd.second, "X", 1);
|
2010-12-24 14:03:54 +00:00
|
|
|
thread->join();
|
|
|
|
delete thread;
|
2011-09-28 16:31:05 +00:00
|
|
|
thread = NULL;
|
2010-12-24 14:03:54 +00:00
|
|
|
}
|
2011-09-28 16:31:05 +00:00
|
|
|
if(pipefd.first >= 0) {
|
2011-02-26 23:17:53 +00:00
|
|
|
close(pipefd.first);
|
2011-09-28 16:31:05 +00:00
|
|
|
pipefd.first = -1;
|
|
|
|
}
|
|
|
|
if(pipefd.second >= 0) {
|
2011-02-26 23:17:53 +00:00
|
|
|
close(pipefd.second);
|
2011-09-28 16:31:05 +00:00
|
|
|
pipefd.second = -1;
|
|
|
|
}
|
2010-12-24 14:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-03-15 14:13:13 +00:00
|
|
|
void callback_base::merge(callback_base &&other)
|
|
|
|
{
|
|
|
|
if(other.period < period) {
|
|
|
|
period = other.period;
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
assert(wait == other.wait);
|
|
|
|
unused = 0;
|
|
|
|
}
|
|
|
|
|
2010-12-24 14:03:54 +00:00
|
|
|
callback_base::handle callback_base::do_register_cb(const handle &h)
|
|
|
|
{
|
2011-03-15 14:13:13 +00:00
|
|
|
const auto &p = callbacks.insert(h);
|
2010-12-24 14:03:54 +00:00
|
|
|
|
2011-03-15 14:13:13 +00:00
|
|
|
if(not p.second)
|
2011-03-27 12:45:38 +00:00
|
|
|
(*p.first)->merge(std::move(*h));
|
2010-12-24 14:03:54 +00:00
|
|
|
|
2011-03-15 14:13:13 +00:00
|
|
|
return *p.first;
|
2010-12-24 14:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2011-03-25 11:33:13 +00:00
|
|
|
|
|
|
|
// clear any remaining posts in case the previous iteration was very slow
|
|
|
|
// (this should only happen if wait == false)
|
|
|
|
while(sem_start.trywait());
|
|
|
|
|
2010-12-24 14:03:54 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|