From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8EvFFxEzM2LwXwAAWB0awg (envelope-from ) for ; Thu, 17 Mar 2022 09:09:37 -0400 Received: by simark.ca (Postfix, from userid 112) id 5CAE71F3CC; Thu, 17 Mar 2022 09:09:37 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id C97931EA69 for ; Thu, 17 Mar 2022 09:09:36 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4046D3952013 for ; Thu, 17 Mar 2022 13:09:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4046D3952013 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647522576; bh=Hdik/lAgUCsZCEeAX/L1tQUVTqM8ZUAxRc9X7uQpurk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=bSOYF8XNltwb8wW9XUJf/GfTzO4L+GgsDU9jKghXTC3JjOXc54x+16J2h6n4V6Bhy ySs6lpevk1k69i2mLoPckDx2H/d2lq0tZENO94bk94Twy5y6+utVoG1uyXpIl3ln7F 88azYRB5BqwyuYyQZYwEWujwCNLvBpe5J5aZgH/U= Received: from jupiter.monnerat.net (jupiter.monnerat.net [46.226.111.226]) by sourceware.org (Postfix) with ESMTPS id 01D86385840F for ; Thu, 17 Mar 2022 13:09:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 01D86385840F Received: from patrick ([192.168.0.128]) by jupiter.monnerat.net (8.14.8/8.14.8) with ESMTP id 22HD9A6l029089 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 17 Mar 2022 14:09:15 +0100 DKIM-Filter: OpenDKIM Filter v2.10.3 jupiter.monnerat.net 22HD9A6l029089 To: gdb-patches@sourceware.org Subject: [PATCH] Add a timeout parameter to gdb_do_one_event Date: Thu, 17 Mar 2022 14:08:46 +0100 Message-Id: <20220317130846.162955-1-patrick@monnerat.net> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Patrick Monnerat via Gdb-patches Reply-To: Patrick Monnerat Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" Since commit b2d8657, having a per-interpreter event/command loop is not possible anymore. As Insight uses a GUI that has its own event loop, gdb and GUI event loops have then to be "merged" (i.e.: work together). But this is problematic as gdb_do_one_event is not aware of this alternate event loop and thus may wait forever. The solution is to implement a wait timeout to gdb_do_one_event. This cannot be done externally as gdb timers are event sources themselves. The new parameter defaults to "no timeout": as it is used by Insight only, there is no need to update calls from the gdb source tree. --- gdbsupport/event-loop.cc | 45 +++++++++++++++++++++++++++++++--------- gdbsupport/event-loop.h | 2 +- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/gdbsupport/event-loop.cc b/gdbsupport/event-loop.cc index 385b45b2de1..14168cae5a6 100644 --- a/gdbsupport/event-loop.cc +++ b/gdbsupport/event-loop.cc @@ -33,6 +33,8 @@ #include #include "gdbsupport/gdb_sys_time.h" #include "gdbsupport/gdb_select.h" +#include "gdbsupport/gdb_optional.h" +#include "gdbsupport/scope-exit.h" /* See event-loop.h. */ @@ -177,12 +179,17 @@ static int update_wait_timeout (void); static int poll_timers (void); /* Process one high level event. If nothing is ready at this time, - wait for something to happen (via gdb_wait_for_event), then process - it. Returns >0 if something was done otherwise returns <0 (this - can happen if there are no event sources to wait for). */ + wait at most MSTIMEOUT milliseconds for something to happen (via + gdb_wait_for_event), then process it. Returns >0 if something was + done, <0 if there are no event sources to wait for, =0 if timeout occurred. + A timeout of 0 allows to serve an already pending event, but does not + wait if none found. + Setting the timeout to a negative value disables it. + The timeout is never used by gdb itself, it is however needed to + integrate gdb event handling within Insight's GUI event loop. */ int -gdb_do_one_event (void) +gdb_do_one_event (int mstimeout) { static int event_source_head = 0; const int number_of_sources = 3; @@ -229,17 +236,35 @@ gdb_do_one_event (void) return 1; } + if (!mstimeout) + return 0; /* Null timeout: do not wait for an event. */ + /* Block waiting for a new event. If gdb_wait_for_event returns -1, we should get out because this means that there are no event sources left. This will make the event loop stop, and the - application exit. */ + application exit. + If a timeout has been given, a new timer is set accordingly + to abort event wait. It is deleted upon gdb_wait_for_event + termination and thus should never be triggered. + When the timeout is reached, events are not monitored again: + they already have been checked in the loop above. */ - if (gdb_wait_for_event (1) < 0) - return -1; + gdb::optional timer_id; - /* If gdb_wait_for_event has returned 1, it means that one event has - been handled. We break out of the loop. */ - return 1; + SCOPE_EXIT + { + if (timer_id.has_value ()) + delete_timer (*timer_id); + }; + + if (mstimeout > 0) + timer_id = create_timer (mstimeout, + [] (gdb_client_data arg) + { + ((gdb::optional *) arg)->reset (); + }, + &timer_id); + return gdb_wait_for_event (1); } /* See event-loop.h */ diff --git a/gdbsupport/event-loop.h b/gdbsupport/event-loop.h index 9ed592877ab..c82493e9bdf 100644 --- a/gdbsupport/event-loop.h +++ b/gdbsupport/event-loop.h @@ -76,7 +76,7 @@ typedef void (timer_handler_func) (gdb_client_data); /* Exported functions from event-loop.c */ -extern int gdb_do_one_event (void); +extern int gdb_do_one_event (int mstimeout = -1); extern void delete_file_handler (int fd); /* Add a file handler/descriptor to the list of descriptors we are -- 2.35.1