mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-12-25 12:10:03 +00:00
preliminary threading changes
git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@518 7f574dfc-610e-0410-a909-a81674777703
This commit is contained in:
parent
778823cee3
commit
e085a14b9a
97
src/conky.c
97
src/conky.c
@ -214,9 +214,6 @@ static int fork_to_background;
|
|||||||
|
|
||||||
static int cpu_avg_samples, net_avg_samples;
|
static int cpu_avg_samples, net_avg_samples;
|
||||||
|
|
||||||
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
|
|
||||||
pthread_t execthread = (pthread_t)0;
|
|
||||||
|
|
||||||
#ifdef X11
|
#ifdef X11
|
||||||
|
|
||||||
/* Always on bottom */
|
/* Always on bottom */
|
||||||
@ -410,7 +407,7 @@ static int special_index; /* used when drawing */
|
|||||||
|
|
||||||
static struct special_t *new_special(char *buf, int t)
|
static struct special_t *new_special(char *buf, int t)
|
||||||
{
|
{
|
||||||
if (special_count >= 128)
|
if (special_count >= 256)
|
||||||
CRIT_ERR("too many special things in text");
|
CRIT_ERR("too many special things in text");
|
||||||
|
|
||||||
buf[0] = SPECIAL_CHAR;
|
buf[0] = SPECIAL_CHAR;
|
||||||
@ -920,6 +917,11 @@ enum text_object_type {
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct thread_info_s {
|
||||||
|
pthread_mutex_t mutex;
|
||||||
|
pthread_t thread;
|
||||||
|
};
|
||||||
|
|
||||||
struct text_object {
|
struct text_object {
|
||||||
int type;
|
int type;
|
||||||
int a, b;
|
int a, b;
|
||||||
@ -973,6 +975,8 @@ struct text_object {
|
|||||||
char *cmd;
|
char *cmd;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
double data;
|
double data;
|
||||||
|
int pos;
|
||||||
|
struct thread_info_s thread_info;
|
||||||
} execi; /* 5 */
|
} execi; /* 5 */
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
@ -998,8 +1002,59 @@ static unsigned int text_object_count;
|
|||||||
static struct text_object *text_objects;
|
static struct text_object *text_objects;
|
||||||
static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur);
|
static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur);
|
||||||
|
|
||||||
void *threaded_exec( struct text_object *obj ) {
|
#define MAX_THREADS 512 // sure whatever
|
||||||
pthread_mutex_lock( &mutex1 );
|
typedef struct thread_info_s *thread_info;
|
||||||
|
thread_info thread_list[MAX_THREADS];
|
||||||
|
int thread_count = 0;
|
||||||
|
|
||||||
|
int register_thread(struct thread_info_s *new_thread)
|
||||||
|
{
|
||||||
|
if (thread_count >= MAX_THREADS) {
|
||||||
|
CRIT_ERR("Uh oh, tried to register too many threads");
|
||||||
|
} else {
|
||||||
|
thread_list[thread_count] = new_thread;
|
||||||
|
thread_count++;
|
||||||
|
}
|
||||||
|
return thread_count - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_shit()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < thread_count; i++) {
|
||||||
|
printf("thread is %i\n", (int)(thread_list[i])->thread);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void replace_thread(struct thread_info_s *new_thread, int pos)
|
||||||
|
{
|
||||||
|
if (pos >= 0 && pos < MAX_THREADS) {
|
||||||
|
thread_list[pos] = new_thread;
|
||||||
|
} else {
|
||||||
|
ERR("thread position out of bounds");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void join_all_threads()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
// now we wait to get the locks
|
||||||
|
for (i = 0; i < thread_count; i++) {
|
||||||
|
// try to lock mutex
|
||||||
|
if (thread_list[i]) {
|
||||||
|
if (thread_list[i]->thread) {
|
||||||
|
printf("thread is %i\n", (int)thread_list[i]->thread);
|
||||||
|
/* pthread_mutex_lock(&(thread_list[i]->mutex));
|
||||||
|
if (pthread_join(thread_list[i]->thread, NULL))
|
||||||
|
ERR("Problem joining thread %i\n", (int)thread_list[i]->thread);*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void *threaded_exec(struct text_object *obj) {
|
||||||
|
pthread_mutex_lock(&(obj->data.execi.thread_info.mutex));
|
||||||
char *p2 = obj->data.execi.buffer;
|
char *p2 = obj->data.execi.buffer;
|
||||||
FILE *fp = popen(obj->data.execi.cmd,"r");
|
FILE *fp = popen(obj->data.execi.cmd,"r");
|
||||||
int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
|
int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
|
||||||
@ -1014,7 +1069,7 @@ void *threaded_exec( struct text_object *obj ) {
|
|||||||
p2++;
|
p2++;
|
||||||
}
|
}
|
||||||
obj->data.execi.last_update = current_update_time;
|
obj->data.execi.last_update = current_update_time;
|
||||||
pthread_mutex_unlock( &mutex1 );
|
pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
|
||||||
pthread_exit(NULL);
|
pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1314,8 +1369,7 @@ static struct text_object *construct_text_object(const char *s, const char *arg,
|
|||||||
}
|
}
|
||||||
END OBJ(texeci, 0) unsigned int n;
|
END OBJ(texeci, 0) unsigned int n;
|
||||||
|
|
||||||
if (!arg
|
if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
|
||||||
|| sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
|
|
||||||
char buf[256];
|
char buf[256];
|
||||||
ERR("${texeci <interval> command}");
|
ERR("${texeci <interval> command}");
|
||||||
obj->type = OBJ_text;
|
obj->type = OBJ_text;
|
||||||
@ -1326,6 +1380,8 @@ static struct text_object *construct_text_object(const char *s, const char *arg,
|
|||||||
obj->data.execi.buffer =
|
obj->data.execi.buffer =
|
||||||
(char *) calloc(1, TEXT_BUFFER_SIZE);
|
(char *) calloc(1, TEXT_BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
|
pthread_mutex_init(&(obj->data.execi.thread_info.mutex), NULL);
|
||||||
|
obj->data.execi.pos = register_thread(&(obj->data.execi.thread_info));
|
||||||
END OBJ(pre_exec, 0) obj->type = OBJ_text;
|
END OBJ(pre_exec, 0) obj->type = OBJ_text;
|
||||||
if (arg) {
|
if (arg) {
|
||||||
FILE *fp = popen(arg, "r");
|
FILE *fp = popen(arg, "r");
|
||||||
@ -2536,19 +2592,19 @@ static void generate_text_internal(char *p, int p_max_size, struct text_object *
|
|||||||
if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
|
if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
|
||||||
snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
|
snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
|
||||||
} else {
|
} else {
|
||||||
if (!execthread && !pthread_mutex_trylock(&mutex1)) {
|
if (!obj->data.execi.thread_info.thread && !pthread_mutex_trylock(&(obj->data.execi.thread_info.mutex))) {
|
||||||
|
|
||||||
if (pthread_create(&execthread, NULL, (void*)threaded_exec, (void*) obj)) {
|
if (pthread_create(&(obj->data.execi.thread_info.thread), NULL, (void*)threaded_exec, (void*) obj)) {
|
||||||
ERR("Error starting thread");
|
ERR("Error starting thread");
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&mutex1);
|
// printf("thread is %i\n", (int)obj->data.execi.thread_info.thread);
|
||||||
|
pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
|
||||||
}
|
}
|
||||||
snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
|
snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
|
||||||
}
|
}
|
||||||
if (!pthread_mutex_trylock(&mutex1)) {
|
if (!pthread_mutex_trylock(&(obj->data.execi.thread_info.mutex))) {
|
||||||
// pthread_join(execthread, NULL);
|
obj->data.execi.thread_info.thread = (pthread_t)0;
|
||||||
execthread = (pthread_t)0;
|
pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
|
||||||
pthread_mutex_unlock(&mutex1);
|
|
||||||
}
|
}
|
||||||
//parse_conky_vars(obj->data.execi.buffer, p, cur);
|
//parse_conky_vars(obj->data.execi.buffer, p, cur);
|
||||||
}
|
}
|
||||||
@ -4273,6 +4329,8 @@ static void main_loop()
|
|||||||
info.looped = 0;
|
info.looped = 0;
|
||||||
while (total_run_times == 0 || info.looped < total_run_times - 1) {
|
while (total_run_times == 0 || info.looped < total_run_times - 1) {
|
||||||
info.looped++;
|
info.looped++;
|
||||||
|
printf("loop %i:\n", (int)info.looped);
|
||||||
|
print_shit();
|
||||||
|
|
||||||
#ifdef SIGNAL_BLOCKING
|
#ifdef SIGNAL_BLOCKING
|
||||||
/* block signals. we will inspect for pending signals later */
|
/* block signals. we will inspect for pending signals later */
|
||||||
@ -4549,7 +4607,7 @@ static void load_config_file(const char *);
|
|||||||
/* reload the config file */
|
/* reload the config file */
|
||||||
void reload_config(void)
|
void reload_config(void)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&mutex1);
|
join_all_threads();
|
||||||
#if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
|
#if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
|
||||||
if (info.xmms.thread) {
|
if (info.xmms.thread) {
|
||||||
if (destroy_xmms_thread()!=0)
|
if (destroy_xmms_thread()!=0)
|
||||||
@ -4586,13 +4644,12 @@ void reload_config(void)
|
|||||||
text = NULL;
|
text = NULL;
|
||||||
update_text();
|
update_text();
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&mutex1);
|
thread_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clean_up(void)
|
void clean_up(void)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&mutex1);
|
join_all_threads();
|
||||||
execthread = (pthread_t)0;
|
|
||||||
#ifdef X11
|
#ifdef X11
|
||||||
#ifdef XDBE
|
#ifdef XDBE
|
||||||
if (use_xdbe) {
|
if (use_xdbe) {
|
||||||
|
Loading…
Reference in New Issue
Block a user