Replaced std::lock_guard with std::scoped_lock

Both do the same thing except the latter can handle multiple mutexes. We don't require such behavior
right now, but if we ever do we can just plug it right in.
This commit is contained in:
Charles Dang 2021-07-21 07:59:56 -04:00
parent 3f053046a6
commit 7205d842ae
4 changed files with 24 additions and 24 deletions

View file

@ -389,12 +389,12 @@ namespace translation
std::string dgettext(const char* domain, const char* msgid)
{
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
return bl::dgettext(domain, msgid, get_manager().get_locale());
}
std::string egettext(char const *msgid)
{
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
return msgid[0] == '\0' ? msgid : bl::gettext(msgid, get_manager().get_locale());
}
@ -414,7 +414,7 @@ std::string dsgettext (const char * domainname, const char *msgid)
std::string dsngettext (const char * domainname, const char *singular, const char *plural, int n)
{
//TODO: only the next line needs to be in the lock.
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
std::string msgval = bl::dngettext(domainname, singular, plural, n, get_manager().get_locale());
if (msgval == singular) {
const char* firsthat = std::strchr (singular, '^');
@ -429,7 +429,7 @@ std::string dsngettext (const char * domainname, const char *singular, const cha
void bind_textdomain(const char* domain, const char* directory, const char* /*encoding*/)
{
LOG_G << "adding textdomain '" << domain << "' in directory '" << directory << "'\n";
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
get_manager().add_messages_domain(domain);
get_manager().add_messages_path(directory);
get_manager().update_locale();
@ -438,7 +438,7 @@ void bind_textdomain(const char* domain, const char* directory, const char* /*en
void set_default_textdomain(const char* domain)
{
LOG_G << "set_default_textdomain: '" << domain << "'\n";
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
get_manager().set_default_messages_domain(domain);
}
@ -448,13 +448,13 @@ void set_language(const std::string& language, const std::vector<std::string>* /
// why should we need alternates? which languages we support should only be related
// to which languages we ship with and not which the os supports
LOG_G << "setting language to '" << language << "' \n";
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
get_manager().set_language(language);
}
int compare(const std::string& s1, const std::string& s2)
{
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
try {
return std::use_facet<std::collate<char>>(get_manager().get_locale()).compare(s1.c_str(), s1.c_str() + s1.size(), s2.c_str(), s2.c_str() + s2.size());
@ -477,7 +477,7 @@ int icompare(const std::string& s1, const std::string& s2)
// https://github.com/wesnoth/wesnoth/issues/2094
return compare(ascii_to_lowercase(s1), ascii_to_lowercase(s2));
#else
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
try {
return std::use_facet<bl::collator<char>>(get_manager().get_locale()).compare(
@ -505,7 +505,7 @@ int icompare(const std::string& s1, const std::string& s2)
std::string strftime(const std::string& format, const std::tm* time)
{
std::basic_ostringstream<char> dummy;
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
dummy.imbue(get_manager().get_locale()); // TODO: Calling imbue() with hard-coded locale appears to work with put_time in glibc, but not with get_locale()...
// Revert to use of boost (from 1.14) instead of std::put_time() because the latter does not appear to handle locale properly in Linux
dummy << bl::as::ftime(format) << mktime(const_cast<std::tm*>(time));
@ -515,7 +515,7 @@ std::string strftime(const std::string& format, const std::tm* time)
bool ci_search(const std::string& s1, const std::string& s2)
{
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
const std::locale& locale = get_manager().get_locale();
std::string ls1 = bl::to_lower(s1, locale);
@ -527,7 +527,7 @@ bool ci_search(const std::string& s1, const std::string& s2)
const boost::locale::info& get_effective_locale_info()
{
std::lock_guard lock(get_mutex());
std::scoped_lock lock(get_mutex());
return std::use_facet<boost::locale::info>(get_manager().get_locale());
}
}

View file

@ -100,7 +100,7 @@ static uint32_t timer_callback(uint32_t, void* id)
Uint32 result;
{
std::lock_guard lock(timers_mutex);
std::scoped_lock lock(timers_mutex);
auto itor = get_timers().find(reinterpret_cast<std::size_t>(id));
if(itor == get_timers().end()) {
@ -133,7 +133,7 @@ std::size_t add_timer(const uint32_t interval,
timer timer;
{
std::lock_guard lock(timers_mutex);
std::scoped_lock lock(timers_mutex);
do {
++next_timer_id;
@ -155,7 +155,7 @@ std::size_t add_timer(const uint32_t interval,
timer.callback = callback;
{
std::lock_guard lock(timers_mutex);
std::scoped_lock lock(timers_mutex);
get_timers().emplace(next_timer_id, timer);
}
@ -168,7 +168,7 @@ bool remove_timer(const std::size_t id)
{
DBG_GUI_E << "Removing timer " << id << ".\n";
std::lock_guard lock(timers_mutex);
std::scoped_lock lock(timers_mutex);
auto itor = get_timers().find(id);
if(itor == get_timers().end()) {
@ -203,7 +203,7 @@ bool execute_timer(const std::size_t id)
std::function<void(size_t)> callback = nullptr;
{
std::lock_guard lock(timers_mutex);
std::scoped_lock lock(timers_mutex);
auto itor = get_timers().find(id);
if(itor == get_timers().end()) {

View file

@ -237,7 +237,7 @@ log_in_progress::log_in_progress(std::ostream& stream)
void log_in_progress::operator|(formatter&& message)
{
std::lock_guard lock(log_mutex);
std::scoped_lock lock(log_mutex);
for(int i = 0; i < indent; ++i)
stream_ << " ";
if(timestamp_) {

View file

@ -340,7 +340,7 @@ std::size_t wesnothd_connection::is_write_complete(const boost::system::error_co
MPTEST_LOG;
if(ec) {
{
std::lock_guard lock(last_error_mutex_);
std::scoped_lock lock(last_error_mutex_);
last_error_ = ec;
}
@ -364,7 +364,7 @@ void wesnothd_connection::handle_write(const boost::system::error_code& ec, std:
if(ec) {
{
std::lock_guard lock(last_error_mutex_);
std::scoped_lock lock(last_error_mutex_);
last_error_ = ec;
}
@ -386,7 +386,7 @@ std::size_t wesnothd_connection::is_read_complete(const boost::system::error_cod
MPTEST_LOG;
if(ec) {
{
std::lock_guard lock(last_error_mutex_);
std::scoped_lock lock(last_error_mutex_);
last_error_ = ec;
}
@ -427,7 +427,7 @@ void wesnothd_connection::handle_read(const boost::system::error_code& ec, std::
bytes_to_read_ = 0;
if(last_error_ && ec != boost::asio::error::eof) {
{
std::lock_guard lock(last_error_mutex_);
std::scoped_lock lock(last_error_mutex_);
last_error_ = ec;
}
@ -443,7 +443,7 @@ void wesnothd_connection::handle_read(const boost::system::error_code& ec, std::
if(!data.empty()) { DBG_NW << "Received:\n" << data; }
{
std::lock_guard lock(recv_queue_mutex_);
std::scoped_lock lock(recv_queue_mutex_);
recv_queue_.emplace(std::move(data));
recv_queue_lock_.notify_all();
}
@ -492,7 +492,7 @@ bool wesnothd_connection::receive_data(config& result)
MPTEST_LOG;
{
std::lock_guard lock(recv_queue_mutex_);
std::scoped_lock lock(recv_queue_mutex_);
if(!recv_queue_.empty()) {
result.swap(recv_queue_.front());
recv_queue_.pop();
@ -501,7 +501,7 @@ bool wesnothd_connection::receive_data(config& result)
}
{
std::lock_guard lock(last_error_mutex_);
std::scoped_lock lock(last_error_mutex_);
if(last_error_) {
std::string user_msg;