Skip to content

Commit ca0a7f3

Browse files
David DeSimoneminggo
authored andcommitted
Changing AsyncTaskPool/Scheduler to better leverage move semantics (#17727)
* Changing CCAsyncTaskPool and CCScheduler to better leverage move semantics, to avoid several lambda copy constructor calls. Chaning call sites in the cocos engine to better leverage move semantics. * Editing binding generator to not generate bindings for AsyncTaskPool::enqueue. Bindings were not previously generated for this function due to it's type signature involving a template parameter. * Adding missing std::move call in AsyncTaskPool to skip a copy constructor call.
1 parent 9e49589 commit ca0a7f3

File tree

7 files changed

+37
-28
lines changed

7 files changed

+37
-28
lines changed

cocos/base/CCAsyncTaskPool.h

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,19 @@ class CC_DLL AsyncTaskPool
8989
* @param type task type is io task, network task or others, each type of task has a thread to deal with it.
9090
* @param callback callback when the task is finished. The callback is called in the main thread instead of task thread.
9191
* @param callbackParam parameter used by the callback.
92-
* @param f task can be lambda function.
92+
* @param task: task can be lambda function to be performed off thread.
9393
* @lua NA
9494
*/
95-
template<class F>
96-
inline void enqueue(TaskType type, const TaskCallBack& callback, void* callbackParam, F&& f);
95+
void enqueue(TaskType type, TaskCallBack callback, void* callbackParam, std::function<void()> task);
96+
97+
/**
98+
* Enqueue a asynchronous task.
99+
*
100+
* @param type task type is io task, network task or others, each type of task has a thread to deal with it.
101+
* @param task: task can be lambda function to be performed off thread.
102+
* @lua NA
103+
*/
104+
void enqueue(AsyncTaskPool::TaskType type, std::function<void()> task);
97105

98106
CC_CONSTRUCTOR_ACCESS:
99107
AsyncTaskPool();
@@ -132,7 +140,7 @@ class CC_DLL AsyncTaskPool
132140
}
133141

134142
task();
135-
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, callback]{ callback.callback(callback.callbackParam); });
143+
Director::getInstance()->getScheduler()->performFunctionInCocosThread(std::bind(callback.callback, callback.callbackParam));
136144
}
137145
}
138146
);
@@ -159,11 +167,13 @@ class CC_DLL AsyncTaskPool
159167
while (_taskCallBacks.size())
160168
_taskCallBacks.pop();
161169
}
162-
template<class F>
163-
void enqueue(const TaskCallBack& callback, void* callbackParam, F&& f)
170+
171+
void enqueue(TaskCallBack callback, void* callbackParam, std::function<void()> task)
164172
{
165-
auto task = f;//std::bind(std::forward<F>(f), std::forward<Args>(args)...);
166-
173+
AsyncTaskCallBack taskCallBack;
174+
taskCallBack.callback = std::move(callback);
175+
taskCallBack.callbackParam = callbackParam;
176+
167177
{
168178
std::unique_lock<std::mutex> lock(_queueMutex);
169179

@@ -174,11 +184,8 @@ class CC_DLL AsyncTaskPool
174184
return;
175185
}
176186

177-
AsyncTaskCallBack taskCallBack;
178-
taskCallBack.callback = callback;
179-
taskCallBack.callbackParam = callbackParam;
180-
_tasks.emplace([task](){ task(); });
181-
_taskCallBacks.emplace(taskCallBack);
187+
_tasks.push(std::move(task));
188+
_taskCallBacks.push(std::move(taskCallBack));
182189
}
183190
_condition.notify_one();
184191
}
@@ -188,7 +195,7 @@ class CC_DLL AsyncTaskPool
188195
std::thread _thread;
189196
// the task queue
190197
std::queue< std::function<void()> > _tasks;
191-
std::queue<AsyncTaskCallBack> _taskCallBacks;
198+
std::queue<AsyncTaskCallBack> _taskCallBacks;
192199

193200
// synchronization
194201
std::mutex _queueMutex;
@@ -208,14 +215,17 @@ inline void AsyncTaskPool::stopTasks(TaskType type)
208215
threadTask.clear();
209216
}
210217

