From patchwork Thu May 5 15:18:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 15799 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 824E2C3256 for ; Thu, 5 May 2022 15:19:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B235765643; Thu, 5 May 2022 17:19:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1651763954; bh=xcfZ1H7WQhV0yufR4gVBIl3oFguirvVmlkN8iP2vPfw=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=D/II3GpxwzEqGPpD6G5X14K0FZwOz78NocZmaxENsvYtk2bua6HT3gyFPzvmkqXC9 s2SX2985Yr0xXZRcTyhBOYAjEaBkpxUo6xo7yUoU2UY4bntYF1n9sGJYEOcLJqZLQZ QEaIaM10fZrqhpBEGPbJv2lDSStaSqFbtnldg2aRxQOZ+bYisGfP8o+L7NxQ/vsBCJ cAKJTtLw841i5zZSOrdvxB5pJEfwZfs01J1ywlXloIW4vCs256jUrZ0icsa0T290ei BBBSUcUmtRaXrGOICv7/bUQZR4YSUmN3DdT4OVTFtgplozPBYunmEGWbc1HbJ4KoSE xMDilcubHrwbQ== Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 521F6603AB for ; Thu, 5 May 2022 17:19:12 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="JUrmO7XI"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651763950; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=leP7SagDNmsqLLhIqUJjkwfGzMU02ITMXoicGnFkI44=; b=JUrmO7XIZCqxP6/HxuuLj3hTA6+8VrYa3LIyuBhzbaPUJ01Y4m5YqJvwu389841v9dz8Zu tn/qQLIaPeaUu2NLsUPKYyvkDXO1NXKrC6Un+6kj1XzJVywWM9WfKavYUpsR+fc3Oa92AI zAhFWYvzQKJrRsHUVwKigu8wuTP4Ymc= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-410-H11vJBASOymvY85ALjFCMg-1; Thu, 05 May 2022 11:19:09 -0400 X-MC-Unique: H11vJBASOymvY85ALjFCMg-1 Received: by mail-wm1-f72.google.com with SMTP id s14-20020a05600c29ce00b00394611a8059so1899141wmd.4 for ; Thu, 05 May 2022 08:19:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=leP7SagDNmsqLLhIqUJjkwfGzMU02ITMXoicGnFkI44=; b=Jypav9XH/JhgqOWzy/m1p8CrQdN5r8bVp9cURdR8ddILRoBx/VevQ0mlV9Sbq1R+OX 6/mjbNvHPIsJOkheswfYfSkBYQfbesazHBhPm8ceMaVMygiVzEE2JWSFPTUGCil/PkWE wRJVqq5c0IE4ntVyYc6zBzwbTuFv+NzqtrFRkHV6+965a2AUBZ/LJtQZ6kFwJOJQd7kb URqk6qzDjJ1W5ocAcZuFqmBqXfVmKLiYtGfFg8+30LbuKNfDMGq05HOaH0Fg7eVm8j3Z ItspDfWDKvggngD6gwX5TwW1D/0452GLqeHaNnIvMuBBfBhXKybtH+hZ+cjyhHetp5m3 G2VQ== X-Gm-Message-State: AOAM531CVwQZ7zeYP1HHEAunBauR1aSCShLQHzBcm4grbjUfnOiZ0DNg /OxPefQB6+4EX9s7tWQOZi4R/9gUfcN1I2RaupwFMsB03VagGBSmBTddb8DoeyuV8bXq8IDmzIL Ke2gRPXgzF6skbQa4i2ZSCzR8LG455xK8sWHqWZsFz74U7I0kBSeQdJwkNtp+zb4VPk0hDir8Z8 UmvG/skYt9 X-Received: by 2002:a05:600c:198f:b0:392:9120:c6ee with SMTP id t15-20020a05600c198f00b003929120c6eemr5614470wmq.18.1651763948172; Thu, 05 May 2022 08:19:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAORZ6i5qRgb2D8pivc34QzuYo6cCFJbub7qD/JSYA3JvM+lWQahN4HvoTOV1dXkGy4swAZg== X-Received: by 2002:a05:600c:198f:b0:392:9120:c6ee with SMTP id t15-20020a05600c198f00b003929120c6eemr5614437wmq.18.1651763947791; Thu, 05 May 2022 08:19:07 -0700 (PDT) Received: from localhost.localdomain ([2001:bb6:9634:7100:33b0:c1b1:8875:4adb]) by smtp.gmail.com with ESMTPSA id z6-20020a056000110600b0020ad05214d5sm1434819wrw.2.2022.05.05.08.19.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 08:19:07 -0700 (PDT) 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ecurtin@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer events to event loop X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Eric Curtin via libcamera-devel From: Eric Curtin Reply-To: Eric Curtin Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Extend the EventLoop class to support periodic timer events. This can be used to run tasks periodically, such as handling the event loop of SDL. Also delete all events in the list, before we event_base_loopbreak. Signed-off-by: Eric Curtin Reviewed-by: Kieran Bingham Tested-by: Jacopo Mondi --- src/cam/event_loop.cpp | 33 +++++++++++++++++++++++++++++++++ src/cam/event_loop.h | 6 ++++++ 2 files changed, 39 insertions(+) diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp index e25784c0..181c971c 100644 --- a/src/cam/event_loop.cpp +++ b/src/cam/event_loop.cpp @@ -47,6 +47,10 @@ int EventLoop::exec() void EventLoop::exit(int code) { exitCode_ = code; + for (auto const &e : events_) { + event_del(e->event_); + } + event_base_loopbreak(base_); } @@ -84,6 +88,35 @@ void EventLoop::addEvent(int fd, EventType type, events_.push_back(std::move(event)); } +void EventLoop::toTimeval(const std::chrono::milliseconds d, struct timeval &tv) +{ + tv.tv_sec = std::chrono::duration_cast(d).count(); + tv.tv_usec = std::chrono::duration_cast(d).count(); +} + +void EventLoop::addTimerEvent(const std::chrono::milliseconds d, + const std::function &callback) +{ + std::unique_ptr event = std::make_unique(callback); + event->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch, + event.get()); + if (!event->event_) { + std::cerr << "Failed to create timer event" << std::endl; + return; + } + + struct timeval tv; + toTimeval(d, tv); + + int ret = event_add(event->event_, &tv); + if (ret < 0) { + std::cerr << "Failed to add timer event" << std::endl; + return; + } + + events_.push_back(std::move(event)); +} + void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd, [[maybe_unused]] short flags, void *param) { diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h index a4613eb2..89215dce 100644 --- a/src/cam/event_loop.h +++ b/src/cam/event_loop.h @@ -14,6 +14,8 @@ #include +using namespace std::chrono_literals; + struct event_base; class EventLoop @@ -37,6 +39,9 @@ public: void addEvent(int fd, EventType type, const std::function &handler); + void addTimerEvent(const std::chrono::milliseconds d, + const std::function &handler); + private: struct Event { Event(const std::function &callback); @@ -57,6 +62,7 @@ private: std::list> events_; std::mutex lock_; + void toTimeval(const std::chrono::milliseconds d, struct timeval &tv); static void dispatchCallback(evutil_socket_t fd, short flags, void *param); void dispatchCall();