mirror of
https://git.libcamera.org/libcamera/libcamera.git
synced 2025-07-13 07:19:45 +03:00
libcamera: bound_method: Avoid deadlock with ConnectionTypeBlocking
ConnectionTypeBlocking always invokes the method through inter-thread message passing, which results in deadlocks if the sender and receiver live in the same thread. The deadlock can easily be avoided by turning the invocation into a direct call in this case. Do so to make ConnectionTypeBlocking easier to use when some of the senders live in the same thread as the receiver while the other senders don't. Extend the object-invoke test to cover this usage. While at it reformat the documentation to avoid long \brief lines. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
parent
9977fc3fcb
commit
8034af7423
2 changed files with 34 additions and 8 deletions
|
@ -35,16 +35,19 @@ namespace libcamera {
|
||||||
* thread.
|
* thread.
|
||||||
*
|
*
|
||||||
* \var ConnectionType::ConnectionTypeQueued
|
* \var ConnectionType::ConnectionTypeQueued
|
||||||
* \brief The receiver is invoked asynchronously in its thread when control
|
* \brief The receiver is invoked asynchronously
|
||||||
* returns to the thread's event loop. The sender proceeds without waiting for
|
*
|
||||||
* the invocation to complete.
|
* Invoke the receiver asynchronously in its thread when control returns to the
|
||||||
|
* thread's event loop. The sender proceeds without waiting for the invocation
|
||||||
|
* to complete.
|
||||||
*
|
*
|
||||||
* \var ConnectionType::ConnectionTypeBlocking
|
* \var ConnectionType::ConnectionTypeBlocking
|
||||||
* \brief The receiver is invoked asynchronously in its thread when control
|
* \brief The receiver is invoked synchronously
|
||||||
* returns to the thread's event loop. The sender blocks until the receiver
|
*
|
||||||
* signals the completion of the invocation. This connection type shall not be
|
* If the sender and the receiver live in the same thread, this is equivalent to
|
||||||
* used when the sender and receiver live in the same thread, otherwise
|
* ConnectionTypeDirect. Otherwise, the receiver is invoked asynchronously in
|
||||||
* deadlock will occur.
|
* its thread when control returns to the thread's event loop. The sender
|
||||||
|
* blocks until the receiver signals the completion of the invocation.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -71,6 +74,9 @@ bool BoundMethodBase::activatePack(std::shared_ptr<BoundMethodPackBase> pack,
|
||||||
type = ConnectionTypeDirect;
|
type = ConnectionTypeDirect;
|
||||||
else
|
else
|
||||||
type = ConnectionTypeQueued;
|
type = ConnectionTypeQueued;
|
||||||
|
} else if (type == ConnectionTypeBlocking) {
|
||||||
|
if (Thread::current() == object_->thread())
|
||||||
|
type = ConnectionTypeDirect;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
|
|
@ -100,6 +100,26 @@ protected:
|
||||||
return TestFail;
|
return TestFail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Test that blocking invocation is delivered directly when the
|
||||||
|
* caller and callee live in the same thread.
|
||||||
|
*/
|
||||||
|
object_.reset();
|
||||||
|
|
||||||
|
object_.invokeMethod(&InvokedObject::method,
|
||||||
|
ConnectionTypeBlocking, 42);
|
||||||
|
|
||||||
|
switch (object_.status()) {
|
||||||
|
case InvokedObject::NoCall:
|
||||||
|
cout << "Method not invoked for main thread (blocking)" << endl;
|
||||||
|
return TestFail;
|
||||||
|
case InvokedObject::InvalidThread:
|
||||||
|
cout << "Method invoked in incorrect thread for main thread (blocking)" << endl;
|
||||||
|
return TestFail;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Move the object to a thread and verify that auto method
|
* Move the object to a thread and verify that auto method
|
||||||
* invocation is delivered in the correct thread.
|
* invocation is delivered in the correct thread.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue