From 4d9db06d669044c0c461a2aed79c85c7fe32a502 Mon Sep 17 00:00:00 2001 From: Han-Lin Chen Date: Tue, 29 Oct 2024 08:57:55 +0000 Subject: libcamera: add method to set thread affinity Add method to set thread affinity to Thread class. Signed-off-by: Han-Lin Chen Co-developed-by: Harvey Yang Signed-off-by: Harvey Yang Reviewed-by: Kieran Bingham Signed-off-by: Kieran Bingham --- test/threads.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'test') diff --git a/test/threads.cpp b/test/threads.cpp index ceb4fa0f..8d6ee151 100644 --- a/test/threads.cpp +++ b/test/threads.cpp @@ -9,9 +9,11 @@ #include #include #include +#include #include #include +#include #include #include "test.h" @@ -66,6 +68,27 @@ private: bool &cancelled_; }; +class CpuSetTester : public Object +{ +public: + CpuSetTester(unsigned int cpuset) + : cpuset_(cpuset) {} + + bool testCpuSet() + { + int ret = sched_getcpu(); + if (static_cast(cpuset_) != ret) { + cout << "Invalid cpuset: " << ret << ", expecting: " << cpuset_ << endl; + return false; + } + + return true; + } + +private: + const unsigned int cpuset_; +}; + class ThreadTest : public Test { protected: @@ -165,6 +188,23 @@ protected: return TestFail; } + const unsigned int numCpus = std::thread::hardware_concurrency(); + for (unsigned int i = 0; i < numCpus; ++i) { + thread = std::make_unique(); + const std::array cpus{ i }; + thread->setThreadAffinity(cpus); + thread->start(); + + CpuSetTester tester(i); + tester.moveToThread(thread.get()); + + if (!tester.invokeMethod(&CpuSetTester::testCpuSet, ConnectionTypeBlocking)) + return TestFail; + + thread->exit(0); + thread->wait(); + } + return TestPass; } -- cgit v1.2.1