{"id":2730,"url":"https://patchwork.libcamera.org/api/1.1/patches/2730/?format=json","web_url":"https://patchwork.libcamera.org/patch/2730/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200123025520.12149-1-laurent.pinchart@ideasonboard.com>","date":"2020-01-23T02:55:19","name":"[libcamera-devel,1/2] libcamera: thread: Support timeout in wait() function","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"c2e28a7cff81ac1c9e2d41c0327ed1fd0f2b1cd0","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2730/mbox/","series":[{"id":646,"url":"https://patchwork.libcamera.org/api/1.1/series/646/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=646","date":"2020-01-23T02:55:19","name":"[libcamera-devel,1/2] libcamera: thread: Support timeout in wait() function","version":1,"mbox":"https://patchwork.libcamera.org/series/646/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2730/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2730/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C933C6044D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Jan 2020 03:55:39 +0100 (CET)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4AF322E5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Jan 2020 03:55:39 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1579748139;\n\tbh=7RMpDogpqHD0w2RGKoj2saC6+NFUolU/sSYcqmT6wyA=;\n\th=From:To:Subject:Date:From;\n\tb=fuCbGBcoD8HpbfW3p6zexBKNwdfqB0EUR7UiPQubabyW/uCIrTnGNYZHSt3noJQb4\n\tWK8HAs5cO3wYah5cdy8p2wSpzGHfQxQSJsYLFgif+f4cso1pZTZaN915ME2Jz/FFFQ\n\tLLFEzsb9veyEWGOKU1nztvCkR38W5RqtuL4qQOw4=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 1/2] libcamera: thread: Support timeout in\n\twait() function","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Thu, 23 Jan 2020 02:55:40 -0000"},"content":"Add a parameter to the Thread::wait() function to wait with a timeout.\nThe delay value waits forever.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/include/thread.h |  4 +++-\n src/libcamera/thread.cpp       | 28 +++++++++++++++++++++++++---\n 2 files changed, 28 insertions(+), 4 deletions(-)","diff":"diff --git a/src/libcamera/include/thread.h b/src/libcamera/include/thread.h\nindex 819a9879ac56..d700f111a3ae 100644\n--- a/src/libcamera/include/thread.h\n+++ b/src/libcamera/include/thread.h\n@@ -14,6 +14,8 @@\n \n #include <libcamera/signal.h>\n \n+#include \"utils.h\"\n+\n namespace libcamera {\n \n class EventDispatcher;\n@@ -33,7 +35,7 @@ public:\n \n \tvoid start();\n \tvoid exit(int code = 0);\n-\tvoid wait();\n+\tbool wait(utils::duration duration = utils::duration::max());\n \n \tbool isRunning();\n \ndiff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp\nindex fe32cd677596..22ab41f847db 100644\n--- a/src/libcamera/thread.cpp\n+++ b/src/libcamera/thread.cpp\n@@ -8,6 +8,7 @@\n #include \"thread.h\"\n \n #include <atomic>\n+#include <condition_variable>\n #include <list>\n #include <unistd.h>\n #include <sys/syscall.h>\n@@ -71,6 +72,7 @@ private:\n \n \tstd::atomic<EventDispatcher *> dispatcher_;\n \n+\tstd::condition_variable cv_;\n \tstd::atomic<bool> exit_;\n \tint exitCode_;\n \n@@ -248,6 +250,7 @@ void Thread::finishThread()\n \tdata_->mutex_.unlock();\n \n \tfinished.emit(this);\n+\tdata_->cv_.notify_all();\n }\n \n /**\n@@ -274,14 +277,33 @@ void Thread::exit(int code)\n \n /**\n  * \\brief Wait for the thread to finish\n+ * \\param[in] duration Maximum wait duration\n  *\n- * This method waits until the thread finishes, or returns immediately if the\n- * thread is not running.\n+ * This function waits until the thread finishes or the \\a duration has\n+ * elapsed, whichever happens first. If \\a duration is equal to\n+ * utils::duration::max(), the wait never times out. If the thread is not\n+ * running the function returns immediately.\n+ *\n+ * \\return True if the thread has finished, or false if the wait timed out\n  */\n-void Thread::wait()\n+bool Thread::wait(utils::duration duration)\n {\n+\tbool timeout = false;\n+\n+\t{\n+\t\tMutexLocker locker(data_->mutex_);\n+\n+\t\tif (duration == utils::duration::max())\n+\t\t\tdata_->cv_.wait(locker, [&]() { return !data_->running_; });\n+\t\telse\n+\t\t\ttimeout = !data_->cv_.wait_for(locker, duration,\n+\t\t\t\t\t\t       [&]() { return !data_->running_; });\n+\t}\n+\n \tif (thread_.joinable())\n \t\tthread_.join();\n+\n+\treturn timeout;\n }\n \n /**\n","prefixes":["libcamera-devel","1/2"]}