1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-09-28 21:19:10 +00:00

Remove timed_thread code as it is no longer used.

This commit is contained in:
Pavel Labath 2011-03-26 21:04:07 +01:00
parent f3dfb21859
commit 75e147fa25
9 changed files with 3 additions and 397 deletions

View File

@ -2,7 +2,7 @@ Any original torsmo code is licensed under the BSD license (see LICENSE.BSD
for a copy)
All code written since the fork of torsmo is licensed under the GPL (see
LICENSE.GPL for a copy), except where noted differently (such as in portmon code, timed thread code, and audacious code which are LGPL, and prss which is an MIT-style license).
LICENSE.GPL for a copy), except where noted differently (such as in portmon and audacious code which are LGPL, and prss which is an MIT-style license).
Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)

View File

@ -308,9 +308,8 @@
the BSD license (see LICENSE.BSD for a copy). All code
written since the fork of torsmo is licensed under the GPL
(see LICENSE.GPL for a copy), except where noted
differently (such as in portmon code, timed thread code,
and audacious code which are LGPL, and prss which is an
MIT-style license).</para>
differently (such as in portmon and audacious code which are LGPL, and
prss which is an MIT-style license).</para>
</refsect1>
<refsect1>
<title>Authors</title>

View File

@ -40,10 +40,6 @@ set(conky_sources c++wrap.cc colours.cc combine.cc common.cc conky.cc core.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 update-cb.cc)
# add timed thread library
add_library(timed-thread timed-thread.cc)
set(conky_libs ${conky_libs} timed-thread)
# Platform specific sources
if(OS_LINUX)
set(linux linux.cc users.cc sony.cc i8k.cc)

View File

@ -32,7 +32,6 @@
#include "text_object.h"
#include "conky.h"
#include "common.h"
#include "timed-thread.h"
#include <iostream>
#include <algorithm>
#include <sstream>
@ -2548,8 +2547,6 @@ void clean_up_without_threads(void *memtofree1, void* memtofree2)
free_and_zero(memtofree1);
free_and_zero(memtofree2);
timed_thread::destroy_registered_threads();
free_and_zero(info.cpu_usage);
#ifdef BUILD_X11
if(out_to_x.get(*state))

View File

@ -36,7 +36,6 @@
#include "common.h"
#include "logging.h"
#include "text_object.h"
#include "timed-thread.h"
#include <errno.h>
#include <stdio.h>

View File

@ -24,7 +24,6 @@
#include "conky.h"
#include "logging.h"
#include "text_object.h"
#include "timed-thread.h"
#include <stdio.h>
#include <stdlib.h>

View File

@ -31,7 +31,6 @@
#include <mutex>
#include "conky.h"
#include "logging.h"
#include "timed-thread.h"
#include "timeinfo.h"
#include "libmpdclient.h"
#include "mpd.h"

View File

