From patchwork Fri May 20 19:01:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 15997 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 D4ECAC0F2A for ; Fri, 20 May 2022 19:02:09 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8DD5E65667; Fri, 20 May 2022 21:02:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653073329; bh=B8VTTXCFUQ2Gnd4inRf4FehAdih3Zg1nyx6zprQwKYY=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=mr5x/uRbDiUp8o9RTmspcxTcNhKhKQCsSx0ApSmqVXsc3w5rbwZKStzlIId73du97 SsISnvWt5mbuIX5quzjfmhU3ACepcT9/zIuhb4HdxbrfkaUq0dpcQRv0CghV7y6MZd H5Jxcu/YrntEhr12J6dNweEMb6EPAhFkEawYKMk4i18oK887JykFfevfMl3o/k9Y2o 6ODtNyNL6jEusyS+391Ty12/ReQzgAFETSyd0+zJ/1oJPtObqSyxQ2bZXvbgyws3tH GB8rRtcCkwzXbCJm/BYIzepbgg0dIkCk4K/Akwh0hzePyTMawgF6R4RE7vOpqQUgVJ aYsYEzQ6NJHbA== Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0D94C65663 for ; Fri, 20 May 2022 21:02:07 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="Soe6ZoVx"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653073326; 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: in-reply-to:in-reply-to:references:references; bh=FzSpgTqmVJ20LOMPTuGRhBF3U13P52ivipiCwXFCx5w=; b=Soe6ZoVxA0HI4bNSC+cboB9oad0vTpFkT1Twz0E0SsqzUyi+iCKhoeEBLXnxhNXE0BzOFO S9wiVslEuHphdc2qk5C54SOVe9gPd7opfw5Pw6VFuf72PSqRG/nbYZWV7R2DNEPFe8OBPQ xy+Ray7k2wDen/w+gPBUgUZpaAWVOjk= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-412-J-vghI-bNA24ml8s7UIQ8A-1; Fri, 20 May 2022 15:02:05 -0400 X-MC-Unique: J-vghI-bNA24ml8s7UIQ8A-1 Received: by mail-wm1-f71.google.com with SMTP id h206-20020a1c21d7000000b003972daa86deso964145wmh.4 for ; Fri, 20 May 2022 12:02:05 -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:in-reply-to :references:mime-version:content-transfer-encoding; bh=FzSpgTqmVJ20LOMPTuGRhBF3U13P52ivipiCwXFCx5w=; b=YItMKVu+LDGV4Bdipu750xZAZDZDoEDz9LF+AmFsOBj8oCZdfQZbTj8AX4kWdLxoUW V9pAWfgkR53qM+kaR8Ro06GOlEb8NXI1DrqWKnJ9vLrISYL837Y5ou5t3JbEHEz0rLMb bA0eDaccMxaNdZ+ySBfF8FzD5p9h0RXpmP5C5ZHsXV6ZbVsjE5oRhvVA4GIPO+ZS0CUI plXN8ZZv7o0C6MqZt8gbK9oXghxwWwMCaKYWRbmIWq/C1hUhXhB1m3ScpQrTagt9Vf7g y5Y/0uT0obPW96hk0Le5DC/gTX/tvUijkJi8CQhODO4WQo4E0OkRB+1beiR+b941XIKI 1uiw== X-Gm-Message-State: AOAM530gvrA1HaiHk73tyhBm35rwyJOBs/gOy1Q9AuE8lOFFL4/GEpPV c6qtKAk9lm4iiLAMRScBJJ6OFHHmuhhrynH/UtEG9I/bhRgOz7mn3bBqEiYdX/k9Aeuq9rpSr3/ R/9uc2DnuBJ9Pq53izlJSadvetPvxLnSUMsPUyxswbpDN8kNZWJnFELS3XgzdqF6mtZQWggKPVC f0YcqOn/yq X-Received: by 2002:a7b:c5d1:0:b0:37f:a8a3:9e17 with SMTP id n17-20020a7bc5d1000000b0037fa8a39e17mr9661229wmk.109.1653073324140; Fri, 20 May 2022 12:02:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyx4BYjfmA59a5sCuLbdZhAq53dO3cWxGhNRrZDG7EoxLZ1ZUjUNf7+nrOrcH/gqqyE4XMOJQ== X-Received: by 2002:a7b:c5d1:0:b0:37f:a8a3:9e17 with SMTP id n17-20020a7bc5d1000000b0037fa8a39e17mr9661203wmk.109.1653073323713; Fri, 20 May 2022 12:02:03 -0700 (PDT) Received: from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340]) by smtp.gmail.com with ESMTPSA id u30-20020adfa19e000000b0020d10a249eesm3337016wru.13.2022.05.20.12.02.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 May 2022 12:02:02 -0700 (PDT) To: libcamera-devel@lists.libcamera.org, laurent.pinchart@ideasonboard.com, kieran.bingham@ideasonboard.com, jacopo@jmondi.org, javierm@redhat.com Date: Fri, 20 May 2022 20:01:04 +0100 Message-Id: <20220520190106.425386-3-ecurtin@redhat.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220520190106.425386-1-ecurtin@redhat.com> References: <20220520190106.425386-1-ecurtin@redhat.com> 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 v9 2/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, in an effort to avoid race conditions. Signed-off-by: Eric Curtin Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham Tested-by: Jacopo Mondi --- src/cam/event_loop.cpp | 27 +++++++++++++++++++++++++++ src/cam/event_loop.h | 7 ++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp index 2e3ce995..315da38a 100644 --- a/src/cam/event_loop.cpp +++ b/src/cam/event_loop.cpp @@ -47,6 +47,8 @@ int EventLoop::exec() void EventLoop::exit(int code) { exitCode_ = code; + events_.clear(); + event_base_loopbreak(base_); } @@ -84,6 +86,31 @@ void EventLoop::addFdEvent(int fd, EventType type, events_.push_back(std::move(event)); } +void EventLoop::addTimerEvent(const duration period, + 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; + const uint64_t usecs = std::chrono::duration_cast(period).count(); + tv.tv_sec = usecs / 1000000ULL; + tv.tv_usec = usecs % 1000000ULL; + + const 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 79902d87..22769ea5 100644 --- a/src/cam/event_loop.h +++ b/src/cam/event_loop.h @@ -7,9 +7,10 @@ #pragma once +#include #include -#include #include +#include #include #include @@ -37,6 +38,10 @@ public: void addFdEvent(int fd, EventType type, const std::function &handler); + using duration = std::chrono::steady_clock::duration; + void addTimerEvent(const duration period, + const std::function &handler); + private: struct Event { Event(const std::function &callback);