XMMS2
collsync.c
Go to the documentation of this file.
1 /* XMMS2 - X Music Multiplexer System
2  * Copyright (C) 2003-2009 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 
23 #include "xmmspriv/xmms_collsync.h"
24 #include "xmms/xmms_log.h"
25 #include <glib.h>
26 
27 static GThread *thread;
28 static GMutex *mutex;
29 static GCond *cond;
30 static gboolean want_sync = FALSE;
31 static gboolean keep_running = TRUE;
32 
33 /**
34  * Wait until no collections have changed for 10 seconds, then sync.
35  * @internal
36  */
37 static gpointer
38 do_loop (gpointer udata)
39 {
40  xmms_coll_dag_t *dag = udata;
41  GTimeVal time;
42 
43  g_mutex_lock (mutex);
44 
45  while (keep_running) {
46  if (!want_sync) {
47  g_cond_wait (cond, mutex);
48  }
49 
50  /* Wait until no requests have been filed for 10 seconds. */
51  while (keep_running && want_sync) {
52  want_sync = FALSE;
53 
54  g_get_current_time (&time);
55  g_time_val_add (&time, 10000000);
56 
57  g_cond_timed_wait (cond, mutex, &time);
58  }
59 
60  if (keep_running) {
61  /* The dag might be locked when calling schedule_sync, so we need to
62  * unlock to avoid deadlocks */
63  g_mutex_unlock (mutex);
64 
65  XMMS_DBG ("Syncing collections to database.");
67 
68  g_mutex_lock (mutex);
69  }
70  }
71 
72  g_mutex_unlock (mutex);
73 
74  return NULL;
75 }
76 
77 /**
78  * Get the collection-to-database-synchronization thread running.
79  */
80 void
82 {
83  cond = g_cond_new ();
84  mutex = g_mutex_new ();
85 
86  thread = g_thread_create (do_loop, dag, TRUE, NULL);
87 }
88 
89 /**
90  * Shutdown the collection-to-database-synchronization thread.
91  */
92 void
94 {
95  g_mutex_lock (mutex);
96  keep_running = FALSE;
97  g_cond_signal (cond);
98  g_mutex_unlock (mutex);
99 
100  g_thread_join (thread);
101 
102  g_mutex_free (mutex);
103  g_cond_free (cond);
104 }
105 
106 /**
107  * Schedule a collection-to-database-synchronization in 10 seconds.
108  */
109 void
111 {
112  g_mutex_lock (mutex);
113  want_sync = TRUE;
114  g_cond_signal (cond);
115  g_mutex_unlock (mutex);
116 }
void xmms_coll_sync_init(xmms_coll_dag_t *dag)
Get the collection-to-database-synchronization thread running.
Definition: collsync.c:81
void xmms_coll_sync_shutdown()
Shutdown the collection-to-database-synchronization thread.
Definition: collsync.c:93
void xmms_coll_sync_schedule_sync()
Schedule a collection-to-database-synchronization in 10 seconds.
Definition: collsync.c:110
#define XMMS_DBG(fmt,...)
Definition: xmms_log.h:32
void xmms_collection_sync(xmms_coll_dag_t *dag)
Synchronize collection data to the database (i.e.
Definition: collection.c:593
struct xmms_coll_dag_St xmms_coll_dag_t