2018-07-30 20:34:23 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "base/algorithm.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
namespace base {
|
|
|
|
|
|
|
|
class binary_guard {
|
|
|
|
public:
|
|
|
|
binary_guard() = default;
|
|
|
|
binary_guard(binary_guard &&other);
|
|
|
|
binary_guard &operator=(binary_guard &&other);
|
|
|
|
~binary_guard();
|
|
|
|
|
|
|
|
bool alive() const;
|
2018-08-08 20:11:28 +00:00
|
|
|
void kill();
|
2018-07-30 20:34:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void destroy();
|
|
|
|
|
|
|
|
std::atomic<bool> *_bothAlive = nullptr;
|
|
|
|
|
|
|
|
friend std::pair<binary_guard, binary_guard> make_binary_guard();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
inline binary_guard::binary_guard(binary_guard &&other)
|
|
|
|
: _bothAlive(base::take(other._bothAlive)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
inline binary_guard &binary_guard::operator=(binary_guard &&other) {
|
|
|
|
if (this != &other) {
|
|
|
|
destroy();
|
|
|
|
_bothAlive = base::take(other._bothAlive);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline binary_guard::~binary_guard() {
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool binary_guard::alive() const {
|
|
|
|
return _bothAlive && _bothAlive->load();
|
|
|
|
}
|
|
|
|
|
2018-08-08 20:11:28 +00:00
|
|
|
inline void binary_guard::kill() {
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
2018-07-30 20:34:23 +00:00
|
|
|
inline void binary_guard::destroy() {
|
2018-08-27 11:35:41 +00:00
|
|
|
if (const auto both = base::take(_bothAlive)) {
|
2018-07-30 20:34:23 +00:00
|
|
|
auto old = true;
|
2018-08-27 11:35:41 +00:00
|
|
|
if (!both->compare_exchange_strong(old, false)) {
|
|
|
|
delete both;
|
2018-07-30 20:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::pair<binary_guard, binary_guard> make_binary_guard() {
|
|
|
|
auto result = std::pair<binary_guard, binary_guard>();
|
|
|
|
result.first._bothAlive
|
|
|
|
= result.second._bothAlive
|
|
|
|
= new std::atomic<bool>(true);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace base
|