This commit adds support to schedule the deletion of an Object to the thread it is bound to (similar to [1]). An Object getting destroyed by a different thread is considered as a violation as per the libcamera threading model. This will be useful for an Object where its ownership is shared via shared pointers in different threads. If the thread which drops the last reference of the Object is a different thread, the destructors get called in that particular thread, not the one Object is bound to. Hence, in order to resolve this kind of situation, the creation of shared pointer can be accompanied by a custom deleter which in turns use deleteLater() to ensure the Object is destroyed in its own thread. [1] https://doc.qt.io/qt-5/qobject.html#deleteLater Signed-off-by: Umang Jain <email@uajain.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
72 lines
1.3 KiB
C++
72 lines
1.3 KiB
C++
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
|
/*
|
|
* Copyright (C) 2019, Google Inc.
|
|
*
|
|
* message.h - Message queue support
|
|
*/
|
|
#ifndef __LIBCAMERA_INTERNAL_MESSAGE_H__
|
|
#define __LIBCAMERA_INTERNAL_MESSAGE_H__
|
|
|
|
#include <atomic>
|
|
|
|
#include <libcamera/bound_method.h>
|
|
|
|
namespace libcamera {
|
|
|
|
class BoundMethodBase;
|
|
class Object;
|
|
class Semaphore;
|
|
class Thread;
|
|
|
|
class Message
|
|
{
|
|
public:
|
|
enum Type {
|
|
None = 0,
|
|
InvokeMessage = 1,
|
|
ThreadMoveMessage = 2,
|
|
DeferredDelete = 3,
|
|
UserMessage = 1000,
|
|
};
|
|
|
|
Message(Type type);
|
|
virtual ~Message();
|
|
|
|
Type type() const { return type_; }
|
|
Object *receiver() const { return receiver_; }
|
|
|
|
static Type registerMessageType();
|
|
|
|
private:
|
|
friend class Thread;
|
|
|
|
Type type_;
|
|
Object *receiver_;
|
|
|
|
static std::atomic_uint nextUserType_;
|
|
};
|
|
|
|
class InvokeMessage : public Message
|
|
{
|
|
public:
|
|
InvokeMessage(BoundMethodBase *method,
|
|
std::shared_ptr<BoundMethodPackBase> pack,
|
|
Semaphore *semaphore = nullptr,
|
|
bool deleteMethod = false);
|
|
~InvokeMessage();
|
|
|
|
Semaphore *semaphore() const { return semaphore_; }
|
|
|
|
void invoke();
|
|
|
|
private:
|
|
BoundMethodBase *method_;
|
|
std::shared_ptr<BoundMethodPackBase> pack_;
|
|
Semaphore *semaphore_;
|
|
bool deleteMethod_;
|
|
};
|
|
|
|
} /* namespace libcamera */
|
|
|
|
#endif /* __LIBCAMERA_INTERNAL_MESSAGE_H__ */
|