2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2010-12-24 14:03:54 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2013-06-20 17:43:46 +00:00
|
|
|
#include "logging.h"
|
2010-12-24 14:03:54 +00:00
|
|
|
|
|
|
|
#include "update-cb.hh"
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
#include <unistd.h>
|
2018-12-21 02:20:43 +00:00
|
|
|
#include <typeinfo>
|
2010-12-24 14:03:54 +00:00
|
|
|
|
|
|
|
namespace conky {
|
2018-05-12 16:03:00 +00:00
|
|
|
namespace {
|
|
|
|
semaphore sem_wait;
|
|
|
|
enum { UNUSED_MAX = 5 };
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace priv {
|
|
|
|
callback_base::~callback_base() { stop(); }
|
|
|
|
|
|
|
|
void callback_base::stop() {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (thread != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
done = true;
|
|
|
|
sem_start.post();
|
2018-05-12 23:26:31 +00:00
|
|
|
if (pipefd.second >= 0) {
|
|
|
|
if (write(pipefd.second, "X", 1) != 1) {
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("can't write 'X' to pipefd %d: %s", pipefd.second,
|
|
|
|
strerror(errno));
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
thread->join();
|
|
|
|
delete thread;
|
2018-05-12 23:26:31 +00:00
|
|
|
thread = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
if (pipefd.first >= 0) {
|
|
|
|
close(pipefd.first);
|
|
|
|
pipefd.first = -1;
|
|
|
|
}
|
|
|
|
if (pipefd.second >= 0) {
|
|
|
|
close(pipefd.second);
|
|
|
|
pipefd.second = -1;
|
|
|
|
}
|
2010-12-24 14:03:54 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +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) {
|
2018-12-21 02:20:43 +00:00
|
|
|
if (a->hash != b->hash) { return false; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-12-22 18:16:38 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wpotentially-evaluated-expression"
|
2018-12-21 02:20:43 +00:00
|
|
|
if (typeid(*a) != typeid(*b)) { return false; }
|
2018-12-22 18:16:38 +00:00
|
|
|
#pragma clang diagnostic pop
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
return *a == *b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a callback is not successfully inserted into the set, it must have
|
|
|
|
* the same hash as an existing callback. If this is so, merge the incoming
|
|
|
|
* callback with the one that prevented insertion. Keep the smaller of the
|
|
|
|
* two periods.
|
|
|
|
*/
|
|
|
|
void callback_base::merge(callback_base &&other) {
|
|
|
|
if (other.period < period) {
|
|
|
|
period = other.period;
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
assert(wait == other.wait);
|
|
|
|
unused = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register a callback (i.e. insert it into the callbacks set)
|
|
|
|
*/
|
|
|
|
callback_base::handle callback_base::do_register_cb(const handle &h) {
|
|
|
|
const auto &p = callbacks.insert(h);
|
|
|
|
|
|
|
|
/* insertion failed; callback already exists */
|
2018-12-21 02:20:43 +00:00
|
|
|
if (!p.second) { (*p.first)->merge(std::move(*h)); }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
return *p.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
void callback_base::run() {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (thread == nullptr) {
|
|
|
|
thread = new std::thread(&callback_base::start_routine, this);
|
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
sem_start.post();
|
|
|
|
}
|
|
|
|
|
|
|
|
void callback_base::start_routine() {
|
|
|
|
for (;;) {
|
|
|
|
sem_start.wait();
|
2018-12-21 02:20:43 +00:00
|
|
|
if (done) { return; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
// clear any remaining posts in case the previous iteration was very slow
|
|
|
|
// (this should only happen if wait == false)
|
2018-12-22 17:45:25 +00:00
|
|
|
while (sem_start.trywait()) {}
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
work();
|
2018-12-21 02:20:43 +00:00
|
|
|
if (wait) { sem_wait.post(); }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback_base::Callbacks callback_base::callbacks(1, get_hash, is_equal);
|
|
|
|
} // namespace priv
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* check whether enough update intervals have elapsed (up to period) */
|
|
|
|
if (cb.remaining-- == 0) {
|
|
|
|
/* run the callback as long as someone holds a pointer to it;
|
|
|
|
* if no one owns the callback, run it at most UNUSED_MAX times */
|
|
|
|
if (!i->unique() || ++cb.unused < UNUSED_MAX) {
|
|
|
|
cb.remaining = cb.period - 1;
|
|
|
|
cb.run();
|
2018-12-21 02:20:43 +00:00
|
|
|
if (cb.wait) { ++wait; }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cb.unused == UNUSED_MAX) {
|
|
|
|
auto t = i;
|
|
|
|
++i;
|
|
|
|
callback_base::callbacks.erase(t);
|
2018-05-12 23:26:31 +00:00
|
|
|
} else {
|
2018-05-12 16:03:00 +00:00
|
|
|
++i;
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
while (wait-- > 0) { sem_wait.wait(); }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
} // namespace conky
|