diff --git a/Telegram/SourceFiles/base/binary_guard.h b/Telegram/SourceFiles/base/binary_guard.h index 5ff3fc37d..2f0978999 100644 --- a/Telegram/SourceFiles/base/binary_guard.h +++ b/Telegram/SourceFiles/base/binary_guard.h @@ -20,9 +20,11 @@ public: binary_guard &operator=(binary_guard &&other); ~binary_guard(); - bool alive() const; - binary_guard &operator=(std::nullptr_t); + + bool alive() const; + binary_guard make_guard(); + explicit operator bool() const; private: @@ -30,8 +32,6 @@ private: std::atomic *_bothAlive = nullptr; - friend std::pair make_binary_guard(); - }; inline binary_guard::binary_guard(binary_guard &&other) @@ -46,6 +46,10 @@ inline binary_guard &binary_guard::operator=(binary_guard &&other) { return *this; } +inline binary_guard::~binary_guard() { + destroy(); +} + inline binary_guard &binary_guard::operator=(std::nullptr_t) { destroy(); return *this; @@ -59,10 +63,6 @@ inline bool binary_guard::alive() const { return _bothAlive && _bothAlive->load(); } -inline binary_guard::~binary_guard() { - destroy(); -} - inline void binary_guard::destroy() { if (const auto both = base::take(_bothAlive)) { auto old = true; @@ -72,11 +72,11 @@ inline void binary_guard::destroy() { } } -inline std::pair make_binary_guard() { - auto result = std::pair(); - result.first._bothAlive - = result.second._bothAlive - = new std::atomic(true); +inline binary_guard binary_guard::make_guard() { + destroy(); + + auto result = binary_guard(); + _bothAlive = result._bothAlive = new std::atomic(true); return result; } diff --git a/Telegram/SourceFiles/base/concurrent_timer.cpp b/Telegram/SourceFiles/base/concurrent_timer.cpp index b5417e17a..7e523a0d1 100644 --- a/Telegram/SourceFiles/base/concurrent_timer.cpp +++ b/Telegram/SourceFiles/base/concurrent_timer.cpp @@ -268,7 +268,6 @@ ConcurrentTimer::ConcurrentTimer( } Fn ConcurrentTimer::createAdjuster() { - auto guards = base::make_binary_guard(); _guard = std::make_shared(true); return [=, runner = _runner, guard = std::weak_ptr(_guard)] { runner([=] { @@ -294,12 +293,10 @@ void ConcurrentTimer::start( } void ConcurrentTimer::cancelAndSchedule(int timeout) { - auto guards = base::make_binary_guard(); - _running = std::move(guards.first); auto method = [ =, runner = _runner, - guard = std::move(guards.second) + guard = _running.make_guard() ]() mutable { if (!guard) { return; diff --git a/Telegram/SourceFiles/boxes/background_preview_box.cpp b/Telegram/SourceFiles/boxes/background_preview_box.cpp index 7cd0dda8a..9560f72a8 100644 --- a/Telegram/SourceFiles/boxes/background_preview_box.cpp +++ b/Telegram/SourceFiles/boxes/background_preview_box.cpp @@ -711,13 +711,11 @@ void BackgroundPreviewBox::checkLoadedDocument() { return; } const auto generateCallback = [=](QImage &&image) { - auto [left, right] = base::make_binary_guard(); - _generating = std::move(left); crl::async([ this, image = std::move(image), patternBackground = patternBackgroundColor(), - guard = std::move(right) + guard = _generating.make_guard() ]() mutable { auto scaled = PrepareScaledFromFull(image, patternBackground); const auto ms = crl::now(); diff --git a/Telegram/SourceFiles/boxes/language_box.cpp b/Telegram/SourceFiles/boxes/language_box.cpp index 8d9bcfaad..07e9b8dc2 100644 --- a/Telegram/SourceFiles/boxes/language_box.cpp +++ b/Telegram/SourceFiles/boxes/language_box.cpp @@ -1134,8 +1134,8 @@ base::binary_guard LanguageBox::Show() { const auto manager = Core::App().langCloudManager(); if (manager->languageList().empty()) { - auto guard = std::make_shared(); - std::tie(result, *guard) = base::make_binary_guard(); + auto guard = std::make_shared( + result.make_guard()); auto alive = std::make_shared>( std::make_unique()); **alive = manager->languageListChanged().add_subscription([=] { diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index cecfd003a..b1a7443f1 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -1555,12 +1555,12 @@ base::binary_guard ReadImageAsync( not_null document, FnMut postprocess, FnMut done) { - auto [left, right] = base::make_binary_guard(); + auto result = base::binary_guard(); crl::async([ bytes = document->data(), path = document->filepath(), postprocess = std::move(postprocess), - guard = std::move(left), + guard = result.make_guard(), callback = std::move(done) ]() mutable { auto format = QByteArray(); @@ -1584,7 +1584,7 @@ base::binary_guard ReadImageAsync( callback(std::move(image)); }); }); - return std::move(right); + return result; } //void HandleUnsupportedMedia( diff --git a/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp b/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp index e350a720f..c8291f997 100644 --- a/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp +++ b/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp @@ -138,10 +138,7 @@ void GoodThumbSource::load( if (loading() || _empty) { return; } - auto [left, right] = base::make_binary_guard(); - _loading = std::move(left); - - auto callback = [=, guard = std::move(right)]( + auto callback = [=, guard = _loading.make_guard()]( QByteArray &&value) mutable { if (value.isEmpty()) { crl::on_main([=, guard = std::move(guard)]() mutable { diff --git a/Telegram/SourceFiles/storage/cache/storage_cache_database_object.cpp b/Telegram/SourceFiles/storage/cache/storage_cache_database_object.cpp index 781b7146b..96fa5bd58 100644 --- a/Telegram/SourceFiles/storage/cache/storage_cache_database_object.cpp +++ b/Telegram/SourceFiles/storage/cache/storage_cache_database_object.cpp @@ -1160,8 +1160,6 @@ void DatabaseObject::writeBundles() { } void DatabaseObject::createCleaner() { - auto [left, right] = base::make_binary_guard(); - _cleaner.guard = std::move(left); auto done = [weak = _weak](Error error) { weak.with([=](DatabaseObject &that) { that.cleanerDone(error); @@ -1169,7 +1167,7 @@ void DatabaseObject::createCleaner() { }; _cleaner.object = std::make_unique( _base, - std::move(right), + _cleaner.guard.make_guard(), std::move(done)); pushStatsDelayed(); } @@ -1196,11 +1194,9 @@ void DatabaseObject::checkCompactor() { info.till = _binlog.size(); info.systemTime = _time.system; info.keysCount = _map.size(); - auto [first, second] = base::make_binary_guard(); - _compactor.guard = std::move(first); _compactor.object = std::make_unique( _weak, - std::move(second), + _compactor.guard.make_guard(), _path, _settings, base::duplicate(_key), diff --git a/Telegram/SourceFiles/storage/file_download.cpp b/Telegram/SourceFiles/storage/file_download.cpp index 2d6cd3514..5f4e8695c 100644 --- a/Telegram/SourceFiles/storage/file_download.cpp +++ b/Telegram/SourceFiles/storage/file_download.cpp @@ -421,9 +421,7 @@ void FileLoader::start(bool loadFirst, bool prior) { void FileLoader::loadLocal(const Storage::Cache::Key &key) { const auto readImage = (_locationType != AudioFileLocation); - auto [first, second] = base::make_binary_guard(); - _localLoading = std::move(first); - auto done = [=, guard = std::move(second)]( + auto done = [=, guard = _localLoading.make_guard()]( QByteArray &&value, QImage &&image, QByteArray &&format) mutable { diff --git a/Telegram/SourceFiles/storage/storage_databases.cpp b/Telegram/SourceFiles/storage/storage_databases.cpp index f0fc1a565..83bd1cd9e 100644 --- a/Telegram/SourceFiles/storage/storage_databases.cpp +++ b/Telegram/SourceFiles/storage/storage_databases.cpp @@ -88,10 +88,11 @@ void Databases::destroy(Cache::Database *database) { auto &kept = entry.second; if (kept.database.get() == database) { Assert(!kept.destroying.alive()); - auto [first, second] = base::make_binary_guard(); - kept.destroying = std::move(first); database->close(); - database->waitForCleaner([=, guard = std::move(second)]() mutable { + database->waitForCleaner([ + =, + guard = kept.destroying.make_guard() + ]() mutable { crl::on_main(std::move(guard), [=] { _map.erase(path); }); diff --git a/Telegram/SourceFiles/support/support_templates.cpp b/Telegram/SourceFiles/support/support_templates.cpp index b20bd565d..deff385e0 100644 --- a/Telegram/SourceFiles/support/support_templates.cpp +++ b/Telegram/SourceFiles/support/support_templates.cpp @@ -481,9 +481,7 @@ void Templates::load() { return; } - auto[left, right] = base::make_binary_guard(); - _reading = std::move(left); - crl::async([=, guard = std::move(right)]() mutable { + crl::async([=, guard = _reading.make_guard()]() mutable { auto result = ReadFiles(cWorkingDir() + "TEMPLATES"); result.index = ComputeIndex(result.result); crl::on_main(std::move(guard), [ diff --git a/Telegram/SourceFiles/ui/emoji_config.cpp b/Telegram/SourceFiles/ui/emoji_config.cpp index 0f57c568c..873a1f816 100644 --- a/Telegram/SourceFiles/ui/emoji_config.cpp +++ b/Telegram/SourceFiles/ui/emoji_config.cpp @@ -940,12 +940,10 @@ void Instance::generateCache() { const auto size = _size; const auto index = _sprites.size(); - auto [left, right] = base::make_binary_guard(); - _generating = std::move(left); crl::async([ =, universal = Universal, - guard = std::move(right) + guard = _generating.make_guard() ]() mutable { crl::on_main(std::move(guard), [ =, diff --git a/Telegram/SourceFiles/window/notifications_manager_default.cpp b/Telegram/SourceFiles/window/notifications_manager_default.cpp index 95d4b2a15..6acaeaf00 100644 --- a/Telegram/SourceFiles/window/notifications_manager_default.cpp +++ b/Telegram/SourceFiles/window/notifications_manager_default.cpp @@ -408,13 +408,14 @@ void Widget::step_shift(float64 ms, bool timer) { void Widget::hideSlow() { if (anim::Disabled()) { _hiding = true; - auto [left, right] = base::make_binary_guard(); - _hidingDelayed = std::move(left); - App::CallDelayed(st::notifySlowHide, this, [=, guard = std::move(right)] { - if (guard && _hiding) { - hideFast(); - } - }); + App::CallDelayed( + st::notifySlowHide, + this, + [=, guard = _hidingDelayed.make_guard()] { + if (guard && _hiding) { + hideFast(); + } + }); } else { hideAnimated(st::notifySlowHide, anim::easeInCirc); }