Add ExitGuard.

GitOrigin-RevId: f8f04daacbee00386e326eb3ca1ec3dfec19cbb0
This commit is contained in:
levlam 2020-10-10 22:37:36 +03:00
parent c484cc4773
commit 7207d76a80
4 changed files with 66 additions and 0 deletions

View File

@ -87,6 +87,7 @@ set(TDUTILS_SOURCE
td/utils/BufferedUdp.cpp
td/utils/check.cpp
td/utils/crypto.cpp
td/utils/ExitGuard.cpp
td/utils/FileLog.cpp
td/utils/filesystem.cpp
td/utils/find_boundary.cpp
@ -187,6 +188,7 @@ set(TDUTILS_SOURCE
td/utils/Destructor.h
td/utils/Enumerator.h
td/utils/EpochBasedMemoryReclamation.h
td/utils/ExitGuard.h
td/utils/FileLog.h
td/utils/filesystem.h
td/utils/find_boundary.h

View File

@ -0,0 +1,13 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/utils/ExitGuard.h"
namespace td {
std::atomic<bool> ExitGuard::is_exited_{false};
} // namespace td

View File

@ -0,0 +1,32 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include <atomic>
namespace td {
class ExitGuard {
public:
ExitGuard() = default;
ExitGuard(ExitGuard &&) = delete;
ExitGuard &operator=(ExitGuard &&) = delete;
ExitGuard(const ExitGuard &) = delete;
ExitGuard &operator=(const ExitGuard &) = delete;
~ExitGuard() {
is_exited_.store(true, std::memory_order_relaxed);
}
static bool is_exited() {
return is_exited_.load(std::memory_order_relaxed);
}
private:
static std::atomic<bool> is_exited_;
};
} // namespace td

View File

@ -10,6 +10,7 @@
#include "td/utils/bits.h"
#include "td/utils/CancellationToken.h"
#include "td/utils/common.h"
#include "td/utils/ExitGuard.h"
#include "td/utils/Hash.h"
#include "td/utils/HashMap.h"
#include "td/utils/HashSet.h"
@ -50,6 +51,24 @@
using namespace td;
struct CheckExitGuard {
explicit CheckExitGuard(bool expected_value): expected_value_(expected_value) {
}
CheckExitGuard(CheckExitGuard &&) = delete;
CheckExitGuard &operator=(CheckExitGuard &&) = delete;
CheckExitGuard(const CheckExitGuard &) = delete;
CheckExitGuard &operator=(const CheckExitGuard &) = delete;
~CheckExitGuard() {
ASSERT_EQ(expected_value_, ExitGuard::is_exited());
}
bool expected_value_;
};
CheckExitGuard check_exit_guard_true{true};
ExitGuard exit_guard;
CheckExitGuard check_exit_guard_false{false};
#if TD_LINUX || TD_DARWIN
TEST(Misc, update_atime_saves_mtime) {
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR));