From ceb5e3b2605716e7241895579eca2c698c4248ea Mon Sep 17 00:00:00 2001 From: Nomango Date: Thu, 20 Feb 2020 19:10:52 +0800 Subject: [PATCH] Remove AsyncTask --- projects/kiwano/kiwano.vcxproj | 2 - projects/kiwano/kiwano.vcxproj.filters | 6 -- src/kiwano/core/AsyncTask.cpp | 94 -------------------------- src/kiwano/core/AsyncTask.h | 79 ---------------------- src/kiwano/kiwano.h | 1 - 5 files changed, 182 deletions(-) delete mode 100644 src/kiwano/core/AsyncTask.cpp delete mode 100644 src/kiwano/core/AsyncTask.h diff --git a/projects/kiwano/kiwano.vcxproj b/projects/kiwano/kiwano.vcxproj index 63ae8b0c..944b57a6 100644 --- a/projects/kiwano/kiwano.vcxproj +++ b/projects/kiwano/kiwano.vcxproj @@ -44,7 +44,6 @@ - @@ -122,7 +121,6 @@ - diff --git a/projects/kiwano/kiwano.vcxproj.filters b/projects/kiwano/kiwano.vcxproj.filters index cc64a0da..83497999 100644 --- a/projects/kiwano/kiwano.vcxproj.filters +++ b/projects/kiwano/kiwano.vcxproj.filters @@ -75,9 +75,6 @@ core - - core - 2d @@ -350,9 +347,6 @@ core - - core - 2d diff --git a/src/kiwano/core/AsyncTask.cpp b/src/kiwano/core/AsyncTask.cpp deleted file mode 100644 index b144cd9a..00000000 --- a/src/kiwano/core/AsyncTask.cpp +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright (c) 2016-2018 Kiwano - Nomango -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include -#include - -namespace kiwano -{ - -AsyncTaskPtr AsyncTask::Create(AsyncTaskFunc func) -{ - AsyncTaskPtr ptr = new (std::nothrow) AsyncTask; - if (ptr) - { - ptr->Then(func); - } - return ptr; -} - -AsyncTask::AsyncTask() - : thread_(Closure(this, &AsyncTask::TaskThread)) -{ -} - -AsyncTask::~AsyncTask() {} - -void AsyncTask::Start() -{ - thread_.detach(); - - // retain this object until finished - Retain(); -} - -AsyncTask& AsyncTask::Then(AsyncTaskFunc func) -{ - std::lock_guard lock(func_mutex_); - thread_func_queue_.push(func); - return (*this); -} - -AsyncTask& AsyncTask::SetCallback(AsyncTaskCallback callback) -{ - thread_cb_ = callback; - return (*this); -} - -void AsyncTask::TaskThread() -{ - while (!thread_func_queue_.empty()) - { - auto& func = thread_func_queue_.front(); - if (func) - { - func(); - } - - func_mutex_.lock(); - thread_func_queue_.pop(); - func_mutex_.unlock(); - } - - Application::GetInstance().PreformInMainThread(Closure(this, &AsyncTask::Complete)); -} - -void AsyncTask::Complete() -{ - if (thread_cb_) - { - thread_cb_(); - } - - // Release this object - Release(); -} - -} // namespace kiwano diff --git a/src/kiwano/core/AsyncTask.h b/src/kiwano/core/AsyncTask.h deleted file mode 100644 index fb10688b..00000000 --- a/src/kiwano/core/AsyncTask.h +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) 2016-2018 Kiwano - Nomango -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#pragma once -#include -#include -#include - -namespace kiwano -{ -KGE_DECLARE_SMART_PTR(AsyncTask); - -typedef Function AsyncTaskFunc; -typedef Function AsyncTaskCallback; - -/// \~chinese -/// @brief 异步任务 -/// @details 在多线程下执行任务并返回 -/// @code -/// AsyncTaskPtr task = new AsyncTask; -/// task->Then(DoSomething); -/// task->Start(); -/// @endcode -class KGE_API AsyncTask : public ObjectBase -{ -public: - /// \~chinese - /// @brief 创建异步任务 - /// @param func 异步回调函数 - static AsyncTaskPtr Create(AsyncTaskFunc func); - - /// \~chinese - /// @brief 构造异步任务 - AsyncTask(); - - virtual ~AsyncTask(); - - /// \~chinese - /// @brief 添加异步任务链 - AsyncTask& Then(AsyncTaskFunc func); - - /// \~chinese - /// @brief 设置任务执行完成后的回调函数 - /// @note 该函数在 Kiwano 主线程中执行 - AsyncTask& SetCallback(AsyncTaskCallback callback); - - /// \~chinese - /// @brief 启动异步任务 - void Start(); - -private: - void TaskThread(); - - void Complete(); - -private: - std::thread thread_; - std::mutex func_mutex_; - Queue thread_func_queue_; - AsyncTaskCallback thread_cb_; -}; -} // namespace kiwano diff --git a/src/kiwano/kiwano.h b/src/kiwano/kiwano.h index a43952c9..cc1877cf 100644 --- a/src/kiwano/kiwano.h +++ b/src/kiwano/kiwano.h @@ -43,7 +43,6 @@ // core // -#include #include #include #include