2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2019-04-03 14:31:57 +02:00
|
|
|
|
|
|
|
#include "ThreadBackgroundCommand.hxx"
|
|
|
|
#include "Client.hxx"
|
|
|
|
#include "Response.hxx"
|
|
|
|
#include "command/CommandError.hxx"
|
|
|
|
|
|
|
|
ThreadBackgroundCommand::ThreadBackgroundCommand(Client &_client) noexcept
|
|
|
|
:thread(BIND_THIS_METHOD(_Run)),
|
|
|
|
defer_finish(_client.GetEventLoop(), BIND_THIS_METHOD(DeferredFinish)),
|
|
|
|
client(_client)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadBackgroundCommand::_Run() noexcept
|
|
|
|
{
|
|
|
|
assert(!error);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Run();
|
|
|
|
} catch (...) {
|
|
|
|
error = std::current_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
defer_finish.Schedule();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadBackgroundCommand::DeferredFinish() noexcept
|
|
|
|
{
|
|
|
|
/* free the Thread */
|
|
|
|
thread.Join();
|
|
|
|
|
|
|
|
/* send the response */
|
|
|
|
Response response(client, 0);
|
|
|
|
|
2019-04-05 14:23:29 +02:00
|
|
|
if (error) {
|
2020-02-06 01:47:14 +01:00
|
|
|
PrintError(response, error);
|
2019-04-03 14:31:57 +02:00
|
|
|
} else {
|
|
|
|
SendResponse(response);
|
2021-10-22 11:51:12 +02:00
|
|
|
client.WriteOK();
|
2019-04-03 14:31:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* delete this object */
|
|
|
|
client.OnBackgroundCommandFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadBackgroundCommand::Cancel() noexcept
|
|
|
|
{
|
|
|
|
CancelThread();
|
|
|
|
thread.Join();
|
|
|
|
|
2020-12-01 16:25:11 +01:00
|
|
|
/* cancel the InjectEvent, just in case the Thread has
|
2019-04-03 14:31:57 +02:00
|
|
|
meanwhile finished execution */
|
|
|
|
defer_finish.Cancel();
|
|
|
|
}
|