libcamera: base: semaphore: Do not unlock prematurely

In `Semaphore::release()`, unlocking the mutex before signalling the condition
variable can be problematic, especially with "temporary" objects such as the
ones `BoundMethodBase::activatePack()` uses to handle `ConnectionTypeBlocking`.

Specifically, `Semaphore::acquire()` might lock the mutex after `Semaphore::release()`
has unlocked it, but before it had the chance to notify the condition variable.
In that case `Semaphore::acquire()` can succeed, and execution may proceed to
destroy the `Semaphore` object while the other thread is in the process of
running `std::condition_variable::notify_all()`.

Bug: https://bugs.libcamera.org/show_bug.cgi?id=225
Fixes: 66e7c5b774 ("libcamera: Add Semaphore class")
Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Barnabás Pőcze
2025-08-14 11:05:40 +02:00
parent 1bd66f54a6
commit 7609719514
+2 -4
View File
@@ -93,11 +93,9 @@ bool Semaphore::tryAcquire(unsigned int n)
*/
void Semaphore::release(unsigned int n)
{
{
MutexLocker locker(mutex_);
available_ += n;
}
MutexLocker locker(mutex_);
available_ += n;
cv_.notify_all();
}