player/Control: convert error from Error to std::exception_ptr

Prepare full C++ exception support in the player thread.
This commit is contained in:
Max Kellermann 2016-09-08 10:21:34 +02:00
parent 6e52ab285a
commit 0ce72cbf9d
3 changed files with 38 additions and 23 deletions

View File

@ -32,6 +32,7 @@
#include "AudioFormat.hxx" #include "AudioFormat.hxx"
#include "ReplayGainConfig.hxx" #include "ReplayGainConfig.hxx"
#include "util/ScopeExit.hxx" #include "util/ScopeExit.hxx"
#include "util/Error.hxx"
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
#include "db/update/Service.hxx" #include "db/update/Service.hxx"
@ -191,10 +192,15 @@ handle_status(Client &client, gcc_unused Request args, Response &r)
} }
#endif #endif
Error error = client.player_control.LockGetError(); try {
if (error.IsDefined()) client.player_control.LockCheckRethrowError();
r.Format(COMMAND_STATUS_ERROR ": %s\n", } catch (const std::exception &e) {
error.GetMessage()); r.Format(COMMAND_STATUS_ERROR ": %s\n", e.what());
} catch (const Error &error) {
r.Format(COMMAND_STATUS_ERROR ": %s\n", error.GetMessage());
} catch (...) {
r.Format(COMMAND_STATUS_ERROR ": unknown\n");
}
song = playlist.GetNextPosition(); song = playlist.GetNextPosition();
if (song >= 0) if (song >= 0)

View File

@ -21,6 +21,7 @@
#include "Control.hxx" #include "Control.hxx"
#include "Idle.hxx" #include "Idle.hxx"
#include "DetachedSong.hxx" #include "DetachedSong.hxx"
#include "util/Error.hxx"
#include <algorithm> #include <algorithm>
@ -160,14 +161,23 @@ PlayerControl::LockGetStatus()
return status; return status;
} }
void
PlayerControl::SetError(PlayerError type, std::exception_ptr &&_error)
{
assert(type != PlayerError::NONE);
assert(_error);
error_type = type;
error = std::move(_error);
}
void void
PlayerControl::SetError(PlayerError type, Error &&_error) PlayerControl::SetError(PlayerError type, Error &&_error)
{ {
assert(type != PlayerError::NONE); assert(type != PlayerError::NONE);
assert(_error.IsDefined()); assert(_error.IsDefined());
error_type = type; SetError(type, std::make_exception_ptr(std::move(_error)));
error = std::move(_error);
} }
void void
@ -223,11 +233,11 @@ PlayerControl::SeekLocked(DetachedSong *song, SongTime t)
assert(next_song == nullptr); assert(next_song == nullptr);
if (error_type != PlayerError::NONE) { if (error_type != PlayerError::NONE) {
assert(error.IsDefined()); assert(error);
throw error; std::rethrow_exception(error);
} }
assert(!error.IsDefined()); assert(!error);
} }
void void

View File

@ -24,12 +24,14 @@
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include "thread/Cond.hxx" #include "thread/Cond.hxx"
#include "thread/Thread.hxx" #include "thread/Thread.hxx"
#include "util/Error.hxx"
#include "CrossFade.hxx" #include "CrossFade.hxx"
#include "Chrono.hxx" #include "Chrono.hxx"
#include <exception>
#include <stdint.h> #include <stdint.h>
class Error;
class PlayerListener; class PlayerListener;
class MultipleOutputs; class MultipleOutputs;
class DetachedSong; class DetachedSong;
@ -135,7 +137,7 @@ struct PlayerControl {
* #PlayerError::NONE. The object must be freed when this * #PlayerError::NONE. The object must be freed when this
* object transitions back to #PlayerError::NONE. * object transitions back to #PlayerError::NONE.
*/ */
Error error; std::exception_ptr error;
/** /**
* A copy of the current #DetachedSong after its tags have * A copy of the current #DetachedSong after its tags have
@ -327,7 +329,7 @@ private:
void ClearError() { void ClearError() {
error_type = PlayerError::NONE; error_type = PlayerError::NONE;
error.Clear(); error = std::exception_ptr();
} }
public: public:
@ -356,28 +358,25 @@ public:
* @param error detailed error information; must be defined. * @param error detailed error information; must be defined.
*/ */
void SetError(PlayerError type, Error &&error); void SetError(PlayerError type, Error &&error);
void SetError(PlayerError type, std::exception_ptr &&_error);
/** /**
* Checks whether an error has occurred, and if so, returns a * Checks whether an error has occurred, and if so, rethrows
* copy of the #Error object. * it.
* *
* Caller must lock the object. * Caller must lock the object.
*/ */
gcc_pure void CheckRethrowError() const {
Error GetError() const {
Error result;
if (error_type != PlayerError::NONE) if (error_type != PlayerError::NONE)
result.Set(error); std::rethrow_exception(error);
return result;
} }
/** /**
* Like GetError(), but locks and unlocks the object. * Like CheckRethrowError(), but locks and unlocks the object.
*/ */
gcc_pure void LockCheckRethrowError() const {
Error LockGetError() const {
const ScopeLock protect(mutex); const ScopeLock protect(mutex);
return GetError(); CheckRethrowError();
} }
void LockClearError(); void LockClearError();