libcamera  v0.0.0+100-debian/0_git20200629+e7aa92a-8-9-g77f5237c-dirty (2021-05-05T16:20:29+01: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_INTERNAL_THREAD_H__
8 #define __LIBCAMERA_INTERNAL_THREAD_H__
9 
10 #include <memory>
11 #include <mutex>
12 #include <sys/types.h>
13 #include <thread>
14 
15 #include <libcamera/signal.h>
16 
19 
20 namespace libcamera {
21 
22 class EventDispatcher;
23 class Message;
24 class Object;
25 class ThreadData;
26 class ThreadMain;
27 
28 using Mutex = std::mutex;
29 using MutexLocker = std::unique_lock<std::mutex>;
30 
31 class Thread
32 {
33 public:
34  Thread();
35  virtual ~Thread();
36 
37  void start();
38  void exit(int code = 0);
39  bool wait(utils::duration duration = utils::duration::max());
40 
41  bool isRunning();
42 
44 
45  static Thread *current();
46  static pid_t currentId();
47 
49 
50  void dispatchMessages(Message::Type type = Message::Type::None);
51 
52 protected:
53  int exec();
54  virtual void run();
55 
56 private:
57  void startThread();
58  void finishThread();
59 
60  void postMessage(std::unique_ptr<Message> msg, Object *receiver);
61  void removeMessages(Object *receiver);
62 
63  friend class Object;
64  friend class ThreadData;
65  friend class ThreadMain;
66 
67  void moveObject(Object *object);
68  void moveObject(Object *object, ThreadData *currentData,
69  ThreadData *targetData);
70 
71  std::thread thread_;
72  ThreadData *data_;
73 };
74 
75 } /* namespace libcamera */
76 
77 #endif /* __LIBCAMERA_INTERNAL_THREAD_H__ */
bool wait(utils::duration duration=utils::duration::max())
Wait for the thread to finish.
Definition: thread.cpp:388
static Thread * current()
Retrieve the Thread instance for the current thread.
Definition: thread.cpp:432
EventDispatcher * eventDispatcher()
Retrieve the event dispatcher.
Definition: thread.cpp:460
Top-level libcamera namespace.
Definition: bound_method.h:15
Interface to manage the libcamera events and timers.
Definition: event_dispatcher.h:17
Message queue support.
Thread-local internal data.
Definition: thread.cpp:142
A thread of execution.
Definition: thread.h:31
static pid_t currentId()
Retrieve the ID of the current thread.
Definition: thread.cpp:446
Miscellaneous utility functions.
virtual void run()
Main method of the thread.
Definition: thread.cpp:340
Signal & slot implementation.
bool isRunning()
Check if the thread is running.
Definition: thread.cpp:417
std::unique_lock< std::mutex > MutexLocker
An alias for std::unique_lock<std::mutex>
Definition: thread.h:29
void dispatchMessages(Message::Type type=Message::Type::None)
Dispatch posted messages for this thread.
Definition: thread.cpp:548
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition: utils.h:67
int exec()
Enter the event loop.
Definition: thread.cpp:306
void exit(int code=0)
Stop the thread&#39;s event loop.
Definition: thread.cpp:365
Thread()
Create a thread.
Definition: thread.cpp:239
Generic signal and slot communication mechanism.
Definition: object.h:20
Signal< Thread * > finished
Signal the end of thread execution.
Definition: thread.h:43
void start()
Start the thread.
Definition: thread.cpp:254
Type
The message type.
Definition: message.h:24
Base object to support automatic signal disconnection.
Definition: object.h:24
Thread wrapper for the main thread.
Definition: thread.cpp:174
std::mutex Mutex
An alias for std::mutex.
Definition: thread.h:28