XMMS2
collsync.c
Go to the documentation of this file.
1/* XMMS2 - X Music Multiplexer System
2 * Copyright (C) 2003-2011 XMMS2 Team
3 *
4 * PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 */
16
17
18/** @file
19 * Manages the synchronization of collections to the database at 10 seconds
20 * after the last collections-change.
21 */
22
25#include "xmms/xmms_log.h"
26#include <glib.h>
27
28static GThread *thread;
29static GMutex *mutex;
30static GCond *cond;
31static gboolean want_sync = FALSE;
32static gboolean keep_running = TRUE;
33
34/**
35 * Wait until no collections have changed for 10 seconds, then sync.
36 * @internal
37 */
38static gpointer
39do_loop (gpointer udata)
40{
41 xmms_coll_dag_t *dag = udata;
42 GTimeVal time;
43
44 xmms_set_thread_name ("x2 coll sync");
45
46 g_mutex_lock (mutex);
47
48 while (keep_running) {
49 if (!want_sync) {
50 g_cond_wait (cond, mutex);
51 }
52
53 /* Wait until no requests have been filed for 10 seconds. */
54 while (keep_running && want_sync) {
55 want_sync = FALSE;
56
57 g_get_current_time (&time);
58 g_time_val_add (&time, 10000000);
59
60 g_cond_timed_wait (cond, mutex, &time);
61 }
62
63 if (keep_running) {
64 /* The dag might be locked when calling schedule_sync, so we need to
65 * unlock to avoid deadlocks */
66 g_mutex_unlock (mutex);
67
68 XMMS_DBG ("Syncing collections to database.");
70
71 g_mutex_lock (mutex);
72 }
73 }
74
75 g_mutex_unlock (mutex);
76
77 return NULL;
78}
79
80/**
81 * Get the collection-to-database-synchronization thread running.
82 */
83void
85{
86 cond = g_cond_new ();
87 mutex = g_mutex_new ();
88
89 thread = g_thread_create (do_loop, dag, TRUE, NULL);
90}
91
92/**
93 * Shutdown the collection-to-database-synchronization thread.
94 */
95void
97{
98 g_mutex_lock (mutex);
99 keep_running = FALSE;
100 g_cond_signal (cond);
101 g_mutex_unlock (mutex);
102
103 g_thread_join (thread);
104
105 g_mutex_free (mutex);
106 g_cond_free (cond);
107}
108
109/**
110 * Schedule a collection-to-database-synchronization in 10 seconds.
111 */
112void
114{
115 g_mutex_lock (mutex);
116 want_sync = TRUE;
117 g_cond_signal (cond);
118 g_mutex_unlock (mutex);
119}
void xmms_coll_sync_schedule_sync()
Schedule a collection-to-database-synchronization in 10 seconds.
Definition: collsync.c:113
void xmms_coll_sync_shutdown()
Shutdown the collection-to-database-synchronization thread.
Definition: collsync.c:96
void xmms_coll_sync_init(xmms_coll_dag_t *dag)
Get the collection-to-database-synchronization thread running.
Definition: collsync.c:84
void xmms_collection_sync(xmms_coll_dag_t *dag)
Synchronize collection data to the database (i.e.
Definition: collection.c:539
void xmms_set_thread_name(const gchar *name)
#define XMMS_DBG(fmt,...)
Definition: xmms_log.h:32
struct xmms_coll_dag_St xmms_coll_dag_t