Skip to content

Commit

Permalink
Clean up mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
badaix committed Sep 18, 2021
1 parent 12aeb58 commit 2efd00d
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 18 deletions.
7 changes: 3 additions & 4 deletions control/meta_mpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ def get_metadata(self):
logger.debug(
f'key: {key}, value: {value}, mapped key: {tag_mapping[key][0]}, mapped value: {snapmeta[tag_mapping[key][0]]}')
except KeyError:
logger.warning(f'tag "{key}" not supported')
logger.debug(f'tag "{key}" not supported')
except (ValueError, TypeError):
logger.warning(
f"Can't cast value {value} to {tag_mapping[key][1]}")
Expand All @@ -602,7 +602,6 @@ def get_metadata(self):
if art_url is not None:
logger.info(f'album art cache hit: "{art_url}"')
snapmeta['artUrl'] = art_url

return snapmeta

def __diff_map(self, old_map, new_map):
Expand Down Expand Up @@ -647,12 +646,12 @@ def _update_properties(self, force=False):

new_song = self.client.currentsong()
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_status = self.client.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 = {}

changed_status = self.__diff_map(self._status, new_status)
Expand Down
4 changes: 3 additions & 1 deletion server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}
// 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"));
result["volume"] = clientInfo->config.volume.toJson();
notification = std::make_shared<jsonrpcpp::Notification>(
Expand Down Expand Up @@ -825,6 +825,8 @@ void Server::onMessageReceived(StreamSession* streamSession, const msg::BaseMess

void Server::saveConfig(const std::chrono::milliseconds& deferred)
{
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
config_timer_.cancel();
config_timer_.expires_after(deferred);
config_timer_.async_wait([](const boost::system::error_code& ec) {
Expand Down
4 changes: 2 additions & 2 deletions server/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class Server : public StreamMessageReceiver, public ControlMessageReceiver, publ
/// @param deferred the delay after the last call to saveConfig
void saveConfig(const std::chrono::milliseconds& deferred = std::chrono::seconds(2));

mutable std::recursive_mutex sessionsMutex_;
mutable std::recursive_mutex clientMutex_;
// mutable std::recursive_mutex controlMutex_;
// mutable std::recursive_mutex clientMutex_;
boost::asio::io_context& io_context_;
boost::asio::steady_timer config_timer_;

Expand Down
2 changes: 1 addition & 1 deletion server/stream_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void StreamServer::onChunkEncoded(const PcmStream* pcmStream, bool isDefaultStre
{
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);
if (group)
{
Expand Down
4 changes: 2 additions & 2 deletions server/stream_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ StreamSession::StreamSession(const net::any_io_executor& executor, StreamMessage

void StreamSession::setPcmStream(PcmStreamPtr pcmStream)
{
std::lock_guard<std::mutex> lock(mutex_);
// std::lock_guard<std::mutex> lock(mutex_);
pcmStream_ = pcmStream;
}


const PcmStreamPtr StreamSession::pcmStream() const
{
std::lock_guard<std::mutex> lock(mutex_);
// std::lock_guard<std::mutex> lock(mutex_);
return pcmStream_;
}

Expand Down
8 changes: 4 additions & 4 deletions server/streamreader/meta_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void MetaStream::stop()
void MetaStream::onPropertiesChanged(const PcmStream* pcmStream, const Properties& properties)
{
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())
return;
setProperties(properties);
Expand All @@ -97,7 +97,7 @@ void MetaStream::onPropertiesChanged(const PcmStream* pcmStream, const Propertie
void MetaStream::onStateChanged(const PcmStream* pcmStream, ReaderState state)
{
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)
return;
Expand Down Expand Up @@ -137,7 +137,7 @@ void MetaStream::onStateChanged(const PcmStream* pcmStream, ReaderState state)
void MetaStream::onChunkRead(const PcmStream* pcmStream, const msg::PcmChunk& chunk)
{
// 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())
return;
// active_stream_->sampleFormat_
Expand Down Expand Up @@ -195,7 +195,7 @@ void MetaStream::onChunkEncoded(const PcmStream* pcmStream, std::shared_ptr<msg:
void MetaStream::onResync(const PcmStream* pcmStream, double ms)
{
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())
return;
resync(std::chrono::nanoseconds(static_cast<int64_t>(ms * 1000000)));
Expand Down
2 changes: 1 addition & 1 deletion server/streamreader/meta_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MetaStream : public PcmStream, public PcmListener
protected:
std::vector<std::shared_ptr<PcmStream>> streams_;
std::shared_ptr<PcmStream> active_stream_;
std::recursive_mutex mutex_;
// std::recursive_mutex mutex_;
std::unique_ptr<Resampler> resampler_;
bool first_read_;
std::chrono::time_point<std::chrono::steady_clock> next_tick_;
Expand Down
6 changes: 3 additions & 3 deletions server/streamreader/pcm_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ void PcmStream::resync(const std::chrono::nanoseconds& duration)

json PcmStream::toJson() const
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
json j = {
{"uri", uri_.toJson()},
{"id", getId()},
Expand Down Expand Up @@ -316,7 +317,7 @@ const Metatags& PcmStream::getMetadata() const

const Properties& PcmStream::getProperties() const
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
// std::lock_guard<std::recursive_mutex> lock(mutex_);
return properties_;
}

Expand Down Expand Up @@ -447,8 +448,7 @@ void PcmStream::play(ResultHandler handler)

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;
// Missing metadata means the data didn't change, so
// enrich the new properites with old metadata
Expand Down

0 comments on commit 2efd00d

Please sign in to comment.