Thread: Add name parameter

For debugging purposes, threads can be assigned a name, which eases
distinguishing between them in e.g. htop or gdb. This uses a
Linux-specific API for now which is limited to 15 characters (+ null
terminator), so truncation is done and names for existing thread
instantiations were chosen to be consise.

[Kieran: Apply checkstyle suggestions, rebase on proxy rework]
Signed-off-by: Schulz, Andreas <andreas.schulz2@karlstorz.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Schulz, Andreas
2025-11-05 11:31:10 +00:00
committed by Kieran Bingham
parent 6af90deaf2
commit 559128b1f1
6 changed files with 17 additions and 7 deletions
+3 -1
View File
@@ -8,6 +8,7 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <string>
#include <sys/types.h> #include <sys/types.h>
#include <thread> #include <thread>
@@ -30,7 +31,7 @@ class ThreadMain;
class Thread class Thread
{ {
public: public:
Thread(); Thread(std::string name = {});
virtual ~Thread(); virtual ~Thread();
void start(); void start();
@@ -74,6 +75,7 @@ private:
void moveObject(Object *object, ThreadData *currentData, void moveObject(Object *object, ThreadData *currentData,
ThreadData *targetData); ThreadData *targetData);
std::string name_;
std::thread thread_; std::thread thread_;
std::unique_ptr<ThreadData> data_; std::unique_ptr<ThreadData> data_;
}; };
+9 -2
View File
@@ -10,6 +10,7 @@
#include <atomic> #include <atomic>
#include <list> #include <list>
#include <optional> #include <optional>
#include <pthread.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
@@ -144,6 +145,7 @@ class ThreadMain : public Thread
{ {
public: public:
ThreadMain() ThreadMain()
: Thread("libcamera-main")
{ {
data_->running_ = true; data_->running_ = true;
} }
@@ -234,8 +236,9 @@ ThreadData *ThreadData::current()
/** /**
* \brief Create a thread * \brief Create a thread
*/ */
Thread::Thread() Thread::Thread(std::string name)
: data_(std::make_unique<ThreadData>(this)) : name_(std::move(name)),
data_(std::make_unique<ThreadData>(this))
{ {
} }
@@ -288,6 +291,10 @@ void Thread::startThread()
data_->tid_ = syscall(SYS_gettid); data_->tid_ = syscall(SYS_gettid);
currentThreadData = data_.get(); currentThreadData = data_.get();
if (!name_.empty())
pthread_setname_np(thread_.native_handle(),
name_.substr(0, 15).c_str());
run(); run();
} }
+1 -1
View File
@@ -39,7 +39,7 @@ LOG_DEFINE_CATEGORY(Camera)
#ifndef __DOXYGEN_PUBLIC__ #ifndef __DOXYGEN_PUBLIC__
CameraManager::Private::Private() CameraManager::Private::Private()
: initialized_(false) : Thread("CameraManager"), initialized_(false)
{ {
ipaManager_ = std::make_unique<IPAManager>(this->configuration()); ipaManager_ = std::make_unique<IPAManager>(this->configuration());
} }
+1 -1
View File
@@ -115,7 +115,7 @@ private:
VirtualCameraData::VirtualCameraData(PipelineHandler *pipe, VirtualCameraData::VirtualCameraData(PipelineHandler *pipe,
const std::vector<Resolution> &supportedResolutions) const std::vector<Resolution> &supportedResolutions)
: Camera::Private(pipe) : Camera::Private(pipe), Thread("VirtualCamera")
{ {
config_.resolutions = supportedResolutions; config_.resolutions = supportedResolutions;
for (const auto &resolution : config_.resolutions) { for (const auto &resolution : config_.resolutions) {
+2 -1
View File
@@ -75,7 +75,8 @@ LOG_DEFINE_CATEGORY(SoftwareIsp)
*/ */
SoftwareIsp::SoftwareIsp(PipelineHandler *pipe, const CameraSensor *sensor, SoftwareIsp::SoftwareIsp(PipelineHandler *pipe, const CameraSensor *sensor,
ControlInfoMap *ipaControls) ControlInfoMap *ipaControls)
: dmaHeap_(DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap | : ispWorkerThread_("SWIspWorker"),
dmaHeap_(DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap |
DmaBufAllocator::DmaBufAllocatorFlag::SystemHeap | DmaBufAllocator::DmaBufAllocatorFlag::SystemHeap |
DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf) DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf)
{ {
@@ -46,7 +46,7 @@ namespace {{ns}} {
{%- endif %} {%- endif %}
{{proxy_name}}Threaded::{{proxy_name}}Threaded(IPAModule *ipam, const GlobalConfiguration &configuration) {{proxy_name}}Threaded::{{proxy_name}}Threaded(IPAModule *ipam, const GlobalConfiguration &configuration)
: {{proxy_name}}(ipam, configuration) : {{proxy_name}}(ipam, configuration), thread_("{{proxy_name}}")
{ {
LOG(IPAProxy, Debug) LOG(IPAProxy, Debug)
<< "initializing {{module_name}} proxy in thread: loading IPA from " << "initializing {{module_name}} proxy in thread: loading IPA from "