From 1d929967b7d5623ec49f0bbed3abe4fea480011e Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sat, 18 Jan 2020 03:44:49 +0200 Subject: libcamera: thread: Add a method to return the ID of the current thread MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current thread ID is useful when logging message to debug concurrency issues. Add a method to retrieve it. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- src/libcamera/include/thread.h | 2 ++ src/libcamera/thread.cpp | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/libcamera/include/thread.h b/src/libcamera/include/thread.h index 37edd4f5..819a9879 100644 --- a/src/libcamera/include/thread.h +++ b/src/libcamera/include/thread.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -39,6 +40,7 @@ public: Signal finished; static Thread *current(); + static pid_t currentId(); EventDispatcher *eventDispatcher(); void setEventDispatcher(std::unique_ptr dispatcher); diff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp index 18ebd16a..fe32cd67 100644 --- a/src/libcamera/thread.cpp +++ b/src/libcamera/thread.cpp @@ -9,6 +9,9 @@ #include #include +#include +#include +#include #include @@ -62,6 +65,7 @@ private: Thread *thread_; bool running_; + pid_t tid_; Mutex mutex_; @@ -108,6 +112,7 @@ ThreadData *ThreadData::current() * started, set it here. */ ThreadData *data = mainThread.data_; + data->tid_ = syscall(SYS_gettid); currentThreadData = data; return data; } @@ -189,6 +194,7 @@ void Thread::startThread() */ thread_local ThreadCleaner cleaner(this, &Thread::finishThread); + data_->tid_ = syscall(SYS_gettid); currentThreadData = data_; run(); @@ -308,6 +314,20 @@ Thread *Thread::current() return data->thread_; } +/** + * \brief Retrieve the ID of the current thread + * + * The thread ID corresponds to the Linux thread ID (TID) as returned by the + * gettid system call. + * + * \return The ID of the current thread + */ +pid_t Thread::currentId() +{ + ThreadData *data = ThreadData::current(); + return data->tid_; +} + /** * \brief Set the event dispatcher * \param[in] dispatcher Pointer to the event dispatcher -- cgit v1.2.1