From 8e0157820747506d192d75f516e92c9f28524c45 Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Wed, 28 Dec 2011 16:13:14 +0300 Subject: [PATCH] [ios][mac] Added implementations for Platform::RunOnGuiThread and Platform::RunAsync @see #486 --- android/jni/Android.mk | 1 - .../com/mapswithme/core/concurrent_runner.cpp | 21 ------ platform/concurrent_runner.hpp | 23 ------- platform/ios_concurrent_runner.mm | 67 ------------------- platform/platform.hpp | 11 ++- platform/platform.pro | 3 - platform/platform_android.cpp | 12 ++++ platform/platform_ios.mm | 25 +++++++ platform/platform_linux.cpp | 12 ++++ platform/platform_mac.mm | 25 +++++++ .../platform_tests/concurrent_runner_test.cpp | 41 ------------ platform/platform_tests/platform_tests.pro | 1 - platform/platform_win.cpp | 12 ++++ platform/qt_concurrent_runner.cpp | 29 -------- storage/storage.cpp | 2 +- 15 files changed, 96 insertions(+), 189 deletions(-) delete mode 100644 android/jni/com/mapswithme/core/concurrent_runner.cpp delete mode 100644 platform/concurrent_runner.hpp delete mode 100644 platform/ios_concurrent_runner.mm delete mode 100644 platform/platform_tests/concurrent_runner_test.cpp delete mode 100644 platform/qt_concurrent_runner.cpp diff --git a/android/jni/Android.mk b/android/jni/Android.mk index a7455d8812..15c87895d6 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -25,7 +25,6 @@ LOCAL_HEADER_FILES := \ nv_event/scoped_profiler.hpp LOCAL_SRC_FILES := \ - com/mapswithme/core/concurrent_runner.cpp \ com/mapswithme/core/jni_helper.cpp \ com/mapswithme/core/jni_string.cpp \ com/mapswithme/core/logging.cpp \ diff --git a/android/jni/com/mapswithme/core/concurrent_runner.cpp b/android/jni/com/mapswithme/core/concurrent_runner.cpp deleted file mode 100644 index 2d05f7c935..0000000000 --- a/android/jni/com/mapswithme/core/concurrent_runner.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "../../../../../platform/concurrent_runner.hpp" - -namespace threads -{ - ConcurrentRunner::ConcurrentRunner() - { - } - - ConcurrentRunner::~ConcurrentRunner() - { - } - - void ConcurrentRunner::Run(RunnerFuncT const & f) const - { - } - - void ConcurrentRunner::Join() - { - } -} - diff --git a/platform/concurrent_runner.hpp b/platform/concurrent_runner.hpp deleted file mode 100644 index c85691853d..0000000000 --- a/platform/concurrent_runner.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "../base/runner.hpp" - -namespace threads -{ - -/// @note All current implementations use one shared system pool -class ConcurrentRunner : public IRunner -{ - class Impl; - Impl * m_pImpl; - -public: - ConcurrentRunner(); - virtual ~ConcurrentRunner(); - virtual void Run(RunnerFuncT const & f) const; - -protected: - virtual void Join(); -}; - -} diff --git a/platform/ios_concurrent_runner.mm b/platform/ios_concurrent_runner.mm deleted file mode 100644 index d82432a970..0000000000 --- a/platform/ios_concurrent_runner.mm +++ /dev/null @@ -1,67 +0,0 @@ -#include "concurrent_runner.hpp" - -#include "../base/assert.hpp" -#include "../base/macros.hpp" -#include "../base/logging.hpp" - -#include "../std/scoped_ptr.hpp" - -#include - -namespace threads -{ - class ConcurrentRunner::Impl - { - public: - dispatch_group_t m_group; - - Impl() - { - m_group = dispatch_group_create(); - } - ~Impl() - { - dispatch_release(m_group); - } - - }; - - ConcurrentRunner::ConcurrentRunner() : m_pImpl(new ConcurrentRunner::Impl) - { - } - - ConcurrentRunner::~ConcurrentRunner() - { - delete m_pImpl; - } - - // work-around for runtime boost exception, see - // http://stackoverflow.com/questions/5438613/why-cant-i-use-a-boostfunction-in-an-objective-c-block - struct BoostExceptionFixer - { - RunnerFuncT m_f; - BoostExceptionFixer(RunnerFuncT const & f) : m_f(f) {} - - void operator()() const - { - scoped_ptr scopedThis; - UNUSED_VALUE(scopedThis); - - IRunner::CallAndCatchAll(m_f); - } - }; - - void ConcurrentRunner::Run(RunnerFuncT const & f) const - { - dispatch_queue_t globalQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); - BoostExceptionFixer * tmp = new BoostExceptionFixer(f); - dispatch_group_async(m_pImpl->m_group, globalQ, ^{ - (*tmp)(); - }); - } - - void ConcurrentRunner::Join() - { - dispatch_group_wait(m_pImpl->m_group, DISPATCH_TIME_FOREVER); - } -} diff --git a/platform/platform.hpp b/platform/platform.hpp index f8f3280ee4..a4c92a4dca 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -72,8 +72,15 @@ public: /// @name Functions for concurrent tasks. //@{ typedef function TFunctor; - inline void RunInGuiThread(TFunctor const & fn) { fn(); } - inline void RunAsync(TFunctor const & fn) { fn(); } + void RunOnGuiThread(TFunctor const & fn); + enum Priority + { + EPriorityBackground, + EPriorityLow, + EPriorityDefault, + EPriorityHigh + }; + void RunAsync(TFunctor const & fn, Priority p = EPriorityDefault); //@} int CpuCores() const; diff --git a/platform/platform.pro b/platform/platform.pro index 2b0da32c5c..a3a33970dc 100644 --- a/platform/platform.pro +++ b/platform/platform.pro @@ -15,7 +15,6 @@ include($$ROOT_DIR/common.pri) SOURCES += platform_qt.cpp \ wifi_location_service.cpp \ - qt_concurrent_runner.cpp \ location_service.cpp HEADERS += wifi_info.hpp \ location_service.hpp @@ -38,7 +37,6 @@ include($$ROOT_DIR/common.pri) } } else:iphone* { OBJECTIVE_SOURCES += ios_video_timer.mm \ - ios_concurrent_runner.mm \ platform_ios.mm } else:android* { SOURCES += platform_android.cpp \ @@ -55,7 +53,6 @@ macx*|iphone* { HEADERS += \ platform.hpp \ location.hpp \ - concurrent_runner.hpp \ preferred_languages.hpp \ settings.hpp \ video_timer.hpp \ diff --git a/platform/platform_android.cpp b/platform/platform_android.cpp index cd550735f5..a0f43fff85 100644 --- a/platform/platform_android.cpp +++ b/platform/platform_android.cpp @@ -167,3 +167,15 @@ bool Platform::IsFeatureSupported(string const & feature) const // @TODO add Search feature support return false; } + +void Platform::RunOnGuiThread(TFunctor const & fn) +{ + // @TODO + fn(); +} + +void Platform::RunAsync(TFunctor const & fn, Priority p) +{ + // @TODO + fn(); +} diff --git a/platform/platform_ios.mm b/platform/platform_ios.mm index 5b4daa3c0e..404a055d53 100644 --- a/platform/platform_ios.mm +++ b/platform/platform_ios.mm @@ -273,6 +273,31 @@ bool Platform::IsFeatureSupported(string const & feature) const return false; } +static void PerformImpl(void * obj) +{ + Platform::TFunctor * f = reinterpret_cast(obj); + (*f)(); + delete f; +} + +void Platform::RunOnGuiThread(TFunctor const & fn) +{ + dispatch_async_f(dispatch_get_main_queue(), new TFunctor(fn), &PerformImpl); +} + +void Platform::RunAsync(TFunctor const & fn, Priority p) +{ + int priority = DISPATCH_QUEUE_PRIORITY_DEFAULT; + switch (p) + { + case EPriorityBackground: priority = DISPATCH_QUEUE_PRIORITY_BACKGROUND; break; + case EPriorityDefault: priority = DISPATCH_QUEUE_PRIORITY_DEFAULT; break; + case EPriorityHigh: priority = DISPATCH_QUEUE_PRIORITY_HIGH; break; + case EPriorityLow: priority = DISPATCH_QUEUE_PRIORITY_LOW; break; + } + dispatch_async_f(dispatch_get_global_queue(priority, 0), new TFunctor(fn), &PerformImpl); +} + //////////////////////////////////////////////////////////////////////// extern "C" Platform & GetPlatform() { diff --git a/platform/platform_linux.cpp b/platform/platform_linux.cpp index 54f21a49a9..9cfebaa4b5 100644 --- a/platform/platform_linux.cpp +++ b/platform/platform_linux.cpp @@ -68,3 +68,15 @@ string Platform::UniqueClientId() const { return "@TODO"; } + +void Platform::RunOnGuiThread(TFunctor const & fn) +{ + // @TODO + fn(); +} + +void Platform::RunAsync(TFunctor const & fn, Priority p) +{ + // @TODO + fn(); +} diff --git a/platform/platform_mac.mm b/platform/platform_mac.mm index ef945a132e..7f2c4c0f0a 100644 --- a/platform/platform_mac.mm +++ b/platform/platform_mac.mm @@ -102,3 +102,28 @@ string Platform::UniqueClientId() const // and use base64 encoding return base64::encode(xoredHash); } + +static void PerformImpl(void * obj) +{ + Platform::TFunctor * f = reinterpret_cast(obj); + (*f)(); + delete f; +} + +void Platform::RunOnGuiThread(TFunctor const & fn) +{ + dispatch_async_f(dispatch_get_main_queue(), new TFunctor(fn), &PerformImpl); +} + +void Platform::RunAsync(TFunctor const & fn, Priority p) +{ + int priority = DISPATCH_QUEUE_PRIORITY_DEFAULT; + switch (p) + { + case EPriorityBackground: priority = DISPATCH_QUEUE_PRIORITY_BACKGROUND; break; + case EPriorityDefault: priority = DISPATCH_QUEUE_PRIORITY_DEFAULT; break; + case EPriorityHigh: priority = DISPATCH_QUEUE_PRIORITY_HIGH; break; + case EPriorityLow: priority = DISPATCH_QUEUE_PRIORITY_LOW; break; + } + dispatch_async_f(dispatch_get_global_queue(priority, 0), new TFunctor(fn), &PerformImpl); +} diff --git a/platform/platform_tests/concurrent_runner_test.cpp b/platform/platform_tests/concurrent_runner_test.cpp deleted file mode 100644 index d992296c58..0000000000 --- a/platform/platform_tests/concurrent_runner_test.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "../../testing/testing.hpp" - -#include "../concurrent_runner.hpp" -#include "../platform.hpp" - -#include "../../base/logging.hpp" -#include "../../base/mutex.hpp" - -#include "../../std/bind.hpp" - -namespace -{ - -class ConcurrentRunnerForTest : public threads::ConcurrentRunner -{ -public: - using threads::ConcurrentRunner::Join; -}; - -} - -int globalCounter = 0; - -threads::Mutex m; - -void f() -{ - threads::MutexGuard g(m); - ++globalCounter; -} - -static const int MAX_THREADS = 20; - -UNIT_TEST(ConcurrentRunnerSmoke) -{ - ConcurrentRunnerForTest r; - for (int i = 0; i < MAX_THREADS; ++i) - r.Run(&f); - r.Join(); - TEST_EQUAL(globalCounter, MAX_THREADS, ()); -} diff --git a/platform/platform_tests/platform_tests.pro b/platform/platform_tests/platform_tests.pro index 40d89c3373..29bee1215e 100644 --- a/platform/platform_tests/platform_tests.pro +++ b/platform/platform_tests/platform_tests.pro @@ -30,7 +30,6 @@ SOURCES += \ ../../testing/testingmain.cpp \ platform_test.cpp \ jansson_test.cpp \ - concurrent_runner_test.cpp \ language_test.cpp \ downloader_test.cpp \ video_timer_test.cpp \ diff --git a/platform/platform_win.cpp b/platform/platform_win.cpp index b8e390209b..62934716b2 100644 --- a/platform/platform_win.cpp +++ b/platform/platform_win.cpp @@ -100,3 +100,15 @@ string Platform::UniqueClientId() const { return "@TODO"; } + +void Platform::RunOnGuiThread(TFunctor const & fn) +{ + // @TODO + fn(); +} + +void Platform::RunAsync(TFunctor const & fn, Priority p) +{ + // @TODO + fn(); +} diff --git a/platform/qt_concurrent_runner.cpp b/platform/qt_concurrent_runner.cpp deleted file mode 100644 index 55d637531f..0000000000 --- a/platform/qt_concurrent_runner.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "concurrent_runner.hpp" - -#include "../std/bind.hpp" - -#include -#include - -namespace threads -{ - -ConcurrentRunner::ConcurrentRunner() -{ -} - -ConcurrentRunner::~ConcurrentRunner() -{ -} - -void ConcurrentRunner::Run(RunnerFuncT const & f) const -{ - QtConcurrent::run(bind(&IRunner::CallAndCatchAll, f)); -} - -void ConcurrentRunner::Join() -{ - QThreadPool::globalInstance()->waitForDone(); -} - -} // namespace threads diff --git a/storage/storage.cpp b/storage/storage.cpp index fde3caafdc..889cf4acb9 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -369,7 +369,7 @@ namespace storage { if (indexer::BuildSearchIndexFromDatFile(fName)) { - GetPlatform().RunInGuiThread(bind(&Storage::UpdateAfterSearchIndex, this, cref(fName))); + GetPlatform().RunOnGuiThread(bind(&Storage::UpdateAfterSearchIndex, this, cref(fName))); } else {