XMMS2

src/xmms/collsync.c

Go to the documentation of this file.
00001 /*  XMMS2 - X Music Multiplexer System
00002  *  Copyright (C) 2003-2009 XMMS2 Team
00003  *
00004  *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2.1 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  */
00016 
00017 
00018 /** @file
00019  *  Manages the synchronization of collections to the database at 10 seconds
00020  *  after the last collections-change.
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  * Wait until no collections have changed for 10 seconds, then sync.
00035  * @internal
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         /* Wait until no requests have been filed for 10 seconds. */
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             /* The dag might be locked when calling schedule_sync, so we need to
00062              * unlock to avoid deadlocks */
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  * Get the collection-to-database-synchronization thread running.
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  * Shutdown the collection-to-database-synchronization thread.
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  * Schedule a collection-to-database-synchronization in 10 seconds.
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 }