aboutsummaryrefslogtreecommitdiffstats
path: root/src/directory.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2008-09-22 02:04:23 -0700
committerEric Wong <normalperson@yhbt.net>2008-09-22 02:07:22 -0700
commitada24f9a921ff95d874195acf253b5a9dd12213d (patch)
tree25829d050689696f1c94500062b6c53ea2437d91 /src/directory.c
parente3cd520c6441f91655e799e60fe49c4bb7de13bc (diff)
downloadmpd-ada24f9a921ff95d874195acf253b5a9dd12213d.tar.gz
mpd-ada24f9a921ff95d874195acf253b5a9dd12213d.tar.xz
mpd-ada24f9a921ff95d874195acf253b5a9dd12213d.zip
directory: update do its work inside a thread
A lot of the preparation was needed (and done in previous months) in making update thread-safe, but here it is. This was the first thing I made work inside a thread when I started mpd-uclinux many years ago, and also the last thing I've done in mainline mpd to work inside a thread, go figure.
Diffstat (limited to 'src/directory.c')
-rw-r--r--src/directory.c158
1 files changed, 50 insertions, 108 deletions
diff --git a/src/directory.c b/src/directory.c
index 08b3fb0e2..cb1995a93 100644
--- a/src/directory.c
+++ b/src/directory.c
@@ -54,17 +54,19 @@ enum update_return {
UPDATE_RETURN_UPDATED = 1
};
+enum update_progress {
+ UPDATE_PROGRESS_IDLE = 0,
+ UPDATE_PROGRESS_RUNNING = 1,
+ UPDATE_PROGRESS_DONE = 2
+} progress;
+
static Directory *mp3rootDirectory;
static time_t directory_dbModTime;
-static sig_atomic_t directory_updatePid;
-
-static sig_atomic_t update_exited;
-
-static sig_atomic_t update_status;
+static pthread_t update_thr;
-static sig_atomic_t directory_updateJobId;
+static int directory_updateJobId;
static DirectoryList *newDirectoryList(void);
@@ -116,127 +118,67 @@ static char *getDbFile(void)
int isUpdatingDB(void)
{
- return directory_updatePid > 0 ? directory_updateJobId : 0;
+ return (progress != UPDATE_PROGRESS_IDLE) ? directory_updateJobId : 0;
}
-void directory_sigChldHandler(int pid, int status)
+void reap_update_task(void)
{
- if (directory_updatePid == pid) {
- update_status = status;
- update_exited = 1;
- wakeup_main_task();
- }
+ if (progress != UPDATE_PROGRESS_DONE)
+ return;
+ pthread_join(update_thr, NULL);
+ progress = UPDATE_PROGRESS_IDLE;
}
-void readDirectoryDBIfUpdateIsFinished(void)
+static void * update_task(void *arg)
{
- int status;
-
- if (!update_exited)
- return;
+ List *path_list = (List *)arg;
+ enum update_return ret = UPDATE_RETURN_NOUPDATE;
- status = update_status;
-
- if (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM) {
- ERROR("update process died from a non-TERM signal: %d\n",
- WTERMSIG(status));
- } else if (!WIFSIGNALED(status)) {
- switch (WEXITSTATUS(status)) {
- case DIRECTORY_UPDATE_EXIT_UPDATE:
- DEBUG("update finished successfully with changes\n");
- readDirectoryDB();
- DEBUG("update changes read into memory\n");
- playlistVersionChange();
- case DIRECTORY_UPDATE_EXIT_NOUPDATE:
- DEBUG("update exited successfully with no changes\n");
- break;
- default:
- ERROR("error updating db\n");
+ if (path_list) {
+ ListNode *node = path_list->firstNode;
+
+ while (node) {
+ switch (updatePath(node->key)) {
+ case UPDATE_RETURN_ERROR:
+ ret = UPDATE_RETURN_ERROR;
+ goto out;
+ case UPDATE_RETURN_NOUPDATE:
+ break;
+ case UPDATE_RETURN_UPDATED:
+ ret = UPDATE_RETURN_UPDATED;
+ }
+ node = node->nextNode;
}
+ free(path_list);
+ } else {
+ ret = updateDirectory(mp3rootDirectory);
}
- update_exited = 0;
- directory_updatePid = 0;
+
+ if (ret == UPDATE_RETURN_UPDATED && writeDirectoryDB() < 0)
+ ret = UPDATE_RETURN_ERROR;
+out:
+ progress = UPDATE_PROGRESS_DONE;
+ wakeup_main_task();
+ return (void *)ret;
}
-int updateInit(int fd, List * pathList)
+int updateInit(int fd, List * path_list)
{
- if (directory_updatePid > 0) {
- commandError(fd, ACK_ERROR_UPDATE_ALREADY, "already updating");
- return -1;
- }
-
- /*
- * need to block CHLD signal, cause it can exit before we
- * even get a chance to assign directory_updatePID
- *
- * Update: our signal blocking is is utterly broken by
- * pthreads(); goal will be to remove dependency on signals;
- * but for now use the my_usleep hack below.
- */
- blockSignals();
- directory_updatePid = fork();
- if (directory_updatePid == 0) {
- /* child */
- enum update_return dbUpdated = UPDATE_RETURN_NOUPDATE;
-
- unblockSignals();
-
- finishSigHandlers();
- closeAllListenSockets();
- client_manager_deinit();
- finishPlaylist();
- finishVolume();
-
- /*
- * XXX HACK to workaround race condition where
- * directory_updatePid is still zero in the parent even upon
- * entry of directory_sigChldHandler.
- */
- my_usleep(100000);
-
- if (pathList) {
- ListNode *node = pathList->firstNode;
-
- while (node) {
- switch (updatePath(node->key)) {
- case 1:
- dbUpdated = UPDATE_RETURN_UPDATED;
- break;
- case 0:
- break;
- default:
- exit(DIRECTORY_UPDATE_EXIT_ERROR);
- }
- node = node->nextNode;
- }
- } else {
- if ((dbUpdated = updateDirectory(mp3rootDirectory)) < 0)
- exit(DIRECTORY_UPDATE_EXIT_ERROR);
- }
+ pthread_attr_t attr;
- if (dbUpdated == UPDATE_RETURN_NOUPDATE)
- exit(DIRECTORY_UPDATE_EXIT_NOUPDATE);
-
- /* ignore signals since we don't want them to corrupt the db */
- ignoreSignals();
- if (writeDirectoryDB() < 0) {
- exit(DIRECTORY_UPDATE_EXIT_ERROR);
- }
- exit(DIRECTORY_UPDATE_EXIT_UPDATE);
- } else if (directory_updatePid < 0) {
- unblockSignals();
- ERROR("updateInit: Problems forking()'ing\n");
- commandError(fd, ACK_ERROR_SYSTEM,
- "problems trying to update");
- directory_updatePid = 0;
+ if (progress != UPDATE_PROGRESS_IDLE) {
+ commandError(fd, ACK_ERROR_UPDATE_ALREADY, "already updating");
return -1;
}
- unblockSignals();
+ progress = UPDATE_PROGRESS_RUNNING;
+ pthread_attr_init(&attr);
+ if (pthread_create(&update_thr, &attr, update_task, path_list))
+ FATAL("Failed to spawn update task: %s\n", strerror(errno));
directory_updateJobId++;
if (directory_updateJobId > 1 << 15)
directory_updateJobId = 1;
- DEBUG("updateInit: fork()'d update child for update job id %i\n",
+ DEBUG("updateInit: spawned update thread for update job id %i\n",
(int)directory_updateJobId);
fdprintf(fd, "updating_db: %i\n", (int)directory_updateJobId);