diff options
author | Max Kellermann <max@duempel.org> | 2008-04-12 04:14:25 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2008-04-12 04:14:25 +0000 |
commit | 0146fbe3ceab96759f1e55e73daaa6d6d7bc4b9b (patch) | |
tree | 88d4b8a9d3d7014b9bb0763d7b86c08a19695307 /src/notify.c | |
parent | 38e0dafc4c1e71a5ff7a9d9563d80b096945f97b (diff) | |
download | mpd-0146fbe3ceab96759f1e55e73daaa6d6d7bc4b9b.tar.gz mpd-0146fbe3ceab96759f1e55e73daaa6d6d7bc4b9b.tar.xz mpd-0146fbe3ceab96759f1e55e73daaa6d6d7bc4b9b.zip |
use the pthread API in notify.c
This patch rewrites notify.c to use the pthread API, namely
pthread_mutex and pthread_cond. This is a lot cheaper and easier than
the pipe() hack.
git-svn-id: https://svn.musicpd.org/mpd/trunk@7280 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to '')
-rw-r--r-- | src/notify.c | 54 |
1 files changed, 37 insertions, 17 deletions
diff --git a/src/notify.c b/src/notify.c index 4eda79391..5d53b231d 100644 --- a/src/notify.c +++ b/src/notify.c @@ -17,32 +17,52 @@ */ #include "notify.h" -#include "os_compat.h" -#include "log.h" -#include "utils.h" -void notifyInit(Notify *notify) +int notifyInit(Notify *notify) { - if (pipe(notify->fds) < 0) - FATAL("Couldn't open pipe: %s", strerror(errno)); - if (set_nonblocking(notify->fds[1]) < 0) - FATAL("Couldn't set non-blocking on notify fd: %s", - strerror(errno)); + int ret; + + ret = pthread_mutex_init(¬ify->mutex, NULL); + if (ret != 0) + return ret; + + ret = pthread_cond_init(¬ify->cond, NULL); + if (ret != 0) { + pthread_mutex_destroy(¬ify->mutex); + return ret; + } + + notify->pending = 0; + + return 0; } -void notifyWait(Notify *notify) +void notifyEnter(Notify *notify) { - char buffer[64]; + pthread_mutex_lock(¬ify->mutex); +} - if (read(notify->fds[0], buffer, sizeof(buffer)) < 0) - FATAL("error reading from pipe: %s\n", strerror(errno)); +void notifyLeave(Notify *notify) +{ + pthread_mutex_unlock(¬ify->mutex); +} + +void notifyWait(Notify *notify) +{ + if (!notify->pending) + pthread_cond_wait(¬ify->cond, ¬ify->mutex); + notify->pending = 0; } void notifySignal(Notify *notify) { - char buffer = 0; + notify->pending = 1; + pthread_cond_signal(¬ify->cond); +} - if (write(notify->fds[1], &buffer, sizeof(buffer)) < 0 && - errno != EAGAIN && errno != EINTR) - FATAL("error writing to pipe: %s\n", strerror(errno)); +void notifySignalSync(Notify *notify) +{ + pthread_mutex_lock(¬ify->mutex); + notifySignal(notify); + pthread_mutex_unlock(¬ify->mutex); } |