Reverted mutexes back to custom atomic bool based locks

This commit is contained in:
aristocratos 2021-10-16 23:24:07 +02:00
parent 3f34a67df6
commit 71d5cd5fd9
4 changed files with 16 additions and 19 deletions

View file

@ -28,7 +28,6 @@ tab-size = 4
#include <tuple>
#include <regex>
#include <chrono>
#include <mutex>
#include <btop_shared.hpp>
#include <btop_tools.hpp>
@ -39,7 +38,7 @@ tab-size = 4
#include <btop_menu.hpp>
using std::string, std::string_view, std::vector, std::atomic, std::endl, std::cout, std::min, std::flush, std::endl;
using std::string_literals::operator""s, std::to_string, std::mutex, std::lock_guard;
using std::string_literals::operator""s, std::to_string;
namespace fs = std::filesystem;
namespace rng = std::ranges;
using namespace Tools;
@ -148,8 +147,8 @@ void argumentParser(const int& argc, char **argv) {
//* Handler for SIGWINCH and general resizing events, does nothing if terminal hasn't been resized unless force=true
void term_resize(bool force) {
static mutex resizing;
lock_guard<mutex> lock(resizing);
static atomic<bool> resizing (false);
atomic_lock lck(resizing, true);
if (auto refreshed = Term::refresh(true); refreshed or force) {
if (force and refreshed) force = false;
}

View file

@ -21,13 +21,12 @@ tab-size = 4
#include <atomic>
#include <fstream>
#include <string_view>
#include <mutex>
#include <btop_config.hpp>
#include <btop_shared.hpp>
#include <btop_tools.hpp>
using std::array, std::atomic, std::string_view, std::string_literals::operator""s, std::mutex, std::lock_guard;
using std::array, std::atomic, std::string_view, std::string_literals::operator""s;
namespace fs = std::filesystem;
namespace rng = std::ranges;
using namespace Tools;
@ -36,7 +35,7 @@ using namespace Tools;
namespace Config {
atomic<bool> locked (false);
mutex writelock;
atomic<bool> writelock (false);
bool write_new;
const vector<array<string, 2>> descriptions = {
@ -257,7 +256,7 @@ namespace Config {
unordered_flat_map<string, int> intsTmp;
bool _locked(const string& name) {
lock_guard<mutex> lock(writelock);
atomic_wait(writelock, true);
if (not write_new and rng::find_if(descriptions, [&name](const auto& a) { return a.at(0) == name; }) != descriptions.end())
write_new = true;
return locked.load();
@ -335,7 +334,7 @@ namespace Config {
}
void lock() {
lock_guard<mutex> lock(writelock);
atomic_wait(writelock);
locked = true;
}
@ -449,7 +448,7 @@ namespace Config {
void unlock() {
if (not locked) return;
atomic_wait(Runner::active);
lock_guard<mutex> lock(writelock);
atomic_lock lck(writelock, true);
try {
if (Proc::shown) {
ints.at("selected_pid") = Proc::selected_pid;

View file

@ -24,7 +24,6 @@ tab-size = 4
#include <iomanip>
#include <utility>
#include <robin_hood.h>
#include <mutex>
#include <unistd.h>
#include <limits.h>
@ -34,8 +33,7 @@ tab-size = 4
#include <btop_shared.hpp>
#include <btop_tools.hpp>
using std::string_view, std::max, std::floor, std::to_string, std::cin, std::cout, std::flush, robin_hood::unordered_flat_map,
std::mutex, std::lock_guard;
using std::string_view, std::max, std::floor, std::to_string, std::cin, std::cout, std::flush, robin_hood::unordered_flat_map;
namespace fs = std::filesystem;
namespace rng = std::ranges;
@ -332,9 +330,11 @@ namespace Tools {
while (atom.load(std::memory_order_relaxed) == old and (time_ms() - start_time < wait_ms)) sleep_ms(1);
}
atomic_lock::atomic_lock(atomic<bool>& atom) : atom(atom) {
atomic_lock::atomic_lock(atomic<bool>& atom, bool wait) : atom(atom) {
active_locks++;
this->atom.store(true);
if (wait) {
while (not this->atom.compare_exchange_strong(this->not_true, true));
} else this->atom.store(true);
}
atomic_lock::~atomic_lock() {
@ -384,8 +384,7 @@ namespace Tools {
namespace Logger {
using namespace Tools;
mutex logger_mtx;
std::atomic<bool> busy (false);
bool first = true;
const string tdf = "%Y/%m/%d (%T) | ";
@ -398,7 +397,7 @@ namespace Logger {
void log_write(const size_t level, const string& msg) {
if (loglevel < level or logfile.empty()) return;
lock_guard<mutex> lock(logger_mtx);
atomic_lock lck(busy, true);
std::error_code ec;
try {
if (fs::exists(logfile) and fs::file_size(logfile, ec) > 1024 << 10 and not ec) {

View file

@ -297,7 +297,7 @@ namespace Tools {
atomic<bool>& atom;
bool not_true = false;
public:
atomic_lock(atomic<bool>& atom);
atomic_lock(atomic<bool>& atom, bool wait=false);
~atomic_lock();
};