[{"id":22952,"web_url":"https://patchwork.libcamera.org/comment/22952/","msgid":"<CAOgh=Fxbk3XER88yZ0Cs0a37O8ZbYxN8Wqnys1cuQgzNH=xFxA@mail.gmail.com>","date":"2022-05-11T11:33:00","subject":"Re: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer\n\tevents to event loop","submitter":{"id":101,"url":"https://patchwork.libcamera.org/api/people/101/","name":"Eric Curtin","email":"ecurtin@redhat.com"},"content":"On Thu, 5 May 2022 at 16:19, Eric Curtin <ecurtin@redhat.com> wrote:\n>\n> Extend the EventLoop class to support periodic timer events. This can be\n> used to run tasks periodically, such as handling the event loop of SDL.\n>\n> Also delete all events in the list, before we event_base_loopbreak.\n>\n> Signed-off-by: Eric Curtin <ecurtin@redhat.com>\n> ---\n>  src/cam/event_loop.cpp | 33 +++++++++++++++++++++++++++++++++\n>  src/cam/event_loop.h   |  6 ++++++\n>  2 files changed, 39 insertions(+)\n>\n> diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp\n> index e25784c0..181c971c 100644\n> --- a/src/cam/event_loop.cpp\n> +++ b/src/cam/event_loop.cpp\n> @@ -47,6 +47,10 @@ int EventLoop::exec()\n>  void EventLoop::exit(int code)\n>  {\n>         exitCode_ = code;\n> +       for (auto const &e : events_) {\n> +               event_del(e->event_);\n> +       }\n> +\n>         event_base_loopbreak(base_);\n>  }\n>\n> @@ -84,6 +88,35 @@ void EventLoop::addEvent(int fd, EventType type,\n>         events_.push_back(std::move(event));\n>  }\n>\n> +void EventLoop::toTimeval(const std::chrono::milliseconds d, struct timeval &tv)\n> +{\n> +       tv.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(d).count();\n> +       tv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d).count();\n> +}\n> +\n> +void EventLoop::addTimerEvent(const std::chrono::milliseconds d,\n> +                             const std::function<void()> &callback)\n> +{\n> +       std::unique_ptr<Event> event = std::make_unique<Event>(callback);\n> +       event->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch,\n> +                                 event.get());\n> +       if (!event->event_) {\n> +               std::cerr << \"Failed to create timer event\" << std::endl;\n> +               return;\n> +       }\n> +\n> +       struct timeval tv;\n> +       toTimeval(d, tv);\n> +\n> +       int ret = event_add(event->event_, &tv);\n> +       if (ret < 0) {\n> +               std::cerr << \"Failed to add timer event\" << std::endl;\n> +               return;\n> +       }\n> +\n> +       events_.push_back(std::move(event));\n> +}\n> +\n>  void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd,\n>                                  [[maybe_unused]] short flags, void *param)\n>  {\n> diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h\n> index a4613eb2..89215dce 100644\n> --- a/src/cam/event_loop.h\n> +++ b/src/cam/event_loop.h\n> @@ -14,6 +14,8 @@\n>\n>  #include <event2/util.h>\n>\n> +using namespace std::chrono_literals;\n> +\n>  struct event_base;\n>\n>  class EventLoop\n> @@ -37,6 +39,9 @@ public:\n>         void addEvent(int fd, EventType type,\n>                       const std::function<void()> &handler);\n>\n> +       void addTimerEvent(const std::chrono::milliseconds d,\n> +                          const std::function<void()> &handler);\n> +\n>  private:\n>         struct Event {\n>                 Event(const std::function<void()> &callback);\n> @@ -57,6 +62,7 @@ private:\n>         std::list<std::unique_ptr<Event>> events_;\n>         std::mutex lock_;\n>\n> +       void toTimeval(const std::chrono::milliseconds d, struct timeval &tv);\n>         static void dispatchCallback(evutil_socket_t fd, short flags,\n>                                      void *param);\n>         void dispatchCall();\n> --\n> 2.35.1\n>\n\nHi Guys,\n\nFriendly reminder if you could review once you get a spare moment.\n\nRegards,\n\nEric","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 3A494C0F2A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 11 May 2022 11:33:23 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8AC7765641;\n\tWed, 11 May 2022 13:33:22 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id CF39B6563F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 11 May 2022 13:33:19 +0200 (CEST)","from mail-oi1-f199.google.com (mail-oi1-f199.google.com\n\t[209.85.167.199]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id\n\tus-mta-504-LF2U08MqPkCU8lVWgUwDHw-1; Wed, 11 May 2022 07:33:17 -0400","by mail-oi1-f199.google.com with SMTP id\n\tbf4-20020a056808190400b003269782e303so1043975oib.5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 11 May 2022 04:33:17 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1652268802;\n\tbh=PwEjLRUL0Qpxr7SARp5/VZZ3lSFkGaisb3NS8qTa5s4=;\n\th=References:In-Reply-To:Date:To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=tPpBYDcxhk1YLS9O1UajIz6FQ4C9V6fCzXs2SJfcjK4N4j+sBQ/OlBzxXLuNn2t6w\n\tkAC9tYBK/JQZbqDxyEcDffKd8wqBPMi92NCt21j8mb9Z+ij3yjSNp77MV6f1PvZYe9\n\tt3SQ+Mp6RdukXayv6lyw1kDQfxkjgbCxy8M7DMw4JiAcJYdHR69QHUHxXRZ45Xv2tq\n\tNqk0pjT8z0UeG8SoV/taz3/+fdgdmvjbA1j2o0730vUpUnUC8kaHviTy22D/ceIssk\n\t9gimUKpTgAL4aR+XFKMxoaZWuGC4vm0xPLdBbqhKMIYHCyecd5+j5bn1nWSUGIgbrZ\n\tpm930jVgjxUFw==","v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1652268798;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=3e/I7VZ7+vB434JgFH5+lGoNrT/x9FnInQlAT9zp/OU=;\n\tb=fTEQfCg9GKRvXG2aby12yPHiCJGapLr9vDdVmWgqgwj1AXFwjSdhG34eq4e7i8t2iAE1ND\n\tkkS9J3ax7giBhJBsFHI2UaxtHa9fbLsZHr5kxzrMaXBPcBAv8Jvhpu+Ce3YsAPLsjTzuIY\n\t1WuSuHSlC9vqPg1SV0+2zon7bC6tBzk="],"Authentication-Results":["lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=redhat.com\n\theader.i=@redhat.com header.b=\"fTEQfCg9\"; \n\tdkim-atps=neutral","relay.mimecast.com;\n\tauth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ecurtin@redhat.com"],"X-MC-Unique":"LF2U08MqPkCU8lVWgUwDHw-1","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:mime-version:references:in-reply-to:from:date\n\t:message-id:subject:to:cc;\n\tbh=3e/I7VZ7+vB434JgFH5+lGoNrT/x9FnInQlAT9zp/OU=;\n\tb=1l0WvrT7iH41TEyOofzcjNxrGmX2gQU0mLCWzgWfbOm+s1j+zUit2x0ll/Fv/TKqVV\n\tnQ7SHq+f/7/PY/OB+3fn5KSmQvaLpb9joqxZ/BqJUUQWn8VNR6OJ/hGpalxuPutfhO0y\n\tjZETuNi6hwtHoYRCLXIWDBx1nQ9splKyQwSHH0X2F35ClglSH3g7GNzmTpNUOBBbuRPh\n\tpv5bkvmH7dLc4F3wiRZD0ofxgdsSdpvt0cjkkEE6tJ89xqhaaMxHBrgmYWMidIBUiGUt\n\tcfy2TV3PG41vNOwbR9JBerranA36rc5aGdimp7nQaIPgDsk5YnEhvIym01k1oHz2yiOt\n\tmcbw==","X-Gm-Message-State":"AOAM533lRR3YvkqGeObhevOS52E6dcisAWOKJyu/OYIT2pqpaF9tPjv0\n\tkp3+iItfOLv/Yz3GmnhLsSBRg1W0poNU2fypscWvEwat9T5FdEWcms7JC05M7SaqhkmYUeWXZit\n\tiXUqgqUPVjzwjZ0at9W+2HlMuo9oFelVddkwsB2d0iCc6kzauNA==","X-Received":["by 2002:a05:6870:c692:b0:e9:5368:10df with SMTP id\n\tcv18-20020a056870c69200b000e9536810dfmr2357682oab.182.1652268796635; \n\tWed, 11 May 2022 04:33:16 -0700 (PDT)","by 2002:a05:6870:c692:b0:e9:5368:10df with SMTP id\n\tcv18-20020a056870c69200b000e9536810dfmr2357666oab.182.1652268796284;\n\tWed, 11 May 2022 04:33:16 -0700 (PDT)"],"X-Google-Smtp-Source":"ABdhPJz02+hToHT/Do4dZnLk+R6c6BHteVgeqxZsaglWEdZNm51tGLe7de6ErjrjTQFkHbCfG99Jt+h8/sJ1jjRCVpI=","MIME-Version":"1.0","References":"<20220505151851.2421-1-ecurtin@redhat.com>","In-Reply-To":"<20220505151851.2421-1-ecurtin@redhat.com>","Date":"Wed, 11 May 2022 12:33:00 +0100","Message-ID":"<CAOgh=Fxbk3XER88yZ0Cs0a37O8ZbYxN8Wqnys1cuQgzNH=xFxA@mail.gmail.com>","To":"libcamera devel <libcamera-devel@lists.libcamera.org>","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain; charset=\"UTF-8\"","Subject":"Re: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer\n\tevents to event loop","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>","From":"Eric Curtin via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Eric Curtin <ecurtin@redhat.com>","Cc":"Javier Martinez Canillas <fmartine@redhat.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":23012,"web_url":"https://patchwork.libcamera.org/comment/23012/","msgid":"<CAOgh=Fx5E_Z=Pc08a5PTnZK6rPD0A1Ggh0d1FHpVppOENFapKQ@mail.gmail.com>","date":"2022-05-17T17:05:33","subject":"Re: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer\n\tevents to event loop","submitter":{"id":101,"url":"https://patchwork.libcamera.org/api/people/101/","name":"Eric Curtin","email":"ecurtin@redhat.com"},"content":"Another friendly reminder. In case you guys get the opportunity.\n\nOn Wed 11 May 2022, 12:33 Eric Curtin, <ecurtin@redhat.com> wrote:\n\n> On Thu, 5 May 2022 at 16:19, Eric Curtin <ecurtin@redhat.com> wrote:\n> >\n> > Extend the EventLoop class to support periodic timer events. This can be\n> > used to run tasks periodically, such as handling the event loop of SDL.\n> >\n> > Also delete all events in the list, before we event_base_loopbreak.\n> >\n> > Signed-off-by: Eric Curtin <ecurtin@redhat.com>\n> > ---\n> >  src/cam/event_loop.cpp | 33 +++++++++++++++++++++++++++++++++\n> >  src/cam/event_loop.h   |  6 ++++++\n> >  2 files changed, 39 insertions(+)\n> >\n> > diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp\n> > index e25784c0..181c971c 100644\n> > --- a/src/cam/event_loop.cpp\n> > +++ b/src/cam/event_loop.cpp\n> > @@ -47,6 +47,10 @@ int EventLoop::exec()\n> >  void EventLoop::exit(int code)\n> >  {\n> >         exitCode_ = code;\n> > +       for (auto const &e : events_) {\n> > +               event_del(e->event_);\n> > +       }\n> > +\n> >         event_base_loopbreak(base_);\n> >  }\n> >\n> > @@ -84,6 +88,35 @@ void EventLoop::addEvent(int fd, EventType type,\n> >         events_.push_back(std::move(event));\n> >  }\n> >\n> > +void EventLoop::toTimeval(const std::chrono::milliseconds d, struct\n> timeval &tv)\n> > +{\n> > +       tv.tv_sec =\n> std::chrono::duration_cast<std::chrono::seconds>(d).count();\n> > +       tv.tv_usec =\n> std::chrono::duration_cast<std::chrono::microseconds>(d).count();\n> > +}\n> > +\n> > +void EventLoop::addTimerEvent(const std::chrono::milliseconds d,\n> > +                             const std::function<void()> &callback)\n> > +{\n> > +       std::unique_ptr<Event> event = std::make_unique<Event>(callback);\n> > +       event->event_ = event_new(base_, -1, EV_PERSIST,\n> &EventLoop::Event::dispatch,\n> > +                                 event.get());\n> > +       if (!event->event_) {\n> > +               std::cerr << \"Failed to create timer event\" << std::endl;\n> > +               return;\n> > +       }\n> > +\n> > +       struct timeval tv;\n> > +       toTimeval(d, tv);\n> > +\n> > +       int ret = event_add(event->event_, &tv);\n> > +       if (ret < 0) {\n> > +               std::cerr << \"Failed to add timer event\" << std::endl;\n> > +               return;\n> > +       }\n> > +\n> > +       events_.push_back(std::move(event));\n> > +}\n> > +\n> >  void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd,\n> >                                  [[maybe_unused]] short flags, void\n> *param)\n> >  {\n> > diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h\n> > index a4613eb2..89215dce 100644\n> > --- a/src/cam/event_loop.h\n> > +++ b/src/cam/event_loop.h\n> > @@ -14,6 +14,8 @@\n> >\n> >  #include <event2/util.h>\n> >\n> > +using namespace std::chrono_literals;\n> > +\n> >  struct event_base;\n> >\n> >  class EventLoop\n> > @@ -37,6 +39,9 @@ public:\n> >         void addEvent(int fd, EventType type,\n> >                       const std::function<void()> &handler);\n> >\n> > +       void addTimerEvent(const std::chrono::milliseconds d,\n> > +                          const std::function<void()> &handler);\n> > +\n> >  private:\n> >         struct Event {\n> >                 Event(const std::function<void()> &callback);\n> > @@ -57,6 +62,7 @@ private:\n> >         std::list<std::unique_ptr<Event>> events_;\n> >         std::mutex lock_;\n> >\n> > +       void toTimeval(const std::chrono::milliseconds d, struct timeval\n> &tv);\n> >         static void dispatchCallback(evutil_socket_t fd, short flags,\n> >                                      void *param);\n> >         void dispatchCall();\n> > --\n> > 2.35.1\n> >\n>\n> Hi Guys,\n>\n> Friendly reminder if you could review once you get a spare moment.\n>\n> Regards,\n>\n> Eric\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id AC5B1C3256\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 17 May 2022 17:05:48 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0A49765659;\n\tTue, 17 May 2022 19:05:48 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 12BFD6041D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 17 May 2022 19:05:46 +0200 (CEST)","from mail-qv1-f70.google.com (mail-qv1-f70.google.com\n\t[209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id\n\tus-mta-35--BjfrnwVPJmeCkGxsDo27w-1; Tue, 17 May 2022 13:05:44 -0400","by mail-qv1-f70.google.com with SMTP id\n\tn5-20020a0cbe85000000b0045aff56564bso15340526qvi.4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 17 May 2022 10:05:44 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1652807148;\n\tbh=5/f/AaiQ3HYYq8nBg2cROPEA+jkJyiBRNLjDO2/A2O8=;\n\th=References:In-Reply-To:Date:To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=pf+RLef8R7gi0goqvXW68FmuUymlsCKR41y4R5R6vMUoPUYampqas3Nz2+mC9wHL+\n\tIxYb7XIYr52Rd5rKlbk2ejkfKHMgFwTfHAWfA3C+a4U5N7C+y8mE+fGaFjSMX68ltT\n\teuVCJTpGqXfrfjhBB98RdWS6NfvXywS7rF6PK3td7yJgLTrJKnCklpvn69FDprIkDj\n\tPBAHpnBimSqvM/HwYplCQCdnfh6wNTnd4+qXuysoy1jFUZ6qdTjVGgNz561N/Dz2dR\n\tzWgtuR+Ywu4BDn4SLNQpCU+9B20TNumNZ3A994aYYO7HOCHVzlE3FBv58k3sNWvdf7\n\tSRZWkROrwqCZA==","v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1652807145;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=YhjRn4U+D8sLi2KssSBBH5ehREHqhrfq7cbsIMWjvmc=;\n\tb=XTREEl/w+d6NBk5r7iMb/35btQNIvPHzhRhRffLR3xSOgX6uA16JiIbzE3es4aAJ7dyOxF\n\tC80SDnjvg1Er1eJN9BWNVcEeTPYxBFjasN6IpTt9naY4p4kMghEnmcK18VTPblhfihLq5T\n\tt/ylJE8vzwhespWPORsSkQG6qDxVP3E="],"Authentication-Results":["lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=redhat.com\n\theader.i=@redhat.com header.b=\"XTREEl/w\"; \n\tdkim-atps=neutral","relay.mimecast.com;\n\tauth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ecurtin@redhat.com"],"X-MC-Unique":"-BjfrnwVPJmeCkGxsDo27w-1","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:mime-version:references:in-reply-to:from:date\n\t:message-id:subject:to:cc;\n\tbh=YhjRn4U+D8sLi2KssSBBH5ehREHqhrfq7cbsIMWjvmc=;\n\tb=kkiaIoaoC7xSNnhHt/4ZmiSu4as+WlcWIBKv6yM/lnomYFIT8D+9/KZIk8KYSg6pqh\n\tQl2Eem8UAQ10YLQVKNnXGUy5toR/pznGgDg17edBMkac1dczfz8eSxiswmChau0Mj5/V\n\tonaK3MqDWPjAOdBY1wE+ffzhsQuUlwE4//eOZ/vSA8n0DSuTsYSU+8lbhmrCqo8r2lwP\n\tnnzohnQJbC0LaBJmFWp5gUHqum3RK13oddWpzdU0BZru/iphrI4DJ2s4mxZ27uIpA2Zq\n\tuUi6RmApUqiSCgYEzD7UeURbrQzvogW6NuYEKZt1XN8+BNGKOy+eVyK7yAYUtpw236MV\n\tLONQ==","X-Gm-Message-State":"AOAM533fV9fH6TFNKquRRUXzQoEjevtJZ+iz2XfVAXQsgk1UqkLDBMOr\n\t+lVLDEhJjgg/zwN7r3KDNM/ywpg2TwuXFz8o2FrNdOBmXgINZatL8moUed1bqHhOL6ciyDFtIrm\n\til7pkqHabzBn2Ze0OQ/ZZa7V3RwMMum/3rnfzF6WfY+8r8C3uaA==","X-Received":["by 2002:a05:620a:2847:b0:67d:2bad:422f with SMTP id\n\th7-20020a05620a284700b0067d2bad422fmr16787601qkp.559.1652807143966; \n\tTue, 17 May 2022 10:05:43 -0700 (PDT)","by 2002:a05:620a:2847:b0:67d:2bad:422f with SMTP id\n\th7-20020a05620a284700b0067d2bad422fmr16787575qkp.559.1652807143697;\n\tTue, 17 May 2022 10:05:43 -0700 (PDT)"],"X-Google-Smtp-Source":"ABdhPJxGkm0CchLA4zSvPRL+ppcEyE2Ypm82rF3cdepL+A2NPi1mpP8dOX7Gt4qAPyeIAdtqg+7L9bt/zeGT6URfLjE=","MIME-Version":"1.0","References":"<20220505151851.2421-1-ecurtin@redhat.com>\n\t<CAOgh=Fxbk3XER88yZ0Cs0a37O8ZbYxN8Wqnys1cuQgzNH=xFxA@mail.gmail.com>","In-Reply-To":"<CAOgh=Fxbk3XER88yZ0Cs0a37O8ZbYxN8Wqnys1cuQgzNH=xFxA@mail.gmail.com>","Date":"Tue, 17 May 2022 18:05:33 +0100","Message-ID":"<CAOgh=Fx5E_Z=Pc08a5PTnZK6rPD0A1Ggh0d1FHpVppOENFapKQ@mail.gmail.com>","To":"libcamera devel <libcamera-devel@lists.libcamera.org>","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Type":"multipart/alternative; boundary=\"000000000000aeca4605df382612\"","Subject":"Re: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer\n\tevents to event loop","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>","From":"Eric Curtin via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Eric Curtin <ecurtin@redhat.com>","Cc":"Javier Martinez Canillas <fmartine@redhat.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":23019,"web_url":"https://patchwork.libcamera.org/comment/23019/","msgid":"<165281517493.2416244.15714492865345492952@Monstersaurus>","date":"2022-05-17T19:19:34","subject":"Re: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer\n\tevents to event loop","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Eric Curtin via libcamera-devel (2022-05-05 16:18:48)\n> Extend the EventLoop class to support periodic timer events. This can be\n> used to run tasks periodically, such as handling the event loop of SDL.\n> \n> Also delete all events in the list, before we event_base_loopbreak.\n> \n> Signed-off-by: Eric Curtin <ecurtin@redhat.com>\n> ---\n>  src/cam/event_loop.cpp | 33 +++++++++++++++++++++++++++++++++\n>  src/cam/event_loop.h   |  6 ++++++\n>  2 files changed, 39 insertions(+)\n> \n> diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp\n> index e25784c0..181c971c 100644\n> --- a/src/cam/event_loop.cpp\n> +++ b/src/cam/event_loop.cpp\n> @@ -47,6 +47,10 @@ int EventLoop::exec()\n>  void EventLoop::exit(int code)\n>  {\n>         exitCode_ = code;\n> +       for (auto const &e : events_) {\n> +               event_del(e->event_);\n> +       }\n\nNo need for { } on a single line statement.\n\n> +\n>         event_base_loopbreak(base_);\n>  }\n>  \n> @@ -84,6 +88,35 @@ void EventLoop::addEvent(int fd, EventType type,\n>         events_.push_back(std::move(event));\n>  }\n>  \n> +void EventLoop::toTimeval(const std::chrono::milliseconds d, struct timeval &tv)\n> +{\n> +       tv.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(d).count();\n\nDo you need to subtract the seconds from here now?\n\n> +       tv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d).count();\n\nOr does this wrap accordingly?\n\nI.e. would 1.5 seconds end up as :\n\t  tv.tv_sec = 1;\n\t  tv.tv_usec = 1500000;\n\nWith that resolved, I think that's a \n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> +}\n> +\n> +void EventLoop::addTimerEvent(const std::chrono::milliseconds d,\n> +                             const std::function<void()> &callback)\n> +{\n> +       std::unique_ptr<Event> event = std::make_unique<Event>(callback);\n> +       event->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch,\n> +                                 event.get());\n> +       if (!event->event_) {\n> +               std::cerr << \"Failed to create timer event\" << std::endl;\n> +               return;\n> +       }\n> +\n> +       struct timeval tv;\n> +       toTimeval(d, tv);\n\nIt's a shame that it's frowned upon to override std namespace to have\ncustom constructors or having a construction to allow:\n\tstruct timeval tv(d);\n\nwould be nice ... (but I don't think it's something we should do here).\n\n--\nKieran\n\n\n> +\n> +       int ret = event_add(event->event_, &tv);\n> +       if (ret < 0) {\n> +               std::cerr << \"Failed to add timer event\" << std::endl;\n> +               return;\n> +       }\n> +\n> +       events_.push_back(std::move(event));\n> +}\n> +\n>  void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd,\n>                                  [[maybe_unused]] short flags, void *param)\n>  {\n> diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h\n> index a4613eb2..89215dce 100644\n> --- a/src/cam/event_loop.h\n> +++ b/src/cam/event_loop.h\n> @@ -14,6 +14,8 @@\n>  \n>  #include <event2/util.h>\n>  \n> +using namespace std::chrono_literals;\n> +\n>  struct event_base;\n>  \n>  class EventLoop\n> @@ -37,6 +39,9 @@ public:\n>         void addEvent(int fd, EventType type,\n>                       const std::function<void()> &handler);\n>  \n> +       void addTimerEvent(const std::chrono::milliseconds d,\n> +                          const std::function<void()> &handler);\n> +\n>  private:\n>         struct Event {\n>                 Event(const std::function<void()> &callback);\n> @@ -57,6 +62,7 @@ private:\n>         std::list<std::unique_ptr<Event>> events_;\n>         std::mutex lock_;\n>  \n> +       void toTimeval(const std::chrono::milliseconds d, struct timeval &tv);\n>         static void dispatchCallback(evutil_socket_t fd, short flags,\n>                                      void *param);\n>         void dispatchCall();\n> -- \n> 2.35.1\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 4A785C0F2A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 17 May 2022 19:19:39 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id F14B36565C;\n\tTue, 17 May 2022 21:19:38 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 95E2F6041E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 17 May 2022 21:19:37 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 508C56DC;\n\tTue, 17 May 2022 21:19:37 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1652815179;\n\tbh=oePPjKV6odxYCt/F+KK5cm9tTLwDjfdK5QhhBJJ9Pb4=;\n\th=In-Reply-To:References:To:Date:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=V0b3Mhc2f9yROAJOdPDZLNh86kzxd/cOzkqU56U6KxzsxKQVDLyUjIa/BrmN9/FtE\n\t7rGY6pKfe7nZIrh26WM5ZvYSF9NsOgKQn8XWCiTMRpRXVLCaPTofE5HxmaXigLphWe\n\theBuVJbxXZlbvyN7F9SG+rjbZfiyjFfiKgkmk9HAVA0ozEzJTzdwDElz3+flLFSUgY\n\tpI/m53tWTcMsrYA6l1mFpQBP+LODg7Dy27OIBM6U481nySDNvrpyfLudJ7+DKAxAo6\n\t5RqSfKhsvq+MvKh/zr7J/KprNIATfxNOIyRO30V/o1n2DkmsAugxaCUHNroaT+f0nj\n\t85CjgjtIOGh6g==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1652815177;\n\tbh=oePPjKV6odxYCt/F+KK5cm9tTLwDjfdK5QhhBJJ9Pb4=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=stiuD9SmQX0f/RUKgMYpNQnWvCKepvpo2JHmmZQe5ou5nnZMSt7dPyjitBbHXlxit\n\tJelK1WoHqRTkukX4zn9VJ+wZ15m4d5KeTyMIj03Qe7zcuBQsbBL9IX1KnCfi191eMX\n\tWZsKWuXddCdAqoSjpP0bHp3I7J4j8k7GWkjsV5Kw="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"stiuD9Sm\"; dkim-atps=neutral","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20220505151851.2421-1-ecurtin@redhat.com>","References":"<20220505151851.2421-1-ecurtin@redhat.com>","To":"Eric Curtin <ecurtin@redhat.com>, libcamera-devel@lists.libcamera.org","Date":"Tue, 17 May 2022 20:19:34 +0100","Message-ID":"<165281517493.2416244.15714492865345492952@Monstersaurus>","User-Agent":"alot/0.10","Subject":"Re: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer\n\tevents to event loop","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>","From":"Kieran Bingham via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":23068,"web_url":"https://patchwork.libcamera.org/comment/23068/","msgid":"<20220518162757.zkrfdnpxjfzjhqkj@uno.localdomain>","date":"2022-05-18T16:27:57","subject":"Re: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer\n\tevents to event loop","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Eric,\n\nOn Thu, May 05, 2022 at 04:18:48PM +0100, Eric Curtin via libcamera-devel wrote:\n> Extend the EventLoop class to support periodic timer events. This can be\n> used to run tasks periodically, such as handling the event loop of SDL.\n>\n> Also delete all events in the list, before we event_base_loopbreak.\n>\n> Signed-off-by: Eric Curtin <ecurtin@redhat.com>\n\nKieran pointed me to your series, and I've just tested it and I can\nsay it works fine for me\n\nTested-by: Jacopo Mondi <jacopo@jmondi.org>\n\nHope it can be merged soon even if I haven't really looked at the patches\nthough, but since you're at v8 I assume they're in pretty good shape!\n\nThanks\n   j\n\n> ---\n>  src/cam/event_loop.cpp | 33 +++++++++++++++++++++++++++++++++\n>  src/cam/event_loop.h   |  6 ++++++\n>  2 files changed, 39 insertions(+)\n>\n> diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp\n> index e25784c0..181c971c 100644\n> --- a/src/cam/event_loop.cpp\n> +++ b/src/cam/event_loop.cpp\n> @@ -47,6 +47,10 @@ int EventLoop::exec()\n>  void EventLoop::exit(int code)\n>  {\n>  \texitCode_ = code;\n> +\tfor (auto const &e : events_) {\n> +\t\tevent_del(e->event_);\n> +\t}\n> +\n>  \tevent_base_loopbreak(base_);\n>  }\n>\n> @@ -84,6 +88,35 @@ void EventLoop::addEvent(int fd, EventType type,\n>  \tevents_.push_back(std::move(event));\n>  }\n>\n> +void EventLoop::toTimeval(const std::chrono::milliseconds d, struct timeval &tv)\n> +{\n> +\ttv.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(d).count();\n> +\ttv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d).count();\n> +}\n> +\n> +void EventLoop::addTimerEvent(const std::chrono::milliseconds d,\n> +\t\t\t      const std::function<void()> &callback)\n> +{\n> +\tstd::unique_ptr<Event> event = std::make_unique<Event>(callback);\n> +\tevent->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch,\n> +\t\t\t\t  event.get());\n> +\tif (!event->event_) {\n> +\t\tstd::cerr << \"Failed to create timer event\" << std::endl;\n> +\t\treturn;\n> +\t}\n> +\n> +\tstruct timeval tv;\n> +\ttoTimeval(d, tv);\n> +\n> +\tint ret = event_add(event->event_, &tv);\n> +\tif (ret < 0) {\n> +\t\tstd::cerr << \"Failed to add timer event\" << std::endl;\n> +\t\treturn;\n> +\t}\n> +\n> +\tevents_.push_back(std::move(event));\n> +}\n> +\n>  void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd,\n>  \t\t\t\t [[maybe_unused]] short flags, void *param)\n>  {\n> diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h\n> index a4613eb2..89215dce 100644\n> --- a/src/cam/event_loop.h\n> +++ b/src/cam/event_loop.h\n> @@ -14,6 +14,8 @@\n>\n>  #include <event2/util.h>\n>\n> +using namespace std::chrono_literals;\n> +\n>  struct event_base;\n>\n>  class EventLoop\n> @@ -37,6 +39,9 @@ public:\n>  \tvoid addEvent(int fd, EventType type,\n>  \t\t      const std::function<void()> &handler);\n>\n> +\tvoid addTimerEvent(const std::chrono::milliseconds d,\n> +\t\t\t   const std::function<void()> &handler);\n> +\n>  private:\n>  \tstruct Event {\n>  \t\tEvent(const std::function<void()> &callback);\n> @@ -57,6 +62,7 @@ private:\n>  \tstd::list<std::unique_ptr<Event>> events_;\n>  \tstd::mutex lock_;\n>\n> +\tvoid toTimeval(const std::chrono::milliseconds d, struct timeval &tv);\n>  \tstatic void dispatchCallback(evutil_socket_t fd, short flags,\n>  \t\t\t\t     void *param);\n>  \tvoid dispatchCall();\n> --\n> 2.35.1\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 46D9BC0F2A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 18 May 2022 16:28:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6C32565659;\n\tWed, 18 May 2022 18:28:02 +0200 (CEST)","from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net\n\t[IPv6:2001:4b98:dc4:8::222])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B467665656\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 18 May 2022 18:28:00 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby mail.gandi.net (Postfix) with ESMTPSA id AE81840006;\n\tWed, 18 May 2022 16:27:59 +0000 (UTC)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1652891282;\n\tbh=YLvyqcJ0IU2PVN27KHG68CJ0lokhcaV90eZsTUtq4oI=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=dSw1Zpm7ty10vbqUbz5Cp9o7OSVRujsIPD7dDIWGB/l2iWy2j8hRgD4MHeiPj+SuW\n\tnH/IO1+a0qqklYA+lEiprvLZ9mLKlM3/SBkPRLTvWThElCkBCLax5ybSP+50m2mENb\n\t3xwopokiwkXmKZF4tT4v/Rzg1IXF75l5aV5F2d68beqQ7hwRxnO4ldyTQV781rTE4e\n\tk/lbto4Ae8VXGRp3IZmD1uDQSz44x4GJfnzJCT80HUbbGIzKTB3YaglLRUolSMXLOy\n\tc70QHmnmsOTEUWC4VcTFSNZrHR5d6NBhD6wqWK7zu0Z+uyWeE17N4IFMU4SBnOrWxX\n\tMtdn7h3876oGg==","Date":"Wed, 18 May 2022 18:27:57 +0200","To":"Eric Curtin <ecurtin@redhat.com>","Message-ID":"<20220518162757.zkrfdnpxjfzjhqkj@uno.localdomain>","References":"<20220505151851.2421-1-ecurtin@redhat.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220505151851.2421-1-ecurtin@redhat.com>","Subject":"Re: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer\n\tevents to event loop","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>","From":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":23071,"web_url":"https://patchwork.libcamera.org/comment/23071/","msgid":"<YoVHtTlRW/cXOw/B@pendragon.ideasonboard.com>","date":"2022-05-18T19:23:33","subject":"Re: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer\n\tevents to event loop","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Eric,\n\nOn Tue, May 17, 2022 at 08:19:34PM +0100, Kieran Bingham via libcamera-devel wrote:\n> Quoting Eric Curtin via libcamera-devel (2022-05-05 16:18:48)\n> > Extend the EventLoop class to support periodic timer events. This can be\n> > used to run tasks periodically, such as handling the event loop of SDL.\n> > \n> > Also delete all events in the list, before we event_base_loopbreak.\n\nCan you extend the commit message to explain why this is needed ?\n\n> > Signed-off-by: Eric Curtin <ecurtin@redhat.com>\n> > ---\n> >  src/cam/event_loop.cpp | 33 +++++++++++++++++++++++++++++++++\n> >  src/cam/event_loop.h   |  6 ++++++\n> >  2 files changed, 39 insertions(+)\n> > \n> > diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp\n> > index e25784c0..181c971c 100644\n> > --- a/src/cam/event_loop.cpp\n> > +++ b/src/cam/event_loop.cpp\n> > @@ -47,6 +47,10 @@ int EventLoop::exec()\n> >  void EventLoop::exit(int code)\n> >  {\n> >         exitCode_ = code;\n> > +       for (auto const &e : events_) {\n> > +               event_del(e->event_);\n> > +       }\n> \n> No need for { } on a single line statement.\n\nAnd as events_ is a vector of unique_ptr, just\n\n\tevents_.clear();\n\nwill do.\n\n> > +\n> >         event_base_loopbreak(base_);\n> >  }\n> >  \n> > @@ -84,6 +88,35 @@ void EventLoop::addEvent(int fd, EventType type,\n> >         events_.push_back(std::move(event));\n> >  }\n> >  \n> > +void EventLoop::toTimeval(const std::chrono::milliseconds d, struct timeval &tv)\n> > +{\n> > +       tv.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(d).count();\n> \n> Do you need to subtract the seconds from here now?\n> \n> > +       tv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d).count();\n> \n> Or does this wrap accordingly?\n> \n> I.e. would 1.5 seconds end up as :\n> \t  tv.tv_sec = 1;\n> \t  tv.tv_usec = 1500000;\n\nLooks like an issue indeed. There's an implementation in\nsrc/libcamera/base/utils.h that you can copy:\n\n\tuint64_t usecs = std::chrono::duration_cast<std::chrono::microseconds(d).count();\n        tv.tv_sec = usecs / 1000000ULL;\n        tv.tv_usec = usecs % 1000000ULL;\n\nAs this is only three lines of code, and used once in addTimerEvent(),\nI'd just move it to that function and drom toTimeval().\n\n> With that resolved, I think that's a \n> \n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> > +}\n> > +\n> > +void EventLoop::addTimerEvent(const std::chrono::milliseconds d,\n\nAs this adds a periodic timer event, let's call the variable period (or\ninterval, up to you) instead of d.\n\n> > +                             const std::function<void()> &callback)\n> > +{\n> > +       std::unique_ptr<Event> event = std::make_unique<Event>(callback);\n> > +       event->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch,\n> > +                                 event.get());\n> > +       if (!event->event_) {\n> > +               std::cerr << \"Failed to create timer event\" << std::endl;\n> > +               return;\n> > +       }\n> > +\n> > +       struct timeval tv;\n> > +       toTimeval(d, tv);\n> \n> It's a shame that it's frowned upon to override std namespace to have\n> custom constructors or having a construction to allow:\n> \tstruct timeval tv(d);\n> \n> would be nice ... (but I don't think it's something we should do here).\n\nThat's something that the C++ standard library could have provided. We\nhave a helper function in base/utils.h but that's not public.\n\n> > +\n> > +       int ret = event_add(event->event_, &tv);\n> > +       if (ret < 0) {\n> > +               std::cerr << \"Failed to add timer event\" << std::endl;\n> > +               return;\n> > +       }\n> > +\n> > +       events_.push_back(std::move(event));\n> > +}\n> > +\n> >  void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd,\n> >                                  [[maybe_unused]] short flags, void *param)\n> >  {\n> > diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h\n> > index a4613eb2..89215dce 100644\n> > --- a/src/cam/event_loop.h\n> > +++ b/src/cam/event_loop.h\n> > @@ -14,6 +14,8 @@\n\nYou should include <chrono> here.\n\n> >  \n> >  #include <event2/util.h>\n> >  \n> > +using namespace std::chrono_literals;\n\nI think this can be dropped, as you don't use literals in this file.\n\nWith these small changes you'll have my Rb tag.\n\n> > +\n> >  struct event_base;\n> >  \n> >  class EventLoop\n> > @@ -37,6 +39,9 @@ public:\n> >         void addEvent(int fd, EventType type,\n> >                       const std::function<void()> &handler);\n> >  \n> > +       void addTimerEvent(const std::chrono::milliseconds d,\n> > +                          const std::function<void()> &handler);\n> > +\n> >  private:\n> >         struct Event {\n> >                 Event(const std::function<void()> &callback);\n> > @@ -57,6 +62,7 @@ private:\n> >         std::list<std::unique_ptr<Event>> events_;\n> >         std::mutex lock_;\n> >  \n> > +       void toTimeval(const std::chrono::milliseconds d, struct timeval &tv);\n> >         static void dispatchCallback(evutil_socket_t fd, short flags,\n> >                                      void *param);\n> >         void dispatchCall();","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id E8C76C3256\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 18 May 2022 19:23:42 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E56A365659;\n\tWed, 18 May 2022 21:23:41 +0200 (CEST)","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 F267865656\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 18 May 2022 21:23:40 +0200 (CEST)","from pendragon.ideasonboard.com (unknown [45.131.31.124])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5A34D1BBB;\n\tWed, 18 May 2022 21:23:40 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1652901822;\n\tbh=UhyG/0yV2pAi2lKugX5bzu+gtZ6mYBa8XsnQXzlrv1Y=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=rpe2/XWKVT2vIF4D8WtApC84KCRKT6Yr8hjB4ftH2/xhb9DKPnLjPkX3OZHS6/N0/\n\t73dq8Fp+iJf/W+BAKb3YZ/VKpKyBLKTVvsqxaXn8xrQlCaw0Dh8a0jrGoEObuQi+0V\n\tlPxdkgvYRo3CYz2cTplQmOgu1NB8SSC0HxrDNOxb0A6zFj9LA1P0ieV3lZPuc0d/Nc\n\tw5X6K0Gg6ctAWnIGPpv/MhhAjNmfT2eNi+iCCxY7O8Y7YsRUcIfRdIq/VNsT1jBxWs\n\tkKsDN30g78jtDgDRQyyuGEDiccKs/8gh6qK3k1tRGUJ+0kkjE5/bmjAc7mBn8aXv7S\n\tKiMICgZeDZp3Q==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1652901820;\n\tbh=UhyG/0yV2pAi2lKugX5bzu+gtZ6mYBa8XsnQXzlrv1Y=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Y7UlqB79UaTCD29wNlrrGBpiTqFlbyTBZnBxNl6v26kmVHj1BEKFCXKoVRr3cu5w5\n\t74MXMeJylxJ/CL1nw9tFc+ao/0rDXHtLK5P2mXKNJUs0xiSev3/pfskrbAyn/usb+p\n\tAEqIi14NeJX+gDXOBttNkTCUMML7bPxTP9DSqcwQ="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Y7UlqB79\"; dkim-atps=neutral","Date":"Wed, 18 May 2022 22:23:33 +0300","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<YoVHtTlRW/cXOw/B@pendragon.ideasonboard.com>","References":"<20220505151851.2421-1-ecurtin@redhat.com>\n\t<165281517493.2416244.15714492865345492952@Monstersaurus>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<165281517493.2416244.15714492865345492952@Monstersaurus>","Subject":"Re: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer\n\tevents to event loop","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]