axmol/core/base/CCScheduler.cpp

1104 lines
30 KiB
C++
Raw Normal View History

2012-02-01 16:45:23 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
2012-02-01 16:45:23 +08:00
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2012-02-01 16:45:23 +08:00
https://axis-project.github.io/
2012-02-01 16:45:23 +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.
****************************************************************************/
2014-04-30 08:37:36 +08:00
#include "base/CCScheduler.h"
#include "base/ccMacros.h"
#include "base/CCDirector.h"
#include "uthash/utlist.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "base/ccCArray.h"
#include "base/CCScriptSupport.h"
2012-02-02 15:58:10 +08:00
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
2012-02-01 16:45:23 +08:00
// data structures
// A list double-linked list used for "updates with priority"
typedef struct _listEntry
{
2021-12-25 10:04:45 +08:00
struct _listEntry *prev, *next;
ccSchedulerFunc callback;
void* target;
int priority;
bool paused;
bool markedForDeletion; // selector will no longer be called and entry will be removed at end of the next tick
2012-02-01 16:45:23 +08:00
} tListEntry;
typedef struct _hashUpdateEntry
{
2021-12-25 10:04:45 +08:00
tListEntry** list; // Which list does it belong to ?
tListEntry* entry; // entry in the list
void* target;
ccSchedulerFunc callback;
UT_hash_handle hh;
2012-02-01 16:45:23 +08:00
} tHashUpdateEntry;
// Hash Element used for "selectors with interval"
typedef struct _hashSelectorEntry
{
2021-12-25 10:04:45 +08:00
ccArray* timers;
void* target;
int timerIndex;
Timer* currentTimer;
bool paused;
UT_hash_handle hh;
2012-11-14 18:05:15 +08:00
} tHashTimerEntry;
2012-02-01 16:45:23 +08:00
// implementation Timer
2012-02-01 16:45:23 +08:00
Timer::Timer()
2021-12-25 10:04:45 +08:00
: _scheduler(nullptr)
, _elapsed(-1)
, _runForever(false)
, _useDelay(false)
, _timesExecuted(0)
, _repeat(0)
, _delay(0.0f)
, _interval(0.0f)
, _aborted(false)
{}
2012-02-02 15:58:10 +08:00
void Timer::setupTimerWithInterval(float seconds, unsigned int repeat, float delay)
2012-02-02 15:58:10 +08:00
{
2021-12-25 10:04:45 +08:00
_elapsed = -1;
_interval = seconds;
_delay = delay;
_useDelay = (_delay > 0.0f) ? true : false;
_repeat = repeat;
_runForever = (_repeat == CC_REPEAT_FOREVER) ? true : false;
_timesExecuted = 0;
2012-02-01 16:45:23 +08:00
}
void Timer::update(float dt)
2012-02-01 16:45:23 +08:00
{
if (_elapsed == -1)
{
2021-12-25 10:04:45 +08:00
_elapsed = 0;
_timesExecuted = 0;
return;
}
// accumulate elapsed time
_elapsed += dt;
2021-12-25 10:04:45 +08:00
// deal with delay
if (_useDelay)
{
if (_elapsed < _delay)
{
return;
}
2021-12-25 10:04:45 +08:00
_timesExecuted += 1; // important to increment before call trigger
trigger(_delay);
2021-12-25 10:04:45 +08:00
_elapsed = _elapsed - _delay;
_useDelay = false;
// after delay, the rest time should compare with interval
if (isExhausted())
2021-12-25 10:04:45 +08:00
{ // unschedule timer
cancel();
return;
}
}
2021-12-25 10:04:45 +08:00
// if _interval == 0, should trigger once every frame
float interval = (_interval > 0) ? _interval : _elapsed;
while ((_elapsed >= interval) && !_aborted)
{
2021-12-25 10:04:45 +08:00
_timesExecuted += 1; // important to increment before call trigger
trigger(interval);
_elapsed -= interval;
if (isExhausted())
{
cancel();
break;
}
2012-02-01 16:45:23 +08:00
if (_elapsed <= 0.f)
{
break;
}
}
}
bool Timer::isExhausted() const
{
return !_runForever && _timesExecuted > _repeat;
}
// TimerTargetSelector
2021-12-25 10:04:45 +08:00
TimerTargetSelector::TimerTargetSelector() : _target(nullptr), _selector(nullptr) {}
2021-12-25 10:04:45 +08:00
bool TimerTargetSelector::initWithSelector(Scheduler* scheduler,
SEL_SCHEDULE selector,
Ref* target,
float seconds,
unsigned int repeat,
float delay)
{
_scheduler = scheduler;
2021-12-25 10:04:45 +08:00
_target = target;
_selector = selector;
setupTimerWithInterval(seconds, repeat, delay);
return true;
}
void TimerTargetSelector::trigger(float dt)
{
if (_target && _selector)
{
(_target->*_selector)(dt);
}
}
void TimerTargetSelector::cancel()
{
_scheduler->unschedule(_selector, _target);
}
// TimerTargetCallback
2021-12-25 10:04:45 +08:00
TimerTargetCallback::TimerTargetCallback() : _target(nullptr), _callback(nullptr) {}
2021-12-25 10:04:45 +08:00
bool TimerTargetCallback::initWithCallback(Scheduler* scheduler,
const ccSchedulerFunc& callback,
void* target,
2021-12-26 23:26:34 +08:00
std::string_view key,
2021-12-25 10:04:45 +08:00
float seconds,
unsigned int repeat,
float delay)
{
_scheduler = scheduler;
2021-12-25 10:04:45 +08:00
_target = target;
_callback = callback;
_key = key;
setupTimerWithInterval(seconds, repeat, delay);
return true;
}
void TimerTargetCallback::trigger(float dt)
{
if (_callback)
{
_callback(dt);
}
}
void TimerTargetCallback::cancel()
{
_scheduler->unschedule(_key, _target);
}
#if CC_ENABLE_SCRIPT_BINDING
// TimerScriptHandler
bool TimerScriptHandler::initWithScriptHandler(int handler, float seconds)
{
_scriptHandler = handler;
2021-12-25 10:04:45 +08:00
_elapsed = -1;
_interval = seconds;
return true;
}
void TimerScriptHandler::trigger(float dt)
{
if (0 != _scriptHandler)
{
2021-12-25 10:04:45 +08:00
SchedulerScriptData data(_scriptHandler, dt);
ScriptEvent event(kScheduleEvent, &data);
ScriptEngineManager::sendEventToLua(event);
}
}
2021-12-25 10:04:45 +08:00
void TimerScriptHandler::cancel() {}
#endif
// implementation of Scheduler
2012-02-01 16:45:23 +08:00
// Priority level reserved for system services.
const int Scheduler::PRIORITY_SYSTEM = INT_MIN;
// Minimum priority level for user scheduling.
const int Scheduler::PRIORITY_NON_SYSTEM_MIN = PRIORITY_SYSTEM + 1;
Scheduler::Scheduler()
2021-12-25 10:04:45 +08:00
: _timeScale(1.0f)
, _updatesNegList(nullptr)
, _updates0List(nullptr)
, _updatesPosList(nullptr)
, _hashForUpdates(nullptr)
, _hashForTimers(nullptr)
, _currentTarget(nullptr)
, _currentTargetSalvaged(false)
, _updateHashLocked(false)
#if CC_ENABLE_SCRIPT_BINDING
2021-12-25 10:04:45 +08:00
, _scriptHandlerEntries(20)
#endif
2012-02-01 16:45:23 +08:00
{
// I don't expect to have more than 30 functions to all per frame
_functionsToPerform.reserve(30);
2012-02-01 16:45:23 +08:00
}
Scheduler::~Scheduler()
2012-02-01 16:45:23 +08:00
{
2012-11-14 18:05:15 +08:00
unscheduleAll();
2012-02-01 16:45:23 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::removeHashElement(_hashSelectorEntry* element)
2012-02-01 16:45:23 +08:00
{
ccArrayFree(element->timers);
HASH_DEL(_hashForTimers, element);
free(element);
2012-02-01 16:45:23 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::schedule(const ccSchedulerFunc& callback,
void* target,
float interval,
bool paused,
2021-12-26 23:26:34 +08:00
std::string_view key)
{
this->schedule(callback, target, interval, CC_REPEAT_FOREVER, 0.0f, paused, key);
}
2021-12-25 10:04:45 +08:00
void Scheduler::schedule(const ccSchedulerFunc& callback,
void* target,
float interval,
unsigned int repeat,
float delay,
bool paused,
2021-12-26 23:26:34 +08:00
std::string_view key)
2012-02-01 16:45:23 +08:00
{
2013-11-16 21:08:00 +08:00
CCASSERT(target, "Argument target must be non-nullptr");
CCASSERT(!key.empty(), "key should not be empty!");
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
2021-12-25 10:04:45 +08:00
if (!element)
{
2021-12-25 10:04:45 +08:00
element = (tHashTimerEntry*)calloc(sizeof(*element), 1);
element->target = target;
HASH_ADD_PTR(_hashForTimers, target, element);
// Is this the 1st element ? Then set the pause level to all the selectors of this target
element->paused = paused;
}
else
{
CCASSERT(element->paused == paused, "element's paused should be paused!");
}
2013-11-16 21:08:00 +08:00
if (element->timers == nullptr)
{
element->timers = ccArrayNew(10);
}
2021-12-25 10:04:45 +08:00
else
{
2013-09-08 11:26:38 +08:00
for (int i = 0; i < element->timers->num; ++i)
{
2021-12-25 10:04:45 +08:00
TimerTargetCallback* timer = dynamic_cast<TimerTargetCallback*>(element->timers->arr[i]);
if (timer && !timer->isExhausted() && key == timer->getKey())
{
2021-12-25 10:04:45 +08:00
CCLOG("CCScheduler#schedule. Reiniting timer with interval %.4f, repeat %u, delay %.4f", interval,
repeat, delay);
timer->setupTimerWithInterval(interval, repeat, delay);
return;
}
}
ccArrayEnsureExtraCapacity(element->timers, 1);
}
2021-12-25 10:04:45 +08:00
TimerTargetCallback* timer = new TimerTargetCallback();
timer->initWithCallback(this, callback, target, key, interval, repeat, delay);
ccArrayAppendObject(element->timers, timer);
timer->release();
2012-02-01 16:45:23 +08:00
}
2021-12-26 23:26:34 +08:00
void Scheduler::unschedule(std::string_view key, void* target)
2012-02-01 16:45:23 +08:00
{
// explicit handle nil arguments when removing an object
if (target == nullptr || key.empty())
{
return;
}
2021-12-25 10:04:45 +08:00
// CCASSERT(target);
// CCASSERT(selector);
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
if (element)
{
2013-09-08 11:26:38 +08:00
for (int i = 0; i < element->timers->num; ++i)
{
2021-12-25 10:04:45 +08:00
TimerTargetCallback* timer = dynamic_cast<TimerTargetCallback*>(element->timers->arr[i]);
if (timer && key == timer->getKey())
{
2021-12-25 10:04:45 +08:00
if (timer == element->currentTimer && (!timer->isAborted()))
{
timer->retain();
timer->setAborted();
}
ccArrayRemoveObjectAtIndex(element->timers, i, true);
// update timerIndex in case we are in tick:, looping over the actions
if (element->timerIndex >= i)
{
element->timerIndex--;
}
if (element->timers->num == 0)
{
if (_currentTarget == element)
{
_currentTargetSalvaged = true;
}
else
{
removeHashElement(element);
}
}
return;
}
}
}
2012-02-01 16:45:23 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::priorityIn(tListEntry** list, const ccSchedulerFunc& callback, void* target, int priority, bool paused)
2012-02-01 16:45:23 +08:00
{
2021-12-25 10:04:45 +08:00
tListEntry* listElement = new tListEntry();
listElement->callback = callback;
2021-12-25 10:04:45 +08:00
listElement->target = target;
listElement->priority = priority;
2021-12-25 10:04:45 +08:00
listElement->paused = paused;
2013-11-16 21:08:00 +08:00
listElement->next = listElement->prev = nullptr;
2021-12-25 10:04:45 +08:00
listElement->markedForDeletion = false;
// empty list ?
2021-12-25 10:04:45 +08:00
if (!*list)
{
DL_APPEND(*list, listElement);
}
else
{
2013-11-16 21:08:00 +08:00
bool added = false;
2021-12-25 10:04:45 +08:00
for (tListEntry* element = *list; element; element = element->next)
{
if (priority < element->priority)
{
if (element == *list)
{
DL_PREPEND(*list, listElement);
}
else
{
listElement->next = element;
listElement->prev = element->prev;
element->prev->next = listElement;
2021-12-25 10:04:45 +08:00
element->prev = listElement;
}
2013-11-16 21:08:00 +08:00
added = true;
break;
}
}
// Not added? priority has the higher value. Append it.
2021-12-25 10:04:45 +08:00
if (!added)
{
DL_APPEND(*list, listElement);
}
}
// update hash entry for quick access
2021-12-25 10:04:45 +08:00
tHashUpdateEntry* hashElement = (tHashUpdateEntry*)calloc(sizeof(*hashElement), 1);
hashElement->target = target;
hashElement->list = list;
hashElement->entry = listElement;
memset(&hashElement->hh, 0, sizeof(hashElement->hh));
2013-11-16 21:08:00 +08:00
HASH_ADD_PTR(_hashForUpdates, target, hashElement);
2012-02-01 16:45:23 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::appendIn(_listEntry** list, const ccSchedulerFunc& callback, void* target, bool paused)
2012-02-01 16:45:23 +08:00
{
2021-12-25 10:04:45 +08:00
tListEntry* listElement = new tListEntry();
2012-02-01 16:45:23 +08:00
2021-12-25 10:04:45 +08:00
listElement->callback = callback;
listElement->target = target;
listElement->paused = paused;
listElement->priority = 0;
listElement->markedForDeletion = false;
2012-02-01 16:45:23 +08:00
DL_APPEND(*list, listElement);
2012-02-01 16:45:23 +08:00
// update hash entry for quicker access
2021-12-25 10:04:45 +08:00
tHashUpdateEntry* hashElement = (tHashUpdateEntry*)calloc(sizeof(*hashElement), 1);
hashElement->target = target;
hashElement->list = list;
hashElement->entry = listElement;
memset(&hashElement->hh, 0, sizeof(hashElement->hh));
HASH_ADD_PTR(_hashForUpdates, target, hashElement);
2012-02-01 16:45:23 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::schedulePerFrame(const ccSchedulerFunc& callback, void* target, int priority, bool paused)
2012-02-01 16:45:23 +08:00
{
2021-12-25 10:04:45 +08:00
tHashUpdateEntry* hashElement = nullptr;
2013-11-16 21:08:00 +08:00
HASH_FIND_PTR(_hashForUpdates, &target, hashElement);
if (hashElement)
{
// change priority: should unschedule it first
if (hashElement->entry->priority != priority)
{
unscheduleUpdate(target);
}
else
{
// don't add it again
return;
}
}
// most of the updates are going to be 0, that's way there
// is an special list for updates with priority 0
if (priority == 0)
{
appendIn(&_updates0List, callback, target, paused);
}
else if (priority < 0)
{
priorityIn(&_updatesNegList, callback, target, priority, paused);
}
else
{
// priority > 0
priorityIn(&_updatesPosList, callback, target, priority, paused);
}
2012-02-01 16:45:23 +08:00
}
2021-12-26 23:26:34 +08:00
bool Scheduler::isScheduled(std::string_view key, const void* target) const
{
CCASSERT(!key.empty(), "Argument key must not be empty");
2013-11-16 21:08:00 +08:00
CCASSERT(target, "Argument target must be non-nullptr");
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
2021-12-25 10:04:45 +08:00
if (!element)
{
return false;
}
2021-12-25 10:04:45 +08:00
2013-11-16 21:08:00 +08:00
if (element->timers == nullptr)
{
return false;
}
2021-12-25 10:04:45 +08:00
for (int i = 0; i < element->timers->num; ++i)
{
2021-12-25 10:04:45 +08:00
TimerTargetCallback* timer = dynamic_cast<TimerTargetCallback*>(element->timers->arr[i]);
if (timer && !timer->isExhausted() && key == timer->getKey())
{
return true;
}
}
2021-12-25 10:04:45 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
void Scheduler::removeUpdateFromHash(struct _listEntry* entry)
2012-02-01 16:45:23 +08:00
{
2021-12-25 10:04:45 +08:00
tHashUpdateEntry* element = nullptr;
HASH_FIND_PTR(_hashForUpdates, &entry->target, element);
if (element)
{
// list entry
DL_DELETE(*element->list, element->entry);
if (!_updateHashLocked)
CC_SAFE_DELETE(element->entry);
else
{
element->entry->markedForDeletion = true;
_updateDeleteVector.push_back(element->entry);
}
// hash entry
HASH_DEL(_hashForUpdates, element);
free(element);
}
2012-02-01 16:45:23 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::unscheduleUpdate(void* target)
2012-02-01 16:45:23 +08:00
{
2013-11-16 21:08:00 +08:00
if (target == nullptr)
{
return;
}
2021-12-25 10:04:45 +08:00
tHashUpdateEntry* element = nullptr;
HASH_FIND_PTR(_hashForUpdates, &target, element);
if (element)
this->removeUpdateFromHash(element->entry);
2012-02-01 16:45:23 +08:00
}
void Scheduler::unscheduleAll()
{
unscheduleAllWithMinPriority(PRIORITY_SYSTEM);
}
2013-11-16 21:08:00 +08:00
void Scheduler::unscheduleAllWithMinPriority(int minPriority)
2012-02-01 16:45:23 +08:00
{
// Custom Selectors
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
tHashTimerEntry* nextElement = nullptr;
2013-11-16 21:08:00 +08:00
for (element = _hashForTimers; element != nullptr;)
{
// element may be removed in unscheduleAllSelectorsForTarget
2021-12-25 10:04:45 +08:00
nextElement = (tHashTimerEntry*)element->hh.next;
unscheduleAllForTarget(element->target);
2012-02-01 16:45:23 +08:00
2013-11-16 21:08:00 +08:00
element = nextElement;
}
// Updates selectors
2013-11-11 12:49:38 +08:00
tListEntry *entry, *tmp;
2021-12-25 10:04:45 +08:00
if (minPriority < 0)
{
2013-11-11 12:49:38 +08:00
DL_FOREACH_SAFE(_updatesNegList, entry, tmp)
{
2021-12-25 10:04:45 +08:00
if (entry->priority >= minPriority)
{
unscheduleUpdate(entry->target);
}
}
}
2021-12-25 10:04:45 +08:00
if (minPriority <= 0)
{
2021-12-25 10:04:45 +08:00
DL_FOREACH_SAFE(_updates0List, entry, tmp) { unscheduleUpdate(entry->target); }
}
2013-11-11 12:49:38 +08:00
DL_FOREACH_SAFE(_updatesPosList, entry, tmp)
{
2021-12-25 10:04:45 +08:00
if (entry->priority >= minPriority)
{
unscheduleUpdate(entry->target);
}
}
#if CC_ENABLE_SCRIPT_BINDING
_scriptHandlerEntries.clear();
#endif
2012-02-09 14:07:11 +08:00
}
2012-02-01 16:45:23 +08:00
2021-12-25 10:04:45 +08:00
void Scheduler::unscheduleAllForTarget(void* target)
2012-02-01 16:45:23 +08:00
{
2013-11-16 21:08:00 +08:00
// explicit nullptr handling
if (target == nullptr)
{
return;
}
// Custom Selectors
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
if (element)
{
2021-12-25 10:04:45 +08:00
if (ccArrayContainsObject(element->timers, element->currentTimer) && (!element->currentTimer->isAborted()))
{
element->currentTimer->retain();
element->currentTimer->setAborted();
}
ccArrayRemoveAllObjects(element->timers);
if (_currentTarget == element)
{
_currentTargetSalvaged = true;
}
else
{
removeHashElement(element);
}
}
// update selector
unscheduleUpdate(target);
2012-02-01 16:45:23 +08:00
}
#if CC_ENABLE_SCRIPT_BINDING
unsigned int Scheduler::scheduleScriptFunc(unsigned int handler, float interval, bool paused)
2012-02-02 15:58:10 +08:00
{
2013-11-16 21:08:00 +08:00
SchedulerScriptHandlerEntry* entry = SchedulerScriptHandlerEntry::create(handler, interval, paused);
_scriptHandlerEntries.pushBack(entry);
2013-11-16 21:08:00 +08:00
return entry->getEntryId();
2012-02-02 15:58:10 +08:00
}
2013-11-16 21:08:00 +08:00
void Scheduler::unscheduleScriptEntry(unsigned int scheduleScriptEntryID)
2012-02-02 15:58:10 +08:00
{
2013-12-12 12:07:20 +08:00
for (ssize_t i = _scriptHandlerEntries.size() - 1; i >= 0; i--)
2012-02-02 15:58:10 +08:00
{
SchedulerScriptHandlerEntry* entry = _scriptHandlerEntries.at(i);
2013-11-16 21:08:00 +08:00
if (entry->getEntryId() == (int)scheduleScriptEntryID)
2012-02-02 15:58:10 +08:00
{
2013-11-16 21:08:00 +08:00
entry->markedForDeletion();
2012-02-02 15:58:10 +08:00
break;
}
}
}
#endif
2021-12-25 10:04:45 +08:00
void Scheduler::resumeTarget(void* target)
2012-02-01 16:45:23 +08:00
{
CCASSERT(target != nullptr, "target can't be nullptr!");
// custom selectors
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
if (element)
{
element->paused = false;
}
// update selector
2021-12-25 10:04:45 +08:00
tHashUpdateEntry* elementUpdate = nullptr;
HASH_FIND_PTR(_hashForUpdates, &target, elementUpdate);
if (elementUpdate)
{
CCASSERT(elementUpdate->entry != nullptr, "elementUpdate's entry can't be nullptr!");
elementUpdate->entry->paused = false;
}
2012-02-01 16:45:23 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::pauseTarget(void* target)
2012-02-01 16:45:23 +08:00
{
CCASSERT(target != nullptr, "target can't be nullptr!");
// custom selectors
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
if (element)
{
element->paused = true;
}
// update selector
2021-12-25 10:04:45 +08:00
tHashUpdateEntry* elementUpdate = nullptr;
HASH_FIND_PTR(_hashForUpdates, &target, elementUpdate);
if (elementUpdate)
{
CCASSERT(elementUpdate->entry != nullptr, "elementUpdate's entry can't be nullptr!");
elementUpdate->entry->paused = true;
}
2012-02-01 16:45:23 +08:00
}
2021-12-25 10:04:45 +08:00
bool Scheduler::isTargetPaused(void* target)
2012-02-01 16:45:23 +08:00
{
2021-12-25 10:04:45 +08:00
CCASSERT(target != nullptr, "target must be non nil");
2012-02-01 16:45:23 +08:00
// Custom selectors
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
2021-12-25 10:04:45 +08:00
if (element)
2012-02-01 16:45:23 +08:00
{
return element->paused;
2012-02-01 16:45:23 +08:00
}
2021-12-25 10:04:45 +08:00
2012-11-14 18:05:15 +08:00
// We should check update selectors if target does not have custom selectors
2021-12-25 10:04:45 +08:00
tHashUpdateEntry* elementUpdate = nullptr;
HASH_FIND_PTR(_hashForUpdates, &target, elementUpdate);
2021-12-25 10:04:45 +08:00
if (elementUpdate)
2012-11-14 18:05:15 +08:00
{
return elementUpdate->entry->paused;
2012-11-14 18:05:15 +08:00
}
2021-12-25 10:04:45 +08:00
2012-02-01 16:45:23 +08:00
return false; // should never get here
}
std::set<void*> Scheduler::pauseAllTargets()
{
return pauseAllTargetsWithMinPriority(PRIORITY_SYSTEM);
}
std::set<void*> Scheduler::pauseAllTargetsWithMinPriority(int minPriority)
{
std::set<void*> idsWithSelectors;
// Custom Selectors
2021-12-25 10:04:45 +08:00
for (tHashTimerEntry* element = _hashForTimers; element != nullptr; element = (tHashTimerEntry*)element->hh.next)
{
element->paused = true;
idsWithSelectors.insert(element->target);
}
// Updates selectors
tListEntry *entry, *tmp;
2021-12-25 10:04:45 +08:00
if (minPriority < 0)
{
2021-12-25 10:04:45 +08:00
DL_FOREACH_SAFE(_updatesNegList, entry, tmp)
{
2021-12-25 10:04:45 +08:00
if (entry->priority >= minPriority)
{
entry->paused = true;
idsWithSelectors.insert(entry->target);
}
}
}
2021-12-25 10:04:45 +08:00
if (minPriority <= 0)
{
2021-12-25 10:04:45 +08:00
DL_FOREACH_SAFE(_updates0List, entry, tmp)
{
entry->paused = true;
idsWithSelectors.insert(entry->target);
}
}
2021-12-25 10:04:45 +08:00
DL_FOREACH_SAFE(_updatesPosList, entry, tmp)
{
2021-12-25 10:04:45 +08:00
if (entry->priority >= minPriority)
{
entry->paused = true;
idsWithSelectors.insert(entry->target);
}
}
return idsWithSelectors;
}
void Scheduler::resumeTargets(const std::set<void*>& targetsToResume)
{
2021-12-25 10:04:45 +08:00
for (const auto& obj : targetsToResume)
{
2013-12-06 18:16:58 +08:00
this->resumeTarget(obj);
}
}
2021-12-25 10:04:45 +08:00
void Scheduler::performFunctionInCocosThread(std::function<void()> function)
{
std::lock_guard<std::mutex> lock(_performMutex);
_functionsToPerform.push_back(std::move(function));
}
void Scheduler::removeAllFunctionsToBePerformedInCocosThread()
{
std::unique_lock<std::mutex> lock(_performMutex);
_functionsToPerform.clear();
}
2012-02-01 16:45:23 +08:00
// main loop
void Scheduler::update(float dt)
2012-02-01 16:45:23 +08:00
{
_updateHashLocked = true;
if (_timeScale != 1.0f)
{
dt *= _timeScale;
}
//
// Selector callbacks
//
// Iterate over all the Updates' selectors
2013-11-11 12:49:38 +08:00
tListEntry *entry, *tmp;
// updates with priority < 0
2013-11-11 12:49:38 +08:00
DL_FOREACH_SAFE(_updatesNegList, entry, tmp)
{
2021-12-25 10:04:45 +08:00
if ((!entry->paused) && (!entry->markedForDeletion))
{
entry->callback(dt);
}
}
// updates with priority == 0
2013-11-11 12:49:38 +08:00
DL_FOREACH_SAFE(_updates0List, entry, tmp)
{
2021-12-25 10:04:45 +08:00
if ((!entry->paused) && (!entry->markedForDeletion))
{
entry->callback(dt);
}
}
// updates with priority > 0
2013-11-11 12:49:38 +08:00
DL_FOREACH_SAFE(_updatesPosList, entry, tmp)
{
2021-12-25 10:04:45 +08:00
if ((!entry->paused) && (!entry->markedForDeletion))
{
entry->callback(dt);
}
}
// Iterate over all the custom selectors
2021-12-25 10:04:45 +08:00
for (tHashTimerEntry* elt = _hashForTimers; elt != nullptr;)
{
2021-12-25 10:04:45 +08:00
_currentTarget = elt;
_currentTargetSalvaged = false;
2021-12-25 10:04:45 +08:00
if (!_currentTarget->paused)
{
// The 'timers' array may change while inside this loop
for (elt->timerIndex = 0; elt->timerIndex < elt->timers->num; ++(elt->timerIndex))
{
elt->currentTimer = (Timer*)(elt->timers->arr[elt->timerIndex]);
2021-12-25 10:04:45 +08:00
CCASSERT(!elt->currentTimer->isAborted(), "An aborted timer should not be updated");
elt->currentTimer->update(dt);
if (elt->currentTimer->isAborted())
{
// The currentTimer told the remove itself. To prevent the timer from
// accidentally deallocating itself before finishing its step, we retained
// it. Now that step is done, it's safe to release it.
elt->currentTimer->release();
}
2013-11-16 21:08:00 +08:00
elt->currentTimer = nullptr;
}
}
// elt, at this moment, is still valid
// so it is safe to ask this here (issue #490)
2021-12-25 10:04:45 +08:00
elt = (tHashTimerEntry*)elt->hh.next;
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
if (_currentTargetSalvaged && _currentTarget->timers->num == 0)
{
removeHashElement(_currentTarget);
}
}
2021-12-25 10:04:45 +08:00
// delete all updates that are removed in update
2021-12-25 10:04:45 +08:00
for (auto& e : _updateDeleteVector)
delete e;
2012-02-06 10:54:51 +08:00
_updateDeleteVector.clear();
_updateHashLocked = false;
2021-12-25 10:04:45 +08:00
_currentTarget = nullptr;
#if CC_ENABLE_SCRIPT_BINDING
//
// Script callbacks
//
// Iterate over all the script callbacks
if (!_scriptHandlerEntries.empty())
{
for (auto i = _scriptHandlerEntries.size() - 1; i >= 0; i--)
{
SchedulerScriptHandlerEntry* eachEntry = _scriptHandlerEntries.at(i);
if (eachEntry->isMarkedForDeletion())
{
_scriptHandlerEntries.erase(i);
}
else if (!eachEntry->isPaused())
{
eachEntry->getTimer()->update(dt);
}
}
}
#endif
//
// Functions allocated from another thread
//
// Testing size is faster than locking / unlocking.
// And almost never there will be functions scheduled to be called.
2021-12-25 10:04:45 +08:00
if (!_functionsToPerform.empty())
{
_performMutex.lock();
2021-12-25 10:04:45 +08:00
// fixed #4123: Save the callback functions, they must be invoked after '_performMutex.unlock()', otherwise if
// new functions are added in callback, it will cause thread deadlock.
auto temp = std::move(_functionsToPerform);
_performMutex.unlock();
2021-12-25 10:04:45 +08:00
for (const auto& function : temp)
{
function();
}
}
2012-02-01 16:45:23 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::schedule(SEL_SCHEDULE selector,
Ref* target,
float interval,
unsigned int repeat,
float delay,
bool paused)
{
CCASSERT(target, "Argument target must be non-nullptr");
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
2021-12-25 10:04:45 +08:00
if (!element)
{
2021-12-25 10:04:45 +08:00
element = (tHashTimerEntry*)calloc(sizeof(*element), 1);
element->target = target;
2021-12-25 10:04:45 +08:00
HASH_ADD_PTR(_hashForTimers, target, element);
2021-12-25 10:04:45 +08:00
// Is this the 1st element ? Then set the pause level to all the selectors of this target
element->paused = paused;
}
else
{
CCASSERT(element->paused == paused, "element's paused should be paused.");
}
2021-12-25 10:04:45 +08:00
if (element->timers == nullptr)
{
element->timers = ccArrayNew(10);
}
else
{
for (int i = 0; i < element->timers->num; ++i)
{
2021-12-25 10:04:45 +08:00
TimerTargetSelector* timer = dynamic_cast<TimerTargetSelector*>(element->timers->arr[i]);
if (timer && !timer->isExhausted() && selector == timer->getSelector())
{
2021-12-25 10:04:45 +08:00
CCLOG("CCScheduler#schedule. Reiniting timer with interval %.4f, repeat %u, delay %.4f", interval,
repeat, delay);
timer->setupTimerWithInterval(interval, repeat, delay);
return;
}
}
ccArrayEnsureExtraCapacity(element->timers, 1);
}
2021-12-25 10:04:45 +08:00
TimerTargetSelector* timer = new TimerTargetSelector();
timer->initWithSelector(this, selector, target, interval, repeat, delay);
ccArrayAppendObject(element->timers, timer);
timer->release();
}
2021-12-25 10:04:45 +08:00
void Scheduler::schedule(SEL_SCHEDULE selector, Ref* target, float interval, bool paused)
{
this->schedule(selector, target, interval, CC_REPEAT_FOREVER, 0.0f, paused);
}
2021-12-25 10:04:45 +08:00
bool Scheduler::isScheduled(SEL_SCHEDULE selector, const Ref* target) const
{
CCASSERT(selector, "Argument selector must be non-nullptr");
CCASSERT(target, "Argument target must be non-nullptr");
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
2021-12-25 10:04:45 +08:00
if (!element)
{
return false;
}
2021-12-25 10:04:45 +08:00
if (element->timers == nullptr)
{
return false;
}
for (int i = 0; i < element->timers->num; ++i)
{
2021-12-25 10:04:45 +08:00
TimerTargetSelector* timer = dynamic_cast<TimerTargetSelector*>(element->timers->arr[i]);
if (timer && !timer->isExhausted() && selector == timer->getSelector())
{
return true;
}
}
2021-12-25 10:04:45 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
void Scheduler::unschedule(SEL_SCHEDULE selector, Ref* target)
{
// explicit handle nil arguments when removing an object
if (target == nullptr || selector == nullptr)
{
return;
}
2021-12-25 10:04:45 +08:00
tHashTimerEntry* element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
2021-12-25 10:04:45 +08:00
if (element)
{
for (int i = 0; i < element->timers->num; ++i)
{
2021-12-25 10:04:45 +08:00
TimerTargetSelector* timer = dynamic_cast<TimerTargetSelector*>(element->timers->arr[i]);
if (timer && selector == timer->getSelector())
{
if (timer == element->currentTimer && !timer->isAborted())
{
timer->retain();
timer->setAborted();
}
2021-12-25 10:04:45 +08:00
ccArrayRemoveObjectAtIndex(element->timers, i, true);
2021-12-25 10:04:45 +08:00
// update timerIndex in case we are in tick:, looping over the actions
if (element->timerIndex >= i)
{
element->timerIndex--;
}
2021-12-25 10:04:45 +08:00
if (element->timers->num == 0)
{
if (_currentTarget == element)
{
_currentTargetSalvaged = true;
}
else
{
removeHashElement(element);
}
}
2021-12-25 10:04:45 +08:00
return;
}
}
}
}
2012-04-18 18:43:45 +08:00
NS_CC_END