libcamera: signal: Support cross-thread signals
Allow signals to cross thread boundaries by posting them to the recipient through messages instead of calling the slot directly when the recipient lives in a different thread. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
This commit is contained in:
parent
01b930964a
commit
cc3ae13d9e
5 changed files with 136 additions and 8 deletions
|
@ -8,6 +8,8 @@
|
||||||
#define __LIBCAMERA_SIGNAL_H__
|
#define __LIBCAMERA_SIGNAL_H__
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <tuple>
|
||||||
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <libcamera/object.h>
|
#include <libcamera/object.h>
|
||||||
|
@ -27,6 +29,9 @@ public:
|
||||||
void *obj() { return obj_; }
|
void *obj() { return obj_; }
|
||||||
bool isObject() const { return isObject_; }
|
bool isObject() const { return isObject_; }
|
||||||
|
|
||||||
|
void activatePack(void *pack);
|
||||||
|
virtual void invokePack(void *pack) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void *obj_;
|
void *obj_;
|
||||||
bool isObject_;
|
bool isObject_;
|
||||||
|
@ -35,24 +40,70 @@ protected:
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
class SlotArgs : public SlotBase
|
class SlotArgs : public SlotBase
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
#ifndef __DOXYGEN__
|
||||||
|
/*
|
||||||
|
* This is a cheap partial implementation of std::integer_sequence<>
|
||||||
|
* from C++14.
|
||||||
|
*/
|
||||||
|
template<int...>
|
||||||
|
struct sequence {
|
||||||
|
};
|
||||||
|
|
||||||
|
template<int N, int... S>
|
||||||
|
struct generator : generator<N-1, N-1, S...> {
|
||||||
|
};
|
||||||
|
|
||||||
|
template<int... S>
|
||||||
|
struct generator<0, S...> {
|
||||||
|
typedef sequence<S...> type;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
using PackType = std::tuple<typename std::remove_reference<Args>::type...>;
|
||||||
|
|
||||||
|
template<int... S>
|
||||||
|
void invokePack(void *pack, sequence<S...>)
|
||||||
|
{
|
||||||
|
PackType *args = static_cast<PackType *>(pack);
|
||||||
|
invoke(std::get<S>(*args)...);
|
||||||
|
delete args;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SlotArgs(void *obj, bool isObject)
|
SlotArgs(void *obj, bool isObject)
|
||||||
: SlotBase(obj, isObject) {}
|
: SlotBase(obj, isObject) {}
|
||||||
|
|
||||||
virtual void invoke(Args... args) = 0;
|
void invokePack(void *pack) override
|
||||||
|
{
|
||||||
|
invokePack(pack, typename generator<sizeof...(Args)>::type());
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
virtual void activate(Args... args) = 0;
|
||||||
friend class Signal<Args...>;
|
virtual void invoke(Args... args) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, typename... Args>
|
template<typename T, typename... Args>
|
||||||
class SlotMember : public SlotArgs<Args...>
|
class SlotMember : public SlotArgs<Args...>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using PackType = std::tuple<typename std::remove_reference<Args>::type...>;
|
||||||
|
|
||||||
SlotMember(T *obj, bool isObject, void (T::*func)(Args...))
|
SlotMember(T *obj, bool isObject, void (T::*func)(Args...))
|
||||||
: SlotArgs<Args...>(obj, isObject), func_(func) {}
|
: SlotArgs<Args...>(obj, isObject), func_(func) {}
|
||||||
|
|
||||||
void invoke(Args... args) { (static_cast<T *>(this->obj_)->*func_)(args...); }
|
void activate(Args... args)
|
||||||
|
{
|
||||||
|
if (this->isObject_)
|
||||||
|
SlotBase::activatePack(new PackType{ args... });
|
||||||
|
else
|
||||||
|
(static_cast<T *>(this->obj_)->*func_)(args...);
|
||||||
|
}
|
||||||
|
|
||||||
|
void invoke(Args... args)
|
||||||
|
{
|
||||||
|
(static_cast<T *>(this->obj_)->*func_)(args...);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Signal<Args...>;
|
friend class Signal<Args...>;
|
||||||
|
@ -66,7 +117,8 @@ public:
|
||||||
SlotStatic(void (*func)(Args...))
|
SlotStatic(void (*func)(Args...))
|
||||||
: SlotArgs<Args...>(nullptr, false), func_(func) {}
|
: SlotArgs<Args...>(nullptr, false), func_(func) {}
|
||||||
|
|
||||||
void invoke(Args... args) { (*func_)(args...); }
|
void activate(Args... args) { (*func_)(args...); }
|
||||||
|
void invoke(Args... args) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Signal<Args...>;
|
friend class Signal<Args...>;
|
||||||
|
@ -186,9 +238,8 @@ public:
|
||||||
* disconnect operation, invalidating the iterator.
|
* disconnect operation, invalidating the iterator.
|
||||||
*/
|
*/
|
||||||
std::vector<SlotBase *> slots{ slots_.begin(), slots_.end() };
|
std::vector<SlotBase *> slots{ slots_.begin(), slots_.end() };
|
||||||
for (SlotBase *slot : slots) {
|
for (SlotBase *slot : slots)
|
||||||
static_cast<SlotArgs<Args...> *>(slot)->invoke(args...);
|
static_cast<SlotArgs<Args...> *>(slot)->activate(args...);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
namespace libcamera {
|
namespace libcamera {
|
||||||
|
|
||||||
class Object;
|
class Object;
|
||||||
|
class SlotBase;
|
||||||
class Thread;
|
class Thread;
|
||||||
|
|
||||||
class Message
|
class Message
|
||||||
|
@ -17,6 +18,7 @@ class Message
|
||||||
public:
|
public:
|
||||||
enum Type {
|
enum Type {
|
||||||
None = 0,
|
None = 0,
|
||||||
|
SignalMessage = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
Message(Type type);
|
Message(Type type);
|
||||||
|
@ -32,6 +34,18 @@ private:
|
||||||
Object *receiver_;
|
Object *receiver_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SignalMessage : public Message
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SignalMessage(SlotBase *slot, void *pack)
|
||||||
|
: Message(Message::SignalMessage), slot_(slot), pack_(pack)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SlotBase *slot_;
|
||||||
|
void *pack_;
|
||||||
|
};
|
||||||
|
|
||||||
} /* namespace libcamera */
|
} /* namespace libcamera */
|
||||||
|
|
||||||
#endif /* __LIBCAMERA_MESSAGE_H__ */
|
#endif /* __LIBCAMERA_MESSAGE_H__ */
|
||||||
|
|
|
@ -68,4 +68,26 @@ Message::~Message()
|
||||||
* \return The message receiver
|
* \return The message receiver
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \class SignalMessage
|
||||||
|
* \brief A message carrying a Signal across threads
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \fn SignalMessage::SignalMessage()
|
||||||
|
* \brief Construct a SignalMessage
|
||||||
|
* \param[in] slot The slot that the signal targets
|
||||||
|
* \param[in] pack The signal arguments
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \var SignalMessage::slot_
|
||||||
|
* \brief The slot that the signal targets
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \var SignalMessage::pack_
|
||||||
|
* \brief The signal arguments
|
||||||
|
*/
|
||||||
|
|
||||||
}; /* namespace libcamera */
|
}; /* namespace libcamera */
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <libcamera/signal.h>
|
#include <libcamera/signal.h>
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "message.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,6 +33,10 @@ namespace libcamera {
|
||||||
* This allows implementing easy message passing between threads by inheriting
|
* This allows implementing easy message passing between threads by inheriting
|
||||||
* from the Object class.
|
* from the Object class.
|
||||||
*
|
*
|
||||||
|
* Object slots connected to signals will also run in the context of the
|
||||||
|
* object's thread, regardless of whether the signal is emitted in the same or
|
||||||
|
* in another thread.
|
||||||
|
*
|
||||||
* \sa Message, Signal, Thread
|
* \sa Message, Signal, Thread
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -82,6 +87,16 @@ void Object::postMessage(std::unique_ptr<Message> msg)
|
||||||
*/
|
*/
|
||||||
void Object::message(Message *msg)
|
void Object::message(Message *msg)
|
||||||
{
|
{
|
||||||
|
switch (msg->type()) {
|
||||||
|
case Message::SignalMessage: {
|
||||||
|
SignalMessage *smsg = static_cast<SignalMessage *>(msg);
|
||||||
|
smsg->slot_->invokePack(smsg->pack_);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -7,6 +7,10 @@
|
||||||
|
|
||||||
#include <libcamera/signal.h>
|
#include <libcamera/signal.h>
|
||||||
|
|
||||||
|
#include "message.h"
|
||||||
|
#include "thread.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \file signal.h
|
* \file signal.h
|
||||||
* \brief Signal & slot implementation
|
* \brief Signal & slot implementation
|
||||||
|
@ -42,8 +46,30 @@ namespace libcamera {
|
||||||
* to the same slot. Duplicate connections between a signal and a slot are
|
* to the same slot. Duplicate connections between a signal and a slot are
|
||||||
* allowed and result in the slot being called multiple times for the same
|
* allowed and result in the slot being called multiple times for the same
|
||||||
* signal emission.
|
* signal emission.
|
||||||
|
*
|
||||||
|
* When a slot belongs to an instance of the Object class, the slot is called
|
||||||
|
* in the context of the thread that the object is bound to. If the signal is
|
||||||
|
* emitted from the same thread, the slot will be called synchronously, before
|
||||||
|
* Signal::emit() returns. If the signal is emitted from a different thread,
|
||||||
|
* the slot will be called asynchronously from the object's thread's event
|
||||||
|
* loop, after the Signal::emit() method returns, with a copy of the signal's
|
||||||
|
* arguments. The emitter shall thus ensure that any pointer or reference
|
||||||
|
* passed through the signal will remain valid after the signal is emitted.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
void SlotBase::activatePack(void *pack)
|
||||||
|
{
|
||||||
|
Object *obj = static_cast<Object *>(obj_);
|
||||||
|
|
||||||
|
if (Thread::current() == obj->thread()) {
|
||||||
|
invokePack(pack);
|
||||||
|
} else {
|
||||||
|
std::unique_ptr<Message> msg =
|
||||||
|
utils::make_unique<SignalMessage>(this, pack);
|
||||||
|
obj->postMessage(std::move(msg));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \fn Signal::connect(T *object, void(T::*func)(Args...))
|
* \fn Signal::connect(T *object, void(T::*func)(Args...))
|
||||||
* \brief Connect the signal to a member function slot
|
* \brief Connect the signal to a member function slot
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue