From patchwork Wed Nov 27 08:49:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2381 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2C82160BB8 for ; Wed, 27 Nov 2019 09:49:26 +0100 (CET) Received: from pendragon.tok.corp.google.com (unknown [104.132.253.101]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id F3A269F4 for ; Wed, 27 Nov 2019 09:49:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1574844565; bh=XdmYqbeOJzAczJosIpJ/e4IR/lacPzAmhE0GxY0CLyg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=riZS7kcV3m0k6MZZtfaaEQSpRixZzMCh+tTAxHy3KMJWJoW3tWVDi8HLvWxz8wXK+ 9ow2t8/ms/kuq0ZD+V5bWvSmPHOgmFdQJx5NNplx0pR55DHn5paKZa/zUxA/jAxzEz MEQWECWT6Rf3QYoIUukd6r9NH1HTcK3QN2bZ7RrU= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Wed, 27 Nov 2019 10:49:07 +0200 Message-Id: <20191127084909.10612-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191127084909.10612-1-laurent.pinchart@ideasonboard.com> References: <20191127084909.10612-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 3/5] libcamera: thread: Fix race condition when dispatching messages X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Nov 2019 08:49:26 -0000 The Object class stores the number of pending messages that have been posted for it, while the actual messages are stored in a per-thread list in the ThreadData class. When dispatching messages, the message is removed from the list, passed to the receiver, and the number of pending messages is updated. In order to avoid too much contention on the list lock, the lock is dropped to pass the message to the receiver and then taken again. This creates a race condition window with Thread::removeMessages(), as the number of pending messages for a receiver is briefly out of sync with the pending messages list. The assertion at the end of removeMessages() thus sometimes fails. Fix this by decrementing the pending messages counter before releasing the lock in Thread::dispatchMessages(). This fixes the slow message receiver test in MessageTest. Fixes: 01b930964acd ("libcamera: thread: Add a messaging passing API") Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- src/libcamera/thread.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp index 029a0e8fddd5..18ebd16a7e2f 100644 --- a/src/libcamera/thread.cpp +++ b/src/libcamera/thread.cpp @@ -439,11 +439,11 @@ void Thread::dispatchMessages() Object *receiver = msg->receiver_; ASSERT(data_ == receiver->thread()->data_); + receiver->pendingMessages_--; + locker.unlock(); receiver->message(msg.get()); locker.lock(); - - receiver->pendingMessages_--; } }