{"id":2274,"url":"https://patchwork.libcamera.org/api/patches/2274/?format=json","web_url":"https://patchwork.libcamera.org/patch/2274/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20191028104913.14985-2-laurent.pinchart@ideasonboard.com>","date":"2019-10-28T10:49:05","name":"[libcamera-devel,v2,1/9] libcamera: Add Semaphore class","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"c3f86983f84fa93f3f2e464f3ebae0550401e298","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2274/mbox/","series":[{"id":562,"url":"https://patchwork.libcamera.org/api/series/562/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=562","date":"2019-10-28T10:49:04","name":"Add support for blocking method invocation","version":2,"mbox":"https://patchwork.libcamera.org/series/562/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2274/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2274/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 D4FCC6017E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 28 Oct 2019 11:49:25 +0100 (CET)","from pendragon.ideasonboard.com (unknown [91.217.168.176])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7E0CE9C2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 28 Oct 2019 11:49:25 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1572259765;\n\tbh=v/OpZsArzT+Zf/lqelVeH/VCkVhny0DLIylZF2B19zE=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=EARnHxndFQDPEtjrB9NFfZd+EGKZrcF9rgbKaOLqbhLxSOiYT1BB5DbqBskn2BJEn\n\tVFIPFIL8kQxTVQAs3aNldjeP3adb/fTzj4hANbMwxF6MywWdUKENAhC53rDY38Y6Je\n\t0lnvL7WrpKqvk5go8jHF8VGa9CAV6iNaz4xOmdSg=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 28 Oct 2019 12:49:05 +0200","Message-Id":"<20191028104913.14985-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.23.0","In-Reply-To":"<20191028104913.14985-1-laurent.pinchart@ideasonboard.com>","References":"<20191028104913.14985-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 1/9] libcamera: Add Semaphore class","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":"Mon, 28 Oct 2019 10:49:26 -0000"},"content":"Add a general-purpose counting semaphore class.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/include/meson.build |   1 +\n src/libcamera/include/semaphore.h |  34 ++++++++++\n src/libcamera/meson.build         |   1 +\n src/libcamera/semaphore.cpp       | 103 ++++++++++++++++++++++++++++++\n 4 files changed, 139 insertions(+)\n create mode 100644 src/libcamera/include/semaphore.h\n create mode 100644 src/libcamera/semaphore.cpp","diff":"diff --git a/src/libcamera/include/meson.build b/src/libcamera/include/meson.build\nindex 2c74d29bd925..64c2155f90cf 100644\n--- a/src/libcamera/include/meson.build\n+++ b/src/libcamera/include/meson.build\n@@ -17,6 +17,7 @@ libcamera_headers = files([\n     'message.h',\n     'pipeline_handler.h',\n     'process.h',\n+    'semaphore.h',\n     'thread.h',\n     'utils.h',\n     'v4l2_controls.h',\ndiff --git a/src/libcamera/include/semaphore.h b/src/libcamera/include/semaphore.h\nnew file mode 100644\nindex 000000000000..c6b286536eb3\n--- /dev/null\n+++ b/src/libcamera/include/semaphore.h\n@@ -0,0 +1,34 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * semaphore.h - General-purpose counting semaphore\n+ */\n+#ifndef __LIBCAMERA_SEMAPHORE_H__\n+#define __LIBCAMERA_SEMAPHORE_H__\n+\n+#include <condition_variable>\n+\n+#include \"thread.h\"\n+\n+namespace libcamera {\n+\n+class Semaphore\n+{\n+public:\n+\tSemaphore(unsigned int n = 0);\n+\n+\tunsigned int available();\n+\tvoid acquire(unsigned int n = 1);\n+\tbool tryAcquire(unsigned int n = 1);\n+\tvoid release(unsigned int n = 1);\n+\n+private:\n+\tMutex mutex_;\n+\tstd::condition_variable cv_;\n+\tunsigned int available_;\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_SEMAPHORE_H__ */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex d329820b9582..dab5dbff77b7 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -27,6 +27,7 @@ libcamera_sources = files([\n     'pipeline_handler.cpp',\n     'process.cpp',\n     'request.cpp',\n+    'semaphore.cpp',\n     'signal.cpp',\n     'stream.cpp',\n     'thread.cpp',\ndiff --git a/src/libcamera/semaphore.cpp b/src/libcamera/semaphore.cpp\nnew file mode 100644\nindex 000000000000..ce1eae4914ed\n--- /dev/null\n+++ b/src/libcamera/semaphore.cpp\n@@ -0,0 +1,103 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * semaphore.cpp - General-purpose counting semaphore\n+ */\n+\n+#include \"semaphore.h\"\n+#include \"thread.h\"\n+\n+/**\n+ * \\file semaphore.h\n+ * \\brief General-purpose counting semaphore\n+ */\n+\n+namespace libcamera {\n+\n+/**\n+ * \\class Semaphore\n+ * \\brief General-purpose counting semaphore\n+ *\n+ * A semaphore is a locking primitive that protects resources. It is created\n+ * with an initial number of resources (which may be 0), and offers two\n+ * primitives to acquire and release resources. The acquire() method tries to\n+ * acquire a number of resources, and blocks if not enough resources are\n+ * available until they get released. The release() method releases a number of\n+ * resources, waking up any consumer blocked on an acquire() call.\n+ */\n+\n+/**\n+ * \\brief Construct a semaphore with \\a n resources\n+ * \\param[in] n The resource count\n+ */\n+Semaphore::Semaphore(unsigned int n)\n+\t: available_(n)\n+{\n+}\n+\n+/**\n+ * \\brief Retrieve the number of available resources\n+ * \\return The number of available resources\n+ */\n+unsigned int Semaphore::available()\n+{\n+\tMutexLocker locker(mutex_);\n+\treturn available_;\n+}\n+\n+/**\n+ * \\brief Acquire \\a n resources\n+ * \\param[in] n The resource count\n+ *\n+ * This method attempts to acquire \\a n resources. If \\a n is higher than the\n+ * number of available resources, the call will block until enough resources\n+ * become available.\n+ */\n+void Semaphore::acquire(unsigned int n)\n+{\n+\tMutexLocker locker(mutex_);\n+\tcv_.wait(locker, [&] { return available_ >= n; });\n+\tavailable_ -= n;\n+}\n+\n+/**\n+ * \\brief Try to acquire \\a n resources without blocking\n+ * \\param[in] n The resource count\n+ *\n+ * This method attempts to acquire \\a n resources. If \\a n is higher than the\n+ * number of available resources, it returns false immediately without\n+ * acquiring any resource. Otherwise it acquires the resources and returns\n+ * true.\n+ *\n+ * \\return True if the resources have been acquired, false otherwise\n+ */\n+bool Semaphore::tryAcquire(unsigned int n)\n+{\n+\tMutexLocker locker(mutex_);\n+\tif (available_ < n)\n+\t\treturn false;\n+\n+\tavailable_ -= n;\n+\treturn true;\n+}\n+\n+/**\n+ * \\brief Release \\a n resources\n+ * \\param[in] n The resource count\n+ *\n+ * This method releases \\a n resources, increasing the available resource count\n+ * by \\a n. If the number of available resources becomes large enough for any\n+ * consumer blocked on an acquire() call, those consumers get woken up.\n+ */\n+void Semaphore::release(unsigned int n)\n+{\n+\t{\n+\t\tMutexLocker locker(mutex_);\n+\t\tavailable_ += n;\n+\t}\n+\n+\tcv_.notify_all();\n+}\n+\n+} /* namespace libcamera */\n","prefixes":["libcamera-devel","v2","1/9"]}