From patchwork Thu Apr 7 13:21:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 15650 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 48836C0F1B for ; Thu, 7 Apr 2022 13:21:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8F64965642; Thu, 7 Apr 2022 15:21:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1649337677; bh=39TTu31F6IWn1IhidMb9LjoGbN7uxwMkGvIwOoAYiSg=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Omd57hxfOcwsIhXuACqf4aSiwqNrUciq79IzsuUrNdggWLH6lMbmm+lFcIq/XTvyl lOLQ3gObHM+avm/AGMQ53xxb/4uMJuGukOVr3Z+KmLrK3hGykppac2FzMohd2vVygx nw45rooxIN5Fm5CuPeBPhW77xNI5aKO2uUZHTJecNwKSRoxS8RQozCvOyVpMBFUpn8 MnupNpF7huVHkSxs3Ekz0Jy3EFu+Ov0f92saO3CJpVxF518EI5Gbzn2B6Pncj/vID7 jQdYYP/WYdbPzt31ydSVNzG9g0ZQkgrSeeytWOokXWI682Emm5FPUOpNWr4KVdEunJ J+r2M4882JgIA== 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 D5C956563F for ; Thu, 7 Apr 2022 15:21:15 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="NdMNBiaW"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649337674; 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=NdMNBiaWnQfnj6pGhc9Kyn7/kXUPAaYBXv2MtJZDmH6Dd7TuXKfmRrDz/fx/TEpWjMONA/ cKvcwB/8y8f1xUOWMp4pf9W60UOMwhluG5k+ONNra75z9UeTZ0Qd4nqmmhpxU6bkDU8YgZ vZb2KSJkZYXN3QcMn/5sTgxjXN9/EnQ= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-489--yYrI10mMcGE8DDfRK708A-1; Thu, 07 Apr 2022 09:21:13 -0400 X-MC-Unique: -yYrI10mMcGE8DDfRK708A-1 Received: by mail-wm1-f69.google.com with SMTP id h205-20020a1c21d6000000b0038e9ce3b29cso14906wmh.2 for ; Thu, 07 Apr 2022 06:21:13 -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=O73PkservewG7Ka4GPmuC1BtXupE5cQ7A+2J9W2PWLBJrhikKrwXZBR3rQyGK81KlX GHQQvhjlSTbvKWj1S5dvItV9tkQQSMTIlL7uAwlKDJTAmmTCwqFW23AvgPdWHJWhyhrs cS0cVJNAzm9SgQ/X3FF2Fv3nlnBXTo9ErM7RKIXbPP3rWaAp0BHnr+s1tAz7wxyFy8J+ xeYDve/y2KVCtWIsXXrrfyk3FzZqlhlUAAB6WfNiUYGj4OwOTv3L5D7J+SY42QNk9Wvs JtR/Y+dq6baATKh3OvRrIDbb0YmMHFrDi5OQCu02bl1cm1Ukqihy0SaSoYqoa7coHAYY zVXQ== X-Gm-Message-State: AOAM533D4x+H7dNwe/3vQCUUEDa18zQv2jvAkTdTY28OqNNABl+veLfc Ut4p2OkZ++eSBi7MQpfeWsXVg8wxDBMyR+v82unA8di7d6yyb8a3cpx2jqtrw2XvdaknIwU+K05 q9xybBUrlLY6Ll0LyQhGhrY+UUwJYt2BgNvqFcTYtlNozXIGiUUe5l7G4i+RIuXgC1/A/BlwEg8 46L7suMGPY X-Received: by 2002:a1c:4e01:0:b0:38c:b316:e6f5 with SMTP id g1-20020a1c4e01000000b0038cb316e6f5mr12266347wmh.93.1649337671795; Thu, 07 Apr 2022 06:21:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxH926Wj30IKvetJ2aOlDGuY6rj0ANy/+bhERg+DvKdHrUWsyzjTmsx/D9nxFdgrZBA0DkKZQ== X-Received: by 2002:a1c:4e01:0:b0:38c:b316:e6f5 with SMTP id g1-20020a1c4e01000000b0038cb316e6f5mr12266324wmh.93.1649337671510; Thu, 07 Apr 2022 06:21:11 -0700 (PDT) Received: from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340]) by smtp.gmail.com with ESMTPSA id m18-20020a05600c4f5200b0038e8f9d7b57sm1789575wmq.42.2022.04.07.06.21.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 06:21:11 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 7 Apr 2022 14:21:00 +0100 Message-Id: <20220407132101.33140-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 v6 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); };