2014-11-26 11:28:14 +08:00
|
|
|
/****************************************************************************
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2014-11-26 11:28:14 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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"
|
2014-12-23 10:12:37 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
2014-11-26 11:28:14 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <queue>
|
|
|
|
#include <memory>
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <future>
|
|
|
|
#include <functional>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2015-03-24 14:12:58 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup base
|
|
|
|
* @{
|
|
|
|
*/
|
2015-03-27 12:01:20 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-03-24 14:12:58 +08:00
|
|
|
|
2015-03-23 22:17:59 +08:00
|
|
|
/**
|
|
|
|
* @class AsyncTaskPool
|
|
|
|
* @brief This class allows to perform background operations without having to manipulate threads.
|
2015-03-28 15:11:51 +08:00
|
|
|
* @js NA
|
2015-03-23 22:17:59 +08:00
|
|
|
*/
|
2014-12-19 11:30:51 +08:00
|
|
|
class CC_DLL AsyncTaskPool
|
2014-11-26 11:28:14 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-12-04 18:31:10 +08:00
|
|
|
typedef std::function<void(void*)> TaskCallBack;
|
|
|
|
|
2014-11-26 11:28:14 +08:00
|
|
|
enum class TaskType
|
|
|
|
{
|
|
|
|
TASK_IO,
|
|
|
|
TASK_NETWORK,
|
2014-12-03 17:40:27 +08:00
|
|
|
TASK_OTHER,
|
2014-11-26 11:28:14 +08:00
|
|
|
TASK_MAX_TYPE,
|
|
|
|
};
|
2015-03-23 22:17:59 +08:00
|
|
|
|
2014-12-04 18:31:10 +08:00
|
|
|
/**
|
2015-03-23 22:17:59 +08:00
|
|
|
* Returns the shared instance of the async task pool.
|
2014-12-04 18:31:10 +08:00
|
|
|
*/
|
2014-11-26 11:28:14 +08:00
|
|
|
static AsyncTaskPool* getInstance();
|
2015-03-23 22:17:59 +08:00
|
|
|
|
2014-12-04 18:31:10 +08:00
|
|
|
/**
|
2015-03-23 22:17:59 +08:00
|
|
|
* Destroys the async task pool.
|
2014-12-04 18:31:10 +08:00
|
|
|
*/
|
2016-02-25 23:06:21 +08:00
|
|
|
static void destroyInstance();
|
|
|
|
|
|
|
|
/** @deprecated Use method destroyInstance() instead. */
|
|
|
|
CC_DEPRECATED_ATTRIBUTE static void destoryInstance() { return destroyInstance(); }
|
2014-11-26 11:28:14 +08:00
|
|
|
|
2014-12-19 10:22:02 +08:00
|
|
|
/**
|
2015-03-23 22:17:59 +08:00
|
|
|
* Stop tasks.
|
|
|
|
*
|
|
|
|
* @param type Task type you want to stop.
|
2014-12-19 10:22:02 +08:00
|
|
|
*/
|
|
|
|
void stopTasks(TaskType type);
|
|
|
|
|
2014-12-05 15:55:01 +08:00
|
|
|
/**
|
2015-03-23 22:17:59 +08:00
|
|
|
* Enqueue a asynchronous task.
|
|
|
|
*
|
2014-12-05 15:55:01 +08:00
|
|
|
* @param type task type is io task, network task or others, each type of task has a thread to deal with it.
|
2015-03-23 22:17:59 +08:00
|
|
|
* @param callback callback when the task is finished. The callback is called in the main thread instead of task thread.
|
|
|
|
* @param callbackParam parameter used by the callback.
|
2017-05-08 13:39:17 +08:00
|
|
|
* @param task: task can be lambda function to be performed off thread.
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua NA
|
2014-12-05 15:55:01 +08:00
|
|
|
*/
|
2017-05-08 13:39:17 +08:00
|
|
|
void enqueue(TaskType type, TaskCallBack callback, void* callbackParam, std::function<void()> task);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
void enqueue(AsyncTaskPool::TaskType type, std::function<void()> task);
|
2014-11-26 11:28:14 +08:00
|
|
|
|
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
|
|
|
AsyncTaskPool();
|
|
|
|
~AsyncTaskPool();
|
|
|
|
|
|
|
|
protected:
|
2014-12-04 18:31:10 +08:00
|
|
|
|
2014-11-26 11:28:14 +08:00
|
|
|
// thread tasks internally used
|
|
|
|
class ThreadTasks {
|
2014-12-23 10:12:37 +08:00
|
|
|
struct AsyncTaskCallBack
|
|
|
|
{
|
|
|
|
TaskCallBack callback;
|
|
|
|
void* callbackParam;
|
|
|
|
};
|
2014-11-26 11:28:14 +08:00
|
|
|
public:
|
|
|
|
ThreadTasks()
|
|
|
|
: _stop(false)
|
|
|
|
{
|
|
|
|
_thread = std::thread(
|
|
|
|
[this]
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
std::function<void()> task;
|
2014-12-04 18:31:10 +08:00
|
|
|
AsyncTaskCallBack callback;
|
2014-11-26 11:28:14 +08:00
|
|
|
{
|
2014-12-22 17:40:02 +08:00
|
|
|
std::unique_lock<std::mutex> lock(this->_queueMutex);
|
2014-11-26 11:28:14 +08:00
|
|
|
this->_condition.wait(lock,
|
|
|
|
[this]{ return this->_stop || !this->_tasks.empty(); });
|
|
|
|
if(this->_stop && this->_tasks.empty())
|
|
|
|
return;
|
|
|
|
task = std::move(this->_tasks.front());
|
2014-12-04 18:31:10 +08:00
|
|
|
callback = std::move(this->_taskCallBacks.front());
|
2014-11-26 11:28:14 +08:00
|
|
|
this->_tasks.pop();
|
2014-12-04 18:31:10 +08:00
|
|
|
this->_taskCallBacks.pop();
|
2014-11-26 11:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
task();
|
2017-05-08 13:39:17 +08:00
|
|
|
Director::getInstance()->getScheduler()->performFunctionInCocosThread(std::bind(callback.callback, callback.callbackParam));
|
2014-11-26 11:28:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
~ThreadTasks()
|
|
|
|
{
|
2014-12-05 15:25:56 +08:00
|
|
|
{
|
2014-12-22 17:40:02 +08:00
|
|
|
std::unique_lock<std::mutex> lock(_queueMutex);
|
2014-12-05 15:25:56 +08:00
|
|
|
_stop = true;
|
2014-12-22 17:40:02 +08:00
|
|
|
|
|
|
|
while(_tasks.size())
|
|
|
|
_tasks.pop();
|
|
|
|
while (_taskCallBacks.size())
|
2014-12-05 15:25:56 +08:00
|
|
|
_taskCallBacks.pop();
|
2014-12-05 15:01:30 +08:00
|
|
|
}
|
2014-11-26 11:28:14 +08:00
|
|
|
_condition.notify_all();
|
|
|
|
_thread.join();
|
|
|
|
}
|
2014-12-23 10:12:37 +08:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_queueMutex);
|
|
|
|
while(_tasks.size())
|
|
|
|
_tasks.pop();
|
|
|
|
while (_taskCallBacks.size())
|
|
|
|
_taskCallBacks.pop();
|
|
|
|
}
|
2017-05-08 13:39:17 +08:00
|
|
|
|
|
|
|
void enqueue(TaskCallBack callback, void* callbackParam, std::function<void()> task)
|
2014-12-23 10:12:37 +08:00
|
|
|
{
|
2017-05-08 13:39:17 +08:00
|
|
|
AsyncTaskCallBack taskCallBack;
|
|
|
|
taskCallBack.callback = std::move(callback);
|
|
|
|
taskCallBack.callbackParam = callbackParam;
|
|
|
|
|
2014-12-23 10:12:37 +08:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_queueMutex);
|
|
|
|
|
|
|
|
// don't allow enqueueing after stopping the pool
|
|
|
|
if(_stop)
|
|
|
|
{
|
|
|
|
CC_ASSERT(0 && "already stop");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:39:17 +08:00
|
|
|
_tasks.push(std::move(task));
|
|
|
|
_taskCallBacks.push(std::move(taskCallBack));
|
2014-12-23 10:12:37 +08:00
|
|
|
}
|
|
|
|
_condition.notify_one();
|
|
|
|
}
|
2014-11-26 11:28:14 +08:00
|
|
|
private:
|
2014-12-04 18:31:10 +08:00
|
|
|
|
2014-11-26 11:28:14 +08:00
|
|
|
// need to keep track of thread so we can join them
|
|
|
|
std::thread _thread;
|
|
|
|
// the task queue
|
|
|
|
std::queue< std::function<void()> > _tasks;
|
2017-05-08 13:39:17 +08:00
|
|
|
std::queue<AsyncTaskCallBack> _taskCallBacks;
|
2014-11-26 11:28:14 +08:00
|
|
|
|
|
|
|
// synchronization
|
2014-12-22 17:40:02 +08:00
|
|
|
std::mutex _queueMutex;
|
2014-11-26 11:28:14 +08:00
|
|
|
std::condition_variable _condition;
|
|
|
|
bool _stop;
|
|
|
|
};
|
|
|
|
|
|
|
|
//tasks
|
2014-12-23 10:12:37 +08:00
|
|
|
ThreadTasks _threadTasks[int(TaskType::TASK_MAX_TYPE)];
|
|
|
|
|
2014-11-26 11:28:14 +08:00
|
|
|
static AsyncTaskPool* s_asyncTaskPool;
|
|
|
|
};
|
|
|
|
|
2014-12-19 10:22:02 +08:00
|
|
|
inline void AsyncTaskPool::stopTasks(TaskType type)
|
|
|
|
{
|
|
|
|
auto& threadTask = _threadTasks[(int)type];
|
2014-12-23 10:12:37 +08:00
|
|
|
threadTask.clear();
|
2014-12-19 10:22:02 +08:00
|
|
|
}
|
|
|
|
|
2017-05-08 13:39:17 +08:00
|
|
|
inline void AsyncTaskPool::enqueue(AsyncTaskPool::TaskType type, TaskCallBack callback, void* callbackParam, std::function<void()> task)
|
2014-12-03 17:40:27 +08:00
|
|
|
{
|
|
|
|
auto& threadTask = _threadTasks[(int)type];
|
|
|
|
|
2017-05-08 13:39:17 +08:00
|
|
|
threadTask.enqueue(std::move(callback), callbackParam, std::move(task));
|
2014-12-03 17:40:27 +08:00
|
|
|
}
|
2014-11-26 11:28:14 +08:00
|
|
|
|
2017-05-08 13:39:17 +08:00
|
|
|
inline void AsyncTaskPool::enqueue(AsyncTaskPool::TaskType type, std::function<void()> task)
|
|
|
|
{
|
|
|
|
enqueue(type, [](void*) {}, nullptr, std::move(task));
|
|
|
|
}
|
2015-03-24 14:12:58 +08:00
|
|
|
|
2014-11-26 11:28:14 +08:00
|
|
|
NS_CC_END
|
2015-03-27 12:01:20 +08:00
|
|
|
// end group
|
|
|
|
/// @}
|
2014-11-26 11:28:14 +08:00
|
|
|
#endif //__CCSYNC_TASK_POOL_H_
|