Add ExitGuard.
GitOrigin-RevId: f8f04daacbee00386e326eb3ca1ec3dfec19cbb0
This commit is contained in:
parent
c484cc4773
commit
7207d76a80
@ -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
|
||||
|
13
tdutils/td/utils/ExitGuard.cpp
Normal file
13
tdutils/td/utils/ExitGuard.cpp
Normal 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
|
32
tdutils/td/utils/ExitGuard.h
Normal file
32
tdutils/td/utils/ExitGuard.h
Normal 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
|
@ -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));
|
||||
|
Loading…
x
Reference in New Issue
Block a user