@ -1,262 +0,0 @@
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
*
* timed_thread.c: Abstraction layer for timed threads
*
* Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
* Copyright (c) 2005-2010 Brenden Matthews, et. al. (see AUTHORS)
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA.
*
*/
#include <config.h>
#include <thread>
#include <list>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include "timed-thread.h"
#include "logging.h"
/*
* In gcc-4.5 condition_variable::wait_until returns a (strong) enum cv_status.
* In gcc-4.4 it returns bool.
* This hack is needed so it can work on both.
*/
#if __GNUC__*100 + __GNUC_MINOR__ >= 405
inline bool cv_status_to_bool(std::cv_status s)
{ return s == std::cv_status::no_timeout; }
#else
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 {
std::auto_ptr<std::thread> thread; /* thread itself */
std::mutex cs_mutex; /* critical section mutex */
std::mutex runnable_mutex; /* runnable section mutex */
std::condition_variable runnable_cond; /* signalled to stop the thread */
clk::time_point last_time; /* last time interval */
int pipefd[2];
int die;
_timed_thread() : die(0) {}
};
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,
std::chrono::microseconds interval_usecs) :
p_timed_thread(new _timed_thread), p_thread_handle(this),
interval_usecs(interval_usecs), running(false)
{
#ifdef DEBUG
assert(interval_usecs >= std::chrono::microseconds(MINIMUM_INTERVAL_USECS));
#endif /* DEBUG */
/* create thread pipe (used to tell threads to die) */
if (pipe(p_timed_thread->pipefd)) {
throw std::runtime_error("couldn't create pipe");
}
/* set initialize to current time */
p_timed_thread->last_time = clk::now();
/* printf("interval_time.tv_sec = %li, .tv_nsec = %li\n",
p_timed_thread->interval_time.tv_sec,
p_timed_thread->interval_time.tv_nsec); */
p_timed_thread->thread = std::auto_ptr<std::thread>(
new std::thread(start_routine, p_thread_handle)
);
DBGP("created thread %ld", (long)p_timed_thread->thread.get());
running = true;
}
/* destroy a timed thread. */
void timed_thread::destroy()
{
DBGP("destroying thread %ld", (long)p_timed_thread->thread.get());
#ifdef DEBUG
assert(running && p_timed_thread->thread->joinable());
#endif /* DEBUG */
{
/* signal thread to stop */
std::lock_guard<std::mutex> l(p_timed_thread->runnable_mutex);
p_timed_thread->runnable_cond.notify_one();
p_timed_thread->die = 1;
}
if (write(p_timed_thread->pipefd[1], "die", 3) == -1)
perror("write()");
/* join the terminating thread */
p_timed_thread->thread->join();
close(p_timed_thread->pipefd[0]);
close(p_timed_thread->pipefd[1]);
running = false;
}
/* lock a timed thread for critical section activity */
void timed_thread::lock(void)
{
#ifdef DEBUG
assert(running);
#endif /* DEBUG */
p_timed_thread->cs_mutex.lock();
}
/* unlock a timed thread after critical section activity */
void timed_thread::unlock(void)
{
#ifdef DEBUG
assert(running);
#endif /* DEBUG */
p_timed_thread->cs_mutex.unlock();
}
std::mutex &timed_thread::mutex()
{
#ifdef DEBUG
assert(running);
#endif /* DEBUG */
return p_timed_thread->cs_mutex;
}
int timed_thread::readfd(void) const
{
#ifdef DEBUG
assert(running);
#endif /* DEBUG */
return p_timed_thread->pipefd[0];
}
/* thread waits interval_usecs for runnable_cond to be signaled.
* returns 1 if signaled, -1 on error, and 0 otherwise.
* caller should call timed_thread::exit() on any non-zero return value. */
int timed_thread::test(int override_wait_time)
{
#ifdef DEBUG
assert(running);
#endif /* DEBUG */
bool rc = false;
/* determine when to wait until */
clk::time_point wait_time = p_timed_thread->last_time + duration_cast<clk::duration>(interval_usecs);
/* acquire runnable_cond mutex */
{
std::unique_lock<std::mutex> lock(p_timed_thread->runnable_mutex);
if (p_timed_thread->die) {
/* if we were kindly asked to die, then die */
return 1;
}
if (override_wait_time) {
wait_time = clk::now();
}
/* release mutex and wait until future time for runnable_cond to signal */
rc = cv_status_to_bool( p_timed_thread->runnable_cond.wait_until(lock, wait_time) );
}
p_timed_thread->last_time = clk::now();
if (wait_time + duration_cast<clk::duration>(interval_usecs) > p_timed_thread->last_time) {
p_timed_thread->last_time = wait_time;
}
/* if runnable_cond was signaled, tell caller to exit thread */
return rc;
}
/* register a timed thread for future destruction via
* timed_thread::destroy_registered_threads() */
int timed_thread::register_(const timed_thread_ptr &timed_thread)
{
thread_list.push_back(timed_thread);
return 0;
}
void timed_thread::deregister(const timed_thread *timed_thread)
{
for (thread_list_t::iterator i = thread_list.begin(); i != thread_list.end(); i++) {
if (i->get() == timed_thread) {
thread_list.erase(i);
break;
}
}
}
/* destroy all registered timed threads */
void timed_thread::destroy_registered_threads(void)
{
for (thread_list_t::iterator i = thread_list.begin(); i != thread_list.end(); i++) {
// (*i)->destroy(false /* don't deregister */);
#ifdef DEBUG
/* if this assert is ever reached, we have an unreleased shared_ptr
* somewhere holding on to this instance */
assert(i->unique());
#endif /* DEBUG */
}
thread_list.clear(); /* that was easy */
}
int thread_handle::test(int override_wait_time) {
return thread->test(override_wait_time);
}
std::mutex &thread_handle::mutex()
{
#ifdef DEBUG
assert(thread->running);
#endif /* DEBUG */
return thread->p_timed_thread->cs_mutex;
}
void thread_handle::lock(void) {
thread->lock();
}
void thread_handle::unlock(void) {
thread->unlock();
}
int thread_handle::readfd(void) const
{
#ifdef DEBUG
assert(thread->running);
#endif /* DEBUG */
return thread->p_timed_thread->pipefd[0];
}

View File

@ -1,121 +0,0 @@
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
*
* timed_thread.h: Abstraction layer for timed threads
*
* Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
* Copyright (c) 2005-2010 Brenden Matthews, et. al. (see AUTHORS)
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA. */
#ifndef _TIMED_THREAD_H_
#define _TIMED_THREAD_H_
#include <stdlib.h>
#include <chrono>
#include <functional>
#include <memory>
/* 10000 microseconds = 10 ms = 0.01 sec */
const unsigned int MINIMUM_INTERVAL_USECS = 10000;
/* private data */
typedef struct _timed_thread _timed_thread;
class timed_thread;
typedef std::shared_ptr<timed_thread> timed_thread_ptr;
namespace std { class mutex; }
class thread_handle {
/* this class is passed to threaded functions allowing them to control
* specific thread aspects */
public:
virtual ~thread_handle() {}
int test(int override_wait_time);
std::mutex &mutex();
void lock(void);
void unlock(void);
int readfd(void) const;
private:
thread_handle(timed_thread *thread) : thread(thread) {}
friend class timed_thread;
timed_thread *thread;
};
class timed_thread {
public:
/* create a timed thread (object creation only) */
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);
}
return ptr;
}
virtual ~timed_thread(void) {
destroy();
}
/* run a timed thread (drop the thread and run it) */
int run(void);
/* lock a timed thread for critical section activity */
void lock(void);
/* unlock a timed thread after critical section activity */
void unlock(void);
/* returns the critical section mutex */
std::mutex &mutex();
/* destroy all registered timed threads */
static void destroy_registered_threads(void);
/* returns read file descriptor for thread pipe */
int readfd(void) const;
private:
/* create a timed thread (object creation only) */
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
* if the thread has been asked kindly to die. */
int test(int override_wait_time);
/* destroy a timed thread */
void destroy();
/* register a timed thread for destruction */
static int register_(const timed_thread_ptr &timed_thread);
/* de-register a timed thread for destruction */
static void deregister(const timed_thread *timed_thread);
/* private internal data */
std::auto_ptr<_timed_thread> p_timed_thread;
thread_handle p_thread_handle;
std::chrono::microseconds interval_usecs;
bool running;
friend class thread_handle;
};
#endif /* #ifdef _TIMED_THREAD_H_ */