unscheduleAll() does not unschedule scheduled with performFunctionInCocosThread. (#16856)

* Fixing issue where Scheduler::unscheduleAll() would not unschedule functions scheduled with Scheduler::performFunctionInCocosThread.

* Removing clear of _functionsToPerform from unscheduleAll, based on code review feedback. Adding a function removeAllFunctionsToBePerfomedInCocosThread, which will remove all pending functions scheduled with performFunctionOnCocosThread.

* Adding test case for Scheduler::removeAllFunctionsToBePerformedInCocosThread

* Adding proper cleanup code to Scheduler test for SchedulerRemoveAllFunctionsToBePerformedInCocosThread
This commit is contained in:
David DeSimone 2016-12-07 19:58:58 -08:00 committed by minggo
parent 964b65e44e
commit 3b7ffcf5d0
4 changed files with 76 additions and 0 deletions

View File

@ -825,6 +825,12 @@ void Scheduler::performFunctionInCocosThread(const std::function<void ()> &funct
_performMutex.unlock();
}
void Scheduler::removeAllFunctionsToBePerformedInCocosThread()
{
std::unique_lock<std::mutex> lock(_performMutex);
_functionsToPerform.clear();
}
// main loop
void Scheduler::update(float dt)
{

View File

@ -430,6 +430,15 @@ public:
*/
void performFunctionInCocosThread( const std::function<void()> &function);
/**
* Remove all pending functions queued to be performed with Scheduler::performFunctionInCocosThread
* Functions unscheduled in this manner will not be executed
* This function is thread safe
* @since v3.14
* @js NA
*/
void removeAllFunctionsToBePerformedInCocosThread();
/////////////////////////////////////
// Deprecated methods:

View File

@ -29,6 +29,7 @@ SchedulerTests::SchedulerTests()
ADD_TEST_CASE(ScheduleCallbackTest);
ADD_TEST_CASE(ScheduleUpdatePriority);
ADD_TEST_CASE(SchedulerIssue10232);
ADD_TEST_CASE(SchedulerRemoveAllFunctionsToBePerformedInCocosThread)
};
//------------------------------------------------------------------
@ -1147,3 +1148,48 @@ std::string SchedulerIssue10232::subtitle() const
{
return "Should not crash";
}
void SchedulerRemoveAllFunctionsToBePerformedInCocosThread::onEnter()
{
SchedulerTestLayer::onEnter();
_sprite = Sprite::create("Images/grossinis_sister1.png");
_sprite->setPosition(VisibleRect::center());
this->addChild(_sprite);
this->scheduleUpdate();
}
void SchedulerRemoveAllFunctionsToBePerformedInCocosThread::onExit()
{
SchedulerTestLayer::onExit();
this->unscheduleUpdate();
}
void SchedulerRemoveAllFunctionsToBePerformedInCocosThread::update(float dt) {
Director::getInstance()->getScheduler()->performFunctionInCocosThread([this] () {
_sprite->setVisible(false);
});
Director::getInstance()->getScheduler()->performFunctionInCocosThread([this] () {
_sprite->setVisible(false);
});
Director::getInstance()->getScheduler()->performFunctionInCocosThread([this] () {
_sprite->setVisible(false);
});
Director::getInstance()->getScheduler()->performFunctionInCocosThread([this] () {
_sprite->setVisible(false);
});
Director::getInstance()->getScheduler()->performFunctionInCocosThread([this] () {
_sprite->setVisible(false);
});
Director::getInstance()->getScheduler()->removeAllFunctionsToBePerformedInCocosThread();
}
std::string SchedulerRemoveAllFunctionsToBePerformedInCocosThread::title() const
{
return "Removing pending main thread tasks";
}
std::string SchedulerRemoveAllFunctionsToBePerformedInCocosThread::subtitle() const
{
return "Sprite should be visible";
}

View File

@ -326,4 +326,19 @@ public:
void update(float dt) override;
};
class SchedulerRemoveAllFunctionsToBePerformedInCocosThread : public SchedulerTestLayer
{
public:
CREATE_FUNC(SchedulerRemoveAllFunctionsToBePerformedInCocosThread);
virtual std::string title() const override;
virtual std::string subtitle() const override;
void onEnter() override;
void onExit() override;
void update(float dt) override;
private:
cocos2d::Sprite *_sprite;
};
#endif