Renaming performFunctionInCocosThread to runOnAxmolThread

This commit is contained in:
halx99 2023-01-01 18:08:25 +08:00
parent 0f97e3fa3a
commit 319aaf3a49
2 changed files with 17 additions and 11 deletions

View File

@ -264,7 +264,7 @@ Scheduler::Scheduler()
#endif
{
// I don't expect to have more than 30 functions to all per frame
_functionsToPerform.reserve(30);
_actionsToPerform.reserve(30);
}
Scheduler::~Scheduler()
@ -812,16 +812,16 @@ void Scheduler::resumeTargets(const std::set<void*>& targetsToResume)
}
}
void Scheduler::performFunctionInCocosThread(std::function<void()> function)
void Scheduler::runOnAxmolThread(std::function<void()> action)
{
std::lock_guard<std::mutex> lock(_performMutex);
_functionsToPerform.emplace_back(std::move(function));
_actionsToPerform.emplace_back(std::move(action));
}
void Scheduler::removeAllFunctionsToBePerformedInCocosThread()
void Scheduler::removeAllPendingActions()
{
std::unique_lock<std::mutex> lock(_performMutex);
_functionsToPerform.clear();
_actionsToPerform.clear();
}
// main loop
@ -944,12 +944,12 @@ void Scheduler::update(float dt)
// Testing size is faster than locking / unlocking.
// And almost never there will be functions scheduled to be called.
if (!_functionsToPerform.empty())
if (!_actionsToPerform.empty())
{
_performMutex.lock();
// 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);
auto temp = std::move(_actionsToPerform);
_performMutex.unlock();
for (const auto& function : temp)

View File

@ -449,7 +449,12 @@ public:
@since v3.0
@js NA
*/
void performFunctionInCocosThread(std::function<void()> function);
void runOnAxmolThread(std::function<void()> action);
AX_DEPRECATED_ATTRIBUTE void performFunctionInCocosThread(std::function<void()> action)
{
runOnAxmolThread(std::move(action));
}
/**
* Remove all pending functions queued to be performed with Scheduler::performFunctionInCocosThread
@ -458,7 +463,8 @@ public:
* @since v3.14
* @js NA
*/
void removeAllFunctionsToBePerformedInCocosThread();
void removeAllPendingActions();
AX_DEPRECATED_ATTRIBUTE void removeAllFunctionsToBePerformedInCocosThread() { removeAllPendingActions(); }
protected:
/** Schedules the 'callback' function for a given target with a given priority.
@ -501,8 +507,8 @@ protected:
Vector<SchedulerScriptHandlerEntry*> _scriptHandlerEntries;
#endif
// Used for "perform Function"
std::vector<std::function<void()>> _functionsToPerform;
// Used for "perform action"
std::vector<std::function<void()>> _actionsToPerform;
std::mutex _performMutex;
};