Merge pull request #4442 from minggo/use-schedule

use Schedule::performFunctionInCocosThread
This commit is contained in:
minggo 2013-12-05 22:30:54 -08:00
commit 0126aaa205
6 changed files with 8 additions and 127 deletions

View File

@ -1037,9 +1037,6 @@ void DisplayLinkDirector::mainLoop()
}
else if (! _invalid)
{
// invoke call back from other thread
ThreadHelper::doCallback();
drawScene();
// release the objects

View File

@ -34,7 +34,6 @@ THE SOFTWARE.
#include "ccMacros.h"
#include "CCDirector.h"
#include "platform/CCFileUtils.h"
#include "platform/CCThread.h"
#include "ccUtils.h"
#include "CCScheduler.h"
#include "CCString.h"
@ -93,18 +92,6 @@ const char* TextureCache::description() const
return String::createWithFormat("<TextureCache | Number of textures = %lu>", _textures.size() )->getCString();
}
//Dictionary* TextureCache::snapshotTextures()
//{
// Dictionary* pRet = new Dictionary();
// DictElement* pElement = NULL;
// CCDICT_FOREACH(_textures, pElement)
// {
// pRet->setObject(pElement->getObject(), pElement->getStrKey());
// }
// pRet->autorelease();
// return pRet;
//}
void TextureCache::addImageAsync(const std::string &path, Object *target, SEL_CallFuncO selector)
{
Texture2D *texture = NULL;

View File

@ -29,10 +29,6 @@ NS_CC_BEGIN
// iOS and Mac already has a Thread.mm
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
std::list<std::function<void(void)>>* ThreadHelper::_callbackList = new std::list<std::function<void(void)>>();
std::mutex* ThreadHelper::_mutex = new std::mutex;
int ThreadHelper::_callbackNumberPerFrame = 5;
void* ThreadHelper::createAutoreleasePool()
{
@ -44,42 +40,6 @@ void ThreadHelper::releaseAutoreleasePool(void* autoreleasePool)
}
void ThreadHelper::runOnGLThread(std::function<void(void)> f)
{
// Insert call back function
_mutex->lock();
_callbackList->push_back(f);
_mutex->unlock();
}
void ThreadHelper::doCallback()
{
_mutex->lock();
auto iter = _callbackList->begin();
int i = 0;
while (iter != _callbackList->end())
{
auto f = *iter;
f();
++i;
if (i >= _callbackNumberPerFrame)
{
break;
}
else
{
iter = _callbackList->erase(iter);
}
}
_mutex->unlock();
}
void ThreadHelper::setCallbackNumberPerFrame(int callbackNumberPerFrame)
{
_callbackNumberPerFrame = callbackNumberPerFrame;
}
#endif
NS_CC_END

View File

@ -59,29 +59,6 @@ public:
* @lua NA
*/
static void releaseAutoreleasePool(void *autoreleasePool);
/** To run a function in gl thread.
* @js NA
* @lua NA
@since v3.0
*/
static void runOnGLThread(std::function<void(void)> f);
/** Set how many callback functions being invoked per frame. Default value is 5.
* @js NA
* @lua NA
@since v3.0
*/
static void setCallbackNumberPerFrame(int callbackNumberPerFrame);
private:
// This function will be call by Director to call some call back function on gl thread
static void doCallback();
static std::list<std::function<void(void)>> *_callbackList;
static std::mutex *_mutex;
// How many callback functions invoked per frame
static int _callbackNumberPerFrame;
};
// end of platform group

View File

@ -26,10 +26,6 @@ THE SOFTWARE.
NS_CC_BEGIN
std::list<std::function<void(void)>>* ThreadHelper::_callbackList = new std::list<std::function<void(void)>>();
std::mutex* ThreadHelper::_mutex = new std::mutex;
int ThreadHelper::_callbackNumberPerFrame = 5;
void* ThreadHelper::createAutoreleasePool()
{
id pool = [[NSAutoreleasePool alloc] init];
@ -41,40 +37,4 @@ void ThreadHelper::releaseAutoreleasePool(void *autoreleasePool)
[(NSAutoreleasePool*)autoreleasePool release];
}
void ThreadHelper::runOnGLThread(std::function<void(void)> f)
{
// Insert call back function
_mutex->lock();
_callbackList->push_back(f);
_mutex->unlock();
}
void ThreadHelper::doCallback()
{
_mutex->lock();
auto iter = _callbackList->begin();
int i = 0;
while (iter != _callbackList->end())
{
auto f = *iter;
f();
++i;
if (i >= _callbackNumberPerFrame)
{
break;
}
else
{
iter = _callbackList->erase(iter);
}
}
_mutex->unlock();
}
void ThreadHelper::setCallbackNumberPerFrame(int callbackNumberPerFrame)
{
_callbackNumberPerFrame = callbackNumberPerFrame;
}
NS_CC_END

View File

@ -156,7 +156,7 @@ bool AssetsManager::checkUpdate()
if (res != 0)
{
ThreadHelper::runOnGLThread([&, this]{
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this]{
if (this->_delegate)
this->_delegate->onError(ErrorCode::NETWORK);
});
@ -168,7 +168,7 @@ bool AssetsManager::checkUpdate()
string recordedVersion = UserDefault::getInstance()->getStringForKey(keyOfVersion().c_str());
if (recordedVersion == _version)
{
ThreadHelper::runOnGLThread([&, this]{
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this]{
if (this->_delegate)
this->_delegate->onError(ErrorCode::NO_NEW_VERSION);
});
@ -191,7 +191,7 @@ void AssetsManager::downloadAndUncompress()
{
if (! downLoad()) break;
ThreadHelper::runOnGLThread([&, this]{
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this]{
UserDefault::getInstance()->setStringForKey(this->keyOfDownloadedVersion().c_str(),
this->_version.c_str());
UserDefault::getInstance()->flush();
@ -201,14 +201,14 @@ void AssetsManager::downloadAndUncompress()
// Uncompress zip file.
if (! uncompress())
{
ThreadHelper::runOnGLThread([&, this]{
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this]{
if (this->_delegate)
this->_delegate->onError(ErrorCode::UNCOMPRESS);
});
break;
}
ThreadHelper::runOnGLThread([&, this] {
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this] {
// Record new version code.
UserDefault::getInstance()->setStringForKey(this->keyOfVersion().c_str(), this->_version.c_str());
@ -479,7 +479,7 @@ int assetsManagerProgressFunc(void *ptr, double totalToDownload, double nowDownl
if (percent != tmp)
{
percent = tmp;
ThreadHelper::runOnGLThread([=]{
Director::getInstance()->getScheduler()->performFunctionInCocosThread([=]{
auto manager = static_cast<AssetsManager*>(ptr);
if (manager->_delegate)
manager->_delegate->onProgress(percent);
@ -498,7 +498,7 @@ bool AssetsManager::downLoad()
FILE *fp = fopen(outFileName.c_str(), "wb");
if (! fp)
{
ThreadHelper::runOnGLThread([&, this]{
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this]{
if (this->_delegate)
this->_delegate->onError(ErrorCode::CREATE_FILE);
});
@ -518,7 +518,7 @@ bool AssetsManager::downLoad()
curl_easy_cleanup(_curl);
if (res != 0)
{
ThreadHelper::runOnGLThread([&, this]{
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this]{
if (this->_delegate)
this->_delegate->onError(ErrorCode::NETWORK);
});