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();
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<bool> *_bothAlive = nullptr;
friend std::pair<binary_guard, binary_guard> 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<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);
inline binary_guard binary_guard::make_guard() {
destroy();
auto result = binary_guard();
_bothAlive = result._bothAlive = new std::atomic<bool>(true);
return result;
}

View file

@ -268,7 +268,6 @@ ConcurrentTimer::ConcurrentTimer(
}
Fn<void()> ConcurrentTimer::createAdjuster() {
auto guards = base::make_binary_guard();
_guard = std::make_shared<bool>(true);
return [=, runner = _runner, guard = std::weak_ptr<bool>(_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;

View file

@ -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();

View file

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

View file

@ -1555,12 +1555,12 @@ base::binary_guard ReadImageAsync(
not_null<DocumentData*> document,
FnMut<QImage(QImage)> postprocess,
FnMut<void(QImage&&)> 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(

View file

@ -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 {

View file

@ -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<Cleaner>(
_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<Compactor>(
_weak,
std::move(second),
_compactor.guard.make_guard(),
_path,
_settings,
base::duplicate(_key),

View file

@ -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 {

View file

@ -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);
});

View file

@ -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), [

View file

@ -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), [
=,

View file

@ -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);
}