{"id":15799,"url":"https://patchwork.libcamera.org/api/1.1/patches/15799/?format=json","web_url":"https://patchwork.libcamera.org/patch/15799/","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":"<20220505151851.2421-1-ecurtin@redhat.com>","date":"2022-05-05T15:18:48","name":"[libcamera-devel,v8,1/4] cam: event_loop: Add timer events to event loop","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"6f0a4c537291ac4b32fa34689ce5076fb79b26a6","submitter":{"id":101,"url":"https://patchwork.libcamera.org/api/1.1/people/101/?format=json","name":"Eric Curtin","email":"ecurtin@redhat.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/15799/mbox/","series":[{"id":3095,"url":"https://patchwork.libcamera.org/api/1.1/series/3095/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3095","date":"2022-05-05T15:18:48","name":"[libcamera-devel,v8,1/4] cam: event_loop: Add timer events to event loop","version":8,"mbox":"https://patchwork.libcamera.org/series/3095/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/15799/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/15799/checks/","tags":{},"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 824E2C3256\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  5 May 2022 15:19:15 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B235765643;\n\tThu,  5 May 2022 17:19:14 +0200 (CEST)","from us-smtp-delivery-74.mimecast.com\n\t(us-smtp-delivery-74.mimecast.com [170.10.133.74])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 521F6603AB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  5 May 2022 17:19:12 +0200 (CEST)","from mail-wm1-f72.google.com (mail-wm1-f72.google.com\n\t[209.85.128.72]) 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-410-H11vJBASOymvY85ALjFCMg-1; Thu, 05 May 2022 11:19:09 -0400","by mail-wm1-f72.google.com with SMTP id\n\ts14-20020a05600c29ce00b00394611a8059so1899141wmd.4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 05 May 2022 08:19:09 -0700 (PDT)","from localhost.localdomain\n\t([2001:bb6:9634:7100:33b0:c1b1:8875:4adb])\n\tby smtp.gmail.com with ESMTPSA id\n\tz6-20020a056000110600b0020ad05214d5sm1434819wrw.2.2022.05.05.08.19.07\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 05 May 2022 08:19:07 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1651763954;\n\tbh=xcfZ1H7WQhV0yufR4gVBIl3oFguirvVmlkN8iP2vPfw=;\n\th=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post:\n\tList-Help:List-Subscribe:From:Reply-To:From;\n\tb=D/II3GpxwzEqGPpD6G5X14K0FZwOz78NocZmaxENsvYtk2bua6HT3gyFPzvmkqXC9\n\ts2SX2985Yr0xXZRcTyhBOYAjEaBkpxUo6xo7yUoU2UY4bntYF1n9sGJYEOcLJqZLQZ\n\tQEaIaM10fZrqhpBEGPbJv2lDSStaSqFbtnldg2aRxQOZ+bYisGfP8o+L7NxQ/vsBCJ\n\tcAKJTtLw841i5zZSOrdvxB5pJEfwZfs01J1ywlXloIW4vCs256jUrZ0icsa0T290ei\n\tBBBSUcUmtRaXrGOICv7/bUQZR4YSUmN3DdT4OVTFtgplozPBYunmEGWbc1HbJ4KoSE\n\txMDilcubHrwbQ==","v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1651763950;\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\tcontent-transfer-encoding:content-transfer-encoding;\n\tbh=leP7SagDNmsqLLhIqUJjkwfGzMU02ITMXoicGnFkI44=;\n\tb=JUrmO7XIZCqxP6/HxuuLj3hTA6+8VrYa3LIyuBhzbaPUJ01Y4m5YqJvwu389841v9dz8Zu\n\ttn/qQLIaPeaUu2NLsUPKYyvkDXO1NXKrC6Un+6kj1XzJVywWM9WfKavYUpsR+fc3Oa92AI\n\tzAhFWYvzQKJrRsHUVwKigu8wuTP4Ymc="],"Authentication-Results":["lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=redhat.com\n\theader.i=@redhat.com header.b=\"JUrmO7XI\"; \n\tdkim-atps=neutral","relay.mimecast.com;\n\tauth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ecurtin@redhat.com"],"X-MC-Unique":"H11vJBASOymvY85ALjFCMg-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:from:to:cc:subject:date:message-id:mime-version\n\t:content-transfer-encoding;\n\tbh=leP7SagDNmsqLLhIqUJjkwfGzMU02ITMXoicGnFkI44=;\n\tb=Jypav9XH/JhgqOWzy/m1p8CrQdN5r8bVp9cURdR8ddILRoBx/VevQ0mlV9Sbq1R+OX\n\t6/mjbNvHPIsJOkheswfYfSkBYQfbesazHBhPm8ceMaVMygiVzEE2JWSFPTUGCil/PkWE\n\twRJVqq5c0IE4ntVyYc6zBzwbTuFv+NzqtrFRkHV6+965a2AUBZ/LJtQZ6kFwJOJQd7kb\n\tURqk6qzDjJ1W5ocAcZuFqmBqXfVmKLiYtGfFg8+30LbuKNfDMGq05HOaH0Fg7eVm8j3Z\n\tItspDfWDKvggngD6gwX5TwW1D/0452GLqeHaNnIvMuBBfBhXKybtH+hZ+cjyhHetp5m3\n\tG2VQ==","X-Gm-Message-State":"AOAM531CVwQZ7zeYP1HHEAunBauR1aSCShLQHzBcm4grbjUfnOiZ0DNg\n\t/OxPefQB6+4EX9s7tWQOZi4R/9gUfcN1I2RaupwFMsB03VagGBSmBTddb8DoeyuV8bXq8IDmzIL\n\tKe2gRPXgzF6skbQa4i2ZSCzR8LG455xK8sWHqWZsFz74U7I0kBSeQdJwkNtp+zb4VPk0hDir8Z8\n\tUmvG/skYt9","X-Received":["by 2002:a05:600c:198f:b0:392:9120:c6ee with SMTP id\n\tt15-20020a05600c198f00b003929120c6eemr5614470wmq.18.1651763948172; \n\tThu, 05 May 2022 08:19:08 -0700 (PDT)","by 2002:a05:600c:198f:b0:392:9120:c6ee with SMTP id\n\tt15-20020a05600c198f00b003929120c6eemr5614437wmq.18.1651763947791; \n\tThu, 05 May 2022 08:19:07 -0700 (PDT)"],"X-Google-Smtp-Source":"ABdhPJxAORZ6i5qRgb2D8pivc34QzuYo6cCFJbub7qD/JSYA3JvM+lWQahN4HvoTOV1dXkGy4swAZg==","To":"libcamera-devel@lists.libcamera.org","Date":"Thu,  5 May 2022 16:18:48 +0100","Message-Id":"<20220505151851.2421-1-ecurtin@redhat.com>","X-Mailer":"git-send-email 2.35.1","MIME-Version":"1.0","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Transfer-Encoding":"8bit","Content-Type":"text/plain; charset=\"US-ASCII\"; x-default=true","Subject":"[libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer events\n\tto 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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Extend the EventLoop class to support periodic timer events. This can be\nused to run tasks periodically, such as handling the event loop of SDL.\n\nAlso delete all events in the list, before we event_base_loopbreak.\n\nSigned-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(+)","diff":"diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp\nindex 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 {\ndiff --git a/src/cam/event_loop.h b/src/cam/event_loop.h\nindex 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","prefixes":["libcamera-devel","v8","1/4"]}