Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the order of callback by performFunctionInCocosThread and add unit-test for it. #17543

Merged
merged 19 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions native/cocos/base/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,7 @@ void Scheduler::runFunctionsToBePerformedInCocosThread() {
}

std::lock_guard<std::mutex> lk(_performMutex);
for (; iter != temp.end(); ++iter) {
_functionsToPerform.emplace_back(std::move(*iter));
}
_functionsToPerform.insert(_functionsToPerform.begin(), std::make_move_iterator(iter), std::make_move_iterator(temp.end()));
}
}

Expand Down
66 changes: 66 additions & 0 deletions native/tests/unit-test/src/scheduler_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/****************************************************************************
Copyright (c) 2024 Xiamen Yaji Software Co., Ltd.

http://www.cocos.com

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.
****************************************************************************/
#include <vector>
#include <chrono>
#include <thread>

#include "base/Scheduler.h"
#include "utils.h"

using namespace cc;

TEST(schedulerTest, performInCocosThreadOrder) {
auto scheduler = std::make_shared<Scheduler>();

std::vector<int> orderResult;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a thread-safe container for orderResult to prevent potential race conditions


for (int i = 0; i < 10; ++i) {
auto task = [&orderResult, i, &scheduler](){
orderResult.emplace_back(i);

if (i == 5) {
scheduler->performFunctionInCocosThread([&orderResult](){
orderResult.emplace_back(10);
});

scheduler->performFunctionInCocosThread([&orderResult](){
orderResult.emplace_back(11);
});

scheduler->performFunctionInCocosThread([&orderResult](){
orderResult.emplace_back(12);
});

std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
};
scheduler->performFunctionInCocosThread(task);
}

scheduler->runFunctionsToBePerformedInCocosThread();
scheduler->runFunctionsToBePerformedInCocosThread();

const std::vector<int> expectedResult{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
EXPECT_EQ(orderResult, expectedResult);
}
Loading