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

libcamera v0.5.1 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/class.h>
17#include <libcamera/base/message.h>
18#include <libcamera/base/signal.h>
19#include <libcamera/base/span.h>
20#include <libcamera/base/utils.h>
21
22namespace libcamera {
23
24class EventDispatcher;
25class Message;
26class Object;
27class ThreadData;
28class ThreadMain;
29
30 class Thread
31{
32public:
33 Thread();
34 virtual ~Thread();
35
36 void start();
37 void exit(int code = 0);
38 bool wait(utils::duration duration = utils::duration::max());
39
40 int setThreadAffinity(const Span<const unsigned int> &cpus);
41
42 bool isRunning();
43
44 Signal<> finished;
45
46 static Thread *current();
47 static pid_t currentId();
48
49 EventDispatcher *eventDispatcher();
50
51 void dispatchMessages(Message::Type type = Message::Type::None,
52 Object *receiver = nullptr);
53
54protected:
55 int exec();
56 virtual void run();
57
58private:
59 LIBCAMERA_DISABLE_COPY_AND_MOVE(Thread)
60
61 void startThread();
62 void finishThread();
63
64 void setThreadAffinityInternal();
65
66 void postMessage(std::unique_ptr<Message> msg, Object *receiver);
67 void removeMessages(Object *receiver);
68
69 friend class Object;
70 friend class ThreadData;
71 friend class ThreadMain;
72
73 void moveObject(Object *object);
74 void moveObject(Object *object, ThreadData *currentData,
75 ThreadData *targetData);
76
77 std::thread thread_;
78 ThreadData *data_;
79};
80
81} /* 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: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::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::dispatchMessages
void dispatchMessages(Message::Type type=Message::Type::None, Object *receiver=nullptr)
Dispatch posted messages for this thread.
Definition thread.cpp:622
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:44
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:73