misc: replace rw_mutex with std::shared_mutex

Change-Id: I6919ab404e5d8f149950d9d3a624a85115b62944
This commit is contained in:
Vladimir Stoiakin
2022-05-22 01:59:17 +03:00
committed by Sébastien Blin
parent b0cfb1e8dd
commit 983ae44f9e
5 changed files with 9 additions and 137 deletions

View File

@ -51,7 +51,6 @@ list (APPEND Source_Files
"${CMAKE_CURRENT_SOURCE_DIR}/registration_states.h"
"${CMAKE_CURRENT_SOURCE_DIR}/ring_api.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/ring_types.h"
"${CMAKE_CURRENT_SOURCE_DIR}/rw_mutex.h"
"${CMAKE_CURRENT_SOURCE_DIR}/scheduled_executor.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/scheduled_executor.h"
"${CMAKE_CURRENT_SOURCE_DIR}/smartools.cpp"
@ -113,4 +112,4 @@ set (Source_Files__upnp ${Source_Files__upnp} PARENT_SCOPE)
set (Source_Files__upnp__protocol ${Source_Files__upnp__protocol} PARENT_SCOPE)
set (Source_Files__upnp__protocol__pupnp ${Source_Files__upnp__protocol__pupnp} PARENT_SCOPE)
set (Source_Files__upnp__protocol__natpmp ${Source_Files__upnp__protocol__natpmp} PARENT_SCOPE)
set (Source_Files__plugin ${Source_Files__plugin} PARENT_SCOPE)
set (Source_Files__plugin ${Source_Files__plugin} PARENT_SCOPE)

View File

@ -112,7 +112,6 @@ libring_la_SOURCES = \
map_utils.h \
string_utils.h \
string_utils.cpp \
rw_mutex.h \
ring_api.cpp \
rational.h \
smartools.cpp \

View File

@ -35,6 +35,7 @@
#include <cmath>
#include <unistd.h>
#include <mutex>
#include <opendht/thread_pool.h>
@ -209,7 +210,7 @@ VideoMixer::updateLayout()
void
VideoMixer::attached(Observable<std::shared_ptr<MediaFrame>>* ob)
{
auto lock(rwMutex_.write());
std::unique_lock lock(rwMutex_);
auto src = std::unique_ptr<VideoMixerSource>(new VideoMixerSource);
src->render_frame = std::make_shared<VideoFrame>();
@ -223,7 +224,7 @@ VideoMixer::attached(Observable<std::shared_ptr<MediaFrame>>* ob)
void
VideoMixer::detached(Observable<std::shared_ptr<MediaFrame>>* ob)
{
auto lock(rwMutex_.write());
std::unique_lock lock(rwMutex_);
for (const auto& x : sources_) {
if (x->source == ob) {
@ -244,7 +245,7 @@ void
VideoMixer::update(Observable<std::shared_ptr<MediaFrame>>* ob,
const std::shared_ptr<MediaFrame>& frame_p)
{
auto lock(rwMutex_.read());
std::shared_lock lock(rwMutex_);
for (const auto& x : sources_) {
if (x->source == ob) {
@ -292,7 +293,7 @@ VideoMixer::process()
{
std::lock_guard<std::mutex> lk(audioOnlySourcesMtx_);
auto lock(rwMutex_.read());
std::shared_lock lock(rwMutex_);
int i = 0;
bool activeFound = false;
@ -507,7 +508,7 @@ VideoMixer::calc_position(std::unique_ptr<VideoMixerSource>& source,
void
VideoMixer::setParameters(int width, int height, AVPixelFormat format)
{
auto lock(rwMutex_.write());
std::unique_lock lock(rwMutex_);
width_ = width;
height_ = height;

View File

@ -25,12 +25,12 @@
#include "video_base.h"
#include "video_scaler.h"
#include "threadloop.h"
#include "rw_mutex.h"
#include "media_stream.h"
#include <list>
#include <chrono>
#include <memory>
#include <shared_mutex>
namespace jami {
namespace video {
@ -143,7 +143,7 @@ private:
int width_ = 0;
int height_ = 0;
AVPixelFormat format_ = AV_PIX_FMT_YUV422P;
rw_mutex rwMutex_;
std::shared_mutex rwMutex_;
std::shared_ptr<SinkClient> sink_;

View File

@ -1,127 +0,0 @@
/*
* Copyright (C) 2004-2022 Savoir-faire Linux Inc.
*
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* 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 3 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.
*/
#ifndef RW_MUTEX_H_
#define RW_MUTEX_H_
#include "noncopyable.h"
#include <mutex>
#include <atomic>
#include <condition_variable>
#include <string>
#include <sstream>
namespace jami {
/**
* rw_mutex is a shared mutex meant to protect
* rarely-modified, often-read data structures.
*
* Its goal is to optimize read throughput and latency.
* Multiple threads can concurrently read data while
* a writer thread gets exclusive access when needed.
*/
class rw_mutex
{
public:
rw_mutex()
: mutex()
, canRead()
, canWrite()
, readers(0)
, writing(false)
{}
void read_enter()
{
std::unique_lock<std::mutex> lck(mutex);
canRead.wait(lck, [this]() { return !writing; });
readers++;
}
void read_exit()
{
// std::lock_guard<std::mutex> lck(mutex);
readers--;
canWrite.notify_one();
}
void write_enter()
{
std::unique_lock<std::mutex> lck(mutex);
canWrite.wait(lck, [this]() { return !writing && readers == 0; });
writing = true;
}
void write_exit()
{
std::lock_guard<std::mutex> lck(mutex);
writing = false;
canWrite.notify_one();
canRead.notify_all();
}
struct read_lock
{
public:
read_lock(rw_mutex& m)
: sem(m)
{
sem.read_enter();
}
~read_lock() { sem.read_exit(); }
private:
rw_mutex& sem;
};
struct write_lock
{
public:
write_lock(rw_mutex& m)
: sem(m)
{
sem.write_enter();
}
~write_lock() { sem.write_exit(); }
private:
rw_mutex& sem;
};
read_lock read() { return read_lock(*this); }
write_lock write() { return write_lock(*this); }
std::string toString()
{
std::stringstream ss;
ss << "[rw_mutex write:" << (writing ? "LOCKED" : "unlocked") << " read:" << readers << "]";
return ss.str();
}
private:
NON_COPYABLE(rw_mutex);
std::mutex mutex;
std::condition_variable canRead, canWrite;
std::atomic<unsigned> readers;
bool writing;
};
} // namespace jami
#endif