Remove AsyncTask

This commit is contained in:
Nomango 2020-02-20 19:10:52 +08:00
parent f71c5b9ff8
commit ceb5e3b260
5 changed files with 0 additions and 182 deletions

View File

@ -44,7 +44,6 @@
<ClInclude Include="..\..\src\kiwano\2d\Sprite.h" />
<ClInclude Include="..\..\src\kiwano\2d\TextActor.h" />
<ClInclude Include="..\..\src\kiwano\2d\Transition.h" />
<ClInclude Include="..\..\src\kiwano\core\AsyncTask.h" />
<ClInclude Include="..\..\src\kiwano\core\Module.h" />
<ClInclude Include="..\..\src\kiwano\core\EventDispatcher.h" />
<ClInclude Include="..\..\src\kiwano\core\EventListener.h" />
@ -122,7 +121,6 @@
<ClCompile Include="..\..\src\kiwano\2d\Sprite.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\TextActor.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\Transition.cpp" />
<ClCompile Include="..\..\src\kiwano\core\AsyncTask.cpp" />
<ClCompile Include="..\..\src\kiwano\core\Module.cpp" />
<ClCompile Include="..\..\src\kiwano\core\Director.cpp" />
<ClCompile Include="..\..\src\kiwano\core\EventDispatcher.cpp" />

View File

@ -75,9 +75,6 @@
<ClInclude Include="..\..\src\kiwano\core\TimerManager.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\core\AsyncTask.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\2d\GifSprite.h">
<Filter>2d</Filter>
</ClInclude>
@ -350,9 +347,6 @@
<ClCompile Include="..\..\src\kiwano\core\TimerManager.cpp">
<Filter>core</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\core\AsyncTask.cpp">
<Filter>core</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\2d\GifSprite.cpp">
<Filter>2d</Filter>
</ClCompile>

View File

@ -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 <kiwano/core/AsyncTask.h>
#include <kiwano/platform/Application.h>
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<std::mutex> 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

View File

@ -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 <kiwano/core/ObjectBase.h>
#include <mutex>
#include <thread>
namespace kiwano
{
KGE_DECLARE_SMART_PTR(AsyncTask);
typedef Function<void()> AsyncTaskFunc;
typedef Function<void()> 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<AsyncTaskFunc> thread_func_queue_;
AsyncTaskCallback thread_cb_;
};
} // namespace kiwano

View File

@ -43,7 +43,6 @@
// core
//
#include <kiwano/core/AsyncTask.h>
#include <kiwano/core/Common.h>
#include <kiwano/core/Director.h>
#include <kiwano/core/EventDispatcher.h>