211-
template<class F>
212-
inline void AsyncTaskPool::enqueue(AsyncTaskPool::TaskType type, const TaskCallBack& callback, void* callbackParam, F&& f)
218+
inline void AsyncTaskPool::enqueue(AsyncTaskPool::TaskType type, TaskCallBack callback, void* callbackParam, std::function<void()> task)
213219
{
214220
auto& threadTask = _threadTasks[(int)type];
215221

216-
threadTask.enqueue(callback, callbackParam, f);
222+
threadTask.enqueue(std::move(callback), callbackParam, std::move(task));
217223
}
218224

225+
inline void AsyncTaskPool::enqueue(AsyncTaskPool::TaskType type, std::function<void()> task)
226+
{
227+
enqueue(type, [](void*) {}, nullptr, std::move(task));
228+
}
219229

220230
NS_CC_END
221231
// end group

cocos/base/CCScheduler.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -804,13 +804,10 @@ void Scheduler::resumeTargets(const std::set<void*>& targetsToResume)
804804
}
805805
}
806806

807-
void Scheduler::performFunctionInCocosThread(const std::function<void ()> &function)
807+
void Scheduler::performFunctionInCocosThread(std::function<void ()> function)
808808
{
809-
_performMutex.lock();
810-
811-
_functionsToPerform.push_back(function);
812-
813-
_performMutex.unlock();
809+
std::lock_guard<std::mutex> lock(_performMutex);
810+
_functionsToPerform.push_back(std::move(function));
814811
}
815812

816813
void Scheduler::removeAllFunctionsToBePerformedInCocosThread()

cocos/base/CCScheduler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ class CC_DLL Scheduler : public Ref
426426
@since v3.0
427427
@js NA
428428
*/
429-
void performFunctionInCocosThread( const std::function<void()> &function);
429+
void performFunctionInCocosThread(std::function<void()> function);
430430

431431
/**
432432
* Remove all pending functions queued to be performed with Scheduler::performFunctionInCocosThread

cocos/base/ccUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void onCaptureScreen(const std::function<void(bool, const std::string&)>& afterC
138138
startedCapture = false;
139139
};
140140

141-
AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, mainThread, nullptr, [image, outputFile]()
141+
AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, std::move(mainThread), nullptr, [image, outputFile]()
142142
{
143143
succeedSaveToFile = image->saveToFile(outputFile);
144144
delete image;

extensions/assets-manager/AssetsManagerEx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ void AssetsManagerEx::decompressDownloadedZip(const std::string &customId, const
493493
}
494494
delete dataInner;
495495
};
496-
AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_OTHER, decompressFinished, (void*)asyncData, [this, asyncData]() {
496+
AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_OTHER, std::move(decompressFinished), (void*)asyncData, [this, asyncData]() {
497497
// Decompress all compressed files
498498
if (decompress(asyncData->zipFile))
499499
{

tools/tojs/cocos2dx.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ skip = Node::[^setPosition$ setGLServerState description getUserObject .*UserDat
140140
Camera::[unproject isVisibleInFrustum],
141141
ClippingNode::[init],
142142
RenderState::[setStateBlock],
143-
ComponentJS::[create getScriptObject update]
143+
ComponentJS::[create getScriptObject update],
144+
AsyncTaskPool::[enqueue]
144145

145146
rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame],
146147
MenuItemFont::[setFontNameObj=setFontName setFontSizeObj=setFontSize getFontSizeObj=getFontSize getFontNameObj=getFontName],

tools/tolua/cocos2dx.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS
144144
RenderState::[finalize setStateBlock],
145145
Properties::[createNonRefCounted],
146146
AutoPolygon::[trace reduce expand triangulate calculateUV generateTriangles generatePolygon],
147-
PolygonInfo::[operator=]
147+
PolygonInfo::[operator=],
148+
AsyncTaskPool::[enqueue]
148149

149150
rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame],
150151
ProgressTimer::[setReverseProgress=setReverseDirection],

0 commit comments

Comments
 (0)