libcamera  v0.0.0+100-debian/0_git20200629+e7aa92a-8-9-g77f5237c-dirty (2021-05-05T16:20:29+01:00)
Supporting cameras in Linux since 2019
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Friends | List of all members
libcamera::Thread Class Reference

A thread of execution. More...

Inheritance diagram for libcamera::Thread:
libcamera::ThreadMain

Public Member Functions

 Thread ()
 Create a thread.
 
void start ()
 Start the thread.
 
void exit (int code=0)
 Stop the thread's event loop. More...
 
bool wait (utils::duration duration=utils::duration::max())
 Wait for the thread to finish. More...
 
bool isRunning ()
 Check if the thread is running. More...
 
EventDispatchereventDispatcher ()
 Retrieve the event dispatcher. More...
 
void dispatchMessages (Message::Type type=Message::Type::None)
 Dispatch posted messages for this thread. More...
 

Static Public Member Functions

static Threadcurrent ()
 Retrieve the Thread instance for the current thread. More...
 
static pid_t currentId ()
 Retrieve the ID of the current thread. More...
 

Public Attributes

Signal< Thread * > finished
 Signal the end of thread execution.
 

Protected Member Functions

int exec ()
 Enter the event loop. More...
 
virtual void run ()
 Main method of the thread. More...
 

Friends

class Object
 
class ThreadData
 
class ThreadMain
 

Detailed Description

A thread of execution.

The Thread class is a wrapper around std::thread that handles integration with the Object, Signal and EventDispatcher classes.

Thread instances by default run an event loop until the exit() method is called. The event loop dispatches events (messages, notifiers and timers) sent to the objects living in the thread. This behaviour can be modified by overriding the run() function.

Thread Safety:
This class is thread-safe.

Member Function Documentation

◆ current()

Thread * libcamera::Thread::current ( )
static

Retrieve the Thread instance for the current thread.

Returns
The Thread instance for the current thread

◆ currentId()

pid_t libcamera::Thread::currentId ( )
static

Retrieve the ID of the current thread.

The thread ID corresponds to the Linux thread ID (TID) as returned by the gettid system call.

Returns
The ID of the current thread

◆ dispatchMessages()

void libcamera::Thread::dispatchMessages ( Message::Type  type = Message::Type::None)

Dispatch posted messages for this thread.

Parameters
[in]typeThe message type

This function immediately dispatches all the messages previously posted for this thread with postMessage() that match the message type. If the type is Message::Type::None, all messages are dispatched.

◆ eventDispatcher()

EventDispatcher * libcamera::Thread::eventDispatcher ( )

Retrieve the event dispatcher.

This function retrieves the internal event dispatcher for the thread. The returned event dispatcher is valid until the thread is destroyed.

Returns
Pointer to the event dispatcher

◆ exec()

int libcamera::Thread::exec ( )
protected

Enter the event loop.

This method enters an event loop based on the event dispatcher instance for the thread, and blocks until the exit() method is called. It is meant to be called within the thread from the run() method and shall not be called outside of the thread.

Returns
The exit code passed to the exit() method

◆ exit()

void libcamera::Thread::exit ( int  code = 0)

Stop the thread's event loop.

Parameters
[in]codeThe exit code

This method interrupts the event loop started by the exec() method, causing exec() to return code.

Calling exit() on a thread that reimplements the run() method and doesn't call exec() will likely have no effect.

◆ isRunning()

bool libcamera::Thread::isRunning ( )

Check if the thread is running.

A Thread instance is considered as running once the underlying thread has started. This method guarantees that it returns true after the start() method returns, and false after the wait() method returns.

Returns
True if the thread is running, false otherwise

◆ run()

void libcamera::Thread::run ( )
protectedvirtual

Main method of the thread.

When the thread is started with start(), it calls this method in the context of the new thread. The run() method can be overridden to perform custom work, either custom initialization and cleanup before and after calling the Thread::exec() function, or a custom thread loop altogether. When this method returns the thread execution is stopped, and the finished signal is emitted.

Note that if this function is overridden and doesn't call Thread::exec(), no events will be dispatched to the objects living in the thread. These objects will not be able to use the EventNotifier, Timer or Message facilities. This includes functions that rely on message dispatching, such as Object::deleteLater().

The base implementation just calls exec().

Reimplemented in libcamera::ThreadMain.

◆ wait()

bool libcamera::Thread::wait ( utils::duration  duration = utils::duration::max())

Wait for the thread to finish.

Parameters
[in]durationMaximum wait duration

This function waits until the thread finishes or the duration has elapsed, whichever happens first. If duration is equal to utils::duration::max(), the wait never times out. If the thread is not running the function returns immediately.

Returns
True if the thread has finished, or false if the wait timed out

The documentation for this class was generated from the following files: