libcamera: thread: Add a method to return the ID of the current thread

The current thread ID is useful when logging message to debug
concurrency issues. Add a method to retrieve it.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
Laurent Pinchart
2020-01-18 03:44:49 +02:00
parent a39b91d44e
commit 1d929967b7
2 changed files with 22 additions and 0 deletions
+20
View File
@@ -9,6 +9,9 @@
#include <atomic>
#include <list>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <libcamera/event_dispatcher.h>
@@ -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