00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "xmmspriv/xmms_collsync.h"
00024 #include "xmms/xmms_log.h"
00025 #include <glib.h>
00026
00027 static GThread *thread;
00028 static GMutex *mutex;
00029 static GCond *cond;
00030 static gboolean want_sync = FALSE;
00031 static gboolean keep_running = TRUE;
00032
00033
00034
00035
00036
00037 static gpointer
00038 do_loop (gpointer udata)
00039 {
00040 xmms_coll_dag_t *dag = udata;
00041 GTimeVal time;
00042
00043 g_mutex_lock (mutex);
00044
00045 while (keep_running) {
00046 if (!want_sync) {
00047 g_cond_wait (cond, mutex);
00048 }
00049
00050
00051 while (keep_running && want_sync) {
00052 want_sync = FALSE;
00053
00054 g_get_current_time (&time);
00055 g_time_val_add (&time, 10000000);
00056
00057 g_cond_timed_wait (cond, mutex, &time);
00058 }
00059
00060 if (keep_running) {
00061
00062
00063 g_mutex_unlock (mutex);
00064
00065 XMMS_DBG ("Syncing collections to database.");
00066 xmms_collection_sync (dag);
00067
00068 g_mutex_lock (mutex);
00069 }
00070 }
00071
00072 g_mutex_unlock (mutex);
00073
00074 return NULL;
00075 }
00076
00077
00078
00079
00080 void
00081 xmms_coll_sync_init (xmms_coll_dag_t *dag)
00082 {
00083 cond = g_cond_new ();
00084 mutex = g_mutex_new ();
00085
00086 thread = g_thread_create (do_loop, dag, TRUE, NULL);
00087 }
00088
00089
00090
00091
00092 void
00093 xmms_coll_sync_shutdown ()
00094 {
00095 g_mutex_lock (mutex);
00096 keep_running = FALSE;
00097 g_cond_signal (cond);
00098 g_mutex_unlock (mutex);
00099
00100 g_thread_join (thread);
00101
00102 g_mutex_free (mutex);
00103 g_cond_free (cond);
00104 }
00105
00106
00107
00108
00109 void
00110 xmms_coll_sync_schedule_sync ()
00111 {
00112 g_mutex_lock (mutex);
00113 want_sync = TRUE;
00114 g_cond_signal (cond);
00115 g_mutex_unlock (mutex);
00116 }