mirror of
https://github.com/Llewellynvdm/conky.git
synced 2025-01-27 09:08:25 +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)
|
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
|
# The version numbers are simply derived from the date and number of commits
|
||||||
# since start of month
|
# since start of month
|
||||||
if(DEBUG)
|
if(DEBUG)
|
||||||
|
@ -43,10 +43,6 @@ if(MAINTAINER_MODE)
|
|||||||
endif(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)
|
option(RELEASE "Build release package" false)
|
||||||
mark_as_advanced(RELEASE)
|
mark_as_advanced(RELEASE)
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build.h)
|
|||||||
endif(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
|
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
|
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)
|
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)
|
endif(BUILD_IBM)
|
||||||
|
|
||||||
if(BUILD_MPD)
|
if(BUILD_MPD)
|
||||||
set(mpd mpd.c libmpdclient.c)
|
set(mpd mpd.cc libmpdclient.c)
|
||||||
set(optional_sources ${optional_sources} ${mpd})
|
set(optional_sources ${optional_sources} ${mpd})
|
||||||
endif(BUILD_MPD)
|
endif(BUILD_MPD)
|
||||||
|
|
||||||
if(BUILD_MOC)
|
if(BUILD_MOC)
|
||||||
set(moc moc.c)
|
set(moc moc.cc)
|
||||||
set(optional_sources ${optional_sources} ${moc})
|
set(optional_sources ${optional_sources} ${moc})
|
||||||
endif(BUILD_MOC)
|
endif(BUILD_MOC)
|
||||||
|
|
||||||
|
@ -33,10 +33,9 @@
|
|||||||
#include "conky.h"
|
#include "conky.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "timed_thread.h"
|
#include "timed_thread.h"
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <math.h>
|
#include <cmath>
|
||||||
#include <time.h>
|
#include <ctime>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@ -787,7 +786,7 @@ void evaluate(const char *text, char *p, int p_max_size)
|
|||||||
struct text_object subroot;
|
struct text_object subroot;
|
||||||
|
|
||||||
parse_conky_vars(&subroot, text, p, p_max_size);
|
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);
|
free_text_objects(&subroot);
|
||||||
}
|
}
|
||||||
@ -2374,7 +2373,7 @@ void clean_up(void *memtofree1, void* memtofree2)
|
|||||||
if(memtofree2) {
|
if(memtofree2) {
|
||||||
free(memtofree2);
|
free(memtofree2);
|
||||||
}
|
}
|
||||||
timed_thread_destroy_registered_threads();
|
timed_thread::destroy_registered_threads();
|
||||||
|
|
||||||
if (info.cpu_usage) {
|
if (info.cpu_usage) {
|
||||||
free(info.cpu_usage);
|
free(info.cpu_usage);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/* -*- 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=c
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
||||||
*
|
*
|
||||||
* Conky, a system monitor, based on torsmo
|
* Conky, a system monitor, based on torsmo
|
||||||
*
|
*
|
||||||
@ -38,6 +38,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
struct execi_data {
|
struct execi_data {
|
||||||
double last_update;
|
double last_update;
|
||||||
@ -45,8 +46,9 @@ struct execi_data {
|
|||||||
char *cmd;
|
char *cmd;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
double data;
|
double data;
|
||||||
timed_thread *p_timed_thread;
|
timed_thread_ptr p_timed_thread;
|
||||||
float barnum;
|
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
|
/* 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;
|
char *buf = NULL;
|
||||||
double barnum;
|
double barnum;
|
||||||
|
|
||||||
buf = malloc(text_buffer_size);
|
buf = (char*)malloc(text_buffer_size);
|
||||||
|
|
||||||
read_exec(data, buf, text_buffer_size);
|
read_exec(data, buf, text_buffer_size);
|
||||||
barnum = get_barnum(buf);
|
barnum = get_barnum(buf);
|
||||||
@ -184,16 +186,13 @@ static double read_exec_barnum(const char *data)
|
|||||||
return barnum;
|
return barnum;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *threaded_exec(void *) __attribute__((noreturn));
|
static void threaded_exec(thread_handle &handle, struct text_object *obj)
|
||||||
|
|
||||||
static void *threaded_exec(void *arg)
|
|
||||||
{
|
{
|
||||||
char *buff, *p2;
|
char *buff, *p2;
|
||||||
struct text_object *obj = arg;
|
struct execi_data *ed = (struct execi_data *)obj->data.opaque;
|
||||||
struct execi_data *ed = obj->data.opaque;
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
buff = malloc(text_buffer_size);
|
buff = (char*)malloc(text_buffer_size);
|
||||||
read_exec(ed->cmd, buff, text_buffer_size);
|
read_exec(ed->cmd, buff, text_buffer_size);
|
||||||
p2 = buff;
|
p2 = buff;
|
||||||
while (*p2) {
|
while (*p2) {
|
||||||
@ -202,13 +201,15 @@ static void *threaded_exec(void *arg)
|
|||||||
}
|
}
|
||||||
p2++;
|
p2++;
|
||||||
}
|
}
|
||||||
timed_thread_lock(ed->p_timed_thread);
|
|
||||||
if (ed->buffer)
|
{
|
||||||
free(ed->buffer);
|
std::lock_guard<std::mutex> lock(handle.mutex());
|
||||||
ed->buffer = buff;
|
if (ed->buffer)
|
||||||
timed_thread_unlock(ed->p_timed_thread);
|
free(ed->buffer);
|
||||||
if (timed_thread_test(ed->p_timed_thread, 0)) {
|
ed->buffer = buff;
|
||||||
timed_thread_exit(ed->p_timed_thread);
|
}
|
||||||
|
if (handle.test(0)) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* never reached */
|
/* never reached */
|
||||||
@ -244,8 +245,7 @@ void scan_execi_arg(struct text_object *obj, const char *arg)
|
|||||||
struct execi_data *ed;
|
struct execi_data *ed;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
ed = malloc(sizeof(struct execi_data));
|
ed = new execi_data;
|
||||||
memset(ed, 0, sizeof(struct execi_data));
|
|
||||||
|
|
||||||
if (sscanf(arg, "%f %n", &ed->interval, &n) <= 0) {
|
if (sscanf(arg, "%f %n", &ed->interval, &n) <= 0) {
|
||||||
NORM_ERR("${execi* <interval> command}");
|
NORM_ERR("${execi* <interval> command}");
|
||||||
@ -276,7 +276,7 @@ void scan_execgraph_arg(struct text_object *obj, const char *arg)
|
|||||||
struct execi_data *ed;
|
struct execi_data *ed;
|
||||||
char *buf;
|
char *buf;
|
||||||
|
|
||||||
ed = malloc(sizeof(struct execi_data));
|
ed = new execi_data;
|
||||||
memset(ed, 0, sizeof(struct execi_data));
|
memset(ed, 0, sizeof(struct execi_data));
|
||||||
|
|
||||||
buf = scan_graph(obj, arg, 100);
|
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;
|
struct text_object subroot;
|
||||||
char *buf;
|
char *buf;
|
||||||
|
|
||||||
buf = malloc(text_buffer_size);
|
buf = (char*)malloc(text_buffer_size);
|
||||||
memset(buf, 0, text_buffer_size);
|
memset(buf, 0, text_buffer_size);
|
||||||
|
|
||||||
read_exec(obj->data.s, buf, 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)
|
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)
|
if (!ed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (time_to_update(ed)) {
|
if (time_to_update(ed)) {
|
||||||
if (!ed->buffer)
|
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);
|
read_exec(ed->cmd, ed->buffer, text_buffer_size);
|
||||||
ed->last_update = current_update_time;
|
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)
|
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;
|
struct text_object subroot;
|
||||||
|
|
||||||
if (!ed)
|
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 (time_to_update(ed)) {
|
||||||
if (!ed->buffer)
|
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);
|
read_exec(ed->cmd, ed->buffer, text_buffer_size);
|
||||||
ed->last_update = current_update_time;
|
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)
|
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)
|
if (!ed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!ed->p_timed_thread) {
|
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
|
* note that we don't register this thread with the
|
||||||
* timed_thread list, because we destroy it manually
|
* timed_thread list, because we destroy it manually
|
||||||
*/
|
*/
|
||||||
if (timed_thread_run(ed->p_timed_thread)) {
|
ed->p_timed_thread = timed_thread::create(std::bind(threaded_exec, std::placeholders::_1, obj), ed->interval * 1000000, false);
|
||||||
NORM_ERR("Error running texeci timed thread");
|
if (!ed->p_timed_thread) {
|
||||||
|
NORM_ERR("Error creating texeci timed thread");
|
||||||
}
|
}
|
||||||
} else {
|
} 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);
|
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)
|
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)
|
if (!ed)
|
||||||
return 0;
|
return 0;
|
||||||
@ -401,13 +396,14 @@ void free_exec(struct text_object *obj)
|
|||||||
|
|
||||||
void free_execi(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)
|
if (!ed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (ed->p_timed_thread)
|
if (ed->p_timed_thread) {
|
||||||
timed_thread_destroy(ed->p_timed_thread, &ed->p_timed_thread);
|
ed->p_timed_thread.reset();
|
||||||
|
}
|
||||||
if (ed->cmd)
|
if (ed->cmd)
|
||||||
free(ed->cmd);
|
free(ed->cmd);
|
||||||
if (ed->buffer)
|
if (ed->buffer)
|
@ -31,10 +31,6 @@
|
|||||||
#ifndef _EXEC_H
|
#ifndef _EXEC_H
|
||||||
#define _EXEC_H
|
#define _EXEC_H
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
extern pid_t childpid;
|
extern pid_t childpid;
|
||||||
|
|
||||||
void scan_exec_arg(struct text_object *, const char *);
|
void scan_exec_arg(struct text_object *, const char *);
|
||||||
@ -52,8 +48,5 @@ double execbarval(struct text_object *);
|
|||||||
double execi_barval(struct text_object *);
|
double execi_barval(struct text_object *);
|
||||||
void free_exec(struct text_object *);
|
void free_exec(struct text_object *);
|
||||||
void free_execi(struct text_object *);
|
void free_execi(struct text_object *);
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* _EXEC_H */
|
#endif /* _EXEC_H */
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <tr1/unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
/* -------------------------------------------------------------------
|
/* -------------------------------------------------------------------
|
||||||
* IMPLEMENTATION INTERFACE
|
* IMPLEMENTATION INTERFACE
|
||||||
@ -85,7 +85,7 @@ namespace {
|
|||||||
* The second parameter provides the mechanism for removing connections if
|
* The second parameter provides the mechanism for removing connections if
|
||||||
* they are not seen again in subsequent update cycles.
|
* 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 */
|
/* 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;
|
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,
|
tcp_port_monitor_t,
|
||||||
port_range_hash> monitor_hash_t;
|
port_range_hash> monitor_hash_t;
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/* -*- 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=c
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
||||||
*
|
*
|
||||||
* Conky, a system monitor, based on torsmo
|
* Conky, a system monitor, based on torsmo
|
||||||
*
|
*
|
||||||
@ -40,6 +40,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <mutex>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@ -75,8 +76,16 @@ struct mail_s { // for imap and pop3
|
|||||||
char pass[128];
|
char pass[128];
|
||||||
char command[1024];
|
char command[1024];
|
||||||
char folder[128];
|
char folder[128];
|
||||||
timed_thread *p_timed_thread;
|
timed_thread_ptr p_timed_thread;
|
||||||
char secure;
|
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 {
|
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));
|
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));
|
memset(locmail, 0, sizeof(struct local_mail_s));
|
||||||
locmail->mbox = strndup(dst, text_buffer_size);
|
locmail->mbox = strndup(dst, text_buffer_size);
|
||||||
locmail->interval = n1;
|
locmail->interval = n1;
|
||||||
@ -371,7 +380,7 @@ void parse_local_mail_args(struct text_object *obj, const char *arg)
|
|||||||
#define PRINT_MAILS_GENERATOR(x) \
|
#define PRINT_MAILS_GENERATOR(x) \
|
||||||
void print_##x##mails(struct text_object *obj, char *p, int p_max_size) \
|
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) \
|
if (!locmail) \
|
||||||
return; \
|
return; \
|
||||||
update_mail_count(locmail); \
|
update_mail_count(locmail); \
|
||||||
@ -393,7 +402,7 @@ PRINT_MAILS_GENERATOR(trashed_)
|
|||||||
|
|
||||||
void free_local_mails(struct text_object *obj)
|
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)
|
if (!locmail)
|
||||||
return;
|
return;
|
||||||
@ -411,8 +420,7 @@ struct mail_s *parse_mail_args(char type, const char *arg)
|
|||||||
struct mail_s *mail;
|
struct mail_s *mail;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
|
|
||||||
mail = malloc(sizeof(struct mail_s));
|
mail = new mail_s;
|
||||||
memset(mail, 0, sizeof(struct mail_s));
|
|
||||||
|
|
||||||
if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
|
if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
|
||||||
!= 3) {
|
!= 3) {
|
||||||
@ -439,21 +447,21 @@ struct mail_s *parse_mail_args(char type, const char *arg)
|
|||||||
tcsetattr(fp, TCSANOW, &term);
|
tcsetattr(fp, TCSANOW, &term);
|
||||||
}
|
}
|
||||||
// now we check for optional args
|
// now we check for optional args
|
||||||
tmp = strstr(arg, "-r ");
|
tmp = (char*)strstr(arg, "-r ");
|
||||||
if (tmp) {
|
if (tmp) {
|
||||||
tmp += 3;
|
tmp += 3;
|
||||||
sscanf(tmp, "%u", &mail->retries);
|
sscanf(tmp, "%u", &mail->retries);
|
||||||
} else {
|
} else {
|
||||||
mail->retries = 5; // 5 retries after failure
|
mail->retries = 5; // 5 retries after failure
|
||||||
}
|
}
|
||||||
tmp = strstr(arg, "-i ");
|
tmp = (char*)strstr(arg, "-i ");
|
||||||
if (tmp) {
|
if (tmp) {
|
||||||
tmp += 3;
|
tmp += 3;
|
||||||
sscanf(tmp, "%f", &mail->interval);
|
sscanf(tmp, "%f", &mail->interval);
|
||||||
} else {
|
} else {
|
||||||
mail->interval = 300; // 5 minutes
|
mail->interval = 300; // 5 minutes
|
||||||
}
|
}
|
||||||
tmp = strstr(arg, "-p ");
|
tmp = (char*)strstr(arg, "-p ");
|
||||||
if (tmp) {
|
if (tmp) {
|
||||||
tmp += 3;
|
tmp += 3;
|
||||||
sscanf(tmp, "%lu", &mail->port);
|
sscanf(tmp, "%lu", &mail->port);
|
||||||
@ -465,12 +473,12 @@ struct mail_s *parse_mail_args(char type, const char *arg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (type == IMAP_TYPE) {
|
if (type == IMAP_TYPE) {
|
||||||
tmp = strstr(arg, "-f ");
|
tmp = (char*)strstr(arg, "-f ");
|
||||||
if (tmp) {
|
if (tmp) {
|
||||||
int len = 1024;
|
int len = 1024;
|
||||||
tmp += 3;
|
tmp += 3;
|
||||||
if (tmp[0] == '\'') {
|
if (tmp[0] == '\'') {
|
||||||
len = strstr(tmp + 1, "'") - tmp - 1;
|
len = (char*)strstr(tmp + 1, "'") - tmp - 1;
|
||||||
if (len > 1024) {
|
if (len > 1024) {
|
||||||
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
|
strncpy(mail->folder, "INBOX", 128); // default imap inbox
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tmp = strstr(arg, "-e ");
|
tmp = (char*)strstr(arg, "-e ");
|
||||||
if (tmp) {
|
if (tmp) {
|
||||||
int len = 1024;
|
int len = 1024;
|
||||||
tmp += 3;
|
tmp += 3;
|
||||||
|
|
||||||
if (tmp[0] == '\'') {
|
if (tmp[0] == '\'') {
|
||||||
len = strstr(tmp + 1, "'") - tmp - 1;
|
len = (char*)strstr(tmp + 1, "'") - tmp - 1;
|
||||||
if (len > 1024) {
|
if (len > 1024) {
|
||||||
len = 1024;
|
len = 1024;
|
||||||
}
|
}
|
||||||
@ -495,7 +503,6 @@ struct mail_s *parse_mail_args(char type, const char *arg)
|
|||||||
} else {
|
} else {
|
||||||
mail->command[0] = '\0';
|
mail->command[0] = '\0';
|
||||||
}
|
}
|
||||||
mail->p_timed_thread = NULL;
|
|
||||||
return mail;
|
return mail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -558,11 +565,12 @@ void free_mail_obj(struct text_object *obj)
|
|||||||
|
|
||||||
if (obj->data.opaque == global_mail) {
|
if (obj->data.opaque == global_mail) {
|
||||||
if (--global_mail_use == 0) {
|
if (--global_mail_use == 0) {
|
||||||
free(global_mail);
|
delete global_mail;
|
||||||
global_mail = 0;
|
global_mail = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
free(obj->data.opaque);
|
struct mail_s *mail = (struct mail_s*)obj->data.opaque;
|
||||||
|
delete mail;
|
||||||
obj->data.opaque = 0;
|
obj->data.opaque = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -596,10 +604,10 @@ int imap_command(int sockfd, const char *command, char *response, const char *ve
|
|||||||
return 0;
|
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;
|
char *reply;
|
||||||
reply = strstr(recvbuf, " (MESSAGES ");
|
reply = (char*)strstr(recvbuf, " (MESSAGES ");
|
||||||
if (!reply || strlen(reply) < 2) {
|
if (!reply || strlen(reply) < 2) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -609,10 +617,9 @@ int imap_check_status(char *recvbuf, struct mail_s *mail)
|
|||||||
NORM_ERR("Error parsing IMAP response: %s", recvbuf);
|
NORM_ERR("Error parsing IMAP response: %s", recvbuf);
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
timed_thread_lock(mail->p_timed_thread);
|
std::lock_guard<std::mutex> lock(handle.mutex());
|
||||||
sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
|
sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
|
||||||
&mail->unseen);
|
&mail->unseen);
|
||||||
timed_thread_unlock(mail->p_timed_thread);
|
|
||||||
}
|
}
|
||||||
return 0;
|
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,
|
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)
|
if (mail->p_timed_thread)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
mail->p_timed_thread = timed_thread_create(thread,
|
mail->p_timed_thread = timed_thread::create(std::bind(func, std::placeholders::_1, mail), mail->interval * 1000000);
|
||||||
mail, mail->interval * 1000000);
|
|
||||||
if (!mail->p_timed_thread) {
|
if (!mail->p_timed_thread) {
|
||||||
NORM_ERR("Error creating %s timed thread", text);
|
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;
|
int sockfd, numbytes;
|
||||||
char recvbuf[MAXDATASIZE];
|
char recvbuf[MAXDATASIZE];
|
||||||
@ -657,9 +658,8 @@ static void *imap_thread(void *arg)
|
|||||||
struct stat stat_buf;
|
struct stat stat_buf;
|
||||||
struct hostent *he_res = 0;
|
struct hostent *he_res = 0;
|
||||||
struct sockaddr_in their_addr; // connector's address information
|
struct sockaddr_in their_addr; // connector's address information
|
||||||
struct mail_s *mail = (struct mail_s *)arg;
|
|
||||||
int has_idle = 0;
|
int has_idle = 0;
|
||||||
int threadfd = timed_thread_readfd(mail->p_timed_thread);
|
int threadfd = handle.readfd();
|
||||||
char resolved_host = 0;
|
char resolved_host = 0;
|
||||||
|
|
||||||
while (fail < mail->retries) {
|
while (fail < mail->retries) {
|
||||||
@ -764,7 +764,7 @@ static void *imap_thread(void *arg)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (imap_check_status(recvbuf, mail)) {
|
if (imap_check_status(recvbuf, mail, handle)) {
|
||||||
fail++;
|
fail++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -801,12 +801,12 @@ static void *imap_thread(void *arg)
|
|||||||
FD_SET(sockfd, &fdset);
|
FD_SET(sockfd, &fdset);
|
||||||
FD_SET(threadfd, &fdset);
|
FD_SET(threadfd, &fdset);
|
||||||
res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, &fetchtimeout);
|
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 ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
|
||||||
/* if a valid socket, close it */
|
/* if a valid socket, close it */
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
}
|
}
|
||||||
timed_thread_exit(mail->p_timed_thread);
|
return;
|
||||||
} else if (res > 0) {
|
} else if (res > 0) {
|
||||||
if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
|
if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
|
||||||
perror("recv idling");
|
perror("recv idling");
|
||||||
@ -823,9 +823,9 @@ static void *imap_thread(void *arg)
|
|||||||
unsigned long messages, recent = 0;
|
unsigned long messages, recent = 0;
|
||||||
char *buf = recvbuf;
|
char *buf = recvbuf;
|
||||||
char force_check = 0;
|
char force_check = 0;
|
||||||
buf = strstr(buf, "EXISTS");
|
buf = (char*)strstr(buf, "EXISTS");
|
||||||
while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
|
while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
|
||||||
buf = strstr(buf + 1, "EXISTS");
|
buf = (char*)strstr(buf + 1, "EXISTS");
|
||||||
}
|
}
|
||||||
if (buf) {
|
if (buf) {
|
||||||
// back up until we reach '*'
|
// back up until we reach '*'
|
||||||
@ -833,18 +833,17 @@ static void *imap_thread(void *arg)
|
|||||||
buf--;
|
buf--;
|
||||||
}
|
}
|
||||||
if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
|
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) {
|
if (mail->messages != messages) {
|
||||||
force_check = 1;
|
force_check = 1;
|
||||||
mail->messages = messages;
|
mail->messages = messages;
|
||||||
}
|
}
|
||||||
timed_thread_unlock(mail->p_timed_thread);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf = recvbuf;
|
buf = recvbuf;
|
||||||
buf = strstr(buf, "RECENT");
|
buf = (char*)strstr(buf, "RECENT");
|
||||||
while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
|
while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
|
||||||
buf = strstr(buf + 1, "RECENT");
|
buf = (char*)strstr(buf + 1, "RECENT");
|
||||||
}
|
}
|
||||||
if (buf) {
|
if (buf) {
|
||||||
// back up until we reach '*'
|
// back up until we reach '*'
|
||||||
@ -874,7 +873,7 @@ static void *imap_thread(void *arg)
|
|||||||
fail++;
|
fail++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (imap_check_status(recvbuf, mail)) {
|
if (imap_check_status(recvbuf, mail, handle)) {
|
||||||
fail++;
|
fail++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -934,44 +933,41 @@ static void *imap_thread(void *arg)
|
|||||||
/* if a valid socket, close it */
|
/* if a valid socket, close it */
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
}
|
}
|
||||||
if (timed_thread_test(mail->p_timed_thread, 0)) {
|
if (handle.test(0)) {
|
||||||
timed_thread_exit(mail->p_timed_thread);
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mail->unseen = 0;
|
mail->unseen = 0;
|
||||||
mail->messages = 0;
|
mail->messages = 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_imap_unseen(struct text_object *obj, char *p, int p_max_size)
|
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)
|
if (!mail)
|
||||||
return;
|
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) {
|
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);
|
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)
|
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)
|
if (!mail)
|
||||||
return;
|
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) {
|
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);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *pop3_thread(void *arg)
|
static void pop3_thread(thread_handle &handle, struct mail_s *mail)
|
||||||
{
|
{
|
||||||
int sockfd, numbytes;
|
int sockfd, numbytes;
|
||||||
char recvbuf[MAXDATASIZE];
|
char recvbuf[MAXDATASIZE];
|
||||||
@ -1014,7 +1010,6 @@ static void *pop3_thread(void *arg)
|
|||||||
struct stat stat_buf;
|
struct stat stat_buf;
|
||||||
struct hostent *he_res = 0;
|
struct hostent *he_res = 0;
|
||||||
struct sockaddr_in their_addr; // connector's address information
|
struct sockaddr_in their_addr; // connector's address information
|
||||||
struct mail_s *mail = (struct mail_s *)arg;
|
|
||||||
char resolved_host = 0;
|
char resolved_host = 0;
|
||||||
|
|
||||||
while (fail < mail->retries) {
|
while (fail < mail->retries) {
|
||||||
@ -1121,9 +1116,8 @@ static void *pop3_thread(void *arg)
|
|||||||
fail++;
|
fail++;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
timed_thread_lock(mail->p_timed_thread);
|
std::lock_guard<std::mutex> lock(handle.mutex());
|
||||||
sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
|
sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
|
||||||
timed_thread_unlock(mail->p_timed_thread);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
|
strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
|
||||||
@ -1146,44 +1140,41 @@ static void *pop3_thread(void *arg)
|
|||||||
/* if a valid socket, close it */
|
/* if a valid socket, close it */
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
}
|
}
|
||||||
if (timed_thread_test(mail->p_timed_thread, 0)) {
|
if (handle.test(0)) {
|
||||||
timed_thread_exit(mail->p_timed_thread);
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mail->unseen = 0;
|
mail->unseen = 0;
|
||||||
mail->used = 0;
|
mail->used = 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_pop3_unseen(struct text_object *obj, char *p, int p_max_size)
|
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)
|
if (!mail)
|
||||||
return;
|
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) {
|
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);
|
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)
|
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)
|
if (!mail)
|
||||||
return;
|
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) {
|
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",
|
snprintf(p, p_max_size, "%.1f",
|
||||||
mail->used / 1024.0 / 1024.0);
|
mail->used / 1024.0 / 1024.0);
|
||||||
timed_thread_unlock(mail->p_timed_thread);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,10 +3,6 @@
|
|||||||
#ifndef _MAIL_H
|
#ifndef _MAIL_H
|
||||||
#define _MAIL_H
|
#define _MAIL_H
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
extern char *current_mail_spool;
|
extern char *current_mail_spool;
|
||||||
|
|
||||||
void parse_local_mail_args(struct text_object *, const char *);
|
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_unseen(struct text_object *, char *, int);
|
||||||
void print_pop3_used(struct text_object *, char *, int);
|
void print_pop3_used(struct text_object *, char *, int);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* _MAIL_H */
|
#endif /* _MAIL_H */
|
||||||
|
@ -23,10 +23,6 @@
|
|||||||
#ifndef MOC_H_
|
#ifndef MOC_H_
|
||||||
#define MOC_H_
|
#define MOC_H_
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void update_moc(void);
|
void update_moc(void);
|
||||||
void free_moc(struct text_object *);
|
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_bitrate(struct text_object *, char *, int);
|
||||||
void print_moc_rate(struct text_object *, char *, int);
|
void print_moc_rate(struct text_object *, char *, int);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* MOC_H_ */
|
#endif /* MOC_H_ */
|
||||||
|
|
||||||
|
@ -3,10 +3,6 @@
|
|||||||
#ifndef MPD_H_
|
#ifndef MPD_H_
|
||||||
#define MPD_H_
|
#define MPD_H_
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* functions for setting the configuration values */
|
/* functions for setting the configuration values */
|
||||||
void mpd_set_host(const char *);
|
void mpd_set_host(const char *);
|
||||||
void mpd_set_password(const char *, int);
|
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);
|
void print_mpd_status(struct text_object *, char *, int);
|
||||||
int check_mpd_playing(struct text_object *);
|
int check_mpd_playing(struct text_object *);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /*MPD_H_*/
|
#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
|
* timed_thread.h: Abstraction layer for timed threads
|
||||||
*
|
*
|
||||||
* Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
|
* 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
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
@ -23,54 +26,95 @@
|
|||||||
#define _TIMED_THREAD_H_
|
#define _TIMED_THREAD_H_
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
/* 10000 microseconds = 10 ms = 0.01 sec */
|
/* 10000 microseconds = 10 ms = 0.01 sec */
|
||||||
#define MINIMUM_INTERVAL_USECS 10000
|
const unsigned int MINIMUM_INTERVAL_USECS = 10000;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
/* private data */
|
||||||
extern "C" {
|
typedef struct _timed_thread _timed_thread;
|
||||||
#endif
|
|
||||||
|
|
||||||
/* opaque structure for clients */
|
class timed_thread;
|
||||||
typedef struct _timed_thread timed_thread;
|
typedef std::shared_ptr<timed_thread> timed_thread_ptr;
|
||||||
|
|
||||||
/* create a timed thread (object creation only) */
|
namespace std { class mutex; }
|
||||||
timed_thread *timed_thread_create(void *start_routine(void *), void *arg,
|
|
||||||
unsigned int interval_usecs);
|
|
||||||
|
|
||||||
/* run a timed thread (drop the thread and run it) */
|
class thread_handle {
|
||||||
int timed_thread_run(timed_thread *p_timed_thread);
|
/* 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 */
|
class timed_thread {
|
||||||
void timed_thread_destroy(timed_thread *p_timed_thread,
|
public:
|
||||||
timed_thread **addr_of_p_timed_thread);
|
/* 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 */
|
virtual ~timed_thread(void) {
|
||||||
int timed_thread_lock(timed_thread *p_timed_thread);
|
destroy();
|
||||||
|
}
|
||||||
|
|
||||||
/* unlock a timed thread after critical section activity */
|
/* run a timed thread (drop the thread and run it) */
|
||||||
int timed_thread_unlock(timed_thread *p_timed_thread);
|
int run(void);
|
||||||
|
|
||||||
/* waits required interval (unless override_wait_time is non-zero) for
|
/* lock a timed thread for critical section activity */
|
||||||
* termination signal returns 1 if received, 0 otherwise. should also return 1
|
void lock(void);
|
||||||
* if the thread has been asked kindly to die. */
|
|
||||||
int timed_thread_test(timed_thread *p_timed_thread, int override_wait_time);
|
|
||||||
|
|
||||||
/* exit a timed thread */
|
/* unlock a timed thread after critical section activity */
|
||||||
void timed_thread_exit(timed_thread *p_timed_thread) __attribute__((noreturn));
|
void unlock(void);
|
||||||
|
|
||||||
/* register a timed thread for future destruction via
|
/* returns the critical section mutex */
|
||||||
* timed_thread_destroy_registered_threads() */
|
std::mutex &mutex();
|
||||||
int timed_thread_register(timed_thread *p_timed_thread,
|
|
||||||
timed_thread **addr_of_p_timed_thread);
|
|
||||||
|
|
||||||
/* destroy all registered timed threads */
|
/* destroy all registered timed threads */
|
||||||
void timed_thread_destroy_registered_threads(void);
|
static void destroy_registered_threads(void);
|
||||||
|
|
||||||
/* returns read file descriptor for thread pipe */
|
/* returns read file descriptor for thread pipe */
|
||||||
int timed_thread_readfd(timed_thread *p_timed_thread);
|
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_ */
|
#endif /* #ifdef _TIMED_THREAD_H_ */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user