diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-03-23 17:33:15 +0200 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-07-11 09:47:16 +0300 |
commit | 525b19c4101235385148ff9358b7b6e778a1f148 (patch) | |
tree | 7e7f32764f418af456b343ad1dae7fb05dab032a /src/libcamera/include/thread.h | |
parent | 10ec09025d6f52f2c1d0ba6b7e6943a603a386d9 (diff) |
libcamera: Add thread support
The new Thread class wraps std::thread in order to integrate it with the
Object, Signal and EventDispatcher classes. By default new threads run
an internal event loop, and their run() method can be overloaded to
provide a custom thread loop.
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Diffstat (limited to 'src/libcamera/include/thread.h')
-rw-r--r-- | src/libcamera/include/thread.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/libcamera/include/thread.h b/src/libcamera/include/thread.h new file mode 100644 index 00000000..e881d90e --- /dev/null +++ b/src/libcamera/include/thread.h @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * thread.h - Thread support + */ +#ifndef __LIBCAMERA_THREAD_H__ +#define __LIBCAMERA_THREAD_H__ + +#include <memory> +#include <mutex> +#include <thread> + +#include <libcamera/signal.h> + +namespace libcamera { + +class EventDispatcher; +class ThreadData; +class ThreadMain; + +using Mutex = std::mutex; +using MutexLocker = std::unique_lock<std::mutex>; + +class Thread +{ +public: + Thread(); + virtual ~Thread(); + + void start(); + void exit(int code = 0); + void wait(); + + bool isRunning(); + + Signal<Thread *> finished; + + static Thread *current(); + + EventDispatcher *eventDispatcher(); + void setEventDispatcher(std::unique_ptr<EventDispatcher> dispatcher); + +protected: + int exec(); + virtual void run(); + +private: + void startThread(); + void finishThread(); + + friend class ThreadData; + friend class ThreadMain; + + std::thread thread_; + ThreadData *data_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_THREAD_H__ */ |