From b8204a317dd3dc88a2cc3fb04f89d35df636a3a7 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 17 Dec 2017 22:41:34 +0400 Subject: [PATCH] Testing crl (concurrency runtime library). --- .gitmodules | 3 + Telegram/SourceFiles/core/launcher.cpp | 13 ++- Telegram/SourceFiles/core/launcher.h | 1 + .../SourceFiles/core/main_queue_processor.cpp | 96 +++++++++++++++++++ .../SourceFiles/core/main_queue_processor.h | 40 ++++++++ Telegram/SourceFiles/mediaview.cpp | 4 +- Telegram/SourceFiles/stdafx.h | 1 + Telegram/ThirdParty/crl | 1 + Telegram/gyp/Telegram.gyp | 2 + Telegram/gyp/crl.gyp | 76 +++++++++++++++ Telegram/gyp/telegram_sources.txt | 2 + 11 files changed, 232 insertions(+), 7 deletions(-) create mode 100644 Telegram/SourceFiles/core/main_queue_processor.cpp create mode 100644 Telegram/SourceFiles/core/main_queue_processor.h create mode 160000 Telegram/ThirdParty/crl create mode 100644 Telegram/gyp/crl.gyp diff --git a/.gitmodules b/.gitmodules index 2887410586..9515436ee8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "Telegram/ThirdParty/Catch"] path = Telegram/ThirdParty/Catch url = https://github.com/philsquared/Catch +[submodule "Telegram/ThirdParty/crl"] + path = Telegram/ThirdParty/crl + url = https://github.com/telegramdesktop/crl.git diff --git a/Telegram/SourceFiles/core/launcher.cpp b/Telegram/SourceFiles/core/launcher.cpp index c059159764..bf21d14d87 100644 --- a/Telegram/SourceFiles/core/launcher.cpp +++ b/Telegram/SourceFiles/core/launcher.cpp @@ -23,6 +23,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "platform/platform_launcher.h" #include "platform/platform_specific.h" #include "core/crash_reports.h" +#include "core/main_queue_processor.h" #include "application.h" namespace Core { @@ -63,11 +64,7 @@ int Launcher::exec() { Logs::start(this); // must be started before Platform is started Platform::start(); // must be started before QApplication is created - auto result = 0; - { - Application app(this, _argc, _argv); - result = app.exec(); - } + auto result = executeApplication(); DEBUG_LOG(("Telegram finished, result: %1").arg(result)); @@ -240,4 +237,10 @@ void Launcher::processArguments() { gStartUrl = parseResult.value("--", QStringList()).join(QString()); } +int Launcher::executeApplication() { + MainQueueProcessor processor; + Application app(this, _argc, _argv); + return app.exec(); +} + } // namespace Core diff --git a/Telegram/SourceFiles/core/launcher.h b/Telegram/SourceFiles/core/launcher.h index 2348d1646f..147e194bf2 100644 --- a/Telegram/SourceFiles/core/launcher.h +++ b/Telegram/SourceFiles/core/launcher.h @@ -58,6 +58,7 @@ private: virtual bool launchUpdater(UpdaterLaunch action) = 0; + int executeApplication(); int _argc; char **_argv; diff --git a/Telegram/SourceFiles/core/main_queue_processor.cpp b/Telegram/SourceFiles/core/main_queue_processor.cpp new file mode 100644 index 0000000000..b8024aba68 --- /dev/null +++ b/Telegram/SourceFiles/core/main_queue_processor.cpp @@ -0,0 +1,96 @@ +/* +This file is part of Telegram Desktop, +the official desktop version of Telegram messaging app, see https://telegram.org + +Telegram Desktop is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +It is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +In addition, as a special exception, the copyright holders give permission +to link the code of portions of this program with the OpenSSL library. + +Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE +Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org +*/ +#include "core/main_queue_processor.h" + +namespace Core { +namespace { + +QMutex ProcessorMutex; +MainQueueProcessor *ProcessorInstance/* = nullptr*/; + +constexpr auto kProcessorEvent = QEvent::Type(QEvent::User + 1); +static_assert(kProcessorEvent < QEvent::MaxUser); + +class ProcessorEvent : public QEvent { +public: + ProcessorEvent(void (*callable)(void*), void *argument); + + void process(); + +private: + void (*_callable)(void*) = nullptr; + void *_argument = nullptr; + +}; + +ProcessorEvent::ProcessorEvent(void (*callable)(void*), void *argument) +: QEvent(kProcessorEvent) +, _callable(callable) +, _argument(argument) { +} + +void ProcessorEvent::process() { + _callable(_argument); +} + +void ProcessMainQueue(void (*callable)(void*), void *argument) { + QMutexLocker lock(&ProcessorMutex); + + if (ProcessorInstance) { + const auto event = new ProcessorEvent(callable, argument); + QCoreApplication::postEvent(ProcessorInstance, event); + } +} + +} // namespace + +MainQueueProcessor::MainQueueProcessor() { + acquire(); + crl::init_main_queue(ProcessMainQueue); +} + +bool MainQueueProcessor::event(QEvent *event) { + if (event->type() == kProcessorEvent) { + static_cast(event)->process(); + return true; + } + return QObject::event(event); +} + +void MainQueueProcessor::acquire() { + Expects(ProcessorInstance == nullptr); + + QMutexLocker lock(&ProcessorMutex); + ProcessorInstance = this; +} + +void MainQueueProcessor::release() { + Expects(ProcessorInstance == this); + + QMutexLocker lock(&ProcessorMutex); + ProcessorInstance = nullptr; +} + +MainQueueProcessor::~MainQueueProcessor() { + release(); +} + +} // namespace diff --git a/Telegram/SourceFiles/core/main_queue_processor.h b/Telegram/SourceFiles/core/main_queue_processor.h new file mode 100644 index 0000000000..d7f978f8fd --- /dev/null +++ b/Telegram/SourceFiles/core/main_queue_processor.h @@ -0,0 +1,40 @@ +/* +This file is part of Telegram Desktop, +the official desktop version of Telegram messaging app, see https://telegram.org + +Telegram Desktop is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +It is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +In addition, as a special exception, the copyright holders give permission +to link the code of portions of this program with the OpenSSL library. + +Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE +Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org +*/ +#pragma once + +namespace Core { + +class MainQueueProcessor : public QObject { +public: + MainQueueProcessor(); + + ~MainQueueProcessor(); + +protected: + bool event(QEvent *event) override; + +private: + void acquire(); + void release(); + +}; + +} // namespace Core diff --git a/Telegram/SourceFiles/mediaview.cpp b/Telegram/SourceFiles/mediaview.cpp index 31d3321091..301a5e57f2 100644 --- a/Telegram/SourceFiles/mediaview.cpp +++ b/Telegram/SourceFiles/mediaview.cpp @@ -1596,9 +1596,9 @@ void MediaView::initThemePreview() { current.backgroundId = Window::Theme::Background()->id(); current.backgroundImage = Window::Theme::Background()->pixmap(); current.backgroundTiled = Window::Theme::Background()->tile(); - base::TaskQueue::Normal().Put([ready = std::move(ready), path, current]() mutable { + crl::async([=] { auto preview = Window::Theme::GeneratePreview(path, current); - base::TaskQueue::Main().Put([ready = std::move(ready), result = std::move(preview)]() mutable { + crl::on_main([ready, result = std::move(preview)]() mutable { ready(std::move(result)); }); }); diff --git a/Telegram/SourceFiles/stdafx.h b/Telegram/SourceFiles/stdafx.h index db9e8c25d2..36ad385b3c 100644 --- a/Telegram/SourceFiles/stdafx.h +++ b/Telegram/SourceFiles/stdafx.h @@ -77,6 +77,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include #include +#include #include "base/variant.h" #include "base/optional.h" diff --git a/Telegram/ThirdParty/crl b/Telegram/ThirdParty/crl new file mode 160000 index 0000000000..68d321c9a5 --- /dev/null +++ b/Telegram/ThirdParty/crl @@ -0,0 +1 @@ +Subproject commit 68d321c9a509256be86fa13097ec84a6c51b6a58 diff --git a/Telegram/gyp/Telegram.gyp b/Telegram/gyp/Telegram.gyp index 31480945b1..84a061531f 100644 --- a/Telegram/gyp/Telegram.gyp +++ b/Telegram/gyp/Telegram.gyp @@ -84,6 +84,7 @@ 'tests/tests.gyp:tests', 'utils.gyp:Updater', '../ThirdParty/libtgvoip/libtgvoip.gyp:libtgvoip', + 'crl.gyp:crl', ], 'defines': [ @@ -108,6 +109,7 @@ '<(emoji_suggestions_loc)', '<(submodules_loc)/GSL/include', '<(submodules_loc)/variant/include', + '<(submodules_loc)/crl/src', ], 'sources': [ '<@(qrc_files)', diff --git a/Telegram/gyp/crl.gyp b/Telegram/gyp/crl.gyp new file mode 100644 index 0000000000..2c975a4c20 --- /dev/null +++ b/Telegram/gyp/crl.gyp @@ -0,0 +1,76 @@ +# This file is part of Telegram Desktop, +# the official desktop version of Telegram messaging app, see https://telegram.org +# +# Telegram Desktop is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# It is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# In addition, as a special exception, the copyright holders give permission +# to link the code of portions of this program with the OpenSSL library. +# +# Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE +# Copyright (c) 2014 John Preston, https://desktop.telegram.org + +{ + 'includes': [ + 'common.gypi', + ], + 'targets': [{ + 'target_name': 'crl', + 'type': 'static_library', + 'dependencies': [], + 'includes': [ + 'common.gypi', + 'qt.gypi', + ], + 'defines': [ + ], + 'variables': { + 'crl_src_loc': '../ThirdParty/crl/src/crl', + 'official_build_target%': '', + }, + 'include_dirs': [ + '../ThirdParty/crl/src', + ], + 'sources': [ + '<(crl_src_loc)/common/crl_common_config.h', + '<(crl_src_loc)/common/crl_common_list.cpp', + '<(crl_src_loc)/common/crl_common_list.h', + '<(crl_src_loc)/common/crl_common_on_main.cpp', + '<(crl_src_loc)/common/crl_common_on_main.h', + '<(crl_src_loc)/common/crl_common_queue.cpp', + '<(crl_src_loc)/common/crl_common_queue.h', + '<(crl_src_loc)/common/crl_common_sync.h', + '<(crl_src_loc)/common/crl_common_utils.h', + '<(crl_src_loc)/dispatch/crl_dispatch_async.cpp', + '<(crl_src_loc)/dispatch/crl_dispatch_async.h', + '<(crl_src_loc)/dispatch/crl_dispatch_on_main.h', + '<(crl_src_loc)/dispatch/crl_dispatch_queue.cpp', + '<(crl_src_loc)/dispatch/crl_dispatch_queue.h', + '<(crl_src_loc)/dispatch/crl_dispatch_semaphore.cpp', + '<(crl_src_loc)/dispatch/crl_dispatch_semaphore.h', + '<(crl_src_loc)/qt/crl_qt_async.cpp', + '<(crl_src_loc)/qt/crl_qt_async.h', + '<(crl_src_loc)/qt/crl_qt_semaphore.cpp', + '<(crl_src_loc)/qt/crl_qt_semaphore.h', + '<(crl_src_loc)/winapi/crl_winapi_async.cpp', + '<(crl_src_loc)/winapi/crl_winapi_async.h', + '<(crl_src_loc)/winapi/crl_winapi_dll.h', + '<(crl_src_loc)/winapi/crl_winapi_list.cpp', + '<(crl_src_loc)/winapi/crl_winapi_list.h', + '<(crl_src_loc)/winapi/crl_winapi_semaphore.cpp', + '<(crl_src_loc)/winapi/crl_winapi_semaphore.h', + '<(crl_src_loc)/crl.h', + '<(crl_src_loc)/crl_async.h', + '<(crl_src_loc)/crl_on_main.h', + '<(crl_src_loc)/crl_queue.h', + '<(crl_src_loc)/crl_semaphore.h', + ], + }], +} diff --git a/Telegram/gyp/telegram_sources.txt b/Telegram/gyp/telegram_sources.txt index f8b7f59555..06a63c251f 100644 --- a/Telegram/gyp/telegram_sources.txt +++ b/Telegram/gyp/telegram_sources.txt @@ -149,6 +149,8 @@ <(src_loc)/core/file_utilities.h <(src_loc)/core/launcher.cpp <(src_loc)/core/launcher.h +<(src_loc)/core/main_queue_processor.cpp +<(src_loc)/core/main_queue_processor.h <(src_loc)/core/single_timer.cpp <(src_loc)/core/single_timer.h <(src_loc)/core/tl_help.h