ThreadInputStream.cxx 3.17 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "ThreadInputStream.hxx"
#include "thread/Name.hxx"
#include "util/CircularBuffer.hxx"
#include "util/HugeAllocator.hxx"

#include <assert.h>
#include <string.h>

ThreadInputStream::~ThreadInputStream()
{
31 32 33 34 35 36 37 38 39
	Lock();
	close = true;
	wake_cond.signal();
	Unlock();

	Cancel();

	thread.Join();

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	if (buffer != nullptr) {
		buffer->Clear();
		HugeFree(buffer->Write().data, buffer_size);
		delete buffer;
	}
}

InputStream *
ThreadInputStream::Start(Error &error)
{
	assert(buffer == nullptr);

	void *p = HugeAllocate(buffer_size);
	if (p == nullptr) {
		error.SetErrno();
		return nullptr;
	}

	buffer = new CircularBuffer<uint8_t>((uint8_t *)p, buffer_size);

	if (!thread.Start(ThreadFunc, this, error))
		return nullptr;

63
	return this;
64 65 66 67 68
}

inline void
ThreadInputStream::ThreadFunc()
{
69
	FormatThreadName("input:%s", plugin);
70

71
	Lock();
72
	if (!Open(postponed_error)) {
73
		cond.broadcast();
74
		Unlock();
75 76 77 78
		return;
	}

	/* we're ready, tell it to our client */
79
	SetReady();
80 81 82 83 84 85

	while (!close) {
		assert(!postponed_error.IsDefined());

		auto w = buffer->Write();
		if (w.IsEmpty()) {
86
			wake_cond.wait(mutex);
87
		} else {
88
			Unlock();
89 90

			Error error;
91
			size_t nbytes = ThreadRead(w.data, w.size, error);
92

93
			Lock();
94
			cond.broadcast();
95 96 97 98 99 100 101 102 103 104 105

			if (nbytes == 0) {
				eof = true;
				postponed_error = std::move(error);
				break;
			}

			buffer->Append(nbytes);
		}
	}

106
	Unlock();
107 108 109 110 111 112 113 114 115 116 117

	Close();
}

void
ThreadInputStream::ThreadFunc(void *ctx)
{
	ThreadInputStream &tis = *(ThreadInputStream *)ctx;
	tis.ThreadFunc();
}

118 119
bool
ThreadInputStream::Check(Error &error)
120
{
121 122
	assert(!thread.IsInside());

123 124 125 126 127 128 129 130 131
	if (postponed_error.IsDefined()) {
		error = std::move(postponed_error);
		return false;
	}

	return true;
}

bool
132
ThreadInputStream::IsAvailable()
133
{
134 135
	assert(!thread.IsInside());

136 137 138 139
	return !buffer->IsEmpty() || eof || postponed_error.IsDefined();
}

inline size_t
140
ThreadInputStream::Read(void *ptr, size_t read_size, Error &error)
141
{
142 143
	assert(!thread.IsInside());

144 145 146 147 148 149 150 151
	while (true) {
		if (postponed_error.IsDefined()) {
			error = std::move(postponed_error);
			return 0;
		}

		auto r = buffer->Read();
		if (!r.IsEmpty()) {
152
			size_t nbytes = std::min(read_size, r.size);
153 154 155
			memcpy(ptr, r.data, nbytes);
			buffer->Consume(nbytes);
			wake_cond.broadcast();
156
			offset += nbytes;
157 158 159 160 161 162
			return nbytes;
		}

		if (eof)
			return 0;

163
		cond.wait(mutex);
164 165 166 167
	}
}

bool
168
ThreadInputStream::IsEOF()
169
{
170 171
	assert(!thread.IsInside());

172
	return eof;
173
}