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>
This commit is contained in:
Laurent Pinchart
2019-03-23 17:33:15 +02:00
parent 10ec09025d
commit 525b19c410
5 changed files with 403 additions and 13 deletions
+61
View File
@@ -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__ */