1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 18:45:10 +00:00

Added disable_auto_reload option, callback fixes.

There were some issues with reloading and the threaded callback
framework, which I think are mostly resolved now, but may need more
testing.
This commit is contained in:
Brenden Matthews 2010-01-02 13:31:09 -08:00
parent 19303af8da
commit 39f01e216b
3 changed files with 52 additions and 20 deletions

View File

@ -157,6 +157,15 @@
<listitem>Default shading color and border's shading color <listitem>Default shading color and border's shading color
<para /></listitem> <para /></listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>
<command>
<option>disable_auto_reload</option>
</command>
</term>
<listitem>Enable to disable the inotify-based auto config reload feature.
<para /></listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term> <term>
<command> <command>

View File

@ -279,10 +279,12 @@ static struct update_cb {
volatile char running; volatile char running;
} update_cb_head = { } update_cb_head = {
.next = NULL, .next = NULL,
.func = NULL,
}; };
static void *run_update_callback(void *); static void *run_update_callback(void *) __attribute__((noreturn));
static int threading_started;
static int threading_started = 0;
/* Register an update callback. Don't allow duplicates, to minimise side /* Register an update callback. Don't allow duplicates, to minimise side
* effects and overhead. */ * effects and overhead. */
@ -308,10 +310,12 @@ void add_update_callback(void (*func)(void))
sem_init(&uc->end_wait, 0, 0); sem_init(&uc->end_wait, 0, 0);
if (threading_started) { if (threading_started) {
if (!uc->running) {
uc->running = 1; uc->running = 1;
pthread_create(&uc->thread, NULL, &run_update_callback, uc); pthread_create(&uc->thread, NULL, &run_update_callback, uc);
} }
} }
}
/* Free the list element uc and all decendants recursively. */ /* Free the list element uc and all decendants recursively. */
static void __free_update_callbacks(struct update_cb *uc) static void __free_update_callbacks(struct update_cb *uc)
@ -323,7 +327,9 @@ static void __free_update_callbacks(struct update_cb *uc)
/* send cancellation request, then trigger and join the thread */ /* send cancellation request, then trigger and join the thread */
uc->running = 0; uc->running = 0;
sem_post(&uc->start_wait); sem_post(&uc->start_wait);
pthread_join(uc->thread, NULL); }
if (pthread_join(uc->thread, NULL)) {
NORM_ERR("Error destroying thread");
} }
/* finally destroy the semaphores */ /* finally destroy the semaphores */
@ -356,21 +362,24 @@ void start_update_threading(void)
threading_started = 1; threading_started = 1;
for (uc = update_cb_head.next; uc; uc = uc->next) { for (uc = update_cb_head.next; uc; uc = uc->next) {
if (!uc->running) {
uc->running = 1; uc->running = 1;
pthread_create(&uc->thread, NULL, &run_update_callback, uc); pthread_create(&uc->thread, NULL, &run_update_callback, uc);
} }
} }
}
static void *run_update_callback(void *data) static void *run_update_callback(void *data)
{ {
struct update_cb *ucb = data; struct update_cb *ucb = data;
if (!ucb || !ucb->func) pthread_exit(NULL);
while (1) { while (1) {
sem_wait(&ucb->start_wait); if (sem_wait(&ucb->start_wait)) pthread_exit(NULL);
if (ucb->running == 0) if (ucb->running == 0) pthread_exit(NULL);
return NULL;
(*ucb->func)(); (*ucb->func)();
sem_post(&ucb->end_wait); if (sem_post(&ucb->end_wait)) pthread_exit(NULL);
} }
} }
@ -397,8 +406,11 @@ void update_stuff(void)
prepare_update(); prepare_update();
for (uc = update_cb_head.next; uc; uc = uc->next) for (uc = update_cb_head.next; uc; uc = uc->next) {
sem_post(&uc->start_wait); if (sem_post(&uc->start_wait)) {
NORM_ERR("Semaphore error");
}
}
/* need to synchronise here, otherwise locking is needed (as data /* need to synchronise here, otherwise locking is needed (as data
* would be printed with some update callbacks still running) */ * would be printed with some update callbacks still running) */
for (uc = update_cb_head.next; uc; uc = uc->next) for (uc = update_cb_head.next; uc; uc = uc->next)

