Clean up mutexes

This commit is contained in:
badaix 2021-09-18 13:42:43 +02:00
parent 12aeb5859c
commit 2efd00d8ef
8 changed files with 19 additions and 18 deletions

View file

@ -576,7 +576,7 @@ class MPDWrapper(object):
logger.debug( logger.debug(
f'key: {key}, value: {value}, mapped key: {tag_mapping[key][0]}, mapped value: {snapmeta[tag_mapping[key][0]]}') f'key: {key}, value: {value}, mapped key: {tag_mapping[key][0]}, mapped value: {snapmeta[tag_mapping[key][0]]}')
except KeyError: except KeyError:
logger.warning(f'tag "{key}" not supported') logger.debug(f'tag "{key}" not supported')
except (ValueError, TypeError): except (ValueError, TypeError):
logger.warning( logger.warning(
f"Can't cast value {value} to {tag_mapping[key][1]}") f"Can't cast value {value} to {tag_mapping[key][1]}")
@ -602,7 +602,6 @@ class MPDWrapper(object):
if art_url is not None: if art_url is not None:
logger.info(f'album art cache hit: "{art_url}"') logger.info(f'album art cache hit: "{art_url}"')
snapmeta['artUrl'] = art_url snapmeta['artUrl'] = art_url
return snapmeta return snapmeta
def __diff_map(self, old_map, new_map): def __diff_map(self, old_map, new_map):
@ -647,12 +646,12 @@ class MPDWrapper(object):
new_song = self.client.currentsong() new_song = self.client.currentsong()
if not new_song: if not new_song:
logger.debug("_update_properties: failed to get current song") logger.warning("_update_properties: failed to get current song")
new_song = {} new_song = {}
new_status = self.client.status() new_status = self.client.status()
if not new_status: if not new_status:
logger.debug("_update_properties: failed to get new status") logger.warning("_update_properties: failed to get new status")
new_status = {} new_status = {}
changed_status = self.__diff_map(self._status, new_status) changed_status = self.__diff_map(self._status, new_status)

View file

