diff options
author | Max Kellermann <max@duempel.org> | 2009-01-18 18:10:15 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2009-01-18 18:10:15 +0100 |
commit | 0dd2dfff9d7fd8208d87eae98c6cd8976d4b2e38 (patch) | |
tree | 736e3572b8d15874db7080c21f60f9d76f38c22c /src/state_file.c | |
parent | 84de45afbaed7ea27f7ac84f3adf1faa26f61879 (diff) | |
download | mpd-0dd2dfff9d7fd8208d87eae98c6cd8976d4b2e38.tar.gz mpd-0dd2dfff9d7fd8208d87eae98c6cd8976d4b2e38.tar.xz mpd-0dd2dfff9d7fd8208d87eae98c6cd8976d4b2e38.zip |
main: moved the save_state timer to state_file.c
The state_file library should manage its own regular saves.
Diffstat (limited to 'src/state_file.c')
-rw-r--r-- | src/state_file.c | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/state_file.c b/src/state_file.c index 1139c7548..64fd67c4c 100644 --- a/src/state_file.c +++ b/src/state_file.c @@ -22,6 +22,7 @@ #include "volume.h" #include <glib.h> +#include <assert.h> #include <string.h> #include <errno.h> @@ -39,7 +40,11 @@ static struct _sf_cb { static char *state_file_path; -void write_state_file(void) +/** the GLib source id for the save timer */ +static guint save_state_source_id; + +static void +state_file_write(void) { unsigned int i; FILE *fp; @@ -68,6 +73,8 @@ state_file_read(void) assert(state_file_path != NULL); + g_debug("Saving state file"); + fp = fopen(state_file_path, "r"); if (G_UNLIKELY(!fp)) { g_warning("failed to open %s: %s", @@ -82,6 +89,17 @@ state_file_read(void) while(fclose(fp) && errno == EINTR) /* nothing */; } +/** + * This function is called every 5 minutes by the GLib main loop, and + * saves the state file. + */ +static gboolean +timer_save_state_file(G_GNUC_UNUSED gpointer data) +{ + state_file_write(); + return true; +} + void state_file_init(const char *path) { @@ -92,13 +110,19 @@ state_file_init(const char *path) state_file_path = g_strdup(path); state_file_read(); + + save_state_source_id = g_timeout_add(5 * 60 * 1000, + timer_save_state_file, NULL); } void state_file_finish(void) { + if (save_state_source_id != 0) + g_source_remove(save_state_source_id); + if (state_file_path != NULL) - write_state_file(); + state_file_write(); g_free(state_file_path); } |