Remove AsyncTask
This commit is contained in:
parent
f71c5b9ff8
commit
ceb5e3b260
|
|
@ -44,7 +44,6 @@
|
||||||
<ClInclude Include="..\..\src\kiwano\2d\Sprite.h" />
|
<ClInclude Include="..\..\src\kiwano\2d\Sprite.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\2d\TextActor.h" />
|
<ClInclude Include="..\..\src\kiwano\2d\TextActor.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\2d\Transition.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\Module.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\core\EventDispatcher.h" />
|
<ClInclude Include="..\..\src\kiwano\core\EventDispatcher.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\core\EventListener.h" />
|
<ClInclude Include="..\..\src\kiwano\core\EventListener.h" />
|
||||||
|
|
@ -122,7 +121,6 @@
|
||||||
<ClCompile Include="..\..\src\kiwano\2d\Sprite.cpp" />
|
<ClCompile Include="..\..\src\kiwano\2d\Sprite.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\2d\TextActor.cpp" />
|
<ClCompile Include="..\..\src\kiwano\2d\TextActor.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\2d\Transition.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\Module.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\core\Director.cpp" />
|
<ClCompile Include="..\..\src\kiwano\core\Director.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\core\EventDispatcher.cpp" />
|
<ClCompile Include="..\..\src\kiwano\core\EventDispatcher.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -75,9 +75,6 @@
|
||||||
<ClInclude Include="..\..\src\kiwano\core\TimerManager.h">
|
<ClInclude Include="..\..\src\kiwano\core\TimerManager.h">
|
||||||
<Filter>core</Filter>
|
<Filter>core</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\src\kiwano\core\AsyncTask.h">
|
|
||||||
<Filter>core</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\src\kiwano\2d\GifSprite.h">
|
<ClInclude Include="..\..\src\kiwano\2d\GifSprite.h">
|
||||||
<Filter>2d</Filter>
|
<Filter>2d</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
|
@ -350,9 +347,6 @@
|
||||||
<ClCompile Include="..\..\src\kiwano\core\TimerManager.cpp">
|
<ClCompile Include="..\..\src\kiwano\core\TimerManager.cpp">
|
||||||
<Filter>core</Filter>
|
<Filter>core</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="..\..\src\kiwano\core\AsyncTask.cpp">
|
|
||||||
<Filter>core</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="..\..\src\kiwano\2d\GifSprite.cpp">
|
<ClCompile Include="..\..\src\kiwano\2d\GifSprite.cpp">
|
||||||
<Filter>2d</Filter>
|
<Filter>2d</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -43,7 +43,6 @@
|
||||||
// core
|
// core
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <kiwano/core/AsyncTask.h>
|
|
||||||
#include <kiwano/core/Common.h>
|
#include <kiwano/core/Common.h>
|
||||||
#include <kiwano/core/Director.h>
|
#include <kiwano/core/Director.h>
|
||||||
#include <kiwano/core/EventDispatcher.h>
|
#include <kiwano/core/EventDispatcher.h>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue