libcamera: include/libcamera/base/thread.h Source File

libcamera v0.2.0 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2019, Google Inc.
4 *
5 * thread.h - Thread support
6 */
7
8#pragma once
9
10#include <memory>
11#include <sys/types.h>
12#include <thread>
13
14#include <libcamera/base/private.h>
15
16#include <libcamera/base/message.h>
17#include <libcamera/base/signal.h>
18#include <libcamera/base/utils.h>
19
20namespace libcamera {
21
22class EventDispatcher;
23class Message;
24class Object;
25class ThreadData;
26class ThreadMain;
27
28 class Thread
29{
30public:
31 Thread();
32 virtual ~Thread();
33
34 void start();
35 void exit(int code = 0);
36 bool wait(utils::duration duration = utils::duration::max());
37
38 bool isRunning();
39
40 Signal<> finished;
41
42 static Thread *current();
43 static pid_t currentId();
44
45 EventDispatcher *eventDispatcher();
46
47 void dispatchMessages(Message::Type type = Message::Type::None);
48
49protected:
50 int exec();
51 virtual void run();
52
53private:
54 void startThread();
55 void finishThread();
56
57 void postMessage(std::unique_ptr<Message> msg, Object *receiver);
58 void removeMessages(Object *receiver);
59
60 friend class Object;
61 friend class ThreadData;
62 friend class ThreadMain;
63
64 void moveObject(Object *object);
65 void moveObject(Object *object, ThreadData *currentData,
66 ThreadData *targetData);
67
68 std::thread thread_;
69 ThreadData *data_;
70};
71
72} /* namespace libcamera */
libcamera::EventDispatcher
Interface to manage the libcamera events and timers.
Definition event_dispatcher.h:20
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:25
libcamera::Signal
Generic signal and slot communication mechanism.
Definition signal.h:39
libcamera::ThreadData
Thread-local internal data.
Definition thread.cpp:140
libcamera::ThreadMain
Thread wrapper for the main thread.
Definition thread.cpp:172
libcamera::Thread
A thread of execution.
Definition thread.h:29
libcamera::Thread::current
static Thread * current()
Retrieve the Thread instance for the current thread.
Definition thread.cpp:469
libcamera::Thread::currentId
static pid_t currentId()
Retrieve the ID of the current thread.
Definition thread.cpp:485
libcamera::Thread::eventDispatcher
EventDispatcher * eventDispatcher()
Retrieve the event dispatcher.
Definition thread.cpp:501
libcamera::Thread::exec
int exec()
Enter the event loop.
Definition thread.cpp:332
libcamera::Thread::exit
void exit(int code=0)
Stop the thread's event loop.
Definition thread.cpp:393
libcamera::Thread::start
void start()
Start the thread.
Definition thread.cpp:280
libcamera::Thread::run
virtual void run()
Main function of the thread.
Definition thread.cpp:366
libcamera::Thread::finished
Signal finished
Signal the end of thread execution.
Definition thread.h:40
libcamera::Thread::dispatchMessages
void dispatchMessages(Message::Type type=Message::Type::None)
Dispatch posted messages for this thread.
Definition thread.cpp:602
libcamera::Thread::wait
bool wait(utils::duration duration=utils::duration::max())
Wait for the thread to finish.
Definition thread.cpp:418
libcamera::Thread::isRunning
bool isRunning()
Check if the thread is running.
Definition thread.cpp:453
libcamera::Thread::Thread
Thread()
Create a thread.
Definition thread.cpp:265
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:72