View File

@ -126,6 +126,9 @@
/* debugging level, used by logging.h */ /* debugging level, used by logging.h */
int global_debug_level = 0; int global_debug_level = 0;
/* disable inotify auto reload feature if desired */
int disable_auto_reload = 0;
/* two strings for internal use */ /* two strings for internal use */
static char *tmpstring1, *tmpstring2; static char *tmpstring1, *tmpstring2;
@ -424,7 +427,7 @@ int get_total_updates(void)
#define SECRIT_MULTILINE_CHAR '\x02' #define SECRIT_MULTILINE_CHAR '\x02'
static inline int calc_text_width(const char *s) int calc_text_width(const char *s)
{ {
size_t slen = strlen(s); size_t slen = strlen(s);
@ -854,7 +857,7 @@ void set_update_interval(double interval)
update_interval_old = interval; update_interval_old = interval;
} }
static inline int get_string_width(const char *s) int get_string_width(const char *s)
{ {
return *s ? calc_text_width(s) : 0; return *s ? calc_text_width(s) : 0;
} }
@ -2272,12 +2275,12 @@ static void main_loop(void)
break; break;
} }
#ifdef HAVE_SYS_INOTIFY_H #ifdef HAVE_SYS_INOTIFY_H
if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) { if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
inotify_config_wd = inotify_add_watch(inotify_fd, inotify_config_wd = inotify_add_watch(inotify_fd,
current_config, current_config,
IN_MODIFY); IN_MODIFY);
} }
if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) { if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
int len = 0, idx = 0; int len = 0, idx = 0;
fd_set descriptors; fd_set descriptors;
struct timeval time_to_wait; struct timeval time_to_wait;
@ -2313,6 +2316,10 @@ static void main_loop(void)
idx += INOTIFY_EVENT_SIZE + ev->len; idx += INOTIFY_EVENT_SIZE + ev->len;
} }
} }
} else if (disable_auto_reload && inotify_fd != -1) {
inotify_rm_watch(inotify_fd, inotify_config_wd);
close(inotify_fd);
inotify_fd = inotify_config_wd = 0;
} }
#endif /* HAVE_SYS_INOTIFY_H */ #endif /* HAVE_SYS_INOTIFY_H */
@ -2342,6 +2349,7 @@ static void reload_config(void)
{ {
char *current_config_copy = strdup(current_config); char *current_config_copy = strdup(current_config);
clean_up(NULL, NULL); clean_up(NULL, NULL);
sleep(1); /* slight pause */
current_config = current_config_copy; current_config = current_config_copy;
initialisation(argc_copy, argv_copy); initialisation(argc_copy, argv_copy);
} }
@ -2350,6 +2358,8 @@ void clean_up(void *memtofree1, void* memtofree2)
{ {
int i; int i;
free_update_callbacks();
#ifdef NCURSES #ifdef NCURSES
if(output_methods & TO_NCURSES) { if(output_methods & TO_NCURSES) {
endwin(); endwin();
@ -2401,8 +2411,6 @@ void clean_up(void *memtofree1, void* memtofree2)
#endif /* X11 */ #endif /* X11 */
free_update_callbacks();
free_templates(); free_templates();
if (info.first_process) { if (info.first_process) {
@ -3169,6 +3177,9 @@ char load_config_file(const char *f)
CONF("extra_newline") { CONF("extra_newline") {
extra_newline = string_to_bool(value); extra_newline = string_to_bool(value);
} }
CONF("disable_auto_reload") {
disable_auto_reload = string_to_bool(value);
}
CONF("out_to_stderr") { CONF("out_to_stderr") {
if(string_to_bool(value)) if(string_to_bool(value))
output_methods |= TO_STDERR; output_methods |= TO_STDERR;