From patchwork Thu Feb 13 12:45:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2805 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 E5A336043A for ; Thu, 13 Feb 2020 13:45:58 +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 6730D504 for ; Thu, 13 Feb 2020 13:45:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1581597958; bh=hZSK0l6AfPfGCcA9tZ491nF3xbSfMbucnv2w4Y3oWRs=; h=From:To:Subject:Date:From; b=B8NXFA0wnF23Rlop2gzIq9z/arqaw4v14z/lFpzKkCCSJVGgrzjsc+2cyZLIfmoHl n9bHEOQoqn1Huk9PaYQt5ZrQz+L1xbMWQQX0eIzcerHrkWTXuv8MPZ+j9O3z9LI3jV +tuYa5IlDlgXId05WHRanx+u8bOjwrqc7Y0K/xOI= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 13 Feb 2020 14:45:36 +0200 Message-Id: <20200213124538.21556-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/3] 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, 13 Feb 2020 12:45:59 -0000 Add a parameter to the Thread::wait() function to wait with a timeout. The delay value utils::duration::max() waits forever. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- Changes since v1: - Invert return value of the wait() function to match the documentation --- 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 1d0a600ab5cc..b613305fdc87 100644 --- a/src/libcamera/thread.cpp +++ b/src/libcamera/thread.cpp @@ -8,6 +8,7 @@ #include "thread.h" #include +#include #include #include #include @@ -155,6 +156,7 @@ private: std::atomic dispatcher_; + std::condition_variable cv_; std::atomic exit_; int exitCode_; @@ -334,6 +336,7 @@ void Thread::finishThread() data_->mutex_.unlock(); finished.emit(this); + data_->cv_.notify_all(); } /** @@ -360,14 +363,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 finished = true; + + { + MutexLocker locker(data_->mutex_); + + if (duration == utils::duration::max()) + data_->cv_.wait(locker, [&]() { return !data_->running_; }); + else + finished = data_->cv_.wait_for(locker, duration, + [&]() { return !data_->running_; }); + } + if (thread_.joinable()) thread_.join(); + + return finished; } /** From patchwork Thu Feb 13 12:45:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2806 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 39F4D6194C for ; Thu, 13 Feb 2020 13:45:59 +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 C73099D3 for ; Thu, 13 Feb 2020 13:45:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1581597959; bh=FdqcIqDdEZpGndnjY0aC5fqpLG4tc4BJ7+pBb/PjPd4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Y6NldCxDx6iXmKptO4I8SUZwyz2lBIcnJjTeUVsRuMHITj0sec6lNCo3TrD0M+QYu QOvD/9EzJ8ekEk/OadBeqC3rVUUn7L3ZrlVQX5LwXRXaqqbbgqiNQtsPTsDm5XpvVr ideQ6obJm2/BdYxbPCpw3TCfd1VwNuTSiSb253x4= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 13 Feb 2020 14:45:37 +0200 Message-Id: <20200213124538.21556-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200213124538.21556-1-laurent.pinchart@ideasonboard.com> References: <20200213124538.21556-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/3] test: threads: Add wait() timeout test 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, 13 Feb 2020 12:45:59 -0000 Add a test case to wait with a timeout, testing both a too short and a long enough duration. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- test/threads.cpp | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/test/threads.cpp b/test/threads.cpp index 9a2d39dfd106..1fa26020ce5c 100644 --- a/test/threads.cpp +++ b/test/threads.cpp @@ -15,24 +15,22 @@ using namespace std; using namespace libcamera; -class InstrumentedThread : public Thread +class DelayThread : public Thread { public: - InstrumentedThread(unsigned int iterations) - : iterations_(iterations) + DelayThread(chrono::steady_clock::duration duration) + : duration_(duration) { } protected: void run() { - for (unsigned int i = 0; i < iterations_; ++i) { - this_thread::sleep_for(chrono::milliseconds(50)); - } + this_thread::sleep_for(duration_); } private: - unsigned int iterations_; + chrono::steady_clock::duration duration_; }; class ThreadTest : public Test @@ -82,6 +80,27 @@ protected: delete thread; + /* Test waiting for completion with a timeout. */ + thread = new DelayThread(chrono::milliseconds(500)); + thread->start(); + thread->exit(0); + + bool timeout = !thread->wait(chrono::milliseconds(100)); + + if (!timeout) { + cout << "Waiting for thread didn't time out" << endl; + return TestFail; + } + + timeout = !thread->wait(chrono::milliseconds(1000)); + + if (timeout) { + cout << "Waiting for thread timed out" << endl; + return TestFail; + } + + delete thread; + return TestPass; } From patchwork Thu Feb 13 12:45:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2807 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 EF27F6043A for ; Thu, 13 Feb 2020 13:45:59 +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 8450C504 for ; Thu, 13 Feb 2020 13:45:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1581597959; bh=yTjfeCUuzzHClP8uKiPMr2KvanveOhM2krUNf7dyVFI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=EGSev8OEUJ7Ntcp+BURwjRwWcJZOz9ecpQbZ12vl1/Z+6Qb/ZMHV7ZdZwJxSmpDXb ddwxfPVNcdiIaBgfYdf821T6wAJpS2yt9YwKXm9SiiLlTNB4dh2eN/Fl/F85c5jfwv mWjfMf3lZkiQ9DjrgTizEagKlzW8VBkUDBt0hCp4= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 13 Feb 2020 14:45:38 +0200 Message-Id: <20200213124538.21556-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200213124538.21556-1-laurent.pinchart@ideasonboard.com> References: <20200213124538.21556-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/3] test: thread: Test waiting on a thread that is not running 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, 13 Feb 2020 12:46:00 -0000 Test that Thread::wait() on a thread that hasn't been started, or on a thread that is known to have completed, returns without a timeout. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- test/threads.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/threads.cpp b/test/threads.cpp index 1fa26020ce5c..0454761de12c 100644 --- a/test/threads.cpp +++ b/test/threads.cpp @@ -101,6 +101,25 @@ protected: delete thread; + /* Test waiting on a thread that isn't running. */ + thread = new Thread(); + + timeout = !thread->wait(); + if (timeout) { + cout << "Waiting for non-started thread timed out" << endl; + return TestFail; + } + + thread->start(); + thread->exit(0); + thread->wait(); + + timeout = !thread->wait(); + if (timeout) { + cout << "Waiting for already stopped thread timed out" << endl; + return TestFail; + } + return TestPass; }