[{"id":2980,"web_url":"https://patchwork.libcamera.org/comment/2980/","msgid":"<20191028110657.GF20198@bigcity.dyn.berto.se>","date":"2019-10-28T11:06:57","subject":"Re: [libcamera-devel] [PATCH v2 1/9] libcamera: Add Semaphore class","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Laurent,\n\nThanks for your patch.\n\nOn 2019-10-28 12:49:05 +0200, Laurent Pinchart wrote:\n> Add a general-purpose counting semaphore class.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\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\n> \n> diff --git a/src/libcamera/include/meson.build b/src/libcamera/include/meson.build\n> index 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',\n> diff --git a/src/libcamera/include/semaphore.h b/src/libcamera/include/semaphore.h\n> new file mode 100644\n> index 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__ */\n> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> index 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',\n> diff --git a/src/libcamera/semaphore.cpp b/src/libcamera/semaphore.cpp\n> new file mode 100644\n> index 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> -- \n> Regards,\n> \n> Laurent Pinchart\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from mail-lj1-x241.google.com (mail-lj1-x241.google.com\n\t[IPv6:2a00:1450:4864:20::241])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8A59260180\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 28 Oct 2019 12:06:58 +0100 (CET)","by mail-lj1-x241.google.com with SMTP id q78so10848081lje.5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 28 Oct 2019 04:06:58 -0700 (PDT)","from localhost (h-93-159.A463.priv.bahnhof.se. [46.59.93.159])\n\tby smtp.gmail.com with ESMTPSA id\n\t201sm4900023ljf.39.2019.10.28.04.06.57\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 28 Oct 2019 04:06:57 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to\n\t:user-agent; bh=WLkbmMS4UIaws2Y0pQj31M6coShEy0ALB9a56LSKdtE=;\n\tb=KMO06Sd2pXpR/jkyH8ftQZ4IjhuBPEvKKJ+yuUX0u3fxLgL5Y3wGRaXCDHmuYY/7gX\n\tKNmL+qjJV0yvJ/kqhWwr3ULD+36oVI4dAxovRz73TFaPC0sZ9kPmZcduynEjGh+6tg1A\n\tzoVzjMryaixxscnOqYrXGpvDgo7+PDIIXv8C8mSuA8M7+5inlcrHU62YM2Zm2b2rky7c\n\t+Hu06yXvST8XufTzWxj8MLwhP+Qrzj+2WHWw/a5E10z1FuBMgLMam0HJznEMW9By9NYN\n\tuSG+BRIv9bbI5lIFFx6o7cdhnvRiDifY9MMrhaWe5V970pYCSE8M5nqMJc/dqntZtBks\n\t7zFw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to:user-agent;\n\tbh=WLkbmMS4UIaws2Y0pQj31M6coShEy0ALB9a56LSKdtE=;\n\tb=GUExAElIDaFXGncQYwnpKc6WIsEmoPg8ZsOR8J3eLD1SitomScD25Als6FsQcE03fk\n\tD0S2lU6AfchG/azRtUt3uawJycdSwlnkq3ygzQtbqtFOgvTcly6SIzO6jAwWiMD0TAFZ\n\tL5XqtN/HQIpdsu9CmB9cpQ2d1f3Nf/7BznEHznZ4TFF74dvVHY9ECCoHHf5qAEF14LsG\n\tQlKq3NXK6bvWKE47zIBobzWpjqxmgXa1Kx4P0gqCgn6ONZbVas0SPi9lbb+X3d0Jlu+i\n\tH8Ir8c8NOqjaJpdr7hi/tahXNSj5nHivQUmFNUH0X1jCav+aaIL+UrkKwkw+0NjGx/WD\n\tzhcA==","X-Gm-Message-State":"APjAAAVdiBHPj5iEu2fuNjEAUC5pLUx6mBK5nquBfmwxdVZbc2ZYaBGN\n\tQXw2mLcOIbPCzdps2CajZYe4Pw==","X-Google-Smtp-Source":"APXvYqyCG8kzHL1l3hEOfIDnFLk31Hp91403P5Id/DOtrFd0yquC5+M7bBGz29fvhgWmJ/bGcMtRNQ==","X-Received":"by 2002:a2e:b4e8:: with SMTP id s8mr11588644ljm.73.1572260818009;\n\tMon, 28 Oct 2019 04:06:58 -0700 (PDT)","Date":"Mon, 28 Oct 2019 12:06:57 +0100","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20191028110657.GF20198@bigcity.dyn.berto.se>","References":"<20191028104913.14985-1-laurent.pinchart@ideasonboard.com>\n\t<20191028104913.14985-2-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20191028104913.14985-2-laurent.pinchart@ideasonboard.com>","User-Agent":"Mutt/1.12.1 (2019-06-15)","Subject":"Re: [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 11:06:58 -0000"}}]