mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-11-18 02:55:12 +00:00
Implement timed_thread rewrite in C++.
Rewrote timed_thread library in C++ using fancy new C++0x features. The main reason for this is to phase out poor encapsulation and C-style function pointers.
This commit is contained in:
parent
58cbbf57a3
commit
01ac47d082
@ -97,6 +97,10 @@ endif(NOT RELEASE)
|
||||
|
||||
mark_as_advanced(APP_GAWK APP_WC APP_DATE APP_UNAME)
|
||||
|
||||
if(CMAKE_BUILD_TYPE MATCHES "Debug")
|
||||
set(DEBUG true)
|
||||
endif(CMAKE_BUILD_TYPE MATCHES "Debug")
|
||||
|
||||
# The version numbers are simply derived from the date and number of commits
|
||||
# since start of month
|
||||
if(DEBUG)
|
||||
|
@ -43,10 +43,6 @@ if(MAINTAINER_MODE)
|
||||
endif(MAINTAINER_MODE)
|
||||
|
||||
|
||||
if(CMAKE_BUILD_TYPE MATCHES "Debug")
|
||||
set(DEBUG true)
|
||||
endif(CMAKE_BUILD_TYPE MATCHES "Debug")
|
||||
|
||||
option(RELEASE "Build release package" false)
|
||||
mark_as_advanced(RELEASE)
|
||||
|
||||
|
@ -35,7 +35,7 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build.h)
|
||||
endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build.h)
|
||||
|
||||
set(conky_sources colours.c combine.c common.c conky.cc core.cc diskio.c
|
||||
entropy.c exec.c fs.c mail.c mixer.c net_stat.c template.c timed_thread.c
|
||||
entropy.c exec.cc fs.c mail.cc mixer.c net_stat.c template.c timed_thread.cc
|
||||
mboxscan.c read_tcp.c scroll.c specials.c tailhead.c temphelper.c
|
||||
text_object.c timeinfo.c top.c algebra.c prioqueue.c proc.c user.c)
|
||||
|
||||
@ -84,12 +84,12 @@ if(BUILD_IBM)
|
||||
endif(BUILD_IBM)
|
||||
|
||||
if(BUILD_MPD)
|
||||
set(mpd mpd.c libmpdclient.c)
|
||||
set(mpd mpd.cc libmpdclient.c)
|
||||
set(optional_sources ${optional_sources} ${mpd})
|
||||
endif(BUILD_MPD)
|
||||
|
||||
if(BUILD_MOC)
|
||||
set(moc moc.c)
|
||||
set(moc moc.cc)
|
||||
set(optional_sources ${optional_sources} ${moc})
|
||||
endif(BUILD_MOC)
|
||||
|
||||
|
@ -33,10 +33,9 @@
|
||||
#include "conky.h"
|
||||
#include "common.h"
|
||||
#include "timed_thread.h"
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <locale.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
@ -787,7 +786,7 @@ void evaluate(const char *text, char *p, int p_max_size)
|
||||
struct text_object subroot;
|
||||
|
||||
parse_conky_vars(&subroot, text, p, p_max_size);
|
||||
DBGP("evaluated '%s' to '%s'", text, p);
|
||||
DBGP2("evaluated '%s' to '%s'", text, p);
|
||||
|
||||
free_text_objects(&subroot);
|
||||
}
|
||||
@ -2374,7 +2373,7 @@ void clean_up(void *memtofree1, void* memtofree2)
|
||||
if(memtofree2) {
|
||||
free(memtofree2);
|
||||
}
|
||||
timed_thread_destroy_registered_threads();
|
||||
timed_thread::destroy_registered_threads();
|
||||
|
||||
if (info.cpu_usage) {
|
||||
free(info.cpu_usage);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
||||
* vim: ts=4 sw=4 noet ai cindent syntax=c
|
||||
/* -*- 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
|
||||
*
|
||||
@ -38,6 +38,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <mutex>
|
||||
|
||||
struct execi_data {
|
||||
double last_update;
|
||||
@ -45,8 +46,9 @@ struct execi_data {
|
||||
char *cmd;
|
||||
char *buffer;
|
||||
double data;
|
||||
timed_thread *p_timed_thread;
|
||||
timed_thread_ptr p_timed_thread;
|
||||
float barnum;
|
||||
execi_data() : last_update(0), interval(0), cmd(0), data(0), barnum(0) {}
|
||||
};
|
||||
|
||||
/* FIXME: this will probably not work, since the variable is being reused
|
||||
@ -175,7 +177,7 @@ static double read_exec_barnum(const char *data)
|
||||
char *buf = NULL;
|
||||
double barnum;
|
||||
|
||||
buf = malloc(text_buffer_size);
|
||||
buf = (char*)malloc(text_buffer_size);
|
||||
|
||||
read_exec(data, buf, text_buffer_size);
|
||||
barnum = get_barnum(buf);
|
||||
@ -184,16 +186,13 @@ static double read_exec_barnum(const char *data)
|
||||
return barnum;
|
||||
}
|
||||
|
||||
static void *threaded_exec(void *) __attribute__((noreturn));
|
||||
|
||||
static void *threaded_exec(void *arg)
|
||||
static void threaded_exec(thread_handle &handle, struct text_object *obj)
|
||||
{
|
||||
char *buff, *p2;
|
||||
struct text_object *obj = arg;
|
||||
struct execi_data *ed = obj->data.opaque;
|
||||
struct execi_data *ed = (struct execi_data *)obj->data.opaque;
|
||||
|
||||
while (1) {
|
||||
buff = malloc(text_buffer_size);
|
||||
buff = (char*)malloc(text_buffer_size);
|
||||
read_exec(ed->cmd, buff, text_buffer_size);
|
||||
p2 = buff;
|
||||
while (*p2) {
|
||||
@ -202,13 +201,15 @@ static void *threaded_exec(void *arg)
|
||||
}
|
||||
p2++;
|
||||
}
|
||||
timed_thread_lock(ed->p_timed_thread);
|
||||
if (ed->buffer)
|
||||
free(ed->buffer);
|
||||
ed->buffer = buff;
|
||||
timed_thread_unlock(ed->p_timed_thread);
|
||||
if (timed_thread_test(ed->p_timed_thread, 0)) {
|
||||
timed_thread_exit(ed->p_timed_thread);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(handle.mutex());
|
||||
if (ed->buffer)
|
||||
free(ed->buffer);
|
||||
ed->buffer = buff;
|
||||
}
|
||||
if (handle.test(0)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* never reached */
|
||||
@ -244,8 +245,7 @@ void scan_execi_arg(struct text_object *obj, const char *arg)
|
||||
struct execi_data *ed;
|
||||
int n;
|
||||
|
||||
ed = malloc(sizeof(struct execi_data));
|
||||
memset(ed, 0, sizeof(struct execi_data));
|
||||
ed = new execi_data;
|
||||
|
||||
if (sscanf(arg, "%f %n", &ed->interval, &n) <= 0) {
|
||||
NORM_ERR("${execi* <interval> command}");
|
||||
@ -276,7 +276,7 @@ void scan_execgraph_arg(struct text_object *obj, const char *arg)
|
||||
struct execi_data *ed;
|
||||
char *buf;
|
||||
|
||||
ed = malloc(sizeof(struct execi_data));
|
||||
ed = new execi_data;
|
||||
memset(ed, 0, sizeof(struct execi_data));
|
||||
|
||||
buf = scan_graph(obj, arg, 100);
|
||||
@ -300,7 +300,7 @@ void print_execp(struct text_object *obj, char *p, int p_max_size)
|
||||
struct text_object subroot;
|
||||
char *buf;
|
||||
|
||||
buf = malloc(text_buffer_size);
|
||||
buf = (char*)malloc(text_buffer_size);
|
||||
memset(buf, 0, text_buffer_size);
|
||||
|
||||
read_exec(obj->data.s, buf, text_buffer_size);
|
||||
@ -312,14 +312,14 @@ void print_execp(struct text_object *obj, char *p, int p_max_size)
|
||||
|
||||
void print_execi(struct text_object *obj, char *p, int p_max_size)
|
||||
{
|
||||
struct execi_data *ed = obj->data.opaque;
|
||||
struct execi_data *ed = (struct execi_data *)obj->data.opaque;
|
||||
|
||||
if (!ed)
|
||||
return;
|
||||
|
||||
if (time_to_update(ed)) {
|
||||
if (!ed->buffer)
|
||||
ed->buffer = malloc(text_buffer_size);
|
||||
ed->buffer = (char*)malloc(text_buffer_size);
|
||||
read_exec(ed->cmd, ed->buffer, text_buffer_size);
|
||||
ed->last_update = current_update_time;
|
||||
}
|
||||
@ -328,7 +328,7 @@ void print_execi(struct text_object *obj, char *p, int p_max_size)
|
||||
|
||||
void print_execpi(struct text_object *obj, char *p, int p_max_size)
|
||||
{
|
||||
struct execi_data *ed = obj->data.opaque;
|
||||
struct execi_data *ed = (struct execi_data *)obj->data.opaque;
|
||||
struct text_object subroot;
|
||||
|
||||
if (!ed)
|
||||
@ -336,7 +336,7 @@ void print_execpi(struct text_object *obj, char *p, int p_max_size)
|
||||
|
||||
if (time_to_update(ed)) {
|
||||
if (!ed->buffer)
|
||||
ed->buffer = malloc(text_buffer_size);
|
||||
ed->buffer = (char*)malloc(text_buffer_size);
|
||||
|
||||
read_exec(ed->cmd, ed->buffer, text_buffer_size);
|
||||
ed->last_update = current_update_time;
|
||||
@ -347,28 +347,23 @@ void print_execpi(struct text_object *obj, char *p, int p_max_size)
|
||||
|
||||
void print_texeci(struct text_object *obj, char *p, int p_max_size)
|
||||
{
|
||||
struct execi_data *ed = obj->data.opaque;
|
||||
struct execi_data *ed = (struct execi_data *)obj->data.opaque;
|
||||
|
||||
if (!ed)
|
||||
return;
|
||||
|
||||
if (!ed->p_timed_thread) {
|
||||
ed->p_timed_thread = timed_thread_create(&threaded_exec,
|
||||
(void *) obj, ed->interval * 1000000);
|
||||
if (!ed->p_timed_thread) {
|
||||
NORM_ERR("Error creating texeci timed thread");
|
||||
}
|
||||
/*
|
||||
* note that we don't register this thread with the
|
||||
* timed_thread list, because we destroy it manually
|
||||
*/
|
||||
if (timed_thread_run(ed->p_timed_thread)) {
|
||||
NORM_ERR("Error running texeci timed thread");
|
||||
ed->p_timed_thread = timed_thread::create(std::bind(threaded_exec, std::placeholders::_1, obj), ed->interval * 1000000, false);
|
||||
if (!ed->p_timed_thread) {
|
||||
NORM_ERR("Error creating texeci timed thread");
|
||||
}
|
||||
} else {
|
||||
timed_thread_lock(ed->p_timed_thread);
|
||||
std::lock_guard<std::mutex> lock(ed->p_timed_thread->mutex());
|
||||
snprintf(p, p_max_size, "%s", ed->buffer);
|
||||
timed_thread_unlock(ed->p_timed_thread);
|
||||
}
|
||||
}
|
||||
|
||||
@ -379,7 +374,7 @@ double execbarval(struct text_object *obj)
|
||||
|
||||
double execi_barval(struct text_object *obj)
|
||||
{
|
||||
struct execi_data *ed = obj->data.opaque;
|
||||
struct execi_data *ed = (struct execi_data *)obj->data.opaque;
|
||||
|
||||
if (!ed)
|
||||
return 0;
|
||||
@ -401,13 +396,14 @@ void free_exec(struct text_object *obj)
|
||||
|
||||
void free_execi(struct text_object *obj)
|
||||
{
|
||||
struct execi_data *ed = obj->data.opaque;
|
||||
struct execi_data *ed = (struct execi_data *)obj->data.opaque;
|
||||
|
||||
if (!ed)
|
||||
return;
|
||||
|
||||
if (ed->p_timed_thread)
|
||||
timed_thread_destroy(ed->p_timed_thread, &ed->p_timed_thread);
|
||||
if (ed->p_timed_thread) {
|
||||
ed->p_timed_thread.reset();
|
||||
}
|
||||
if (ed->cmd)
|
||||
free(ed->cmd);
|
||||
if (ed->buffer)
|
@ -31,10 +31,6 @@
|
||||
#ifndef _EXEC_H
|
||||
#define _EXEC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern pid_t childpid;
|
||||
|
||||
void scan_exec_arg(struct text_object *, const char *);
|
||||
@ -52,8 +48,5 @@ double execbarval(struct text_object *);
|
||||
double execi_barval(struct text_object *);
|
||||
void free_exec(struct text_object *);
|
||||
void free_execi(struct text_object *);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _EXEC_H */
|
||||
|
@ -31,7 +31,7 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <tr1/unordered_map>
|
||||
#include <unordered_map>
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
* IMPLEMENTATION INTERFACE
|
||||
@ -85,7 +85,7 @@ namespace {
|
||||
* The second parameter provides the mechanism for removing connections if
|
||||
* they are not seen again in subsequent update cycles.
|
||||
* ------------------------------------------------------------------------ */
|
||||
typedef std::tr1::unordered_map<tcp_connection_t, int, tcp_connection_hash> connection_hash_t;
|
||||
typedef std::unordered_map<tcp_connection_t, int, tcp_connection_hash> connection_hash_t;
|
||||
|
||||
/* start and end of port monitor range. Set start=end to monitor a single port */
|
||||
typedef std::pair<in_port_t, in_port_t> port_range_t;
|
||||
@ -98,7 +98,7 @@ namespace {
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::tr1::unordered_map<port_range_t,
|
||||
typedef std::unordered_map<port_range_t,
|
||||
tcp_port_monitor_t,
|
||||
port_range_hash> monitor_hash_t;
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
||||
* vim: ts=4 sw=4 noet ai cindent syntax=c
|
||||
/* -*- 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
|
||||
*
|
||||
@ -40,6 +40,7 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <mutex>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
@ -75,8 +76,16 @@ struct mail_s { // for imap and pop3
|
||||
char pass[128];
|
||||
char command[1024];
|
||||
char folder[128];
|
||||
timed_thread *p_timed_thread;
|
||||
timed_thread_ptr p_timed_thread;
|
||||
char secure;
|
||||
mail_s() : unseen(0), messages(0), used(0), quota(0), port(0), retries(0),
|
||||
interval(0), last_update(0), secure(0) {
|
||||
host[0] = 0;
|
||||
user[0] = 0;
|
||||
pass[0] = 0;
|
||||
command[0] = 0;
|
||||
folder[0] = 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct local_mail_s {
|
||||
@ -361,7 +370,7 @@ void parse_local_mail_args(struct text_object *obj, const char *arg)
|
||||
|
||||
variable_substitute(mbox, dst, sizeof(dst));
|
||||
|
||||
locmail = malloc(sizeof(struct local_mail_s));
|
||||
locmail = (struct local_mail_s*)malloc(sizeof(struct local_mail_s));
|
||||
memset(locmail, 0, sizeof(struct local_mail_s));
|
||||
locmail->mbox = strndup(dst, text_buffer_size);
|
||||
locmail->interval = n1;
|
||||
@ -371,7 +380,7 @@ void parse_local_mail_args(struct text_object *obj, const char *arg)
|
||||
#define PRINT_MAILS_GENERATOR(x) \
|
||||
void print_##x##mails(struct text_object *obj, char *p, int p_max_size) \
|
||||
{ \
|
||||
struct local_mail_s *locmail = obj->data.opaque; \
|
||||
struct local_mail_s *locmail = (struct local_mail_s *)obj->data.opaque; \
|
||||
if (!locmail) \
|
||||
return; \
|
||||
update_mail_count(locmail); \
|
||||
@ -393,7 +402,7 @@ PRINT_MAILS_GENERATOR(trashed_)
|
||||
|
||||
void free_local_mails(struct text_object *obj)
|
||||
{
|
||||
struct local_mail_s *locmail = obj->data.opaque;
|
||||
struct local_mail_s *locmail = (struct local_mail_s *)obj->data.opaque;
|
||||
|
||||
if (!locmail)
|
||||
return;
|
||||
@ -411,8 +420,7 @@ struct mail_s *parse_mail_args(char type, const char *arg)
|
||||
struct mail_s *mail;
|
||||
char *tmp;
|
||||
|
||||
mail = malloc(sizeof(struct mail_s));
|
||||
memset(mail, 0, sizeof(struct mail_s));
|
||||
mail = new mail_s;
|
||||
|
||||
if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
|
||||
!= 3) {
|
||||
@ -439,21 +447,21 @@ struct mail_s *parse_mail_args(char type, const char *arg)
|
||||
tcsetattr(fp, TCSANOW, &term);
|
||||
}
|
||||
// now we check for optional args
|
||||
tmp = strstr(arg, "-r ");
|
||||
tmp = (char*)strstr(arg, "-r ");
|
||||
if (tmp) {
|
||||
tmp += 3;
|
||||
sscanf(tmp, "%u", &mail->retries);
|
||||
} else {
|
||||
mail->retries = 5; // 5 retries after failure
|
||||
}
|
||||
tmp = strstr(arg, "-i ");
|
||||
tmp = (char*)strstr(arg, "-i ");
|
||||
if (tmp) {
|
||||
tmp += 3;
|
||||
sscanf(tmp, "%f", &mail->interval);
|
||||
} else {
|
||||
mail->interval = 300; // 5 minutes
|
||||
}
|
||||
tmp = strstr(arg, "-p ");
|
||||
tmp = (char*)strstr(arg, "-p ");
|
||||
if (tmp) {
|
||||
tmp += 3;
|
||||
sscanf(tmp, "%lu", &mail->port);
|
||||
@ -465,12 +473,12 @@ struct mail_s *parse_mail_args(char type, const char *arg)
|
||||
}
|
||||
}
|
||||
if (type == IMAP_TYPE) {
|
||||
tmp = strstr(arg, "-f ");
|
||||
tmp = (char*)strstr(arg, "-f ");
|
||||
if (tmp) {
|
||||
int len = 1024;
|
||||
tmp += 3;
|
||||
if (tmp[0] == '\'') {
|
||||
len = strstr(tmp + 1, "'") - tmp - 1;
|
||||
len = (char*)strstr(tmp + 1, "'") - tmp - 1;
|
||||
if (len > 1024) {
|
||||
len = 1024;
|
||||
}
|
||||
@ -480,13 +488,13 @@ struct mail_s *parse_mail_args(char type, const char *arg)
|
||||
strncpy(mail->folder, "INBOX", 128); // default imap inbox
|
||||
}
|
||||
}
|
||||
tmp = strstr(arg, "-e ");
|
||||
tmp = (char*)strstr(arg, "-e ");
|
||||
if (tmp) {
|
||||
int len = 1024;
|
||||
tmp += 3;
|
||||
|
||||
if (tmp[0] == '\'') {
|
||||
len = strstr(tmp + 1, "'") - tmp - 1;
|
||||
len = (char*)strstr(tmp + 1, "'") - tmp - 1;
|
||||
if (len > 1024) {
|
||||
len = 1024;
|
||||
}
|
||||
@ -495,7 +503,6 @@ struct mail_s *parse_mail_args(char type, const char *arg)
|
||||
} else {
|
||||
mail->command[0] = '\0';
|
||||
}
|
||||
mail->p_timed_thread = NULL;
|
||||
return mail;
|
||||
}
|
||||
|
||||
@ -558,11 +565,12 @@ void free_mail_obj(struct text_object *obj)
|
||||
|
||||
if (obj->data.opaque == global_mail) {
|
||||
if (--global_mail_use == 0) {
|
||||
free(global_mail);
|
||||
delete global_mail;
|
||||
global_mail = 0;
|
||||
}
|
||||
} else {
|
||||
free(obj->data.opaque);
|
||||
struct mail_s *mail = (struct mail_s*)obj->data.opaque;
|
||||
delete mail;
|
||||
obj->data.opaque = 0;
|
||||
}
|
||||
}
|
||||
@ -596,10 +604,10 @@ int imap_command(int sockfd, const char *command, char *response, const char *ve
|
||||
return 0;
|
||||
}
|
||||
|
||||
int imap_check_status(char *recvbuf, struct mail_s *mail)
|
||||
int imap_check_status(char *recvbuf, struct mail_s *mail, thread_handle &handle)
|
||||
{
|
||||
char *reply;
|
||||
reply = strstr(recvbuf, " (MESSAGES ");
|
||||
reply = (char*)strstr(recvbuf, " (MESSAGES ");
|
||||
if (!reply || strlen(reply) < 2) {
|
||||
return -1;
|
||||
}
|
||||
@ -609,10 +617,9 @@ int imap_check_status(char *recvbuf, struct mail_s *mail)
|
||||
NORM_ERR("Error parsing IMAP response: %s", recvbuf);
|
||||
return -1;
|
||||
} else {
|
||||
timed_thread_lock(mail->p_timed_thread);
|
||||
std::lock_guard<std::mutex> lock(handle.mutex());
|
||||
sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
|
||||
&mail->unseen);
|
||||
timed_thread_unlock(mail->p_timed_thread);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -629,24 +636,18 @@ void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned
|
||||
}
|
||||
|
||||
static void ensure_mail_thread(struct mail_s *mail,
|
||||
void *thread(void *), const char *text)
|
||||
const std::function<void(thread_handle &, struct mail_s *mail)> &func, const char *text)
|
||||
{
|
||||
if (mail->p_timed_thread)
|
||||
return;
|
||||
|
||||
mail->p_timed_thread = timed_thread_create(thread,
|
||||
mail, mail->interval * 1000000);
|
||||
mail->p_timed_thread = timed_thread::create(std::bind(func, std::placeholders::_1, mail), mail->interval * 1000000);
|
||||
if (!mail->p_timed_thread) {
|
||||
NORM_ERR("Error creating %s timed thread", text);
|
||||
}
|
||||
timed_thread_register(mail->p_timed_thread,
|
||||
&mail->p_timed_thread);
|
||||
if (timed_thread_run(mail->p_timed_thread)) {
|
||||
NORM_ERR("Error running %s timed thread", text);
|
||||
}
|
||||
}
|
||||
|
||||
static void *imap_thread(void *arg)
|
||||
static void imap_thread(thread_handle &handle, struct mail_s *mail)
|
||||
{
|
||||
int sockfd, numbytes;
|
||||
char recvbuf[MAXDATASIZE];
|
||||
@ -657,9 +658,8 @@ static void *imap_thread(void *arg)
|
||||
struct stat stat_buf;
|
||||
struct hostent *he_res = 0;
|
||||
struct sockaddr_in their_addr; // connector's address information
|
||||
struct mail_s *mail = (struct mail_s *)arg;
|
||||
int has_idle = 0;
|
||||
int threadfd = timed_thread_readfd(mail->p_timed_thread);
|
||||
int threadfd = handle.readfd();
|
||||
char resolved_host = 0;
|
||||
|
||||
while (fail < mail->retries) {
|
||||
@ -764,7 +764,7 @@ static void *imap_thread(void *arg)
|
||||
break;
|
||||
}
|
||||
|
||||
if (imap_check_status(recvbuf, mail)) {
|
||||
if (imap_check_status(recvbuf, mail, handle)) {
|
||||
fail++;
|
||||
break;
|
||||
}
|
||||
@ -801,12 +801,12 @@ static void *imap_thread(void *arg)
|
||||
FD_SET(sockfd, &fdset);
|
||||
FD_SET(threadfd, &fdset);
|
||||
res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, &fetchtimeout);
|
||||
if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
|
||||
if (handle.test(1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
|
||||
if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
|
||||
/* if a valid socket, close it */
|
||||
close(sockfd);
|
||||
}
|
||||
timed_thread_exit(mail->p_timed_thread);
|
||||
return;
|
||||
} else if (res > 0) {
|
||||
if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
|
||||
perror("recv idling");
|
||||
@ -823,9 +823,9 @@ static void *imap_thread(void *arg)
|
||||
unsigned long messages, recent = 0;
|
||||
char *buf = recvbuf;
|
||||
char force_check = 0;
|
||||
buf = strstr(buf, "EXISTS");
|
||||
buf = (char*)strstr(buf, "EXISTS");
|
||||
while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
|
||||
buf = strstr(buf + 1, "EXISTS");
|
||||
buf = (char*)strstr(buf + 1, "EXISTS");
|
||||
}
|
||||
if (buf) {
|
||||
// back up until we reach '*'
|
||||
@ -833,18 +833,17 @@ static void *imap_thread(void *arg)
|
||||
buf--;
|
||||
}
|
||||
if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
|
||||
timed_thread_lock(mail->p_timed_thread);
|
||||
std::lock_guard<std::mutex> lock(handle.mutex());
|
||||
if (mail->messages != messages) {
|
||||
force_check = 1;
|
||||
mail->messages = messages;
|
||||
}
|
||||
timed_thread_unlock(mail->p_timed_thread);
|
||||
}
|
||||
}
|
||||
buf = recvbuf;
|
||||
buf = strstr(buf, "RECENT");
|
||||
buf = (char*)strstr(buf, "RECENT");
|
||||
while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
|
||||
buf = strstr(buf + 1, "RECENT");
|
||||
buf = (char*)strstr(buf + 1, "RECENT");
|
||||
}
|
||||
if (buf) {
|
||||
// back up until we reach '*'
|
||||
@ -874,7 +873,7 @@ static void *imap_thread(void *arg)
|
||||
fail++;
|
||||
break;
|
||||
}
|
||||
if (imap_check_status(recvbuf, mail)) {
|
||||
if (imap_check_status(recvbuf, mail, handle)) {
|
||||
fail++;
|
||||
break;
|
||||
}
|
||||
@ -934,44 +933,41 @@ static void *imap_thread(void *arg)
|
||||
/* if a valid socket, close it */
|
||||
close(sockfd);
|
||||
}
|
||||
if (timed_thread_test(mail->p_timed_thread, 0)) {
|
||||
timed_thread_exit(mail->p_timed_thread);
|
||||
if (handle.test(0)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
mail->unseen = 0;
|
||||
mail->messages = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_imap_unseen(struct text_object *obj, char *p, int p_max_size)
|
||||
{
|
||||
struct mail_s *mail = obj->data.opaque;
|
||||
struct mail_s *mail = (struct mail_s*)obj->data.opaque;
|
||||
|
||||
if (!mail)
|
||||
return;
|
||||
|
||||
ensure_mail_thread(mail, imap_thread, "imap");
|
||||
ensure_mail_thread(mail, std::bind(imap_thread, std::placeholders::_1, std::placeholders::_2), "imap");
|
||||
|
||||
if (mail && mail->p_timed_thread) {
|
||||
timed_thread_lock(mail->p_timed_thread);
|
||||
std::lock_guard<std::mutex> lock(mail->p_timed_thread->mutex());
|
||||
snprintf(p, p_max_size, "%lu", mail->unseen);
|
||||
timed_thread_unlock(mail->p_timed_thread);
|
||||
}
|
||||
}
|
||||
|
||||
void print_imap_messages(struct text_object *obj, char *p, int p_max_size)
|
||||
{
|
||||
struct mail_s *mail = obj->data.opaque;
|
||||
struct mail_s *mail = (struct mail_s*)obj->data.opaque;
|
||||
|
||||
if (!mail)
|
||||
return;
|
||||
|
||||
ensure_mail_thread(mail, imap_thread, "imap");
|
||||
ensure_mail_thread(mail, std::bind(imap_thread, std::placeholders::_1, std::placeholders::_2), "imap");
|
||||
|
||||
if (mail && mail->p_timed_thread) {
|
||||
timed_thread_lock(mail->p_timed_thread);
|
||||
std::lock_guard<std::mutex> lock(mail->p_timed_thread->mutex());
|
||||
snprintf(p, p_max_size, "%lu", mail->messages);
|
||||
timed_thread_unlock(mail->p_timed_thread);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1003,7 +999,7 @@ int pop3_command(int sockfd, const char *command, char *response, const char *ve
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *pop3_thread(void *arg)
|
||||
static void pop3_thread(thread_handle &handle, struct mail_s *mail)
|
||||
{
|
||||
int sockfd, numbytes;
|
||||
char recvbuf[MAXDATASIZE];
|
||||
@ -1014,7 +1010,6 @@ static void *pop3_thread(void *arg)
|
||||
struct stat stat_buf;
|
||||
struct hostent *he_res = 0;
|
||||
struct sockaddr_in their_addr; // connector's address information
|
||||
struct mail_s *mail = (struct mail_s *)arg;
|
||||
char resolved_host = 0;
|
||||
|
||||
while (fail < mail->retries) {
|
||||
@ -1121,9 +1116,8 @@ static void *pop3_thread(void *arg)
|
||||
fail++;
|
||||
break;
|
||||
} else {
|
||||
timed_thread_lock(mail->p_timed_thread);
|
||||
std::lock_guard<std::mutex> lock(handle.mutex());
|
||||
sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
|
||||
timed_thread_unlock(mail->p_timed_thread);
|
||||
}
|
||||
|
||||
strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
|
||||
@ -1146,44 +1140,41 @@ static void *pop3_thread(void *arg)
|
||||
/* if a valid socket, close it */
|
||||
close(sockfd);
|
||||
}
|
||||
if (timed_thread_test(mail->p_timed_thread, 0)) {
|
||||
timed_thread_exit(mail->p_timed_thread);
|
||||
if (handle.test(0)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
mail->unseen = 0;
|
||||
mail->used = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_pop3_unseen(struct text_object *obj, char *p, int p_max_size)
|
||||
{
|
||||
struct mail_s *mail = obj->data.opaque;
|
||||
struct mail_s *mail = (struct mail_s *)obj->data.opaque;
|
||||
|
||||
if (!mail)
|
||||
return;
|
||||
|
||||
ensure_mail_thread(mail, pop3_thread, "pop3");
|
||||
ensure_mail_thread(mail, std::bind(pop3_thread, std::placeholders::_1, std::placeholders::_2), "pop3");
|
||||
|
||||
if (mail && mail->p_timed_thread) {
|
||||
timed_thread_lock(mail->p_timed_thread);
|
||||
std::lock_guard<std::mutex> lock(mail->p_timed_thread->mutex());
|
||||
snprintf(p, p_max_size, "%lu", mail->unseen);
|
||||
timed_thread_unlock(mail->p_timed_thread);
|
||||
}
|
||||
}
|
||||
|
||||
void print_pop3_used(struct text_object *obj, char *p, int p_max_size)
|
||||
{
|
||||
struct mail_s *mail = obj->data.opaque;
|
||||
struct mail_s *mail = (struct mail_s *)obj->data.opaque;
|
||||
|
||||
if (!mail)
|
||||
return;
|
||||
|
||||
ensure_mail_thread(mail, pop3_thread, "pop3");
|
||||
ensure_mail_thread(mail, std::bind(pop3_thread, std::placeholders::_1, std::placeholders::_2), "pop3");
|
||||
|
||||
if (mail && mail->p_timed_thread) {
|
||||
timed_thread_lock(mail->p_timed_thread);
|
||||
std::lock_guard<std::mutex> lock(mail->p_timed_thread->mutex());
|
||||
snprintf(p, p_max_size, "%.1f",
|
||||
mail->used / 1024.0 / 1024.0);
|
||||
timed_thread_unlock(mail->p_timed_thread);
|
||||
}
|
||||
}
|
@ -3,10 +3,6 @@
|
||||
#ifndef _MAIL_H
|
||||
#define _MAIL_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern char *current_mail_spool;
|
||||
|
||||
void parse_local_mail_args(struct text_object *, const char *);
|
||||
@ -40,8 +36,4 @@ void print_imap_messages(struct text_object *, char *, int);
|
||||
void print_pop3_unseen(struct text_object *, char *, int);
|
||||
void print_pop3_used(struct text_object *, char *, int);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _MAIL_H */
|
||||
|
@ -23,10 +23,6 @@
|
||||
#ifndef MOC_H_
|
||||
#define MOC_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void update_moc(void);
|
||||
void free_moc(struct text_object *);
|
||||
|
||||
@ -42,9 +38,5 @@ void print_moc_curtime(struct text_object *, char *, int);
|
||||
void print_moc_bitrate(struct text_object *, char *, int);
|
||||
void print_moc_rate(struct text_object *, char *, int);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MOC_H_ */
|
||||
|
||||
|
@ -3,10 +3,6 @@
|
||||
#ifndef MPD_H_
|
||||
#define MPD_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* functions for setting the configuration values */
|
||||
void mpd_set_host(const char *);
|
||||
void mpd_set_password(const char *, int);
|
||||
@ -36,8 +32,4 @@ void print_mpd_bitrate(struct text_object *, char *, int);
|
||||
void print_mpd_status(struct text_object *, char *, int);
|
||||
int check_mpd_playing(struct text_object *);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*MPD_H_*/
|
||||
|
@ -1,320 +0,0 @@
|
||||
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
||||
* vim: ts=4 sw=4 noet ai cindent syntax=c
|
||||
*
|
||||
* timed_thread.c: Abstraction layer for timed threads
|
||||
*
|
||||
* Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <pthread.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#ifndef HAVE_CLOCK_GETTIME
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#include "timed_thread.h"
|
||||
|
||||
/* Abstraction layer for timed threads */
|
||||
|
||||
static int now(struct timespec *);
|
||||
|
||||
/* private */
|
||||
struct _timed_thread {
|
||||
pthread_t thread; /* thread itself */
|
||||
pthread_attr_t thread_attr; /* thread attributes */
|
||||
pthread_mutex_t cs_mutex; /* critical section mutex */
|
||||
pthread_mutex_t runnable_mutex; /* only for the runnable_cond */
|
||||
pthread_cond_t runnable_cond; /* signalled to stop the thread */
|
||||
void *(*start_routine)(void *); /* thread function to run */
|
||||
void *arg; /* thread function argument */
|
||||
struct timespec interval_time; /* interval_usecs as a struct timespec */
|
||||
struct timespec wait_time; /* absolute future time next timed_thread_test will wait until */
|
||||
int pipefd[2];
|
||||
int die;
|
||||
};
|
||||
|
||||
/* linked list of created threads */
|
||||
typedef struct _timed_thread_list {
|
||||
timed_thread *p_timed_thread;
|
||||
timed_thread **addr_of_p_timed_thread;
|
||||
struct _timed_thread_list *next;
|
||||
} timed_thread_node, timed_thread_list;
|
||||
|
||||
static timed_thread_list *p_timed_thread_list_head = NULL;
|
||||
static timed_thread_list *p_timed_thread_list_tail = NULL;
|
||||
|
||||
int timed_thread_readfd(timed_thread *p_timed_thread)
|
||||
{
|
||||
return p_timed_thread->pipefd[0];
|
||||
}
|
||||
|
||||
static int now(struct timespec *abstime)
|
||||
{
|
||||
#ifndef HAVE_CLOCK_GETTIME
|
||||
struct timeval tv;
|
||||
#endif
|
||||
|
||||
if (!abstime) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef HAVE_CLOCK_GETTIME
|
||||
return clock_gettime(CLOCK_REALTIME, abstime);
|
||||
#else
|
||||
/* fallback to gettimeofday () */
|
||||
if (gettimeofday(&tv, NULL) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
abstime->tv_sec = tv.tv_sec;
|
||||
abstime->tv_nsec = tv.tv_usec * 1000;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* create a timed thread (object creation only) */
|
||||
timed_thread *timed_thread_create(void *start_routine(void *), void *arg,
|
||||
unsigned int interval_usecs)
|
||||
{
|
||||
timed_thread *p_timed_thread;
|
||||
|
||||
assert(start_routine != NULL);
|
||||
assert(interval_usecs >= MINIMUM_INTERVAL_USECS);
|
||||
|
||||
if ((p_timed_thread = calloc(sizeof(timed_thread), 1)) == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* create thread pipe (used to tell threads to die) */
|
||||
if (pipe(p_timed_thread->pipefd)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* init attributes, e.g. joinable thread */
|
||||
pthread_attr_init(&p_timed_thread->thread_attr);
|
||||
pthread_attr_setdetachstate(&p_timed_thread->thread_attr,
|
||||
PTHREAD_CREATE_JOINABLE);
|
||||
/* init mutexes */
|
||||
pthread_mutex_init(&p_timed_thread->cs_mutex, NULL);
|
||||
pthread_mutex_init(&p_timed_thread->runnable_mutex, NULL);
|
||||
/* init cond */
|
||||
pthread_cond_init(&p_timed_thread->runnable_cond, NULL);
|
||||
|
||||
p_timed_thread->start_routine = start_routine;
|
||||
p_timed_thread->arg = arg;
|
||||
|
||||
/* set wait time to current time */
|
||||
if (now(&p_timed_thread->wait_time)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* seconds portion of the microseconds interval */
|
||||
p_timed_thread->interval_time.tv_sec = (time_t) (interval_usecs / 1000000);
|
||||
/* remaining microseconds convert to nanoseconds */
|
||||
p_timed_thread->interval_time.tv_nsec =
|
||||
(long) ((interval_usecs % 1000000) * 1000);
|
||||
|
||||
/* printf("interval_time.tv_sec = %li, .tv_nsec = %li\n",
|
||||
p_timed_thread->interval_time.tv_sec,
|
||||
p_timed_thread->interval_time.tv_nsec); */
|
||||
return p_timed_thread;
|
||||
}
|
||||
|
||||
/* run a timed thread (drop the thread and run it) */
|
||||
int timed_thread_run(timed_thread *p_timed_thread)
|
||||
{
|
||||
return pthread_create(&p_timed_thread->thread, &p_timed_thread->thread_attr,
|
||||
p_timed_thread->start_routine, p_timed_thread->arg);
|
||||
}
|
||||
|
||||
/* destroy a timed thread.
|
||||
* optional addr_of_p_timed_thread to set callers pointer to NULL as a
|
||||
* convenience. */
|
||||
void timed_thread_destroy(timed_thread *p_timed_thread,
|
||||
timed_thread **addr_of_p_timed_thread)
|
||||
{
|
||||
assert(p_timed_thread != NULL);
|
||||
assert((addr_of_p_timed_thread == NULL)
|
||||
|| (*addr_of_p_timed_thread == p_timed_thread));
|
||||
|
||||
/* signal thread to stop */
|
||||
pthread_mutex_lock(&p_timed_thread->runnable_mutex);
|
||||
pthread_cond_signal(&p_timed_thread->runnable_cond);
|
||||
p_timed_thread->die = 1;
|
||||
pthread_mutex_unlock(&p_timed_thread->runnable_mutex);
|
||||
if (write(p_timed_thread->pipefd[1], "die", 3) == -1)
|
||||
perror("write()");
|
||||
|
||||
/* join the terminating thread */
|
||||
if (p_timed_thread->thread) {
|
||||
pthread_join(p_timed_thread->thread, NULL);
|
||||
}
|
||||
|
||||
/* clean up */
|
||||
pthread_attr_destroy(&p_timed_thread->thread_attr);
|
||||
pthread_mutex_destroy(&p_timed_thread->cs_mutex);
|
||||
pthread_mutex_destroy(&p_timed_thread->runnable_mutex);
|
||||
pthread_cond_destroy(&p_timed_thread->runnable_cond);
|
||||
|
||||
free(p_timed_thread);
|
||||
if (addr_of_p_timed_thread) {
|
||||
*addr_of_p_timed_thread = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* lock a timed thread for critical section activity */
|
||||
int timed_thread_lock(timed_thread *p_timed_thread)
|
||||
{
|
||||
assert(p_timed_thread != NULL);
|
||||
|
||||
return pthread_mutex_lock(&p_timed_thread->cs_mutex);
|
||||
}
|
||||
|
||||
/* unlock a timed thread after critical section activity */
|
||||
int timed_thread_unlock(timed_thread *p_timed_thread)
|
||||
{
|
||||
assert(p_timed_thread != NULL);
|
||||
|
||||
return pthread_mutex_unlock(&p_timed_thread->cs_mutex);
|
||||
}
|
||||
|
||||
/* 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(timed_thread *p_timed_thread, int override_wait_time)
|
||||
{
|
||||
struct timespec now_time;
|
||||
int rc;
|
||||
|
||||
assert(p_timed_thread != NULL);
|
||||
|
||||
/* acquire runnable_cond mutex */
|
||||
if (pthread_mutex_lock(&p_timed_thread->runnable_mutex)) {
|
||||
/* could not acquire runnable_cond mutex,
|
||||
* so tell caller to exit thread */
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (p_timed_thread->die) {
|
||||
/* if we were kindly asked to die, then die */
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (override_wait_time && now(&p_timed_thread->wait_time)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* release mutex and wait until future time for runnable_cond to signal */
|
||||
rc = pthread_cond_timedwait(&p_timed_thread->runnable_cond,
|
||||
&p_timed_thread->runnable_mutex, &p_timed_thread->wait_time);
|
||||
/* mutex re-acquired, so release it */
|
||||
pthread_mutex_unlock(&p_timed_thread->runnable_mutex);
|
||||
|
||||
if (now(&now_time)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (rc == 0) {
|
||||
/* runnable_cond was signaled, so tell caller to exit thread */
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* absolute future time for next pass */
|
||||
p_timed_thread->wait_time.tv_sec += p_timed_thread->interval_time.tv_sec;
|
||||
p_timed_thread->wait_time.tv_nsec += p_timed_thread->interval_time.tv_nsec;
|
||||
p_timed_thread->wait_time.tv_sec += p_timed_thread->wait_time.tv_nsec / 1000000000;
|
||||
p_timed_thread->wait_time.tv_nsec = p_timed_thread->wait_time.tv_nsec % 1000000000;
|
||||
|
||||
/* ensure our future wait time is sane */
|
||||
if (p_timed_thread->wait_time.tv_sec > (now_time.tv_sec + p_timed_thread->interval_time.tv_sec) || p_timed_thread->wait_time.tv_sec < now_time.tv_sec) {
|
||||
p_timed_thread->wait_time.tv_sec = now_time.tv_sec + p_timed_thread->interval_time.tv_sec;
|
||||
p_timed_thread->wait_time.tv_nsec = now_time.tv_nsec + p_timed_thread->interval_time.tv_nsec;
|
||||
p_timed_thread->wait_time.tv_sec += p_timed_thread->wait_time.tv_nsec / 1000000000;
|
||||
p_timed_thread->wait_time.tv_nsec = p_timed_thread->wait_time.tv_nsec % 1000000000;
|
||||
}
|
||||
|
||||
/* tell caller not to exit yet */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* exit a timed thread */
|
||||
void timed_thread_exit(timed_thread *p_timed_thread)
|
||||
{
|
||||
assert(p_timed_thread != NULL);
|
||||
|
||||
close(p_timed_thread->pipefd[0]);
|
||||
close(p_timed_thread->pipefd[1]);
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
/* register a timed thread for future destruction via
|
||||
* timed_thread_destroy_registered_threads() */
|
||||
int timed_thread_register(timed_thread *p_timed_thread,
|
||||
timed_thread **addr_of_p_timed_thread)
|
||||
{
|
||||
timed_thread_node *p_node;
|
||||
|
||||
assert((addr_of_p_timed_thread == NULL)
|
||||
|| (*addr_of_p_timed_thread == p_timed_thread));
|
||||
|
||||
if ((p_node = calloc(sizeof(timed_thread_node), 1)) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
p_node->p_timed_thread = p_timed_thread;
|
||||
p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
|
||||
p_node->next = NULL;
|
||||
|
||||
if (!p_timed_thread_list_tail) {
|
||||
/* first node of empty list */
|
||||
p_timed_thread_list_tail = p_node;
|
||||
p_timed_thread_list_head = p_node;
|
||||
} else {
|
||||
/* add node to tail of non-empty list */
|
||||
p_timed_thread_list_tail->next = p_node;
|
||||
p_timed_thread_list_tail = p_node;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* destroy all registered timed threads */
|
||||
void timed_thread_destroy_registered_threads(void)
|
||||
{
|
||||
timed_thread_node *p_node, *p_next;
|
||||
|
||||
for (p_node = p_timed_thread_list_head; p_node; p_node = p_next) {
|
||||
p_next = p_node->next;
|
||||
timed_thread_destroy(p_node->p_timed_thread,
|
||||
p_node->addr_of_p_timed_thread);
|
||||
free(p_node);
|
||||
p_node = NULL;
|
||||
}
|
||||
|
||||
p_timed_thread_list_head = NULL;
|
||||
p_timed_thread_list_tail = NULL;
|
||||
}
|
274
src/timed_thread.cc
Normal file
274
src/timed_thread.cc
Normal file
@ -0,0 +1,274 @@
|
||||
/* -*- 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <thread>
|
||||
#include <list>
|
||||
#include <chrono>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include "timed_thread.h"
|
||||
#include "logging.h"
|
||||
|
||||
/* Abstraction layer for timed threads */
|
||||
|
||||
typedef struct std::chrono::system_clock clk;
|
||||
|
||||
/* 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;
|
||||
};
|
||||
|
||||
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) :
|
||||
p_timed_thread(new _timed_thread), p_thread_handle(this),
|
||||
interval_usecs(interval_usecs), running(false)
|
||||
{
|
||||
|
||||
#ifdef DEBUG
|
||||
assert(interval_usecs >= 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(void)
|
||||
{
|
||||
DBGP("destroying thread %ld", (long)p_timed_thread->thread.get());
|
||||
#ifdef DEBUG
|
||||
assert(running);
|
||||
#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]);
|
||||
|
||||
deregister(this);
|
||||
|
||||
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 */
|
||||
#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
|
||||
|
||||
/* 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 = p_timed_thread->runnable_cond.wait_until(lock, 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) {
|
||||
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;
|
||||
}
|
||||
|
||||
/* 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();
|
||||
#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];
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
||||
/* -*- 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
|
||||
@ -23,54 +26,95 @@
|
||||
#define _TIMED_THREAD_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
/* 10000 microseconds = 10 ms = 0.01 sec */
|
||||
#define MINIMUM_INTERVAL_USECS 10000
|
||||
const unsigned int MINIMUM_INTERVAL_USECS = 10000;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/* private data */
|
||||
typedef struct _timed_thread _timed_thread;
|
||||
|
||||
/* opaque structure for clients */
|
||||
typedef struct _timed_thread timed_thread;
|
||||
class timed_thread;
|
||||
typedef std::shared_ptr<timed_thread> timed_thread_ptr;
|
||||
|
||||
/* create a timed thread (object creation only) */
|
||||
timed_thread *timed_thread_create(void *start_routine(void *), void *arg,
|
||||
unsigned int interval_usecs);
|
||||
namespace std { class mutex; }
|
||||
|
||||
/* run a timed thread (drop the thread and run it) */
|
||||
int timed_thread_run(timed_thread *p_timed_thread);
|
||||
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;
|
||||
};
|
||||
|
||||
/* destroy a timed thread */
|
||||
void timed_thread_destroy(timed_thread *p_timed_thread,
|
||||
timed_thread **addr_of_p_timed_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, const unsigned int
|
||||
interval_usecs, bool register_for_destruction = true) {
|
||||
timed_thread_ptr ptr(new timed_thread(std::cref(start_routine), interval_usecs));
|
||||
if (register_for_destruction) {
|
||||
register_(ptr);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/* lock a timed thread for critical section activity */
|
||||
int timed_thread_lock(timed_thread *p_timed_thread);
|
||||
virtual ~timed_thread(void) {
|
||||
destroy();
|
||||
}
|
||||
|
||||
/* unlock a timed thread after critical section activity */
|
||||
int timed_thread_unlock(timed_thread *p_timed_thread);
|
||||
/* run a timed thread (drop the thread and run it) */
|
||||
int run(void);
|
||||
|
||||
/* 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 timed_thread_test(timed_thread *p_timed_thread, int override_wait_time);
|
||||
/* lock a timed thread for critical section activity */
|
||||
void lock(void);
|
||||
|
||||
/* exit a timed thread */
|
||||
void timed_thread_exit(timed_thread *p_timed_thread) __attribute__((noreturn));
|
||||
/* unlock a timed thread after critical section activity */
|
||||
void unlock(void);
|
||||
|
||||
/* register a timed thread for future destruction via
|
||||
* timed_thread_destroy_registered_threads() */
|
||||
int timed_thread_register(timed_thread *p_timed_thread,
|
||||
timed_thread **addr_of_p_timed_thread);
|
||||
/* returns the critical section mutex */
|
||||
std::mutex &mutex();
|
||||
|
||||
/* destroy all registered timed threads */
|
||||
void timed_thread_destroy_registered_threads(void);
|
||||
/* destroy all registered timed threads */
|
||||
static void destroy_registered_threads(void);
|
||||
|
||||
/* returns read file descriptor for thread pipe */
|
||||
int timed_thread_readfd(timed_thread *p_timed_thread);
|
||||
/* 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, unsigned int
|
||||
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(void);
|
||||
|
||||
/* 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;
|
||||
unsigned int interval_usecs;
|
||||
bool running;
|
||||
friend class thread_handle;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* #ifdef _TIMED_THREAD_H_ */
|
||||
|
Loading…
Reference in New Issue
Block a user