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
19#include <libcamera/base/span.h>
21
22namespace libcamera {
23
24class EventDispatcher;
25class Message;
26class Object;
27class ThreadData;
28class ThreadMain;
29
30class 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
45
46 static Thread *current();
47 static pid_t currentId();
48
50
52 Object *receiver = nullptr);
53
54protected:
55 int exec();
56 virtual void run();
57
58private:
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 */
Utilities to help constructing class interfaces.
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
Definition class.h:29
Interface to manage the libcamera events and timers.
Definition event_dispatcher.h:18
A message that can be posted to a Thread.
Definition message.h:24
Type
The message type.
Definition message.h:26
@ None
Invalid message type.
Definition message.h:27
Base object to support automatic signal disconnection.
Definition object.h:27
Generic signal and slot communication mechanism.
Definition signal.h:39
Thread-local internal data.
Definition thread.cpp:106
Thread wrapper for the main thread.
Definition thread.cpp:140
static Thread * current()
Retrieve the Thread instance for the current thread.
Definition thread.cpp:487
static pid_t currentId()
Retrieve the ID of the current thread.
Definition thread.cpp:503
EventDispatcher * eventDispatcher()
Retrieve the event dispatcher.
Definition thread.cpp:519
int exec()
Enter the event loop.
Definition thread.cpp:302
void exit(int code=0)
Stop the thread's event loop.
Definition thread.cpp:369
void start()
Start the thread.
Definition thread.cpp:248
virtual void run()
Main function of the thread.
Definition thread.cpp:336
bool wait(utils::duration duration=utils::duration::max())
Wait for the thread to finish.
Definition thread.cpp:394
bool isRunning()
Check if the thread is running.
Definition thread.cpp:471
void dispatchMessages(Message::Type type=Message::Type::None, Object *receiver=nullptr)
Dispatch posted messages for this thread.
Definition thread.cpp:622
int setThreadAffinity(const Span< const unsigned int > &cpus)
Set the CPU affinity mask of the thread.
Definition thread.cpp:428
Thread()
Create a thread.
Definition thread.cpp:233
Signal finished
Signal the end of thread execution.
Definition thread.h:44
Message queue support.
Top-level libcamera namespace.
Definition backtrace.h:17
Signal & slot implementation.
Miscellaneous utility functions.
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition utils.h:73