libcamera v0.7.0+1-4ceceb68
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 <string>
12#include <sys/types.h>
13#include <thread>
14
15#include <libcamera/base/private.h>
16
20#include <libcamera/base/span.h>
22
23namespace libcamera {
24
25class EventDispatcher;
26class Message;
27class Object;
28class ThreadData;
29class ThreadMain;
30
31class Thread
32{
33public:
34 Thread(std::string name = {});
35 virtual ~Thread();
36
37 void start();
38 void exit(int code = 0);
39 bool wait(utils::duration duration = utils::duration::max());
40
41 int setThreadAffinity(const Span<const unsigned int> &cpus);
42
43 bool isRunning();
44
46
47 static Thread *current();
48 static pid_t currentId();
49
51
53 Object *receiver = nullptr);
54 void removeMessages(Object *receiver);
55
56protected:
57 int exec();
58 virtual void run();
59
60private:
62
63 void startThread();
64 void finishThread();
65
66 void setThreadAffinityInternal();
67
68 void postMessage(std::unique_ptr<Message> msg, Object *receiver);
69
70 friend class Object;
71 friend class ThreadData;
72 friend class ThreadMain;
73
74 void moveObject(Object *object);
75 void moveObject(Object *object, ThreadData *currentData,
76 ThreadData *targetData);
77
78 std::string name_;
79 std::thread thread_;
80 std::unique_ptr<ThreadData> data_;
81};
82
83} /* 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
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:107
Thread wrapper for the main thread.
Definition thread.cpp:145
A thread of execution.
Definition thread.h:32
static Thread * current()
Retrieve the Thread instance for the current thread.
Definition thread.cpp:495
static pid_t currentId()
Retrieve the ID of the current thread.
Definition thread.cpp:511
EventDispatcher * eventDispatcher()
Retrieve the event dispatcher.
Definition thread.cpp:525
int exec()
Enter the event loop.
Definition thread.cpp:310
void exit(int code=0)
Stop the thread's event loop.
Definition thread.cpp:377
void start()
Start the thread.
Definition thread.cpp:253
virtual void run()
Main function of the thread.
Definition thread.cpp:344
Signal finished
Signal the end of thread execution.
Definition thread.h:45
void removeMessages(Object *receiver)
Remove all posted messages for the receiver.
Definition thread.cpp:581
bool wait(utils::duration duration=utils::duration::max())
Wait for the thread to finish.
Definition thread.cpp:402
bool isRunning()
Check if the thread is running.
Definition thread.cpp:479
void dispatchMessages(Message::Type type=Message::Type::None, Object *receiver=nullptr)
Dispatch posted messages for this thread.
Definition thread.cpp:630
int setThreadAffinity(const Span< const unsigned int > &cpus)
Set the CPU affinity mask of the thread.
Definition thread.cpp:436
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:74