Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* Thread support
*/
#pragma once
#include <memory>
#include <string>
#include <sys/types.h>
#include <thread>
#include <libcamera/base/private.h>
#include <libcamera/base/class.h>
#include <libcamera/base/message.h>
#include <libcamera/base/signal.h>
#include <libcamera/base/span.h>
#include <libcamera/base/utils.h>
namespace libcamera {
class EventDispatcher;
class Message;
class Object;
class ThreadData;
class ThreadMain;
class Thread
{
public:
Thread(std::string name = {});
virtual ~Thread();
void start();
void exit(int code = 0);
bool wait(utils::duration duration = utils::duration::max());
int setThreadAffinity(const Span<const unsigned int> &cpus);
bool isRunning();
Signal<> finished;
static Thread *current();
static pid_t currentId();
EventDispatcher *eventDispatcher();
void dispatchMessages(Message::Type type = Message::Type::None,
Object *receiver = nullptr);
void removeMessages(Object *receiver);
protected:
int exec();
virtual void run();
private:
LIBCAMERA_DISABLE_COPY_AND_MOVE(Thread)
void startThread();
void finishThread();
void setThreadAffinityInternal();
void postMessage(std::unique_ptr<Message> msg, Object *receiver);
friend class Object;
friend class ThreadData;
friend class ThreadMain;
void moveObject(Object *object);
void moveObject(Object *object, ThreadData *currentData,
ThreadData *targetData);
std::string name_;
std::thread thread_;
std::unique_ptr<ThreadData> data_;
};
} /* namespace libcamera */
class.h
Utilities to help constructing class interfaces.
LIBCAMERA_DISABLE_COPY_AND_MOVE
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
Definition class.h:29
libcamera::EventDispatcher
Interface to manage the libcamera events and timers.
Definition event_dispatcher.h:18
libcamera::Message
A message that can be posted to a Thread.
Definition message.h:24
libcamera::Message::Type
Type
The message type.
Definition message.h:26
libcamera::Message::None
@ None
Invalid message type.
Definition message.h:27
libcamera::Object
Base object to support automatic signal disconnection.
Definition object.h:27
libcamera::Signal
Generic signal and slot communication mechanism.
Definition signal.h:39
libcamera::ThreadData
Thread-local internal data.
Definition thread.cpp:107
libcamera::ThreadMain
Thread wrapper for the main thread.
Definition thread.cpp:145
libcamera::Thread::current
static Thread * current()
Retrieve the Thread instance for the current thread.
Definition thread.cpp:495
libcamera::Thread::currentId
static pid_t currentId()
Retrieve the ID of the current thread.
Definition thread.cpp:511
libcamera::Thread::eventDispatcher
EventDispatcher * eventDispatcher()
Retrieve the event dispatcher.
Definition thread.cpp:525
libcamera::Thread::exec
int exec()
Enter the event loop.
Definition thread.cpp:310
libcamera::Thread::exit
void exit(int code=0)
Stop the thread's event loop.
Definition thread.cpp:377
libcamera::Thread::Thread
Thread(std::string name={})
Create a thread.
Definition thread.cpp:239
libcamera::Thread::start
void start()
Start the thread.
Definition thread.cpp:253
libcamera::Thread::run
virtual void run()
Main function of the thread.
Definition thread.cpp:344
libcamera::Thread::removeMessages
void removeMessages(Object *receiver)
Remove all posted messages for the receiver.
Definition thread.cpp:581
libcamera::Thread::wait
bool wait(utils::duration duration=utils::duration::max())
Wait for the thread to finish.
Definition thread.cpp:402
libcamera::Thread::isRunning
bool isRunning()
Check if the thread is running.
Definition thread.cpp:479
libcamera::Thread::dispatchMessages
void dispatchMessages(Message::Type type=Message::Type::None, Object *receiver=nullptr)
Dispatch posted messages for this thread.
Definition thread.cpp:630
libcamera::Thread::setThreadAffinity
int setThreadAffinity(const Span< const unsigned int > &cpus)
Set the CPU affinity mask of the thread.
Definition thread.cpp:436
libcamera::Thread::finished
Signal finished
Signal the end of thread execution.
Definition thread.h:45
message.h
Message queue support.
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
signal.h
Signal & slot implementation.
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:74