From: Francis Dupont Date: Sat, 28 Mar 2020 09:17:11 +0000 (+0100) Subject: [#1089] Protected last error concurrent accesses X-Git-Tag: Kea-1.7.7~94 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9fbc2164ae7e43fd5195c257698ffa35f30c226d;p=thirdparty%2Fkea.git [#1089] Protected last error concurrent accesses --- diff --git a/src/lib/util/watched_thread.cc b/src/lib/util/watched_thread.cc index ab9c30bb6e..df7a43917b 100644 --- a/src/lib/util/watched_thread.cc +++ b/src/lib/util/watched_thread.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -15,7 +15,10 @@ WatchedThread::start(const boost::function& thread_main) { clearReady(ERROR); clearReady(READY); clearReady(TERMINATE); - last_error_ = "no error"; + { + std::lock_guard lock(last_error_mutex_); + last_error_ = "no error"; + } thread_.reset(new std::thread(thread_main)); } @@ -59,17 +62,22 @@ WatchedThread::stop() { clearReady(ERROR); clearReady(READY); + std::lock_guard lock(last_error_mutex_); last_error_ = "thread stopped"; } void WatchedThread::setError(const std::string& error_msg) { - last_error_ = error_msg; + { + std::lock_guard lock(last_error_mutex_); + last_error_ = error_msg; + } markReady(ERROR); } std::string WatchedThread::getLastError() { + std::lock_guard lock(last_error_mutex_); return (last_error_); } } // end of namespace isc::util diff --git a/src/lib/util/watched_thread.h b/src/lib/util/watched_thread.h index b05294dc28..1cf9b4c34c 100644 --- a/src/lib/util/watched_thread.h +++ b/src/lib/util/watched_thread.h @@ -1,4 +1,4 @@ -// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -12,6 +12,7 @@ #include #include +#include #include namespace isc { @@ -109,6 +110,9 @@ public: /// @brief Error message of the last error encountered std::string last_error_; + /// @brief Mutex to protect last error message concurrent access + std::mutex last_error_mutex_; + /// @brief WatchSockets that are used to communicate with the owning thread /// There are three: /// -# ERROR - Marked as ready by the thread when it experiences an error.