2004-05-07 04:42:49 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-05-07 04:42:49 +02:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2008-11-02 14:12:52 +01:00
|
|
|
#include "pipe.h"
|
2009-03-06 00:42:03 +01:00
|
|
|
#include "buffer.h"
|
2009-03-03 22:23:25 +01:00
|
|
|
#include "chunk.h"
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-11-02 16:55:48 +01:00
|
|
|
#include <glib.h>
|
2008-04-12 06:21:09 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
#include <assert.h>
|
2008-04-12 06:13:51 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
struct music_pipe {
|
|
|
|
/** the first chunk */
|
|
|
|
struct music_chunk *head;
|
2008-11-02 16:58:49 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
/** a pointer to the tail of the chunk */
|
|
|
|
struct music_chunk **tail_r;
|
2008-04-12 06:13:51 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
/** the current number of chunks */
|
|
|
|
unsigned size;
|
2008-04-12 06:12:53 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
/** a mutex which protects #head and #tail_r */
|
|
|
|
GMutex *mutex;
|
|
|
|
};
|
2008-04-12 06:11:41 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
struct music_pipe *
|
|
|
|
music_pipe_new(void)
|
2008-04-12 06:12:42 +02:00
|
|
|
{
|
2009-03-06 00:42:03 +01:00
|
|
|
struct music_pipe *mp = g_new(struct music_pipe, 1);
|
2008-04-12 06:12:42 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
mp->head = NULL;
|
|
|
|
mp->tail_r = &mp->head;
|
|
|
|
mp->size = 0;
|
|
|
|
mp->mutex = g_mutex_new();
|
2008-04-12 06:12:42 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
return mp;
|
2008-04-12 06:12:42 +02:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
void
|
|
|
|
music_pipe_free(struct music_pipe *mp)
|
2008-04-12 06:13:11 +02:00
|
|
|
{
|
2009-03-06 00:42:03 +01:00
|
|
|
assert(mp->head == NULL);
|
|
|
|
assert(mp->tail_r == &mp->head);
|
2008-04-12 06:13:11 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
g_mutex_free(mp->mutex);
|
|
|
|
g_free(mp);
|
2008-04-12 06:13:11 +02:00
|
|
|
}
|
|
|
|
|
2009-03-07 19:56:31 +01:00
|
|
|
const struct music_chunk *
|
|
|
|
music_pipe_peek(const struct music_pipe *mp)
|
|
|
|
{
|
|
|
|
return mp->head;
|
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
struct music_chunk *
|
2009-03-06 00:42:03 +01:00
|
|
|
music_pipe_shift(struct music_pipe *mp)
|
2008-04-12 06:11:23 +02:00
|
|
|
{
|
2008-11-02 14:15:47 +01:00
|
|
|
struct music_chunk *chunk;
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
g_mutex_lock(mp->mutex);
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
chunk = mp->head;
|
|
|
|
if (chunk != NULL) {
|
2009-03-07 21:41:25 +01:00
|
|
|
assert(!music_chunk_is_empty(chunk));
|
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
mp->head = chunk->next;
|
|
|
|
--mp->size;
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
if (mp->head == NULL) {
|
|
|
|
assert(mp->size == 0);
|
|
|
|
assert(mp->tail_r == &chunk->next);
|
2009-01-17 13:09:29 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
mp->tail_r = &mp->head;
|
|
|
|
} else {
|
|
|
|
assert(mp->size > 0);
|
|
|
|
assert(mp->tail_r != &chunk->next);
|
|
|
|
}
|
2009-03-07 21:40:13 +01:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
/* poison the "next" reference */
|
|
|
|
chunk->next = (void*)0x01010101;
|
|
|
|
#endif
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
2009-01-17 13:09:29 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
g_mutex_unlock(mp->mutex);
|
2008-11-02 16:56:49 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
return chunk;
|
2008-11-02 16:56:49 +01:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
void
|
2009-03-06 00:42:03 +01:00
|
|
|
music_pipe_clear(struct music_pipe *mp, struct music_buffer *buffer)
|
2008-04-12 06:18:19 +02:00
|
|
|
{
|
2009-03-06 00:42:03 +01:00
|
|
|
struct music_chunk *chunk;
|
2008-11-02 16:58:49 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
while ((chunk = music_pipe_shift(mp)) != NULL)
|
|
|
|
music_buffer_return(buffer, chunk);
|
2008-04-12 06:18:19 +02:00
|
|
|
}
|
2008-11-13 02:06:58 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
void
|
|
|
|
music_pipe_push(struct music_pipe *mp, struct music_chunk *chunk)
|
2008-11-13 02:09:33 +01:00
|
|
|
{
|
2009-03-07 21:41:25 +01:00
|
|
|
assert(!music_chunk_is_empty(chunk));
|
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
g_mutex_lock(mp->mutex);
|
2008-11-13 02:09:33 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
chunk->next = NULL;
|
|
|
|
*mp->tail_r = chunk;
|
|
|
|
mp->tail_r = &chunk->next;
|
2008-11-13 02:09:33 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
++mp->size;
|
2008-11-13 02:09:33 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
g_mutex_unlock(mp->mutex);
|
2008-11-13 02:09:33 +01:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
unsigned
|
|
|
|
music_pipe_size(const struct music_pipe *mp)
|
2008-11-13 02:06:58 +01:00
|
|
|
{
|
2009-03-06 00:42:03 +01:00
|
|
|
return mp->size;
|
2008-11-13 02:06:58 +01:00
|
|
|
}
|