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

libcamera v0.4.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 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/span.h>
19#include <libcamera/base/utils.h>
20
21namespace libcamera {
22
23class EventDispatcher;
24class Message;
25class Object;
26class ThreadData;
27class ThreadMain;
28
29 class Thread
30{
31public:
32 Thread();
33 virtual ~Thread();
34
35 void start();
36 void exit(int code = 0);
37 bool wait(utils::duration duration = utils::duration::max());
38
39 int setThreadAffinity(const Span<const unsigned int> &cpus);
40
41 bool isRunning();
42
43 Signal<> finished;
44
45 static Thread *current();
46 static pid_t currentId();
47
48 EventDispatcher *eventDispatcher();
49
50 void dispatchMessages(Message::Type type = Message::Type::None);
51
52protected:
53 int exec();
54 virtual void run();
55
56private:
57 void startThread();
58 void finishThread();
59
60 void setThreadAffinityInternal();
61
62 void postMessage(std::unique_ptr<Message> msg, Object *receiver);
63 void removeMessages(Object *receiver);
64
65 friend class Object;
66 friend class ThreadData;
67 friend class ThreadMain;
68
69 void moveObject(Object *object);
70 void moveObject(Object *object, ThreadData *currentData,
71 ThreadData *targetData);
72
73 std::thread thread_;
74 ThreadData *data_;
75};
76
77} /* namespace libcamera */
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:25
libcamera::Signal
Generic signal and slot communication mechanism.
Definition signal.h:39
libcamera::ThreadData
Thread-local internal data.
Definition thread.cpp:106
libcamera::ThreadMain
Thread wrapper for the main thread.
Definition thread.cpp:140
libcamera::Thread::current
static Thread * current()
Retrieve the Thread instance for the current thread.
Definition thread.cpp:487
libcamera::Thread::currentId
static pid_t currentId()
Retrieve the ID of the current thread.
Definition thread.cpp:503
libcamera::Thread::eventDispatcher
EventDispatcher * eventDispatcher()
Retrieve the event dispatcher.
Definition thread.cpp:519
libcamera::Thread::exec
int exec()
Enter the event loop.
Definition thread.cpp:302
libcamera::Thread::exit
void exit(int code=0)
Stop the thread's event loop.
Definition thread.cpp:369
libcamera::Thread::start
void start()
Start the thread.
Definition thread.cpp:248
libcamera::Thread::run
virtual void run()
Main function of the thread.
Definition thread.cpp:336
libcamera::Thread::dispatchMessages
void dispatchMessages(Message::Type type=Message::Type::None)
Dispatch posted messages for this thread.
Definition thread.cpp:620
libcamera::Thread::wait
bool wait(utils::duration duration=utils::duration::max())
Wait for the thread to finish.
Definition thread.cpp:394
libcamera::Thread::isRunning
bool isRunning()
Check if the thread is running.
Definition thread.cpp:471
libcamera::Thread::setThreadAffinity
int setThreadAffinity(const Span< const unsigned int > &cpus)
Set the CPU affinity mask of the thread.
Definition thread.cpp:428
libcamera::Thread::Thread
Thread()
Create a thread.
Definition thread.cpp:233
libcamera::Thread::finished
Signal finished
Signal the end of thread execution.
Definition thread.h:43
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