ci / ubuntu-latest / clang (push) Failing after 32s
ci / ubuntu-latest / gcc (push) Failing after 42s
ci / coverage / gcc (push) Failing after 32s
ci / macos-latest / apple-clang (push) Canceled after 0s
ci / windows-latest / mingw (push) Canceled after 0s
ci / windows-latest / msvc (push) Canceled after 0s
373 lines
14 KiB
C++
373 lines
14 KiB
C++
//===--------------------------------------------------------------===//
|
||
// VDCS - 几何约束求解引擎
|
||
// File: tests/unit/test_thread_safety.cpp
|
||
// Description: C ABI 并发句柄契约(行业级宿主前提:不同 handle 可并行使
|
||
// 用)——纯确定性断言、无任何计时断言:
|
||
// 1. 独立模型并发求解 == 串行基准(8 线程 × 8 轮,1e-9
|
||
// 逐参数对拍;收敛标志与残差上限同步校验);
|
||
// 2. 并发建实体:UID 全局唯一(跨 64 个模型无重复;覆盖
|
||
// 实体与约束两类出参);
|
||
// 3. 关节链全局注册表并发 create/IK/destroy:互不串扰
|
||
// (prismatic 目标 10mm 逐线程逐位 1e-9,句柄独立销毁);
|
||
// 4. 并发原生序列化 save/load:8 线程 × 4 轮各自独立文件,
|
||
// 加载后求解与串行基准逐参数 1e-9(UID 跨序列化保持);
|
||
// 5. 并发模型级求解回调:8 线程 × 4 轮各自计数器,事件数
|
||
// 与串行基准严格一致(回调无跨句柄串扰)。
|
||
// Note: 本测试只证明「独立句柄并行」契约;同一 handle 的并发读写
|
||
// 不在契约内(宿主职责)。
|
||
//===--------------------------------------------------------------===//
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <atomic>
|
||
#include <cmath>
|
||
#include <cstddef>
|
||
#include <cstdio>
|
||
#include <mutex>
|
||
#include <set>
|
||
#include <thread>
|
||
#include <vector>
|
||
|
||
#include "vdcs/c_api/vdcs_c_api.h"
|
||
|
||
namespace {
|
||
|
||
constexpr int kPointsPerModel = 24; // p0 + 23 派生点
|
||
constexpr int kThreads = 8;
|
||
constexpr int kRoundsPerThread = 8;
|
||
constexpr int kJointRoundsPerThread = 24;
|
||
constexpr int kSerializationRounds = 4;
|
||
constexpr int kCallbackRounds = 4;
|
||
constexpr double kParamTolerance = 1e-9;
|
||
constexpr double kResidualLimit = 1e-8;
|
||
// IK 引擎收敛判据为位置残差 1e-3(src/kinematics/joint_chain.cpp
|
||
// kPosToleranceMm)——关节值断言取同口径,跨线程一致性另按逐位相等
|
||
constexpr double kIkValueTolerance = 1e-3;
|
||
|
||
struct ChainModel {
|
||
vdcs_model* model = nullptr;
|
||
std::vector<vdcs_uid> points;
|
||
std::vector<vdcs_uid> uids; // 全部出参 UID(实体 + 约束)
|
||
};
|
||
|
||
void Destroy(ChainModel& cm) {
|
||
if (cm.model != nullptr) {
|
||
vdcs_model_destroy(cm.model);
|
||
cm.model = nullptr;
|
||
}
|
||
}
|
||
|
||
/// 构造定解模型:p0 Fixed(1.5,-2.0);p_i 由 DistanceX/DistanceY(p0,p_i)
|
||
/// 双侧钉扎——参数总数 = 方程数(2 + 2·23 = 48 = 2·24),解唯一。
|
||
bool BuildChainModel(ChainModel& cm) {
|
||
if (vdcs_model_create(&cm.model) != VDCS_OK) return false;
|
||
vdcs_uid p0 = 0;
|
||
vdcs_uid c = 0;
|
||
if (vdcs_add_point2d(cm.model, "p0", 1.5, -2.0, &p0) != VDCS_OK) return false;
|
||
cm.points.push_back(p0);
|
||
cm.uids.push_back(p0);
|
||
if (vdcs_add_fixed2d(cm.model, nullptr, p0, &c) != VDCS_OK) return false;
|
||
cm.uids.push_back(c);
|
||
for (int i = 1; i < kPointsPerModel; ++i) {
|
||
vdcs_uid p = 0;
|
||
if (vdcs_add_point2d(cm.model, nullptr, 10.0 * i + 2.5, 3.0, &p) !=
|
||
VDCS_OK) {
|
||
return false;
|
||
}
|
||
cm.points.push_back(p);
|
||
cm.uids.push_back(p);
|
||
if (vdcs_add_distance_x2d(cm.model, nullptr, p0, p, 10.0 * i, &c) !=
|
||
VDCS_OK) {
|
||
return false;
|
||
}
|
||
cm.uids.push_back(c);
|
||
if (vdcs_add_distance_y2d(cm.model, nullptr, p0, p, 0.0, &c) !=
|
||
VDCS_OK) {
|
||
return false;
|
||
}
|
||
cm.uids.push_back(c);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
struct Solution {
|
||
bool ok = false;
|
||
bool converged = false;
|
||
double residual = 0.0;
|
||
std::vector<double> values; // 逐点 x,y(与 points 同序)
|
||
};
|
||
|
||
Solution SolveAndRead(ChainModel& cm) {
|
||
Solution s;
|
||
vdcs_solve_options opts{};
|
||
opts.max_iterations = 200;
|
||
opts.tolerance = 1e-10;
|
||
opts.initial_damping = 1e-3;
|
||
vdcs_solve_result* res = nullptr;
|
||
if (vdcs_solve(cm.model, &opts, &res) != VDCS_OK) return s;
|
||
s.converged = vdcs_solve_result_converged(res) != 0;
|
||
s.residual = vdcs_solve_result_residual(res);
|
||
vdcs_solve_result_free(res);
|
||
s.values.reserve(cm.points.size() * 2);
|
||
for (const vdcs_uid uid : cm.points) {
|
||
double x = 0.0;
|
||
double y = 0.0;
|
||
if (vdcs_entity_get_param(cm.model, uid, "x", &x) != VDCS_OK) return s;
|
||
if (vdcs_entity_get_param(cm.model, uid, "y", &y) != VDCS_OK) return s;
|
||
s.values.push_back(x);
|
||
s.values.push_back(y);
|
||
}
|
||
s.ok = true;
|
||
return s;
|
||
}
|
||
|
||
bool ValuesMatch(const std::vector<double>& got,
|
||
const std::vector<double>& expected) {
|
||
if (got.size() != expected.size()) return false;
|
||
for (std::size_t i = 0; i < got.size(); ++i) {
|
||
if (!std::isfinite(got[i])) return false;
|
||
if (std::fabs(got[i] - expected[i]) > kParamTolerance) return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
struct SharedUids {
|
||
std::mutex mutex;
|
||
std::vector<vdcs_uid> all;
|
||
void Add(const std::vector<vdcs_uid>& v) {
|
||
std::lock_guard<std::mutex> lock(mutex);
|
||
all.insert(all.end(), v.begin(), v.end());
|
||
}
|
||
};
|
||
|
||
struct CallbackCounter {
|
||
std::atomic<int> calls{0};
|
||
};
|
||
|
||
// 模型级求解回调:只做原子计数(求解可能在工作线程上报,避免非原子写)
|
||
void CountEvents(const vdcs_solve_event* event, void* user) {
|
||
(void)event;
|
||
static_cast<CallbackCounter*>(user)->calls.fetch_add(
|
||
1, std::memory_order_relaxed);
|
||
}
|
||
|
||
bool RunModelWorker(const std::vector<double>& expected, SharedUids* shared) {
|
||
for (int r = 0; r < kRoundsPerThread; ++r) {
|
||
ChainModel cm;
|
||
const bool built = BuildChainModel(cm);
|
||
bool ok = built;
|
||
Solution got;
|
||
if (built) {
|
||
got = SolveAndRead(cm);
|
||
shared->Add(cm.uids);
|
||
ok = got.ok && got.converged && got.residual <= kResidualLimit &&
|
||
ValuesMatch(got.values, expected);
|
||
}
|
||
Destroy(cm);
|
||
if (!ok) return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(ThreadSafetyTest, IndependentModelsMatchSequentialBaseline) {
|
||
ChainModel base;
|
||
ASSERT_TRUE(BuildChainModel(base));
|
||
const Solution ref = SolveAndRead(base);
|
||
Destroy(base);
|
||
ASSERT_TRUE(ref.ok);
|
||
ASSERT_TRUE(ref.converged);
|
||
ASSERT_LE(ref.residual, kResidualLimit);
|
||
ASSERT_EQ(ref.values.size(),
|
||
static_cast<std::size_t>(kPointsPerModel) * 2);
|
||
|
||
SharedUids shared;
|
||
std::vector<std::thread> threads;
|
||
std::vector<int> results(kThreads, 0);
|
||
for (int t = 0; t < kThreads; ++t) {
|
||
threads.emplace_back([&, t] {
|
||
results[t] = RunModelWorker(ref.values, &shared) ? 1 : 0;
|
||
});
|
||
}
|
||
for (std::thread& th : threads) th.join();
|
||
for (int t = 0; t < kThreads; ++t) {
|
||
EXPECT_EQ(results[t], 1) << "thread " << t;
|
||
}
|
||
|
||
static_assert(kThreads * kRoundsPerThread == 64,
|
||
"UID 唯一性基数按 64 个模型计");
|
||
EXPECT_EQ(shared.all.size(),
|
||
static_cast<std::size_t>(kThreads * kRoundsPerThread) *
|
||
(kPointsPerModel + 1 + 2 * (kPointsPerModel - 1)));
|
||
std::set<vdcs_uid> unique(shared.all.begin(), shared.all.end());
|
||
EXPECT_EQ(unique.size(), shared.all.size());
|
||
}
|
||
|
||
TEST(ThreadSafetyTest, JointChainRegistryConcurrentLifecycle) {
|
||
std::vector<std::thread> threads;
|
||
std::vector<int> results(kThreads, 0); // 0=未完成,1=OK,其余见 worker
|
||
std::vector<double> got(kThreads, 0.0);
|
||
for (int t = 0; t < kThreads; ++t) {
|
||
threads.emplace_back([&, t] {
|
||
int code = 1;
|
||
for (int r = 0; r < kJointRoundsPerThread && code == 1; ++r) {
|
||
vdcs_joint_desc jd{};
|
||
jd.parent_body = 900000 + t;
|
||
jd.child_body = 900001 + t;
|
||
jd.type = 1; // prismatic
|
||
jd.axis[2] = 1.0;
|
||
jd.limit_min = 0.0;
|
||
jd.limit_max = 25.0;
|
||
vdcs_uid chain = 0;
|
||
if (vdcs_joint_chain_create(&jd, 1, &chain) != VDCS_OK) {
|
||
code = 2;
|
||
break;
|
||
}
|
||
vdcs_ik_target target{};
|
||
target.end_body = jd.child_body;
|
||
target.position[2] = 10.0;
|
||
double values[1] = {0.0};
|
||
int count = 0;
|
||
vdcs_ik_result res{};
|
||
const vdcs_result solved =
|
||
vdcs_ik_solve(chain, &target, 1, &res, values, &count);
|
||
if (solved != VDCS_OK) {
|
||
code = 3;
|
||
} else if (count != 1 || res.converged == 0 ||
|
||
res.residual_mm > kIkValueTolerance) {
|
||
code = 4;
|
||
} else if (std::fabs(values[0] - 10.0) > kIkValueTolerance) {
|
||
code = 5;
|
||
}
|
||
got[t] = values[0];
|
||
if (vdcs_joint_chain_destroy(chain) != VDCS_OK) code = 6;
|
||
}
|
||
results[t] = code;
|
||
});
|
||
}
|
||
for (std::thread& th : threads) th.join();
|
||
for (int t = 0; t < kThreads; ++t) {
|
||
EXPECT_EQ(results[t], 1) << "thread " << t << " value=" << got[t];
|
||
}
|
||
// 同构输入独立链:求解结果逐位一致(注册表/UID 分配无串扰)
|
||
for (int t = 1; t < kThreads; ++t) {
|
||
EXPECT_EQ(got[t], got[0]) << "thread " << t;
|
||
}
|
||
}
|
||
|
||
TEST(ThreadSafetyTest, ConcurrentSaveLoadIndependentModels) {
|
||
ChainModel base;
|
||
ASSERT_TRUE(BuildChainModel(base));
|
||
const Solution ref = SolveAndRead(base);
|
||
Destroy(base);
|
||
ASSERT_TRUE(ref.ok);
|
||
ASSERT_TRUE(ref.converged);
|
||
ASSERT_LE(ref.residual, kResidualLimit);
|
||
|
||
std::vector<std::thread> threads;
|
||
std::vector<int> results(kThreads, 0);
|
||
for (int t = 0; t < kThreads; ++t) {
|
||
threads.emplace_back([&, t] {
|
||
int code = 1;
|
||
for (int r = 0; r < kSerializationRounds && code == 1; ++r) {
|
||
ChainModel cm;
|
||
if (!BuildChainModel(cm)) {
|
||
Destroy(cm);
|
||
code = 2;
|
||
break;
|
||
}
|
||
char path[96];
|
||
std::snprintf(path, sizeof(path), "vdcs_ts_%d_%d.vdcs", t, r);
|
||
// RAII 清理:失败分支 break 也不遗留临时文件
|
||
struct FileRemover {
|
||
const char* path;
|
||
~FileRemover() { std::remove(path); }
|
||
} remover{path};
|
||
const std::vector<vdcs_uid> points = cm.points;
|
||
if (vdcs_model_save(cm.model, path) != VDCS_OK) {
|
||
Destroy(cm);
|
||
code = 3;
|
||
break;
|
||
}
|
||
Destroy(cm);
|
||
vdcs_model* loaded = nullptr;
|
||
if (vdcs_model_load(&loaded, path) != VDCS_OK ||
|
||
loaded == nullptr) {
|
||
if (loaded != nullptr) vdcs_model_destroy(loaded);
|
||
code = 4;
|
||
} else {
|
||
ChainModel lm;
|
||
lm.model = loaded;
|
||
lm.points = points; // UID 跨序列化保持(serializer 契约)
|
||
const Solution got = SolveAndRead(lm);
|
||
if (!got.ok || !got.converged ||
|
||
got.residual > kResidualLimit ||
|
||
!ValuesMatch(got.values, ref.values)) {
|
||
code = 5;
|
||
}
|
||
Destroy(lm);
|
||
}
|
||
}
|
||
results[t] = code;
|
||
});
|
||
}
|
||
for (std::thread& th : threads) th.join();
|
||
for (int t = 0; t < kThreads; ++t) {
|
||
EXPECT_EQ(results[t], 1) << "thread " << t;
|
||
}
|
||
}
|
||
|
||
TEST(ThreadSafetyTest, ConcurrentSolveCallbacksPerModel) {
|
||
ChainModel base;
|
||
ASSERT_TRUE(BuildChainModel(base));
|
||
CallbackCounter ref_counter;
|
||
ASSERT_EQ(vdcs_set_solve_callback(base.model, &CountEvents, &ref_counter),
|
||
VDCS_OK);
|
||
const Solution ref = SolveAndRead(base);
|
||
Destroy(base);
|
||
ASSERT_TRUE(ref.ok);
|
||
ASSERT_TRUE(ref.converged);
|
||
ASSERT_LE(ref.residual, kResidualLimit);
|
||
ASSERT_GE(ref_counter.calls.load(), 1);
|
||
const int ref_calls = ref_counter.calls.load();
|
||
|
||
std::vector<std::thread> threads;
|
||
std::vector<int> results(kThreads, 0);
|
||
std::vector<int> calls(kThreads, -1);
|
||
for (int t = 0; t < kThreads; ++t) {
|
||
threads.emplace_back([&, t] {
|
||
int code = 1;
|
||
CallbackCounter counter;
|
||
for (int r = 0; r < kCallbackRounds && code == 1; ++r) {
|
||
ChainModel cm;
|
||
if (!BuildChainModel(cm)) {
|
||
Destroy(cm);
|
||
code = 2;
|
||
break;
|
||
}
|
||
if (vdcs_set_solve_callback(cm.model, &CountEvents, &counter) !=
|
||
VDCS_OK) {
|
||
Destroy(cm);
|
||
code = 3;
|
||
break;
|
||
}
|
||
const Solution got = SolveAndRead(cm);
|
||
if (!got.ok || !got.converged ||
|
||
got.residual > kResidualLimit ||
|
||
!ValuesMatch(got.values, ref.values)) {
|
||
code = 4;
|
||
}
|
||
Destroy(cm);
|
||
}
|
||
calls[t] = counter.calls.load();
|
||
results[t] = code;
|
||
});
|
||
}
|
||
for (std::thread& th : threads) th.join();
|
||
for (int t = 0; t < kThreads; ++t) {
|
||
EXPECT_EQ(results[t], 1) << "thread " << t;
|
||
// 每轮事件数与串行基准严格一致:回调无跨句柄串扰
|
||
EXPECT_EQ(calls[t], ref_calls * kCallbackRounds) << "thread " << t;
|
||
}
|
||
}
|