libcamera  v0.4.0+139-39419ce4
Supporting cameras in Linux since 2019
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 
22 namespace libcamera {
23 
24 class EventDispatcher;
25 class Message;
26 class Object;
27 class ThreadData;
28 class ThreadMain;
29 
30 class Thread
31 {
32 public:
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 
51  void dispatchMessages(Message::Type type = Message::Type::None,
52  Object *receiver = nullptr);
53 
54 protected:
55  int exec();
56  virtual void run();
57 
58 private:
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 */
bool wait(utils::duration duration=utils::duration::max())
Wait for the thread to finish.
Definition: thread.cpp:394
Utilities to help constructing class interfaces.
static Thread * current()
Retrieve the Thread instance for the current thread.
Definition: thread.cpp:487
EventDispatcher * eventDispatcher()
Retrieve the event dispatcher.
Definition: thread.cpp:519
Top-level libcamera namespace.
Definition: backtrace.h:17
Interface to manage the libcamera events and timers.
Definition: event_dispatcher.h:17
Message queue support.
Thread-local internal data.
Definition: thread.cpp:105
A thread of execution.
Definition: thread.h:30
static pid_t currentId()
Retrieve the ID of the current thread.
Definition: thread.cpp:503
int setThreadAffinity(const Span< const unsigned int > &cpus)
Set the CPU affinity mask of the thread.
Definition: thread.cpp:428
Miscellaneous utility functions.
virtual void run()
Main function of the thread.
Definition: thread.cpp:336
Signal & slot implementation.
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
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition: utils.h:73
int exec()
Enter the event loop.
Definition: thread.cpp:302
void exit(int code=0)
Stop the thread&#39;s event loop.
Definition: thread.cpp:369
Thread()
Create a thread.
Definition: thread.cpp:233
Generic signal and slot communication mechanism.
Definition: object.h:21
Signal finished
Signal the end of thread execution.
Definition: thread.h:44
void start()
Start the thread.
Definition: thread.cpp:248
Type
The message type.
Definition: message.h:26
Base object to support automatic signal disconnection.
Definition: object.h:25
Thread wrapper for the main thread.
Definition: thread.cpp:139