thread.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* thread.h - Thread support
*/
#ifndef __LIBCAMERA_INTERNAL_THREAD_H__
#define __LIBCAMERA_INTERNAL_THREAD_H__
#include <memory>
#include <mutex>
#include <sys/types.h>
#include <thread>
#include <libcamera/signal.h>
#include "libcamera/internal/utils.h"
namespace libcamera {
class EventDispatcher;
class Message;
class Object;
class ThreadData;
class ThreadMain;
using Mutex = std::mutex;
using MutexLocker = std::unique_lock<std::mutex>;
class Thread
{
public:
Thread();
virtual ~Thread();
void start();
void exit(int code = 0);
bool wait(utils::duration duration = utils::duration::max());
bool isRunning();
Signal<Thread *> finished;
static Thread *current();
static pid_t currentId();
EventDispatcher *eventDispatcher();
void setEventDispatcher(std::unique_ptr<EventDispatcher> dispatcher);
void dispatchMessages();
protected:
int exec();
virtual void run();
private:
void startThread();
void finishThread();
void postMessage(std::unique_ptr<Message> msg, Object *receiver);
void removeMessages(Object *receiver);
friend class Object;
friend class ThreadData;
friend class ThreadMain;
void moveObject(Object *object);
void moveObject(Object *object, ThreadData *currentData,
ThreadData *targetData);
std::thread thread_;
ThreadData *data_;
};
} /* namespace libcamera */
#endif /* __LIBCAMERA_INTERNAL_THREAD_H__ */
libcamera::EventDispatcher
Interface to manage the libcamera events and timers.
Definition: event_dispatcher.h:18
libcamera::Object
Base object to support automatic signal disconnection.
Definition: object.h:25
libcamera::Signal
Generic signal and slot communication mechanism.
Definition: signal.h:39
libcamera::ThreadData
Thread-local internal data.
Definition: thread.cpp:138
libcamera::ThreadMain
Thread wrapper for the main thread.
Definition: thread.cpp:170
libcamera::Thread
A thread of execution.
Definition: thread.h:31
libcamera::Thread::current
static Thread * current()
Retrieve the Thread instance for the current thread.
Definition: thread.cpp:419
libcamera::Thread::currentId
static pid_t currentId()
Retrieve the ID of the current thread.
Definition: thread.cpp:433
libcamera::Thread::eventDispatcher
EventDispatcher * eventDispatcher()
Retrieve the event dispatcher.
Definition: thread.cpp:474
libcamera::Thread::dispatchMessages
void dispatchMessages()
Dispatch all posted messages for this thread.
Definition: thread.cpp:557
libcamera::Thread::exec
int exec()
Enter the event loop.
Definition: thread.cpp:301
libcamera::Thread::exit
void exit(int code=0)
Stop the thread's event loop.
Definition: thread.cpp:352
libcamera::Thread::setEventDispatcher
void setEventDispatcher(std::unique_ptr< EventDispatcher > dispatcher)
Set the event dispatcher.
Definition: thread.cpp:452
libcamera::Thread::start
void start()
Start the thread.
Definition: thread.cpp:249
libcamera::Thread::run
virtual void run()
Main method of the thread.
Definition: thread.cpp:327
libcamera::Thread::finished
Signal< Thread * > finished
Signal the end of thread execution.
Definition: thread.h:42
libcamera::Thread::wait
bool wait(utils::duration duration=utils::duration::max())
Wait for the thread to finish.
Definition: thread.cpp:375
libcamera::Thread::isRunning
bool isRunning()
Check if the thread is running.
Definition: thread.cpp:404
libcamera::Thread::Thread
Thread()
Create a thread.
Definition: thread.cpp:234
signal.h
Signal & slot implementation.
libcamera::MutexLocker
std::unique_lock< std::mutex > MutexLocker
An alias for std::unique_lock<std::mutex>
Definition: thread.h:28
libcamera::Mutex
std::mutex Mutex
An alias for std::mutex.
Definition: thread.h:27
utils.h
Miscellaneous utility functions.
libcamera::utils::duration
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition: utils.h:66