axmol/core/base/CCAsyncTaskPool.h

232 lines
7.0 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-01-04 12:36:20 +08:00
https://adxeproject.github.io/
2019-11-23 20:27:39 +08:00
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.
****************************************************************************/
#ifndef __CCSYNC_TASK_POOL_H_
#define __CCSYNC_TASK_POOL_H_
#include "platform/CCPlatformMacros.h"
#include "base/CCDirector.h"
#include "base/CCScheduler.h"
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>
/**
2021-12-25 10:04:45 +08:00
* @addtogroup base
* @{
*/
2019-11-23 20:27:39 +08:00
NS_CC_BEGIN
/**
* @class AsyncTaskPool
* @brief This class allows to perform background operations without having to manipulate threads.
* @js NA
*/
class CC_DLL AsyncTaskPool
{
public:
typedef std::function<void(void*)> TaskCallBack;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
enum class TaskType
{
TASK_IO,
TASK_NETWORK,
TASK_OTHER,
TASK_MAX_TYPE,
};
/**
* Returns the shared instance of the async task pool.
*/
static AsyncTaskPool* getInstance();
/**
* Destroys the async task pool.
*/
static void destroyInstance();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Stop tasks.
*
* @param type Task type you want to stop.
*/
void stopTasks(TaskType type);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Enqueue a asynchronous task.
*
* @param type task type is io task, network task or others, each type of task has a thread to deal with it.
2021-12-25 10:04:45 +08:00
* @param callback callback when the task is finished. The callback is called in the main thread instead of task
* thread.
2019-11-23 20:27:39 +08:00
* @param callbackParam parameter used by the callback.
* @param task: task can be lambda function to be performed off thread.
* @lua NA
*/
void enqueue(TaskType type, TaskCallBack callback, void* callbackParam, std::function<void()> task);
/**
2021-12-25 10:04:45 +08:00
* Enqueue a asynchronous task.
*
* @param type task type is io task, network task or others, each type of task has a thread to deal with it.
* @param task: task can be lambda function to be performed off thread.
* @lua NA
*/
2019-11-23 20:27:39 +08:00
void enqueue(AsyncTaskPool::TaskType type, std::function<void()> task);
2021-12-25 10:04:45 +08:00
CC_CONSTRUCTOR_ACCESS : AsyncTaskPool();
2019-11-23 20:27:39 +08:00
~AsyncTaskPool();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
// thread tasks internally used
2021-12-25 10:04:45 +08:00
class ThreadTasks
{
2019-11-23 20:27:39 +08:00
struct AsyncTaskCallBack
{
2021-12-25 10:04:45 +08:00
TaskCallBack callback;
void* callbackParam;
2019-11-23 20:27:39 +08:00
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
2021-12-25 10:04:45 +08:00
ThreadTasks() : _stop(false)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_thread = std::thread([this] {
for (;;)
{
std::function<void()> task;
AsyncTaskCallBack callback;
{
std::unique_lock<std::mutex> lock(this->_queueMutex);
this->_condition.wait(lock, [this] { return this->_stop || !this->_tasks.empty(); });
if (this->_stop && this->_tasks.empty())
return;
task = std::move(this->_tasks.front());
callback = std::move(this->_taskCallBacks.front());
this->_tasks.pop();
this->_taskCallBacks.pop();
}
task();
Director::getInstance()->getScheduler()->performFunctionInCocosThread(
std::bind(callback.callback, callback.callbackParam));
}
});
2019-11-23 20:27:39 +08:00
}
~ThreadTasks()
{
{
std::unique_lock<std::mutex> lock(_queueMutex);
_stop = true;
2021-12-25 10:04:45 +08:00
while (_tasks.size())
2019-11-23 20:27:39 +08:00
_tasks.pop();
while (_taskCallBacks.size())
_taskCallBacks.pop();
}
_condition.notify_all();
_thread.join();
}
void clear()
{
std::unique_lock<std::mutex> lock(_queueMutex);
2021-12-25 10:04:45 +08:00
while (_tasks.size())
2019-11-23 20:27:39 +08:00
_tasks.pop();
while (_taskCallBacks.size())
_taskCallBacks.pop();
}
void enqueue(TaskCallBack callback, void* callbackParam, std::function<void()> task)
{
AsyncTaskCallBack taskCallBack;
2021-12-25 10:04:45 +08:00
taskCallBack.callback = std::move(callback);
2019-11-23 20:27:39 +08:00
taskCallBack.callbackParam = callbackParam;
{
std::unique_lock<std::mutex> lock(_queueMutex);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// don't allow enqueueing after stopping the pool
2021-12-25 10:04:45 +08:00
if (_stop)
2019-11-23 20:27:39 +08:00
{
CC_ASSERT(0 && "already stop");
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_tasks.push(std::move(task));
_taskCallBacks.push(std::move(taskCallBack));
}
_condition.notify_one();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
private:
// need to keep track of thread so we can join them
std::thread _thread;
// the task queue
2021-12-25 10:04:45 +08:00
std::queue<std::function<void()>> _tasks;
2019-11-23 20:27:39 +08:00
std::queue<AsyncTaskCallBack> _taskCallBacks;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// synchronization
std::mutex _queueMutex;
std::condition_variable _condition;
bool _stop;
};
2021-12-25 10:04:45 +08:00
// tasks
2019-11-23 20:27:39 +08:00
ThreadTasks _threadTasks[int(TaskType::TASK_MAX_TYPE)];
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
static AsyncTaskPool* s_asyncTaskPool;
};
inline void AsyncTaskPool::stopTasks(TaskType type)
{
auto& threadTask = _threadTasks[(int)type];
threadTask.clear();
}
2021-12-25 10:04:45 +08:00
inline void AsyncTaskPool::enqueue(AsyncTaskPool::TaskType type,
TaskCallBack callback,
void* callbackParam,
std::function<void()> task)
2019-11-23 20:27:39 +08:00
{
auto& threadTask = _threadTasks[(int)type];
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
threadTask.enqueue(std::move(callback), callbackParam, std::move(task));
}
inline void AsyncTaskPool::enqueue(AsyncTaskPool::TaskType type, std::function<void()> task)
{
2021-12-25 10:04:45 +08:00
enqueue(
type, [](void*) {}, nullptr, std::move(task));
2019-11-23 20:27:39 +08:00
}
NS_CC_END
// end group
/// @}
2021-12-25 10:04:45 +08:00
#endif //__CCSYNC_TASK_POOL_H_