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

24 25
#include <cassert>

26 27
#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
		close = true;
50
		wake_cond.notify_one();
51
	}
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
	std::unique_lock<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(lock);
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() const noexcept
127
{
128 129
	assert(!thread.IsInside());

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

inline size_t
134 135
ThreadInputStream::Read(std::unique_lock<Mutex> &lock,
			void *ptr, size_t read_size)
136
{
137 138
	assert(!thread.IsInside());

139 140
	CondInputStreamHandler cond_handler;

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

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

		if (eof)
			return 0;

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

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

168
	return eof;
169
}