From 81e32009b1ccfdcb8e9750d02c31c771c36cd731 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Thu, 23 Feb 2023 14:00:47 +0100 Subject: [PATCH] Use C++17's std::scoped_lock where appropriate. Stick to C++14's std::lock_guard in public headers. --- ...u-synchro-condition-variable-waituntil.cpp | 11 +++--- .../s4u-synchro-condition-variable.cpp | 6 +-- .../cpp/synchro-mutex/s4u-synchro-mutex.cpp | 12 +++--- .../cpp/synchro-mutex/s4u-synchro-mutex.tesh | 12 +++--- include/simgrid/plugins/ProducerConsumer.hpp | 2 +- src/smpi/mpi/smpi_file.cpp | 27 ++++++------- src/smpi/mpi/smpi_request.cpp | 24 ++++-------- src/smpi/mpi/smpi_win.cpp | 39 ++++++++----------- src/xbt/OsSemaphore.hpp | 2 +- src/xbt/log.cpp | 3 +- src/xbt/parmap.hpp | 12 +++--- .../mc/mutex-handling/mutex-handling.cpp | 9 ++--- 12 files changed, 69 insertions(+), 90 deletions(-) diff --git a/examples/cpp/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.cpp b/examples/cpp/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.cpp index 0edd2ab68d..67d1ac27b1 100644 --- a/examples/cpp/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.cpp +++ b/examples/cpp/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.cpp @@ -3,7 +3,7 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include /* std::mutex and std::lock_guard */ +#include /* std::mutex and std::scoped_lock */ #include /* All of S4U */ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category"); @@ -12,13 +12,12 @@ namespace sg4 = simgrid::s4u; static void competitor(int id, sg4::ConditionVariablePtr cv, sg4::MutexPtr mtx, std::shared_ptr ready) { XBT_INFO("Entering the race..."); - std::unique_lock lck(*mtx); + std::unique_lock lock(*mtx); while (not *ready) { auto now = sg4::Engine::get_clock(); - if (cv->wait_until(lck, now + (id+1)*0.25) == std::cv_status::timeout) { + if (cv->wait_until(lock, now + (id + 1) * 0.25) == std::cv_status::timeout) { XBT_INFO("Out of wait_until (timeout)"); - } - else { + } else { XBT_INFO("Out of wait_until (YAY!)"); } } @@ -29,7 +28,7 @@ static void go(sg4::ConditionVariablePtr cv, sg4::MutexPtr mtx, std::shared_ptr< { XBT_INFO("Are you ready? ..."); sg4::this_actor::sleep_for(3); - std::unique_lock lck(*mtx); + const std::scoped_lock lock(*mtx); XBT_INFO("Go go go!"); *ready = true; cv->notify_all(); diff --git a/examples/cpp/synchro-condition-variable/s4u-synchro-condition-variable.cpp b/examples/cpp/synchro-condition-variable/s4u-synchro-condition-variable.cpp index cf1a1db372..b471b658d8 100644 --- a/examples/cpp/synchro-condition-variable/s4u-synchro-condition-variable.cpp +++ b/examples/cpp/synchro-condition-variable/s4u-synchro-condition-variable.cpp @@ -3,7 +3,7 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include /* std::mutex and std::lock_guard */ +#include /* std::mutex and std::scoped_lock */ #include /* All of S4U */ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category"); @@ -11,7 +11,7 @@ namespace sg4 = simgrid::s4u; static void worker_fun(sg4::ConditionVariablePtr cv, sg4::MutexPtr mutex, std::string& data, bool& done) { - std::unique_lock lock(*mutex); + const std::scoped_lock lock(*mutex); XBT_INFO("Start processing data which is '%s'.", data.c_str()); data += " after processing"; @@ -34,7 +34,7 @@ static void master_fun() std::ref(done)); // wait for the worker - cv->wait(std::unique_lock(*mutex), [&done]() { return done; }); + cv->wait(std::unique_lock(*mutex), [&done]() { return done; }); XBT_INFO("data is now '%s'.", data.c_str()); worker->join(); diff --git a/examples/cpp/synchro-mutex/s4u-synchro-mutex.cpp b/examples/cpp/synchro-mutex/s4u-synchro-mutex.cpp index 429fccd068..bab673122f 100644 --- a/examples/cpp/synchro-mutex/s4u-synchro-mutex.cpp +++ b/examples/cpp/synchro-mutex/s4u-synchro-mutex.cpp @@ -5,7 +5,7 @@ #include "simgrid/s4u.hpp" /* All of S4U */ #include "xbt/config.hpp" -#include /* std::mutex and std::lock_guard */ +#include /* std::mutex and std::scoped_lock */ namespace sg4 = simgrid::s4u; @@ -29,14 +29,14 @@ static void worker(sg4::MutexPtr mutex, int& result) mutex->unlock(); } -static void workerLockGuard(sg4::MutexPtr mutex, int& result) +static void workerScopedLock(sg4::MutexPtr mutex, int& result) { - // Simply use the std::lock_guard like this + // Simply use the std::scoped_lock like this // It's like a lock() that would do the unlock() automatically when getting out of scope - std::lock_guard lock(*mutex); + const std::scoped_lock lock(*mutex); // then you are in a safe zone - XBT_INFO("Hello s4u, I'm ready to compute after a lock_guard"); + XBT_INFO("Hello s4u, I'm ready to compute after a scoped_lock"); // update the results result += 1; XBT_INFO("I'm done, good bye"); @@ -54,7 +54,7 @@ int main(int argc, char** argv) for (int i = 0; i < cfg_actor_count; i++) { sg4::MutexPtr mutex = sg4::Mutex::create(); - sg4::Actor::create("worker", sg4::Host::by_name("Jupiter"), workerLockGuard, mutex, std::ref(result[i])); + sg4::Actor::create("worker", sg4::Host::by_name("Jupiter"), workerScopedLock, mutex, std::ref(result[i])); sg4::Actor::create("worker", sg4::Host::by_name("Tremblay"), worker, mutex, std::ref(result[i])); } diff --git a/examples/cpp/synchro-mutex/s4u-synchro-mutex.tesh b/examples/cpp/synchro-mutex/s4u-synchro-mutex.tesh index fbec803310..ca6b4894e8 100644 --- a/examples/cpp/synchro-mutex/s4u-synchro-mutex.tesh +++ b/examples/cpp/synchro-mutex/s4u-synchro-mutex.tesh @@ -1,17 +1,17 @@ #!/usr/bin/env tesh $ ${bindir:=.}/s4u-synchro-mutex -> [Jupiter:worker:(1) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a lock_guard +> [Jupiter:worker:(1) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a scoped_lock > [Jupiter:worker:(1) 0.000000] [s4u_test/INFO] I'm done, good bye -> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a lock_guard +> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a scoped_lock > [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] I'm done, good bye -> [Jupiter:worker:(5) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a lock_guard +> [Jupiter:worker:(5) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a scoped_lock > [Jupiter:worker:(5) 0.000000] [s4u_test/INFO] I'm done, good bye -> [Jupiter:worker:(7) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a lock_guard +> [Jupiter:worker:(7) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a scoped_lock > [Jupiter:worker:(7) 0.000000] [s4u_test/INFO] I'm done, good bye -> [Jupiter:worker:(9) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a lock_guard +> [Jupiter:worker:(9) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a scoped_lock > [Jupiter:worker:(9) 0.000000] [s4u_test/INFO] I'm done, good bye -> [Jupiter:worker:(11) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a lock_guard +> [Jupiter:worker:(11) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a scoped_lock > [Jupiter:worker:(11) 0.000000] [s4u_test/INFO] I'm done, good bye > [Tremblay:worker:(2) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to compute after a regular lock > [Tremblay:worker:(2) 0.000000] [s4u_test/INFO] I'm done, good bye diff --git a/include/simgrid/plugins/ProducerConsumer.hpp b/include/simgrid/plugins/ProducerConsumer.hpp index 1ecaa1e6b1..55dc6d6644 100644 --- a/include/simgrid/plugins/ProducerConsumer.hpp +++ b/include/simgrid/plugins/ProducerConsumer.hpp @@ -104,7 +104,7 @@ public: */ ProducerConsumer* set_max_queue_size(unsigned int max_queue_size) { - std::unique_lock lock(*mutex_); + const std::lock_guard lock(*mutex_); max_queue_size_ = max_queue_size; return this; } diff --git a/src/smpi/mpi/smpi_file.cpp b/src/smpi/mpi/smpi_file.cpp index 57b61704e3..0a78eedeab 100644 --- a/src/smpi/mpi/smpi_file.cpp +++ b/src/smpi/mpi/smpi_file.cpp @@ -17,6 +17,8 @@ #include "simgrid/s4u/Host.hpp" #include "simgrid/plugins/file_system.h" +#include // std::scoped_lock + #define FP_SIZE sizeof(MPI_Offset) XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)"); @@ -117,9 +119,8 @@ int File::get_position(MPI_Offset* offset) const int File::get_position_shared(MPI_Offset* offset) const { - shared_mutex_->lock(); + const std::scoped_lock lock(*shared_mutex_); *offset = *shared_file_pointer_/etype_->get_extent(); - shared_mutex_->unlock(); return MPI_SUCCESS; } @@ -146,10 +147,9 @@ int File::seek(MPI_Offset offset, int whence) int File::seek_shared(MPI_Offset offset, int whence) { - shared_mutex_->lock(); + const std::scoped_lock lock(*shared_mutex_); seek(offset, whence); *shared_file_pointer_ = file_->tell(); - shared_mutex_->unlock(); return MPI_SUCCESS; } @@ -184,11 +184,11 @@ int File::read(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, /* }*/ int File::read_shared(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status) { - fh->shared_mutex_->lock(); - fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET); - read(fh, buf, count, datatype, status); - *(fh->shared_file_pointer_) = fh->file_->tell(); - fh->shared_mutex_->unlock(); + if (const std::scoped_lock lock(*fh->shared_mutex_); true) { + fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET); + read(fh, buf, count, datatype, status); + *(fh->shared_file_pointer_) = fh->file_->tell(); + } fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET); return MPI_SUCCESS; } @@ -210,9 +210,8 @@ int File::read_ordered(MPI_File fh, void* buf, int count, const Datatype* dataty fh->seek(result, MPI_SEEK_SET); int ret = fh->op_all(buf, count, datatype, status); if (fh->comm_->rank() == fh->comm_->size() - 1) { - fh->shared_mutex_->lock(); + const std::scoped_lock lock(*fh->shared_mutex_); *(fh->shared_file_pointer_)=fh->file_->tell(); - fh->shared_mutex_->unlock(); } char c; simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_); @@ -241,14 +240,13 @@ int File::write(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, int File::write_shared(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status) { - fh->shared_mutex_->lock(); + const std::scoped_lock lock(*fh->shared_mutex_); XBT_DEBUG("Write shared on %s - Shared ptr before : %lld", fh->file_->get_path(), *(fh->shared_file_pointer_)); fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET); write(fh, const_cast(buf), count, datatype, status); *(fh->shared_file_pointer_) = fh->file_->tell(); XBT_DEBUG("Write shared on %s - Shared ptr after : %lld", fh->file_->get_path(), *(fh->shared_file_pointer_)); fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET); - fh->shared_mutex_->unlock(); return MPI_SUCCESS; } @@ -268,9 +266,8 @@ int File::write_ordered(MPI_File fh, const void* buf, int count, const Datatype* fh->seek(result, MPI_SEEK_SET); int ret = fh->op_all(const_cast(buf), count, datatype, status); if (fh->comm_->rank() == fh->comm_->size() - 1) { - fh->shared_mutex_->lock(); + const std::scoped_lock lock(*fh->shared_mutex_); *(fh->shared_file_pointer_)=fh->file_->tell(); - fh->shared_mutex_->unlock(); } char c; simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_); diff --git a/src/smpi/mpi/smpi_request.cpp b/src/smpi/mpi/smpi_request.cpp index 1a24c54631..f2a700da71 100644 --- a/src/smpi/mpi/smpi_request.cpp +++ b/src/smpi/mpi/smpi_request.cpp @@ -24,6 +24,7 @@ #include #include +#include // std::scoped_lock and std::unique_lock XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (request)"); @@ -463,9 +464,9 @@ void Request::start() simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_)); - simgrid::s4u::MutexPtr mut = process->mailboxes_mutex(); + std::unique_lock mut_lock; if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0) - mut->lock(); + mut_lock = std::unique_lock(*process->mailboxes_mutex()); bool is_probe = ((flags_ & MPI_REQ_PROBE) != 0); flags_ |= MPI_REQ_PROBE; @@ -520,9 +521,6 @@ void Request::start() &observer); XBT_DEBUG("recv simcall posted"); - - if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0) - mut->unlock(); } else { /* the RECV flag was not set, so this is a send */ const simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_)); xbt_assert(process, "Actor pid=%ld is gone??", dst_); @@ -574,10 +572,9 @@ void Request::start() XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime); } - simgrid::s4u::MutexPtr mut = process->mailboxes_mutex(); - + std::unique_lock mut_lock; if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0) - mut->lock(); + mut_lock = std::unique_lock(*process->mailboxes_mutex()); if (not(smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)) { mailbox = process->mailbox(); @@ -629,9 +626,6 @@ void Request::start() boost::static_pointer_cast(action_)->set_tracing_category( smpi_process()->get_tracing_category()); } - - if (smpi_cfg_async_small_thresh() != 0 || ((flags_ & MPI_REQ_RMA) != 0)) - mut->unlock(); } } @@ -1093,9 +1087,8 @@ int Request::wait(MPI_Request * request, MPI_Status * status) if ((*request)->flags_ & MPI_REQ_GENERALIZED) { if (not((*request)->flags_ & MPI_REQ_COMPLETE)) { - ((*request)->generalized_funcs)->mutex->lock(); - ((*request)->generalized_funcs)->cond->wait(((*request)->generalized_funcs)->mutex); - ((*request)->generalized_funcs)->mutex->unlock(); + const std::scoped_lock lock(*(*request)->generalized_funcs->mutex); + (*request)->generalized_funcs->cond->wait((*request)->generalized_funcs->mutex); } MPI_Status tmp_status; MPI_Status* mystatus; @@ -1331,10 +1324,9 @@ int Request::grequest_complete(MPI_Request request) { if ((not(request->flags_ & MPI_REQ_GENERALIZED)) || request->generalized_funcs->mutex == nullptr) return MPI_ERR_REQUEST; - request->generalized_funcs->mutex->lock(); + const std::scoped_lock lock(*request->generalized_funcs->mutex); request->flags_ |= MPI_REQ_COMPLETE; // in case wait would be called after complete request->generalized_funcs->cond->notify_one(); - request->generalized_funcs->mutex->unlock(); return MPI_SUCCESS; } diff --git a/src/smpi/mpi/smpi_win.cpp b/src/smpi/mpi/smpi_win.cpp index c40ac06d17..7b33c80243 100644 --- a/src/smpi/mpi/smpi_win.cpp +++ b/src/smpi/mpi/smpi_win.cpp @@ -17,6 +17,7 @@ #include "src/mc/mc_replay.hpp" #include +#include // std::scoped_lock XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_rma, smpi, "Logging specific to SMPI (RMA operations)"); @@ -239,16 +240,14 @@ int Win::put(const void *origin_addr, int origin_count, MPI_Datatype origin_data if(request!=nullptr){ *request=sreq; }else{ - mut_->lock(); + const std::scoped_lock lock(*mut_); requests_.push_back(sreq); - mut_->unlock(); } //push request to receiver's win - recv_win->mut_->lock(); + const std::scoped_lock recv_lock(*recv_win->mut_); recv_win->requests_.push_back(rreq); rreq->start(); - recv_win->mut_->unlock(); } else { XBT_DEBUG("Entering MPI_Put from myself to myself, rank %d", target_rank); Datatype::copy(origin_addr, origin_count, origin_datatype, recv_addr, target_count, target_datatype); @@ -283,9 +282,9 @@ int Win::get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, //start the send, with another process than us as sender. sreq->start(); // push request to sender's win - send_win->mut_->lock(); - send_win->requests_.push_back(sreq); - send_win->mut_->unlock(); + if (const std::scoped_lock send_lock(*send_win->mut_); true) { + send_win->requests_.push_back(sreq); + } //start recv rreq->start(); @@ -293,9 +292,8 @@ int Win::get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, if(request!=nullptr){ *request=rreq; }else{ - mut_->lock(); + const std::scoped_lock lock(*mut_); requests_.push_back(rreq); - mut_->unlock(); } } else { Datatype::copy(send_addr, target_count, target_datatype, origin_addr, origin_count, origin_datatype); @@ -334,17 +332,16 @@ int Win::accumulate(const void *origin_addr, int origin_count, MPI_Datatype orig // start send sreq->start(); // push request to receiver's win - recv_win->mut_->lock(); - recv_win->requests_.push_back(rreq); - rreq->start(); - recv_win->mut_->unlock(); + if (const std::scoped_lock recv_lock(*recv_win->mut_); true) { + recv_win->requests_.push_back(rreq); + rreq->start(); + } if (request != nullptr) { *request = sreq; } else { - mut_->lock(); + const std::scoped_lock lock(*mut_); requests_.push_back(sreq); - mut_->unlock(); } // FIXME: The current implementation fails to ensure the correct ordering of the accumulate requests. The following @@ -367,7 +364,7 @@ int Win::get_accumulate(const void* origin_addr, int origin_count, MPI_Datatype XBT_DEBUG("Entering MPI_Get_accumulate from %d", target_rank); //need to be sure ops are correctly ordered, so finish request here ? slow. MPI_Request req = MPI_REQUEST_NULL; - send_win->atomic_mut_->lock(); + const std::scoped_lock lock(*send_win->atomic_mut_); get(result_addr, result_count, result_datatype, target_rank, target_disp, target_count, target_datatype, &req); if (req != MPI_REQUEST_NULL) @@ -377,7 +374,6 @@ int Win::get_accumulate(const void* origin_addr, int origin_count, MPI_Datatype target_disp, target_count, target_datatype, op, &req); if (req != MPI_REQUEST_NULL) Request::wait(&req, MPI_STATUS_IGNORE); - send_win->atomic_mut_->unlock(); return MPI_SUCCESS; } @@ -391,7 +387,7 @@ int Win::compare_and_swap(const void* origin_addr, const void* compare_addr, voi XBT_DEBUG("Entering MPI_Compare_and_swap with %d", target_rank); MPI_Request req = MPI_REQUEST_NULL; - send_win->atomic_mut_->lock(); + const std::scoped_lock lock(*send_win->atomic_mut_); get(result_addr, 1, datatype, target_rank, target_disp, 1, datatype, &req); if (req != MPI_REQUEST_NULL) @@ -400,7 +396,6 @@ int Win::compare_and_swap(const void* origin_addr, const void* compare_addr, voi put(origin_addr, 1, datatype, target_rank, target_disp, 1, datatype); } - send_win->atomic_mut_->unlock(); return MPI_SUCCESS; } @@ -614,7 +609,7 @@ int Win::finish_comms(){ // Without this, the vector could get redimensioned when another process pushes. // This would result in the array used by Request::waitall() to be invalidated. // Another solution would be to copy the data and cleanup the vector *before* Request::waitall - mut_->lock(); + const std::scoped_lock lock(*mut_); //Finish own requests int size = static_cast(requests_.size()); if (size > 0) { @@ -622,13 +617,12 @@ int Win::finish_comms(){ Request::waitall(size, treqs, MPI_STATUSES_IGNORE); requests_.clear(); } - mut_->unlock(); return size; } int Win::finish_comms(int rank){ // See comment about the mutex in finish_comms() above - mut_->lock(); + const std::scoped_lock lock(*mut_); // Finish own requests // Let's see if we're either the destination or the sender of this request // because we only wait for requests that we are responsible for. @@ -646,7 +640,6 @@ int Win::finish_comms(int rank){ Request::waitall(size, treqs, MPI_STATUSES_IGNORE); myreqqs.clear(); } - mut_->unlock(); return size; } diff --git a/src/xbt/OsSemaphore.hpp b/src/xbt/OsSemaphore.hpp index 2b7e1f25b6..821075f6b0 100644 --- a/src/xbt/OsSemaphore.hpp +++ b/src/xbt/OsSemaphore.hpp @@ -22,7 +22,7 @@ public: inline void release() { - std::unique_lock lock(mutex_); + const std::scoped_lock lock(mutex_); ++capa_; condition_.notify_one(); } diff --git a/src/xbt/log.cpp b/src/xbt/log.cpp index 65c5dae424..d21ea0e07b 100644 --- a/src/xbt/log.cpp +++ b/src/xbt/log.cpp @@ -235,7 +235,7 @@ int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority return priority >= category->threshold; static std::recursive_mutex log_cat_init_mutex; - log_cat_init_mutex.lock(); + const std::scoped_lock lock(log_cat_init_mutex); XBT_DEBUG("Initializing category '%s' (firstChild=%s, nextSibling=%s)", category->name, (category->firstChild ? category->firstChild->name : "none"), @@ -279,7 +279,6 @@ int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority } category->initialized = 1; - log_cat_init_mutex.unlock(); return priority >= category->threshold; } diff --git a/src/xbt/parmap.hpp b/src/xbt/parmap.hpp index d8b5eb722c..7c02fa1dd7 100644 --- a/src/xbt/parmap.hpp +++ b/src/xbt/parmap.hpp @@ -309,7 +309,7 @@ template void Parmap::worker_main(ThreadData* data) template void Parmap::PosixSynchro::master_signal() { - std::unique_lock lk(ready_mutex); + const std::scoped_lock lock(ready_mutex); this->parmap.thread_counter = 1; this->parmap.work_round++; /* wake all workers */ @@ -318,14 +318,14 @@ template void Parmap::PosixSynchro::master_signal() template void Parmap::PosixSynchro::master_wait() { - std::unique_lock lk(done_mutex); + std::unique_lock lock(done_mutex); /* wait for all workers to be ready */ - done_cond.wait(lk, [this]() { return this->parmap.thread_counter >= this->parmap.num_workers; }); + done_cond.wait(lock, [this]() { return this->parmap.thread_counter >= this->parmap.num_workers; }); } template void Parmap::PosixSynchro::worker_signal() { - std::unique_lock lk(done_mutex); + const std::scoped_lock lock(done_mutex); this->parmap.thread_counter++; if (this->parmap.thread_counter == this->parmap.num_workers) { /* all workers have finished, wake the controller */ @@ -335,9 +335,9 @@ template void Parmap::PosixSynchro::worker_signal() template void Parmap::PosixSynchro::worker_wait(unsigned expected_round) { - std::unique_lock lk(ready_mutex); + std::unique_lock lock(ready_mutex); /* wait for more work */ - ready_cond.wait(lk, [this, expected_round]() { return this->parmap.work_round == expected_round; }); + ready_cond.wait(lock, [this, expected_round]() { return this->parmap.work_round == expected_round; }); } #if HAVE_FUTEX_H diff --git a/teshsuite/mc/mutex-handling/mutex-handling.cpp b/teshsuite/mc/mutex-handling/mutex-handling.cpp index 22224b656d..adb9519796 100644 --- a/teshsuite/mc/mutex-handling/mutex-handling.cpp +++ b/teshsuite/mc/mutex-handling/mutex-handling.cpp @@ -24,6 +24,8 @@ #include "simgrid/s4u/Mailbox.hpp" #include "simgrid/s4u/Mutex.hpp" +#include // std::unique_lock + XBT_LOG_NEW_DEFAULT_CATEGORY(mutex_handling, "Messages specific for this test"); static int receiver(const char* box_name) @@ -45,14 +47,11 @@ static int sender(const char* box_name, simgrid::s4u::MutexPtr mutex, int value) auto* payload = new int(value); auto mb = simgrid::s4u::Mailbox::by_name(box_name); + std::unique_lock lock; if (mutex) - mutex->lock(); + lock = std::unique_lock(*mutex); mb->put(payload, 8); - - if (mutex) - mutex->unlock(); - return 0; } -- 2.20.1