Converting Glib's mutex (obsolete) to std::mutex
(no issue)
This commit is contained in:
parent
bb97a30058
commit
55cc71608b
@ -83,59 +83,6 @@ void IdleRegister::destroy()
|
|||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
gboolean giveMeAGo(void* data) {
|
|
||||||
GThreadLock *threadMutex = static_cast<GThreadLock*>(data);
|
|
||||||
printf("A\n");
|
|
||||||
Glib::Threads::Mutex::Lock GUILock(threadMutex->GUI);
|
|
||||||
printf("B\n");
|
|
||||||
{
|
|
||||||
Glib::Threads::Mutex::Lock operationLock(threadMutex->operation);
|
|
||||||
printf("C\n");
|
|
||||||
|
|
||||||
threadMutex->operationCond.signal();
|
|
||||||
printf("D\n");
|
|
||||||
operationLock.release(); // because we're not sure that "lock" destructor happens here...
|
|
||||||
}
|
|
||||||
threadMutex->GUICond.wait(threadMutex->GUI);
|
|
||||||
printf("E\n");
|
|
||||||
|
|
||||||
GUILock.release();
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
GThreadLock::GThreadLock() : sameThread(false) {
|
|
||||||
if (Glib::Threads::Thread::self() == mainThread) {
|
|
||||||
sameThread = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("10\n");
|
|
||||||
{
|
|
||||||
Glib::Threads::Mutex::Lock operationLock(operation);
|
|
||||||
|
|
||||||
printf("20\n");
|
|
||||||
gdk_threads_add_idle(giveMeAGo, this);
|
|
||||||
|
|
||||||
printf("30\n");
|
|
||||||
operationCond.wait(operation);
|
|
||||||
printf("40\n");
|
|
||||||
operationLock.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GThreadLock::~GThreadLock() {
|
|
||||||
if (!sameThread) {
|
|
||||||
printf("50\n");
|
|
||||||
Glib::Threads::Mutex::Lock lock(GUI);
|
|
||||||
printf("60\n");
|
|
||||||
GUICond.signal();
|
|
||||||
printf("Fin\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Glib::ustring escapeHtmlChars(const Glib::ustring &src)
|
Glib::ustring escapeHtmlChars(const Glib::ustring &src)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
|
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include "threadutils.h"
|
#include "threadutils.h"
|
||||||
|
#include <glibmm/threads.h>
|
||||||
|
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -67,7 +68,7 @@ void MyReaderLock::acquire ()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Glib::Threads::Mutex::Lock lock (mutex.mutex);
|
std::lock_guard<std::mutex> lock (mutex.mutex);
|
||||||
|
|
||||||
if (mutex.writerCount == 0) {
|
if (mutex.writerCount == 0) {
|
||||||
// There's no writer operating, we can increment the writer count which will lock writers.
|
// There's no writer operating, we can increment the writer count which will lock writers.
|
||||||
@ -95,7 +96,7 @@ void MyReaderLock::release ()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Glib::Threads::Mutex::Lock lock (mutex.mutex);
|
std::lock_guard<std::mutex> lock (mutex.mutex);
|
||||||
|
|
||||||
// decrement the writer number first...
|
// decrement the writer number first...
|
||||||
--mutex.readerCount;
|
--mutex.readerCount;
|
||||||
@ -105,7 +106,7 @@ void MyReaderLock::release ()
|
|||||||
--mutex.writerCount;
|
--mutex.writerCount;
|
||||||
|
|
||||||
// ...and signal the next waiting reader/writer that it's free
|
// ...and signal the next waiting reader/writer that it's free
|
||||||
mutex.cond.broadcast ();
|
mutex.cond.notify_one(); // notify_all ?
|
||||||
}
|
}
|
||||||
|
|
||||||
locked = false;
|
locked = false;
|
||||||
@ -117,7 +118,7 @@ void MyWriterLock::acquire ()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Glib::Threads::Mutex::Lock lock (mutex.mutex);
|
std::lock_guard<std::mutex> lock (mutex.mutex);
|
||||||
|
|
||||||
// The writer count is not zero, so we have to wait for it to be zero again...
|
// The writer count is not zero, so we have to wait for it to be zero again...
|
||||||
while (mutex.writerCount != 0) {
|
while (mutex.writerCount != 0) {
|
||||||
@ -136,12 +137,12 @@ void MyWriterLock::release ()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Glib::Threads::Mutex::Lock lock (mutex.mutex);
|
std::lock_guard<std::mutex> lock (mutex.mutex);
|
||||||
|
|
||||||
// Decrement the writer number first...
|
// Decrement the writer number first...
|
||||||
if (--mutex.writerCount == 0) {
|
if (--mutex.writerCount == 0) {
|
||||||
// ...and if the writer count is zero again, we can wake up the next writer or reader.
|
// ...and if the writer count is zero again, we can wake up the next writer or reader.
|
||||||
mutex.cond.broadcast ();
|
mutex.cond.notify_one(); // notify_all ?
|
||||||
}
|
}
|
||||||
|
|
||||||
locked = false;
|
locked = false;
|
||||||
@ -170,7 +171,7 @@ void MyReaderLock::acquire (const char* file, int line)
|
|||||||
|
|
||||||
trace (file, line) << "Acquiring MyReaderLock..." << std::endl;
|
trace (file, line) << "Acquiring MyReaderLock..." << std::endl;
|
||||||
|
|
||||||
Glib::Threads::Mutex::Lock lock (mutex.mutex);
|
std::lock_guard<std::mutex> lock (mutex.mutex);
|
||||||
|
|
||||||
if (mutex.writerCount == 0) {
|
if (mutex.writerCount == 0) {
|
||||||
// There's no writer operating, we can increment the writer count which will lock writers.
|
// There's no writer operating, we can increment the writer count which will lock writers.
|
||||||
@ -211,7 +212,7 @@ void MyReaderLock::release (const char* file, int line)
|
|||||||
|
|
||||||
trace (file, line) << "Releasing MyReaderLock..." << std::endl;
|
trace (file, line) << "Releasing MyReaderLock..." << std::endl;
|
||||||
|
|
||||||
Glib::Threads::Mutex::Lock lock (mutex.mutex);
|
std::lock_guard<std::mutex> lock (mutex.mutex);
|
||||||
|
|
||||||
// decrement the writer number first...
|
// decrement the writer number first...
|
||||||
--mutex.readerCount;
|
--mutex.readerCount;
|
||||||
@ -221,7 +222,7 @@ void MyReaderLock::release (const char* file, int line)
|
|||||||
--mutex.writerCount;
|
--mutex.writerCount;
|
||||||
|
|
||||||
// ...and signal the next waiting reader/writer that it's free
|
// ...and signal the next waiting reader/writer that it's free
|
||||||
mutex.cond.broadcast ();
|
mutex.cond.notify_one(); // notify_all ?
|
||||||
|
|
||||||
mutex.ownerThread = nullptr;
|
mutex.ownerThread = nullptr;
|
||||||
mutex.lastWriterFile = "";
|
mutex.lastWriterFile = "";
|
||||||
@ -241,7 +242,7 @@ void MyWriterLock::acquire (const char* file, int line)
|
|||||||
|
|
||||||
trace (file, line) << "Acquiring MyWriterLock..." << std::endl;
|
trace (file, line) << "Acquiring MyWriterLock..." << std::endl;
|
||||||
|
|
||||||
Glib::Threads::Mutex::Lock lock (mutex.mutex);
|
std::lock_guard<std::mutex> lock (mutex.mutex);
|
||||||
|
|
||||||
// The writer count is not zero, so we have to wait for it to be zero again...
|
// The writer count is not zero, so we have to wait for it to be zero again...
|
||||||
while (mutex.writerCount != 0) {
|
while (mutex.writerCount != 0) {
|
||||||
@ -273,12 +274,12 @@ void MyWriterLock::release (const char* file, int line)
|
|||||||
|
|
||||||
trace (file, line) << "Releasing MyWriterLock..." << std::endl;
|
trace (file, line) << "Releasing MyWriterLock..." << std::endl;
|
||||||
|
|
||||||
Glib::Threads::Mutex::Lock lock (mutex.mutex);
|
std::lock_guard<std::mutex> lock (mutex.mutex);
|
||||||
|
|
||||||
// Decrement the writer number first...
|
// Decrement the writer number first...
|
||||||
if (--mutex.writerCount == 0) {
|
if (--mutex.writerCount == 0) {
|
||||||
// ...and if the writer count is zero again, we can wake up the next writer or reader.
|
// ...and if the writer count is zero again, we can wake up the next writer or reader.
|
||||||
mutex.cond.broadcast ();
|
mutex.cond.notify_one(); // notify_all ?
|
||||||
|
|
||||||
mutex.ownerThread = nullptr;
|
mutex.ownerThread = nullptr;
|
||||||
mutex.lastWriterFile = "";
|
mutex.lastWriterFile = "";
|
||||||
|
@ -25,14 +25,14 @@
|
|||||||
//#undef STRICT_MUTEX
|
//#undef STRICT_MUTEX
|
||||||
//#define STRICT_MUTEX 1
|
//#define STRICT_MUTEX 1
|
||||||
|
|
||||||
#include <glibmm/threads.h>
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
#include "../rtengine/noncopyable.h"
|
#include "../rtengine/noncopyable.h"
|
||||||
|
|
||||||
#if STRICT_MUTEX && NDEBUG
|
#if STRICT_MUTEX && NDEBUG
|
||||||
using MyMutexBase = Glib::Threads::Mutex;
|
using MyMutexBase = std::mutex;
|
||||||
#else
|
#else
|
||||||
using MyMutexBase = Glib::Threads::RecMutex;
|
using MyMutexBase = std::recursive_mutex;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,8 +67,6 @@ class MyMutex::MyLock :
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit MyLock (MyMutex& mutex);
|
explicit MyLock (MyMutex& mutex);
|
||||||
MyLock (MyMutex& mutex, Glib::Threads::NotLock);
|
|
||||||
MyLock (MyMutex& mutex, Glib::Threads::TryLock);
|
|
||||||
|
|
||||||
~MyLock ();
|
~MyLock ();
|
||||||
|
|
||||||
@ -92,8 +90,8 @@ public:
|
|||||||
friend class MyWriterLock;
|
friend class MyWriterLock;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Glib::Threads::Mutex mutex;
|
std::mutex mutex;
|
||||||
Glib::Threads::Cond cond;
|
std::condition_variable_any cond;
|
||||||
|
|
||||||
std::size_t writerCount = 0;
|
std::size_t writerCount = 0;
|
||||||
std::size_t readerCount = 0;
|
std::size_t readerCount = 0;
|
||||||
@ -168,7 +166,7 @@ inline void MyMutex::lock ()
|
|||||||
|
|
||||||
inline bool MyMutex::trylock ()
|
inline bool MyMutex::trylock ()
|
||||||
{
|
{
|
||||||
if (MyMutexBase::trylock ()) {
|
if (MyMutexBase::try_lock ()) {
|
||||||
#if STRICT_MUTEX && !NDEBUG
|
#if STRICT_MUTEX && !NDEBUG
|
||||||
checkLock ();
|
checkLock ();
|
||||||
#endif
|
#endif
|
||||||
@ -195,18 +193,6 @@ inline MyMutex::MyLock::MyLock (MyMutex& mutex)
|
|||||||
mutex.lock();
|
mutex.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline MyMutex::MyLock::MyLock (MyMutex& mutex, Glib::Threads::NotLock)
|
|
||||||
: mutex (mutex)
|
|
||||||
, locked (false)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
inline MyMutex::MyLock::MyLock (MyMutex& mutex, Glib::Threads::TryLock)
|
|
||||||
: mutex (mutex)
|
|
||||||
, locked (mutex.trylock ())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
inline MyMutex::MyLock::~MyLock ()
|
inline MyMutex::MyLock::~MyLock ()
|
||||||
{
|
{
|
||||||
if (locked) {
|
if (locked) {
|
||||||
|
@ -83,9 +83,9 @@ public:
|
|||||||
|
|
||||||
Glib::ThreadPool* threadPool_;
|
Glib::ThreadPool* threadPool_;
|
||||||
|
|
||||||
// Need to be a Glib::Threads::Mutex because used in a Glib::Threads::Cond object...
|
// Need to be a std::mutex because used in a std::condition_variable_any object...
|
||||||
// This is the only exceptions along with GThreadMutex (guiutils.cc), MyMutex is used everywhere else
|
// This is the only exceptions along with GThreadMutex (guiutils.cc), MyMutex is used everywhere else
|
||||||
Glib::Threads::Mutex mutex_;
|
std::mutex mutex_;
|
||||||
|
|
||||||
JobList jobs_;
|
JobList jobs_;
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ public:
|
|||||||
|
|
||||||
bool inactive_waiting_;
|
bool inactive_waiting_;
|
||||||
|
|
||||||
Glib::Threads::Cond inactive_;
|
std::condition_variable_any inactive_;
|
||||||
|
|
||||||
void
|
void
|
||||||
processNextJob()
|
processNextJob()
|
||||||
@ -101,7 +101,7 @@ public:
|
|||||||
Job j;
|
Job j;
|
||||||
|
|
||||||
{
|
{
|
||||||
Glib::Threads::Mutex::Lock lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
|
||||||
// nothing to do; could be jobs have been removed
|
// nothing to do; could be jobs have been removed
|
||||||
if ( jobs_.empty() ) {
|
if ( jobs_.empty() ) {
|
||||||
@ -164,10 +164,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( --active_ == 0 ) {
|
if ( --active_ == 0 ) {
|
||||||
Glib::Threads::Mutex::Lock lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
if (inactive_waiting_) {
|
if (inactive_waiting_) {
|
||||||
inactive_waiting_ = false;
|
inactive_waiting_ = false;
|
||||||
inactive_.broadcast();
|
inactive_.notify_one(); // notify_all ?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -196,7 +196,7 @@ void ThumbImageUpdater::add(ThumbBrowserEntryBase* tbe, bool* priority, bool upg
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Glib::Threads::Mutex::Lock lock(impl_->mutex_);
|
std::lock_guard<std::mutex> lock(impl_->mutex_);
|
||||||
|
|
||||||
// look up if an older version is in the queue
|
// look up if an older version is in the queue
|
||||||
Impl::JobList::iterator i(impl_->jobs_.begin());
|
Impl::JobList::iterator i(impl_->jobs_.begin());
|
||||||
@ -228,7 +228,7 @@ void ThumbImageUpdater::removeJobs(ThumbImageUpdateListener* listener)
|
|||||||
DEBUG("removeJobs(%p)", listener);
|
DEBUG("removeJobs(%p)", listener);
|
||||||
|
|
||||||
{
|
{
|
||||||
Glib::Threads::Mutex::Lock lock(impl_->mutex_);
|
std::lock_guard<std::mutex> lock(impl_->mutex_);
|
||||||
|
|
||||||
for( Impl::JobList::iterator i(impl_->jobs_.begin()); i != impl_->jobs_.end(); ) {
|
for( Impl::JobList::iterator i(impl_->jobs_.begin()); i != impl_->jobs_.end(); ) {
|
||||||
if (i->listener_ == listener) {
|
if (i->listener_ == listener) {
|
||||||
@ -244,7 +244,7 @@ void ThumbImageUpdater::removeJobs(ThumbImageUpdateListener* listener)
|
|||||||
while ( impl_->active_ != 0 ) {
|
while ( impl_->active_ != 0 ) {
|
||||||
DEBUG("waiting for running jobs1");
|
DEBUG("waiting for running jobs1");
|
||||||
{
|
{
|
||||||
Glib::Threads::Mutex::Lock lock(impl_->mutex_);
|
std::lock_guard<std::mutex> lock(impl_->mutex_);
|
||||||
impl_->inactive_waiting_ = true;
|
impl_->inactive_waiting_ = true;
|
||||||
impl_->inactive_.wait(impl_->mutex_);
|
impl_->inactive_.wait(impl_->mutex_);
|
||||||
}
|
}
|
||||||
@ -256,7 +256,7 @@ void ThumbImageUpdater::removeAllJobs()
|
|||||||
DEBUG("stop");
|
DEBUG("stop");
|
||||||
|
|
||||||
{
|
{
|
||||||
Glib::Threads::Mutex::Lock lock(impl_->mutex_);
|
std::lock_guard<std::mutex> lock(impl_->mutex_);
|
||||||
|
|
||||||
impl_->jobs_.clear();
|
impl_->jobs_.clear();
|
||||||
}
|
}
|
||||||
@ -264,7 +264,7 @@ void ThumbImageUpdater::removeAllJobs()
|
|||||||
while ( impl_->active_ != 0 ) {
|
while ( impl_->active_ != 0 ) {
|
||||||
DEBUG("waiting for running jobs2");
|
DEBUG("waiting for running jobs2");
|
||||||
{
|
{
|
||||||
Glib::Threads::Mutex::Lock lock(impl_->mutex_);
|
std::lock_guard<std::mutex> lock(impl_->mutex_);
|
||||||
impl_->inactive_waiting_ = true;
|
impl_->inactive_waiting_ = true;
|
||||||
impl_->inactive_.wait(impl_->mutex_);
|
impl_->inactive_.wait(impl_->mutex_);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user