libcamera  v0.0.0+3-3b6e2d50-dirty (2022-01-27T16:18:04+08:00)
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.h - Thread support
6  */
7 #ifndef __LIBCAMERA_BASE_THREAD_H__
8 #define __LIBCAMERA_BASE_THREAD_H__
9 
10 #include <memory>
11 #include <mutex>
12 #include <sys/types.h>
13 #include <thread>
14 
15 #include <libcamera/base/private.h>
16 
17 #include <libcamera/base/message.h>
18 #include <libcamera/base/signal.h>
19 #include <libcamera/base/utils.h>
20 
21 namespace libcamera {
22 
23 class EventDispatcher;
24 class Message;
25 class Object;
26 class ThreadData;
27 class ThreadMain;
28 
29 using Mutex = std::mutex;
30 using MutexLocker = std::unique_lock<std::mutex>;
31 
32 class Thread
33 {
34 public:
35  Thread();
36  virtual ~Thread();
37 
38  void start();
39  void exit(int code = 0);
40  bool wait(utils::duration duration = utils::duration::max());
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 
53 protected:
54  int exec();
55  virtual void run();
56 
57 private:
58  void startThread();
59  void finishThread();
60 
61  void postMessage(std::unique_ptr<Message> msg, Object *receiver);
62  void removeMessages(Object *receiver);
63 
64  friend class Object;
65  friend class ThreadData;
66  friend class ThreadMain;
67 
68  void moveObject(Object *object);
69  void moveObject(Object *object, ThreadData *currentData,
70  ThreadData *targetData);
71 
72  std::thread thread_;
73  ThreadData *data_;
74 };
75 
76 } /* namespace libcamera */
77 
78 #endif /* __LIBCAMERA_BASE_THREAD_H__ */
Interface to manage the libcamera events and timers.
Definition: event_dispatcher.h:20
Type
The message type.
Definition: message.h:24
Base object to support automatic signal disconnection.
Definition: object.h:25
Generic signal and slot communication mechanism.
Definition: signal.h:39
Thread-local internal data.
Definition: thread.cpp:140
Thread wrapper for the main thread.
Definition: thread.cpp:172
A thread of execution.
Definition: thread.h:33
static Thread * current()
Retrieve the Thread instance for the current thread.
Definition: thread.cpp:475
static pid_t currentId()
Retrieve the ID of the current thread.
Definition: thread.cpp:491
EventDispatcher * eventDispatcher()
Retrieve the event dispatcher.
Definition: thread.cpp:507
int exec()
Enter the event loop.
Definition: thread.cpp:342
void exit(int code=0)
Stop the thread's event loop.
Definition: thread.cpp:403
void start()
Start the thread.
Definition: thread.cpp:290
virtual void run()
Main function of the thread.
Definition: thread.cpp:376
Signal finished
Signal the end of thread execution.
Definition: thread.h:44
void dispatchMessages(Message::Type type=Message::Type::None)
Dispatch posted messages for this thread.
Definition: thread.cpp:608
bool wait(utils::duration duration=utils::duration::max())
Wait for the thread to finish.
Definition: thread.cpp:428
bool isRunning()
Check if the thread is running.
Definition: thread.cpp:459
Thread()
Create a thread.
Definition: thread.cpp:275
Message queue support.
Top-level libcamera namespace.
Definition: bound_method.h:15
std::unique_lock< std::mutex > MutexLocker
An alias for std::unique_lock<std::mutex>
Definition: thread.h:30
std::mutex Mutex
An alias for std::mutex.
Definition: thread.h:29
Signal & slot implementation.
Miscellaneous utility functions.
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition: utils.h:72