XMMS2
|
00001 /* XMMS2 - X Music Multiplexer System 00002 * Copyright (C) 2003-2011 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 "xmmspriv/xmms_thread_name.h" 00025 #include "xmms/xmms_log.h" 00026 #include <glib.h> 00027 00028 static GThread *thread; 00029 static GMutex *mutex; 00030 static GCond *cond; 00031 static gboolean want_sync = FALSE; 00032 static gboolean keep_running = TRUE; 00033 00034 /** 00035 * Wait until no collections have changed for 10 seconds, then sync. 00036 * @internal 00037 */ 00038 static gpointer 00039 do_loop (gpointer udata) 00040 { 00041 xmms_coll_dag_t *dag = udata; 00042 GTimeVal time; 00043 00044 xmms_set_thread_name ("x2 coll sync"); 00045 00046 g_mutex_lock (mutex); 00047 00048 while (keep_running) { 00049 if (!want_sync) { 00050 g_cond_wait (cond, mutex); 00051 } 00052 00053 /* Wait until no requests have been filed for 10 seconds. */ 00054 while (keep_running && want_sync) { 00055 want_sync = FALSE; 00056 00057 g_get_current_time (&time); 00058 g_time_val_add (&time, 10000000); 00059 00060 g_cond_timed_wait (cond, mutex, &time); 00061 } 00062 00063 if (keep_running) { 00064 /* The dag might be locked when calling schedule_sync, so we need to 00065 * unlock to avoid deadlocks */ 00066 g_mutex_unlock (mutex); 00067 00068 XMMS_DBG ("Syncing collections to database."); 00069 xmms_collection_sync (dag); 00070 00071 g_mutex_lock (mutex); 00072 } 00073 } 00074 00075 g_mutex_unlock (mutex); 00076 00077 return NULL; 00078 } 00079 00080 /** 00081 * Get the collection-to-database-synchronization thread running. 00082 */ 00083 void 00084 xmms_coll_sync_init (xmms_coll_dag_t *dag) 00085 { 00086 cond = g_cond_new (); 00087 mutex = g_mutex_new (); 00088 00089 thread = g_thread_create (do_loop, dag, TRUE, NULL); 00090 } 00091 00092 /** 00093 * Shutdown the collection-to-database-synchronization thread. 00094 */ 00095 void 00096 xmms_coll_sync_shutdown () 00097 { 00098 g_mutex_lock (mutex); 00099 keep_running = FALSE; 00100 g_cond_signal (cond); 00101 g_mutex_unlock (mutex); 00102 00103 g_thread_join (thread); 00104 00105 g_mutex_free (mutex); 00106 g_cond_free (cond); 00107 } 00108 00109 /** 00110 * Schedule a collection-to-database-synchronization in 10 seconds. 00111 */ 00112 void 00113 xmms_coll_sync_schedule_sync () 00114 { 00115 g_mutex_lock (mutex); 00116 want_sync = TRUE; 00117 g_cond_signal (cond); 00118 g_mutex_unlock (mutex); 00119 }