@ -153,7 +153,7 @@ void Server::processRequest(const jsonrpcpp::request_ptr request, const OnRespon
// Notification: {"jsonrpc":"2.0","method":"Client.OnVolumeChanged","params":{"id":"00:21:6a:7d:74:fc","volume":{"muted":false,"percent":74}}} // Notification: {"jsonrpc":"2.0","method":"Client.OnVolumeChanged","params":{"id":"00:21:6a:7d:74:fc","volume":{"muted":false,"percent":74}}}
// clang-format on // clang-format on
std::lock_guard<std::recursive_mutex> lock(clientMutex_); // std::lock_guard<std::recursive_mutex> lock(clientMutex_);
clientInfo->config.volume.fromJson(request->params().get("volume")); clientInfo->config.volume.fromJson(request->params().get("volume"));
result["volume"] = clientInfo->config.volume.toJson(); result["volume"] = clientInfo->config.volume.toJson();
notification = std::make_shared<jsonrpcpp::Notification>( notification = std::make_shared<jsonrpcpp::Notification>(
@ -825,6 +825,8 @@ void Server::onMessageReceived(StreamSession* streamSession, const msg::BaseMess
void Server::saveConfig(const std::chrono::milliseconds& deferred) void Server::saveConfig(const std::chrono::milliseconds& deferred)
{ {
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
config_timer_.cancel(); config_timer_.cancel();
config_timer_.expires_after(deferred); config_timer_.expires_after(deferred);
config_timer_.async_wait([](const boost::system::error_code& ec) { config_timer_.async_wait([](const boost::system::error_code& ec) {

View file

@ -90,8 +90,8 @@ private:
/// @param deferred the delay after the last call to saveConfig /// @param deferred the delay after the last call to saveConfig
void saveConfig(const std::chrono::milliseconds& deferred = std::chrono::seconds(2)); void saveConfig(const std::chrono::milliseconds& deferred = std::chrono::seconds(2));
mutable std::recursive_mutex sessionsMutex_; // mutable std::recursive_mutex controlMutex_;
mutable std::recursive_mutex clientMutex_; // mutable std::recursive_mutex clientMutex_;
boost::asio::io_context& io_context_; boost::asio::io_context& io_context_;
boost::asio::steady_timer config_timer_; boost::asio::steady_timer config_timer_;

View file

@ -84,7 +84,7 @@ void StreamServer::onChunkEncoded(const PcmStream* pcmStream, bool isDefaultStre
{ {
if (!settings_.stream.sendAudioToMutedClients) if (!settings_.stream.sendAudioToMutedClients)
{ {
std::lock_guard<std::recursive_mutex> lock(clientMutex_); // std::lock_guard<std::recursive_mutex> lock(clientMutex_);
GroupPtr group = Config::instance().getGroupFromClient(session->clientId); GroupPtr group = Config::instance().getGroupFromClient(session->clientId);
if (group) if (group)
{ {

View file

@ -38,14 +38,14 @@ StreamSession::StreamSession(const net::any_io_executor& executor, StreamMessage
void StreamSession::setPcmStream(PcmStreamPtr pcmStream) void StreamSession::setPcmStream(PcmStreamPtr pcmStream)
{ {
std::lock_guard<std::mutex> lock(mutex_); // std::lock_guard<std::mutex> lock(mutex_);
pcmStream_ = pcmStream; pcmStream_ = pcmStream;
} }
const PcmStreamPtr StreamSession::pcmStream() const const PcmStreamPtr StreamSession::pcmStream() const
{ {
std::lock_guard<std::mutex> lock(mutex_); // std::lock_guard<std::mutex> lock(mutex_);
return pcmStream_; return pcmStream_;
} }

View file

@ -87,7 +87,7 @@ void MetaStream::stop()
void MetaStream::onPropertiesChanged(const PcmStream* pcmStream, const Properties& properties) void MetaStream::onPropertiesChanged(const PcmStream* pcmStream, const Properties& properties)
{ {
LOG(DEBUG, LOG_TAG) << "onPropertiesChanged: " << pcmStream->getName() << "\n"; LOG(DEBUG, LOG_TAG) << "onPropertiesChanged: " << pcmStream->getName() << "\n";
std::lock_guard<std::recursive_mutex> lock(mutex_); // std::lock_guard<std::recursive_mutex> lock(mutex_);
if (pcmStream != active_stream_.get()) if (pcmStream != active_stream_.get())
return; return;
setProperties(properties); setProperties(properties);
@ -97,7 +97,7 @@ void MetaStream::onPropertiesChanged(const PcmStream* pcmStream, const Propertie
void MetaStream::onStateChanged(const PcmStream* pcmStream, ReaderState state) void MetaStream::onStateChanged(const PcmStream* pcmStream, ReaderState state)
{ {
LOG(DEBUG, LOG_TAG) << "onStateChanged: " << pcmStream->getName() << ", state: " << state << "\n"; LOG(DEBUG, LOG_TAG) << "onStateChanged: " << pcmStream->getName() << ", state: " << state << "\n";
std::lock_guard<std::recursive_mutex> lock(mutex_); // std::lock_guard<std::recursive_mutex> lock(mutex_);
if (active_stream_->getProperties().playback_status == PlaybackStatus::kPaused) if (active_stream_->getProperties().playback_status == PlaybackStatus::kPaused)
return; return;
@ -137,7 +137,7 @@ void MetaStream::onStateChanged(const PcmStream* pcmStream, ReaderState state)
void MetaStream::onChunkRead(const PcmStream* pcmStream, const msg::PcmChunk& chunk) void MetaStream::onChunkRead(const PcmStream* pcmStream, const msg::PcmChunk& chunk)
{ {
// LOG(TRACE, LOG_TAG) << "onChunkRead: " << pcmStream->getName() << ", duration: " << chunk.durationMs() << "\n"; // LOG(TRACE, LOG_TAG) << "onChunkRead: " << pcmStream->getName() << ", duration: " << chunk.durationMs() << "\n";
std::lock_guard<std::recursive_mutex> lock(mutex_); // std::lock_guard<std::recursive_mutex> lock(mutex_);
if (pcmStream != active_stream_.get()) if (pcmStream != active_stream_.get())
return; return;
// active_stream_->sampleFormat_ // active_stream_->sampleFormat_
@ -195,7 +195,7 @@ void MetaStream::onChunkEncoded(const PcmStream* pcmStream, std::shared_ptr<msg:
void MetaStream::onResync(const PcmStream* pcmStream, double ms) void MetaStream::onResync(const PcmStream* pcmStream, double ms)
{ {
LOG(DEBUG, LOG_TAG) << "onResync: " << pcmStream->getName() << ", duration: " << ms << " ms\n"; LOG(DEBUG, LOG_TAG) << "onResync: " << pcmStream->getName() << ", duration: " << ms << " ms\n";
std::lock_guard<std::recursive_mutex> lock(mutex_); // std::lock_guard<std::recursive_mutex> lock(mutex_);
if (pcmStream != active_stream_.get()) if (pcmStream != active_stream_.get())
return; return;
resync(std::chrono::nanoseconds(static_cast<int64_t>(ms * 1000000))); resync(std::chrono::nanoseconds(static_cast<int64_t>(ms * 1000000)));

View file

@ -71,7 +71,7 @@ protected:
protected: protected:
std::vector<std::shared_ptr<PcmStream>> streams_; std::vector<std::shared_ptr<PcmStream>> streams_;
std::shared_ptr<PcmStream> active_stream_; std::shared_ptr<PcmStream> active_stream_;
std::recursive_mutex mutex_; // std::recursive_mutex mutex_;
std::unique_ptr<Resampler> resampler_; std::unique_ptr<Resampler> resampler_;
bool first_read_; bool first_read_;
std::chrono::time_point<std::chrono::steady_clock> next_tick_; std::chrono::time_point<std::chrono::steady_clock> next_tick_;

View file

@ -288,6 +288,7 @@ void PcmStream::resync(const std::chrono::nanoseconds& duration)
json PcmStream::toJson() const json PcmStream::toJson() const
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_);
json j = { json j = {
{"uri", uri_.toJson()}, {"uri", uri_.toJson()},
{"id", getId()}, {"id", getId()},
@ -316,7 +317,7 @@ const Metatags& PcmStream::getMetadata() const
const Properties& PcmStream::getProperties() const const Properties& PcmStream::getProperties() const
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); // std::lock_guard<std::recursive_mutex> lock(mutex_);
return properties_; return properties_;
} }
@ -447,8 +448,7 @@ void PcmStream::play(ResultHandler handler)
void PcmStream::setProperties(const Properties& properties) void PcmStream::setProperties(const Properties& properties)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); // std::lock_guard<std::recursive_mutex> lock(mutex_);
Properties props = properties; Properties props = properties;
// Missing metadata means the data didn't change, so // Missing metadata means the data didn't change, so
// enrich the new properites with old metadata // enrich the new properites with old metadata