From 8869697c4977fcbdccf85962060df75d48e68056 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 31 Aug 2021 10:47:47 +0800 Subject: [PATCH] Params: faster atomic clearAll (#21973) * faster clearAll * use unlink * fsync_dir if removed > 0 * remove macro ERR_NO_VALUE * always fsync * keep call to unlink Co-authored-by: Willem Melching old-commit-hash: 383ff35790705ec3bbc8d36215fe1c2dde5edef3 --- selfdrive/common/params.cc | 14 +++++++++++--- selfdrive/common/params.h | 2 -- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/selfdrive/common/params.cc b/selfdrive/common/params.cc index 03574cf033..b5b7941e5f 100644 --- a/selfdrive/common/params.cc +++ b/selfdrive/common/params.cc @@ -270,9 +270,8 @@ int Params::remove(const char *key) { std::lock_guard lk(file_lock); // Delete value. std::string path = params_path + "/d/" + key; - int result = ::remove(path.c_str()); + int result = unlink(path.c_str()); if (result != 0) { - result = ERR_NO_VALUE; return result; } // fsync parent directory @@ -313,9 +312,18 @@ std::map Params::readAll() { } void Params::clearAll(ParamKeyType key_type) { + FileLock file_lock(params_path + "/.lock", LOCK_EX); + std::lock_guard lk(file_lock); + + std::string path; for (auto &[key, type] : keys) { if (type & key_type) { - remove(key); + path = params_path + "/d/" + key; + unlink(path.c_str()); } } + + // fsync parent directory + path = params_path + "/d"; + fsync_dir(path.c_str()); } diff --git a/selfdrive/common/params.h b/selfdrive/common/params.h index 3c9d99895f..505bfcb082 100644 --- a/selfdrive/common/params.h +++ b/selfdrive/common/params.h @@ -4,8 +4,6 @@ #include #include -#define ERR_NO_VALUE -33 - enum ParamKeyType { PERSISTENT = 0x02, CLEAR_ON_MANAGER_START = 0x04,