2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "update_internal.h"
26 #include "event_pipe.h"
36 static enum update_progress
{
37 UPDATE_PROGRESS_IDLE
= 0,
38 UPDATE_PROGRESS_RUNNING
= 1,
39 UPDATE_PROGRESS_DONE
= 2
44 static GThread
*update_thr
;
46 static const unsigned update_task_id_max
= 1 << 15;
48 static unsigned update_task_id
;
50 /* XXX this flag is passed to update_task() */
56 return (progress
!= UPDATE_PROGRESS_IDLE
) ? update_task_id
: 0;
59 static void * update_task(void *_path
)
61 const char *path
= _path
;
63 if (path
!= NULL
&& *path
!= 0)
64 g_debug("starting: %s", path
);
68 modified
= update_walk(path
, discard
);
70 if (modified
|| !db_exists())
73 if (path
!= NULL
&& *path
!= 0)
74 g_debug("finished: %s", path
);
79 progress
= UPDATE_PROGRESS_DONE
;
80 event_pipe_emit(PIPE_EVENT_UPDATE
);
85 spawn_update_task(const char *path
)
89 assert(g_thread_self() == main_task
);
91 progress
= UPDATE_PROGRESS_RUNNING
;
94 update_thr
= g_thread_create(update_task
, g_strdup(path
), TRUE
, &e
);
95 if (update_thr
== NULL
)
96 g_error("Failed to spawn update task: %s", e
->message
);
98 if (++update_task_id
> update_task_id_max
)
100 g_debug("spawned thread for update job id %i", update_task_id
);
104 update_enqueue(const char *path
, bool _discard
)
106 assert(g_thread_self() == main_task
);
108 if (!mapper_has_music_directory())
111 if (progress
!= UPDATE_PROGRESS_IDLE
) {
112 unsigned next_task_id
=
113 update_queue_push(path
, discard
, update_task_id
);
114 if (next_task_id
== 0)
117 return next_task_id
> update_task_id_max
? 1 : next_task_id
;
121 spawn_update_task(path
);
123 idle_add(IDLE_UPDATE
);
125 return update_task_id
;
129 * Called in the main thread after the database update is finished.
131 static void update_finished_event(void)
135 assert(progress
== UPDATE_PROGRESS_DONE
);
137 g_thread_join(update_thr
);
139 idle_add(IDLE_UPDATE
);
142 /* send "idle" events */
143 playlist_increment_version_all(&g_playlist
);
144 idle_add(IDLE_DATABASE
);
147 path
= update_queue_shift(&discard
);
149 /* schedule the next path */
150 spawn_update_task(path
);
153 progress
= UPDATE_PROGRESS_IDLE
;
159 void update_global_init(void)
161 event_pipe_register(PIPE_EVENT_UPDATE
, update_finished_event
);
163 update_remove_global_init();
164 update_walk_global_init();
167 void update_global_finish(void)
169 update_walk_global_finish();
170 update_remove_global_finish();