From patchwork Fri Apr 8 16:00:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 15661 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 A12FDC3256 for ; Fri, 8 Apr 2022 16:00:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AF08265642; Fri, 8 Apr 2022 18:00:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1649433624; bh=39TTu31F6IWn1IhidMb9LjoGbN7uxwMkGvIwOoAYiSg=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Enn829P6Gk0ykCaEhOboTVixhJNp6rAqJ2vCaJqT+SvwIQyss45roxZSGfZ1hTkyI MO2knKtIKr9mf/W2Djk47C5HZUeLbKkATNRhVnCLdNhCu9Fm+LpCQb836sIR0K0VV4 oG3WcrPl8FpXjhh0lNiuvtURo3kwoh01OFfM/DB/TcKmm9WWHpGLYpWynjUtv+TVtE rBptFYuk54djYv5BWmbxzZqirfUsuvw/rBVt4ViJxdiTYg4zi8kKvCBJT9CGaEJEcK LLla3Kdc96hR+GEvqrIjGNxs0yHtIJ6NVgtvd/NUY7eweV7IGqqoiI38j9yE19+1FR Ptwh9SOHdeARQ== Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D5E17604BA for ; Fri, 8 Apr 2022 18:00:22 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="Pon2DWUN"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649433621; 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=uUzjs17hCGVsCj1b3oArrT45VOxKw8/vUjWEopdaK+0=; b=Pon2DWUNClzkqk/MCktVzh4NOFnaY90eM+lQ8hxzfqwhgphgFtxG6izW/jKQ/7UJOhg45v BofANjug75l5GapvsUtCsHHZSOQwlb8EQAAUgM1xwp0cwVX4xfVpCbRvcEQO/xtbcerhV4 YCCU/NrLNccQD1cfDzvuE3M4xiJud9Y= 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-657-BvvyTz1PMZmI3IArVY0SBw-1; Fri, 08 Apr 2022 12:00:20 -0400 X-MC-Unique: BvvyTz1PMZmI3IArVY0SBw-1 Received: by mail-wm1-f72.google.com with SMTP id l4-20020a05600c1d0400b0038eaa928a67so550131wms.7 for ; Fri, 08 Apr 2022 09:00:19 -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=uUzjs17hCGVsCj1b3oArrT45VOxKw8/vUjWEopdaK+0=; b=Ek7mT4Mwz2e8GfHdlb9/VbXdwWPL4QWvWHKTIZm7v/BOwDMA/U8hv8b3DLSMsSGvWY 3rpM/xE26+Gg0/LpqhErWHHKUAnQiQx/OFPXdW+x9QRz2flKxErzm5+/NY7WdFZzgYdn 5qNNuRF4LAKe/OUbdFsyyGeYm7V7GiYmsdMS5L/znVxKAIHWW6/90YrP2eU2xPSQhUSS DDy9LcFgxakutcvg4Gz89z8iWBMwKmeMstX/G75xeyDlaARkWR21b6vOzpErsjpolo8y Jg9WINk9W6hdjUEY/U0A/AG13s5t/zy4xiWk4KPtrRuUTIzwvyKxoObEmjisNXmNoS3T Q+MQ== X-Gm-Message-State: AOAM5317wtt742lV3YFqKd6vcaLTRRltPVljvVDzcdK5nua18JmWj+KW g7srNFJqKdlAXNCLG+pDq8MlDY8cpEZq2LJtcJALca2qocrRhsd9yQ3T/TS0XQbJaJU+mUZb8Sx DQGOLsFtvEXo9Ml3QT29rltOTC0LvbFODNmLzB5Xv01GYW03MCphuquLtPKS2Vmm1jsTcSPxfXI i3jQ7Rs4Jh X-Received: by 2002:a5d:64ad:0:b0:204:2f7:b904 with SMTP id m13-20020a5d64ad000000b0020402f7b904mr15134555wrp.458.1649433618407; Fri, 08 Apr 2022 09:00:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBOtuWHNsNwtGNLZbPr299wti7ddRdUY+8kM+oBz+q7mBI6vzQuusFlApr3/TlNBra31+30w== X-Received: by 2002:a5d:64ad:0:b0:204:2f7:b904 with SMTP id m13-20020a5d64ad000000b0020402f7b904mr15134533wrp.458.1649433618105; Fri, 08 Apr 2022 09:00:18 -0700 (PDT) Received: from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340]) by smtp.gmail.com with ESMTPSA id a12-20020a5d53cc000000b00205a0ee9c74sm18913521wrw.89.2022.04.08.09.00.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Apr 2022 09:00:17 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Fri, 8 Apr 2022 17:00:14 +0100 Message-Id: <20220408160015.33612-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 v7 1/2] 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. Signed-off-by: Eric Curtin --- src/cam/event_loop.cpp | 37 +++++++++++++++++++++++++++++++++---- src/cam/event_loop.h | 3 +++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp index e25784c0..1e1a4269 100644 --- a/src/cam/event_loop.cpp +++ b/src/cam/event_loop.cpp @@ -60,6 +60,17 @@ void EventLoop::callLater(const std::function &func) event_base_once(base_, -1, EV_TIMEOUT, dispatchCallback, this, nullptr); } +int EventLoop::eventNew(const std::unique_ptr &event, const int fd, const short events) +{ + event->event_ = event_new(base_, fd, events, &EventLoop::Event::dispatch, event.get()); + if (!event->event_) { + std::cerr << "Failed to create event for fd " << fd << std::endl; + return -EINVAL; + } + + return 0; +} + void EventLoop::addEvent(int fd, EventType type, const std::function &callback) { @@ -68,10 +79,7 @@ void EventLoop::addEvent(int fd, EventType type, | (type & Write ? EV_WRITE : 0) | EV_PERSIST; - event->event_ = event_new(base_, fd, events, &EventLoop::Event::dispatch, - event.get()); - if (!event->event_) { - std::cerr << "Failed to create event for fd " << fd << std::endl; + if (eventNew(event, fd, events)) { return; } @@ -84,6 +92,27 @@ void EventLoop::addEvent(int fd, EventType type, events_.push_back(std::move(event)); } +void EventLoop::addTimerEvent(const suseconds_t period, + const std::function &callback) +{ + std::unique_ptr event = std::make_unique(callback); + if (eventNew(event, -1, EV_PERSIST)) { + return; + } + + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = period; + + 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..3d3561e5 100644 --- a/src/cam/event_loop.h +++ b/src/cam/event_loop.h @@ -36,6 +36,8 @@ public: void addEvent(int fd, EventType type, const std::function &handler); + void addTimerEvent(const suseconds_t period, + const std::function &handler); private: struct Event { @@ -60,4 +62,5 @@ private: static void dispatchCallback(evutil_socket_t fd, short flags, void *param); void dispatchCall(); + int eventNew(const std::unique_ptr &event, const int fd, const short events); };