Improve base::binary_guard interface.

This commit is contained in:
John Preston 2019-03-27 16:11:38 +04:00
parent efb2972d28
commit d3bf489bea
12 changed files with 38 additions and 54 deletions

View File

@ -20,9 +20,11 @@ public:
binary_guard &operator=(binary_guard &&other); binary_guard &operator=(binary_guard &&other);
~binary_guard(); ~binary_guard();
bool alive() const;
binary_guard &operator=(std::nullptr_t); binary_guard &operator=(std::nullptr_t);
bool alive() const;
binary_guard make_guard();
explicit operator bool() const; explicit operator bool() const;
private: private:
@ -30,8 +32,6 @@ private:
std::atomic<bool> *_bothAlive = nullptr; std::atomic<bool> *_bothAlive = nullptr;
friend std::pair<binary_guard, binary_guard> make_binary_guard();
}; };
inline binary_guard::binary_guard(binary_guard &&other) inline binary_guard::binary_guard(binary_guard &&other)
@ -46,6 +46,10 @@ inline binary_guard &binary_guard::operator=(binary_guard &&other) {
return *this; return *this;
} }
inline binary_guard::~binary_guard() {
destroy();
}
inline binary_guard &binary_guard::operator=(std::nullptr_t) { inline binary_guard &binary_guard::operator=(std::nullptr_t) {
destroy(); destroy();
return *this; return *this;
@ -59,10 +63,6 @@ inline bool binary_guard::alive() const {
return _bothAlive && _bothAlive->load(); return _bothAlive && _bothAlive->load();
} }
inline binary_guard::~binary_guard() {
destroy();
}
inline void binary_guard::destroy() { inline void binary_guard::destroy() {
if (const auto both = base::take(_bothAlive)) { if (const auto both = base::take(_bothAlive)) {
auto old = true; auto old = true;
@ -72,11 +72,11 @@ inline void binary_guard::destroy() {
} }
} }
inline std::pair<binary_guard, binary_guard> make_binary_guard() { inline binary_guard binary_guard::make_guard() {
auto result = std::pair<binary_guard, binary_guard>(); destroy();
result.first._bothAlive
= result.second._bothAlive auto result = binary_guard();
= new std::atomic<bool>(true); _bothAlive = result._bothAlive = new std::atomic<bool>(true);
return result; return result;
} }

View File

@ -268,7 +268,6 @@ ConcurrentTimer::ConcurrentTimer(
} }
Fn<void()> ConcurrentTimer::createAdjuster() { Fn<void()> ConcurrentTimer::createAdjuster() {
auto guards = base::make_binary_guard();
_guard = std::make_shared<bool>(true); _guard = std::make_shared<bool>(true);
return [=, runner = _runner, guard = std::weak_ptr<bool>(_guard)] { return [=, runner = _runner, guard = std::weak_ptr<bool>(_guard)] {
runner([=] { runner([=] {
@ -294,12 +293,10 @@ void ConcurrentTimer::start(
} }
void ConcurrentTimer::cancelAndSchedule(int timeout) { void ConcurrentTimer::cancelAndSchedule(int timeout) {
auto guards = base::make_binary_guard();
_running = std::move(guards.first);
auto method = [ auto method = [
=, =,
runner = _runner, runner = _runner,
guard = std::move(guards.second) guard = _running.make_guard()
]() mutable { ]() mutable {
if (!guard) { if (!guard) {
return; return;

View File

@ -711,13 +711,11 @@ void BackgroundPreviewBox::checkLoadedDocument() {
return; return;
} }
const auto generateCallback = [=](QImage &&image) { const auto generateCallback = [=](QImage &&image) {
auto [left, right] = base::make_binary_guard();
_generating = std::move(left);
crl::async([ crl::async([
this, this,
image = std::move(image), image = std::move(image),
patternBackground = patternBackgroundColor(), patternBackground = patternBackgroundColor(),
guard = std::move(right) guard = _generating.make_guard()
]() mutable { ]() mutable {
auto scaled = PrepareScaledFromFull(image, patternBackground); auto scaled = PrepareScaledFromFull(image, patternBackground);
const auto ms = crl::now(); const auto ms = crl::now();

View File

@ -1134,8 +1134,8 @@ base::binary_guard LanguageBox::Show() {
const auto manager = Core::App().langCloudManager(); const auto manager = Core::App().langCloudManager();
if (manager->languageList().empty()) { if (manager->languageList().empty()) {
auto guard = std::make_shared<base::binary_guard>(); auto guard = std::make_shared<base::binary_guard>(
std::tie(result, *guard) = base::make_binary_guard(); result.make_guard());
auto alive = std::make_shared<std::unique_ptr<base::Subscription>>( auto alive = std::make_shared<std::unique_ptr<base::Subscription>>(
std::make_unique<base::Subscription>()); std::make_unique<base::Subscription>());
**alive = manager->languageListChanged().add_subscription([=] { **alive = manager->languageListChanged().add_subscription([=] {

View File

@ -1555,12 +1555,12 @@ base::binary_guard ReadImageAsync(
not_null<DocumentData*> document, not_null<DocumentData*> document,
FnMut<QImage(QImage)> postprocess, FnMut<QImage(QImage)> postprocess,
FnMut<void(QImage&&)> done) { FnMut<void(QImage&&)> done) {
auto [left, right] = base::make_binary_guard(); auto result = base::binary_guard();
crl::async([ crl::async([
bytes = document->data(), bytes = document->data(),
path = document->filepath(), path = document->filepath(),
postprocess = std::move(postprocess), postprocess = std::move(postprocess),
guard = std::move(left), guard = result.make_guard(),
callback = std::move(done) callback = std::move(done)
]() mutable { ]() mutable {
auto format = QByteArray(); auto format = QByteArray();
@ -1584,7 +1584,7 @@ base::binary_guard ReadImageAsync(
callback(std::move(image)); callback(std::move(image));
}); });
}); });
return std::move(right); return result;
} }
//void HandleUnsupportedMedia( //void HandleUnsupportedMedia(

View File

@ -138,10 +138,7 @@ void GoodThumbSource::load(
if (loading() || _empty) { if (loading() || _empty) {
return; return;
} }
auto [left, right] = base::make_binary_guard(); auto callback = [=, guard = _loading.make_guard()](
_loading = std::move(left);
auto callback = [=, guard = std::move(right)](
QByteArray &&value) mutable { QByteArray &&value) mutable {
if (value.isEmpty()) { if (value.isEmpty()) {
crl::on_main([=, guard = std::move(guard)]() mutable { crl::on_main([=, guard = std::move(guard)]() mutable {

View File

@ -1160,8 +1160,6 @@ void DatabaseObject::writeBundles() {
} }
void DatabaseObject::createCleaner() { void DatabaseObject::createCleaner() {
auto [left, right] = base::make_binary_guard();
_cleaner.guard = std::move(left);
auto done = [weak = _weak](Error error) { auto done = [weak = _weak](Error error) {
weak.with([=](DatabaseObject &that) { weak.with([=](DatabaseObject &that) {
that.cleanerDone(error); that.cleanerDone(error);
@ -1169,7 +1167,7 @@ void DatabaseObject::createCleaner() {
}; };
_cleaner.object = std::make_unique<Cleaner>( _cleaner.object = std::make_unique<Cleaner>(
_base, _base,
std::move(right), _cleaner.guard.make_guard(),
std::move(done)); std::move(done));
pushStatsDelayed(); pushStatsDelayed();
} }
@ -1196,11 +1194,9 @@ void DatabaseObject::checkCompactor() {
info.till = _binlog.size(); info.till = _binlog.size();
info.systemTime = _time.system; info.systemTime = _time.system;
info.keysCount = _map.size(); info.keysCount = _map.size();
auto [first, second] = base::make_binary_guard();
_compactor.guard = std::move(first);
_compactor.object = std::make_unique<Compactor>( _compactor.object = std::make_unique<Compactor>(
_weak, _weak,
std::move(second), _compactor.guard.make_guard(),
_path, _path,
_settings, _settings,
base::duplicate(_key), base::duplicate(_key),

View File

@ -421,9 +421,7 @@ void FileLoader::start(bool loadFirst, bool prior) {
void FileLoader::loadLocal(const Storage::Cache::Key &key) { void FileLoader::loadLocal(const Storage::Cache::Key &key) {
const auto readImage = (_locationType != AudioFileLocation); const auto readImage = (_locationType != AudioFileLocation);
auto [first, second] = base::make_binary_guard(); auto done = [=, guard = _localLoading.make_guard()](
_localLoading = std::move(first);
auto done = [=, guard = std::move(second)](
QByteArray &&value, QByteArray &&value,
QImage &&image, QImage &&image,
QByteArray &&format) mutable { QByteArray &&format) mutable {

View File

@ -88,10 +88,11 @@ void Databases::destroy(Cache::Database *database) {
auto &kept = entry.second; auto &kept = entry.second;
if (kept.database.get() == database) { if (kept.database.get() == database) {
Assert(!kept.destroying.alive()); Assert(!kept.destroying.alive());
auto [first, second] = base::make_binary_guard();
kept.destroying = std::move(first);
database->close(); database->close();
database->waitForCleaner([=, guard = std::move(second)]() mutable { database->waitForCleaner([
=,
guard = kept.destroying.make_guard()
]() mutable {
crl::on_main(std::move(guard), [=] { crl::on_main(std::move(guard), [=] {
_map.erase(path); _map.erase(path);
}); });

View File

@ -481,9 +481,7 @@ void Templates::load() {
return; return;
} }
auto[left, right] = base::make_binary_guard(); crl::async([=, guard = _reading.make_guard()]() mutable {
_reading = std::move(left);
crl::async([=, guard = std::move(right)]() mutable {
auto result = ReadFiles(cWorkingDir() + "TEMPLATES"); auto result = ReadFiles(cWorkingDir() + "TEMPLATES");
result.index = ComputeIndex(result.result); result.index = ComputeIndex(result.result);
crl::on_main(std::move(guard), [ crl::on_main(std::move(guard), [

View File

@ -940,12 +940,10 @@ void Instance::generateCache() {
const auto size = _size; const auto size = _size;
const auto index = _sprites.size(); const auto index = _sprites.size();
auto [left, right] = base::make_binary_guard();
_generating = std::move(left);
crl::async([ crl::async([
=, =,
universal = Universal, universal = Universal,
guard = std::move(right) guard = _generating.make_guard()
]() mutable { ]() mutable {
crl::on_main(std::move(guard), [ crl::on_main(std::move(guard), [
=, =,

View File

@ -408,13 +408,14 @@ void Widget::step_shift(float64 ms, bool timer) {
void Widget::hideSlow() { void Widget::hideSlow() {
if (anim::Disabled()) { if (anim::Disabled()) {
_hiding = true; _hiding = true;
auto [left, right] = base::make_binary_guard(); App::CallDelayed(
_hidingDelayed = std::move(left); st::notifySlowHide,
App::CallDelayed(st::notifySlowHide, this, [=, guard = std::move(right)] { this,
if (guard && _hiding) { [=, guard = _hidingDelayed.make_guard()] {
hideFast(); if (guard && _hiding) {
} hideFast();
}); }
});
} else { } else {
hideAnimated(st::notifySlowHide, anim::easeInCirc); hideAnimated(st::notifySlowHide, anim::easeInCirc);
} }