axmol/cocos/base/CCAsyncTaskPool.h

206 lines
7.0 KiB
C
Raw Normal View History

2014-11-26 11:28:14 +08:00
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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>
NS_CC_BEGIN
/////////////////////////////////////////////////////////////////////////////
2014-12-19 11:30:51 +08:00
class CC_DLL AsyncTaskPool
2014-11-26 11:28:14 +08:00
{
public:
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,
};
/**
* get instance
*/
2014-11-26 11:28:14 +08:00
static AsyncTaskPool* getInstance();
/**
* destroy instance
*/
2014-11-26 11:28:14 +08:00
static void destoryInstance();
2014-12-19 10:22:02 +08:00
/**
* stop tasks
* @param type task type you want to stop
*/
void stopTasks(TaskType type);
2014-12-05 15:55:01 +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 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
2014-12-05 15:59:21 +08:00
* @param f task can be lambda function
* @param args task parameters
2014-12-05 15:55:01 +08:00
*/
2014-12-19 10:22:02 +08:00
template<class F>
inline void enqueue(TaskType type, const TaskCallBack& callback, void* callbackParam, F&& f);
2014-11-26 11:28:14 +08:00
CC_CONSTRUCTOR_ACCESS:
AsyncTaskPool();
~AsyncTaskPool();
protected:
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;
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());
callback = std::move(this->_taskCallBacks.front());
2014-11-26 11:28:14 +08:00
this->_tasks.pop();
this->_taskCallBacks.pop();
2014-11-26 11:28:14 +08:00
}
task();
2014-12-23 10:12:37 +08:00
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, callback]{ 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();
}
template<class F>
void enqueue(const TaskCallBack& callback, void* callbackParam, F&& f)
{
auto task = f;//std::bind(std::forward<F>(f), std::forward<Args>(args)...);
{
std::unique_lock<std::mutex> lock(_queueMutex);
// don't allow enqueueing after stopping the pool
if(_stop)
{
CC_ASSERT(0 && "already stop");
return;
}
AsyncTaskCallBack taskCallBack;
taskCallBack.callback = callback;
taskCallBack.callbackParam = callbackParam;
_tasks.emplace([task](){ task(); });
_taskCallBacks.emplace(taskCallBack);
}
_condition.notify_one();
}
2014-11-26 11:28:14 +08:00
private:
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;
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
}
template<class F>
inline void AsyncTaskPool::enqueue(AsyncTaskPool::TaskType type, const TaskCallBack& callback, void* callbackParam, F&& f)
2014-12-03 17:40:27 +08:00
{
auto& threadTask = _threadTasks[(int)type];
2014-12-23 10:12:37 +08:00
threadTask.enqueue(callback, callbackParam, f);
2014-12-03 17:40:27 +08:00
}
2014-11-26 11:28:14 +08:00
NS_CC_END
#endif //__CCSYNC_TASK_POOL_H_