Adds performFunctionInCocos2dThread

... in Scheduler
This commit is contained in:
Ricardo Quesada 2013-11-26 16:58:14 -08:00
parent 4a59cc6297
commit 2f1dffe46b
2 changed files with 67 additions and 21 deletions

View File

@ -258,7 +258,8 @@ Scheduler::Scheduler(void)
, _updateHashLocked(false) , _updateHashLocked(false)
, _scriptHandlerEntries(nullptr) , _scriptHandlerEntries(nullptr)
{ {
// I don't expect to have more than 30 functions to all per frame
_functionsToPerform.reserve(30);
} }
Scheduler::~Scheduler(void) Scheduler::~Scheduler(void)
@ -825,6 +826,15 @@ void Scheduler::resumeTargets(Set* targetsToResume)
} }
} }
void Scheduler::performFunctionInCocosThread(const std::function<void ()> &function)
{
_performMutex.lock();
_functionsToPerform.push_back(function);
_performMutex.unlock();
}
// main loop // main loop
void Scheduler::update(float dt) void Scheduler::update(float dt)
{ {
@ -835,6 +845,10 @@ void Scheduler::update(float dt)
dt *= _timeScale; dt *= _timeScale;
} }
//
// Selector callbacks
//
// Iterate over all the Updates' selectors // Iterate over all the Updates' selectors
tListEntry *entry, *tmp; tListEntry *entry, *tmp;
@ -904,23 +918,6 @@ void Scheduler::update(float dt)
} }
} }
// Iterate over all the script callbacks
if (_scriptHandlerEntries)
{
for (int i = _scriptHandlerEntries->count() - 1; i >= 0; i--)
{
SchedulerScriptHandlerEntry* eachEntry = static_cast<SchedulerScriptHandlerEntry*>(_scriptHandlerEntries->getObjectAtIndex(i));
if (eachEntry->isMarkedForDeletion())
{
_scriptHandlerEntries->removeObjectAtIndex(i);
}
else if (!eachEntry->isPaused())
{
eachEntry->getTimer()->update(dt);
}
}
}
// delete all updates that are marked for deletion // delete all updates that are marked for deletion
// updates with priority < 0 // updates with priority < 0
DL_FOREACH_SAFE(_updatesNegList, entry, tmp) DL_FOREACH_SAFE(_updatesNegList, entry, tmp)
@ -950,8 +947,43 @@ void Scheduler::update(float dt)
} }
_updateHashLocked = false; _updateHashLocked = false;
_currentTarget = nullptr; _currentTarget = nullptr;
//
// Script callbacks
//
// Iterate over all the script callbacks
if (_scriptHandlerEntries)
{
for (auto i = _scriptHandlerEntries->count() - 1; i >= 0; i--)
{
SchedulerScriptHandlerEntry* eachEntry = static_cast<SchedulerScriptHandlerEntry*>(_scriptHandlerEntries->getObjectAtIndex(i));
if (eachEntry->isMarkedForDeletion())
{
_scriptHandlerEntries->removeObjectAtIndex(i);
}
else if (!eachEntry->isPaused())
{
eachEntry->getTimer()->update(dt);
}
}
}
//
// Functions allocated from another thread
//
// Testing size is faster than locking / unlocking.
// And almost never there will be functions scheduled to be called.
if( _functionsToPerform.size() > 0 ) {
_performMutex.lock();
for( const auto &function : _functionsToPerform ) {
function();
}
_functionsToPerform.clear();
_performMutex.unlock();
}
} }

View File

@ -27,6 +27,10 @@ THE SOFTWARE.
#ifndef __CCSCHEDULER_H__ #ifndef __CCSCHEDULER_H__
#define __CCSCHEDULER_H__ #define __CCSCHEDULER_H__
#include <vector>
#include <functional>
#include <mutex>
#include "CCObject.h" #include "CCObject.h"
#include "uthash.h" #include "uthash.h"
@ -257,7 +261,13 @@ public:
*/ */
void resumeTargets(Set* targetsToResume); void resumeTargets(Set* targetsToResume);
private: /** calls a function on the cocos2d thread. Useful when you need to call a cocos2d function from another thread.
This function is thread safe.
@since v3.0
*/
void performFunctionInCocosThread( const std::function<void()> &function);
protected:
void removeHashElement(struct _hashSelectorEntry *element); void removeHashElement(struct _hashSelectorEntry *element);
void removeUpdateFromHash(struct _listEntry *entry); void removeUpdateFromHash(struct _listEntry *entry);
@ -266,7 +276,7 @@ private:
void priorityIn(struct _listEntry **list, Object *target, int priority, bool paused); void priorityIn(struct _listEntry **list, Object *target, int priority, bool paused);
void appendIn(struct _listEntry **list, Object *target, bool paused); void appendIn(struct _listEntry **list, Object *target, bool paused);
protected:
float _timeScale; float _timeScale;
// //
@ -284,6 +294,10 @@ protected:
// If true unschedule will not remove anything from a hash. Elements will only be marked for deletion. // If true unschedule will not remove anything from a hash. Elements will only be marked for deletion.
bool _updateHashLocked; bool _updateHashLocked;
Array* _scriptHandlerEntries; Array* _scriptHandlerEntries;
// Used for "perform Function"
std::vector<std::function<void()>> _functionsToPerform;
std::mutex _performMutex;
}; };
// end of global group // end of global group