From patchwork Thu Jan 23 02:55:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2730 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 C933C6044D for ; Thu, 23 Jan 2020 03:55:39 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 4AF322E5 for ; Thu, 23 Jan 2020 03:55:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1579748139; bh=7RMpDogpqHD0w2RGKoj2saC6+NFUolU/sSYcqmT6wyA=; h=From:To:Subject:Date:From; b=fuCbGBcoD8HpbfW3p6zexBKNwdfqB0EUR7UiPQubabyW/uCIrTnGNYZHSt3noJQb4 WK8HAs5cO3wYah5cdy8p2wSpzGHfQxQSJsYLFgif+f4cso1pZTZaN915ME2Jz/FFFQ LLFEzsb9veyEWGOKU1nztvCkR38W5RqtuL4qQOw4= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 23 Jan 2020 04:55:19 +0200 Message-Id: <20200123025520.12149-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/2] libcamera: thread: Support timeout in wait() function 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: Thu, 23 Jan 2020 02:55:40 -0000 Add a parameter to the Thread::wait() function to wait with a timeout. The delay value waits forever. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- src/libcamera/include/thread.h | 4 +++- src/libcamera/thread.cpp | 28 +++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/libcamera/include/thread.h b/src/libcamera/include/thread.h index 819a9879ac56..d700f111a3ae 100644 --- a/src/libcamera/include/thread.h +++ b/src/libcamera/include/thread.h @@ -14,6 +14,8 @@ #include +#include "utils.h" + namespace libcamera { class EventDispatcher; @@ -33,7 +35,7 @@ public: void start(); void exit(int code = 0); - void wait(); + bool wait(utils::duration duration = utils::duration::max()); bool isRunning(); diff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp index fe32cd677596..22ab41f847db 100644 --- a/src/libcamera/thread.cpp +++ b/src/libcamera/thread.cpp @@ -8,6 +8,7 @@ #include "thread.h" #include +#include #include #include #include @@ -71,6 +72,7 @@ private: std::atomic dispatcher_; + std::condition_variable cv_; std::atomic exit_; int exitCode_; @@ -248,6 +250,7 @@ void Thread::finishThread() data_->mutex_.unlock(); finished.emit(this); + data_->cv_.notify_all(); } /** @@ -274,14 +277,33 @@ void Thread::exit(int code) /** * \brief Wait for the thread to finish + * \param[in] duration Maximum wait duration * - * This method waits until the thread finishes, or returns immediately if the - * thread is not running. + * This function waits until the thread finishes or the \a duration has + * elapsed, whichever happens first. If \a duration is equal to + * utils::duration::max(), the wait never times out. If the thread is not + * running the function returns immediately. + * + * \return True if the thread has finished, or false if the wait timed out */ -void Thread::wait() +bool Thread::wait(utils::duration duration) { + bool timeout = false; + + { + MutexLocker locker(data_->mutex_); + + if (duration == utils::duration::max()) + data_->cv_.wait(locker, [&]() { return !data_->running_; }); + else + timeout = !data_->cv_.wait_for(locker, duration, + [&]() { return !data_->running_; }); + } + if (thread_.joinable()) thread_.join(); + + return timeout; } /**