ThreadInputStream.cxx 3.23 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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"
22
#include "CondHandler.hxx"
23 24 25 26 27
#include "thread/Name.hxx"

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

28 29
ThreadInputStream::ThreadInputStream(const char *_plugin,
				     const char *_uri,
30
				     Mutex &_mutex,
31
				     size_t _buffer_size) noexcept
32
	:InputStream(_uri, _mutex),
33 34
	 plugin(_plugin),
	 thread(BIND_THIS_METHOD(ThreadFunc)),
35 36
	 allocation(_buffer_size),
	 buffer(&allocation.front(), allocation.size())
37
{
38
	allocation.ForkCow(false);
39 40
}

41 42
void
ThreadInputStream::Stop() noexcept
43
{
44 45 46
	if (!thread.IsDefined())
		return;

47
	{
48
		const std::lock_guard<Mutex> lock(mutex);
49 50 51
		close = true;
		wake_cond.signal();
	}
52 53 54 55 56

	Cancel();

	thread.Join();

57
	buffer.Clear();
58 59
}

60 61
void
ThreadInputStream::Start()
62
{
63
	thread.Start();
64 65
}

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

71
	const std::lock_guard<Mutex> lock(mutex);
72

73 74 75 76
	try {
		Open();
	} catch (...) {
		postponed_exception = std::current_exception();
77
		SetReady();
78 79 80 81
		return;
	}

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

	while (!close) {
85
		assert(!postponed_exception);
86

87
		auto w = buffer.Write();
88
		if (w.empty()) {
89
			wake_cond.wait(mutex);
90
		} else {
91 92
			size_t nbytes;

93
			try {
94
				const ScopeUnlock unlock(mutex);
95 96 97
				nbytes = ThreadRead(w.data, w.size);
			} catch (...) {
				postponed_exception = std::current_exception();
98
				InvokeOnAvailable();
99
				break;
100
			}
101

102
			InvokeOnAvailable();
103 104 105 106 107 108

			if (nbytes == 0) {
				eof = true;
				break;
			}

109
			buffer.Append(nbytes);
110 111 112 113 114 115
		}
	}

	Close();
}

116 117
void
ThreadInputStream::Check()
118
{
119 120
	assert(!thread.IsInside());

121 122
	if (postponed_exception)
		std::rethrow_exception(postponed_exception);
123 124 125
}

bool
126
ThreadInputStream::IsAvailable() noexcept
127
{
128 129
	assert(!thread.IsInside());

130
	return !buffer.empty() || eof || postponed_exception;
131 132 133
}

inline size_t
134
ThreadInputStream::Read(void *ptr, size_t read_size)
135
{
136 137
	assert(!thread.IsInside());

138 139
	CondInputStreamHandler cond_handler;

140
	while (true) {
141 142
		if (postponed_exception)
			std::rethrow_exception(postponed_exception);
143

144
		auto r = buffer.Read();
145
		if (!r.empty()) {
146
			size_t nbytes = std::min(read_size, r.size);
147
			memcpy(ptr, r.data, nbytes);
148
			buffer.Consume(nbytes);
149
			wake_cond.broadcast();
150
			offset += nbytes;
151 152 153 154 155 156
			return nbytes;
		}

		if (eof)
			return 0;

157 158
		const ScopeExchangeInputStreamHandler h(*this, &cond_handler);
		cond_handler.cond.wait(mutex);
159 160 161 162
	}
}

bool
163
ThreadInputStream::IsEOF() noexcept
164
{
165 166
	assert(!thread.IsInside());

167
	return eof;
168
}