diff options
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/decode.c | 1 | ||||
-rw-r--r-- | src/notify.c | 70 | ||||
-rw-r--r-- | src/notify.h | 47 | ||||
-rw-r--r-- | src/outputBuffer.c | 4 | ||||
-rw-r--r-- | src/outputBuffer.h | 2 |
6 files changed, 125 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 82e46e6ee..91accd88b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -29,6 +29,7 @@ mpd_inputPlugins = \ mpd_headers = \ + notify.h \ ack.h \ audio.h \ audioOutput.h \ @@ -87,6 +88,7 @@ mpd_SOURCES = \ $(mpd_headers) \ $(mpd_audioOutputs) \ $(mpd_inputPlugins) \ + notify.c \ audio.c \ audioOutput.c \ buffer2array.c \ diff --git a/src/decode.c b/src/decode.c index 316cd34ba..3681e8025 100644 --- a/src/decode.c +++ b/src/decode.c @@ -623,6 +623,7 @@ static void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb->begin = 0; } else cb->begin++; + signalNotify(&cb->notify); } else if (cb->begin != end && cb->begin == next) { if (doCrossFade == 1 && nextChunk >= 0) { nextChunk = cb->begin + crossFadeChunks; diff --git a/src/notify.c b/src/notify.c new file mode 100644 index 000000000..85db46192 --- /dev/null +++ b/src/notify.c @@ -0,0 +1,70 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Max Kellermann <max@duempel.org> + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "notify.h" + +#include <assert.h> +#include <fcntl.h> +#include <unistd.h> + +int set_nonblock(int fd) +{ + int ret; + + assert(fd >= 0); + + ret = fcntl(fd, F_GETFL, 0); + if (ret < 0) + return ret; + + return fcntl(fd, F_SETFL, ret|O_NONBLOCK); +} + +int initNotify(Notify *notify) +{ + int ret; + + ret = pipe(notify->fds); + if (ret < 0) + return -1; + + ret = set_nonblock(notify->fds[1]); + if (ret < 0) { + close(notify->fds[0]); + close(notify->fds[1]); + return -1; + } + + return 0; +} + +int waitNotify(Notify *notify) +{ + char buffer[64]; + ssize_t nbytes; + + nbytes = read(notify->fds[0], buffer, sizeof(buffer)); + return (int)nbytes; +} + +int signalNotify(Notify *notify) +{ + char buffer[1] = { 0 }; + + return (int)write(notify->fds[1], &buffer, sizeof(buffer)); +} diff --git a/src/notify.h b/src/notify.h new file mode 100644 index 000000000..ce6396a96 --- /dev/null +++ b/src/notify.h @@ -0,0 +1,47 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Max Kellermann <max@duempel.org> + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef NOTIFY_H +#define NOTIFY_H + +/* + * This library implements inter-process signalling using blocking + * read() on an anonymous pipe. As a side effect, the read() system + * call has the same signal interruption behaviour as the old sleep + * function. + * + * As soon as mpd uses threading instead of fork()/shm, we can replace + * this library with a pthread_cond object. + * + * This code is experimental and carries a lot of overhead. Still, it + * uses less resources than the old polling code with a fixed sleep + * time. + * + */ + +typedef struct _Notify { + int fds[2]; +} Notify; + +int initNotify(Notify *notify); + +int waitNotify(Notify *notify); + +int signalNotify(Notify *notify); + +#endif diff --git a/src/outputBuffer.c b/src/outputBuffer.c index 25c9b3c12..cda56d4b1 100644 --- a/src/outputBuffer.c +++ b/src/outputBuffer.c @@ -45,6 +45,8 @@ void initOutputBuffer(OutputBuffer * cb, char *chunks) (float *)(((char *)cb->metaChunk) + buffered_chunks * sizeof(mpd_sint8)); cb->acceptMetadata = 0; + + initNotify(&cb->notify); } void clearAllMetaChunkSets(OutputBuffer * cb) @@ -131,7 +133,7 @@ int sendDataToOutputBuffer(OutputBuffer * cb, InputStream * inStream, } if (!inStream || bufferInputStream(inStream) <= 0) { - my_usleep(10000); + waitNotify(&cb->notify); } } if (dc->stop) diff --git a/src/outputBuffer.h b/src/outputBuffer.h index 9bd9d48c8..99d3010f0 100644 --- a/src/outputBuffer.h +++ b/src/outputBuffer.h @@ -26,6 +26,7 @@ #include "inputStream.h" #include "metadataChunk.h" #include "replayGain.h" +#include "notify.h" #define OUTPUT_BUFFER_DC_STOP -1 #define OUTPUT_BUFFER_DC_SEEK -2 @@ -45,6 +46,7 @@ typedef struct _OutputBuffer { mpd_sint8 metaChunkSet[BUFFERED_METACHUNKS]; mpd_sint8 *volatile metaChunk; volatile mpd_sint8 acceptMetadata; + Notify notify; } OutputBuffer; void initOutputBuffer(OutputBuffer * cb, char *chunks); |