From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id OC/DDwXeJ2HCNgAAWB0awg (envelope-from ) for ; Thu, 26 Aug 2021 14:31:33 -0400 Received: by simark.ca (Postfix, from userid 112) id 2FA3B1EE1B; Thu, 26 Aug 2021 14:31:33 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=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 4520E1ECEB for ; Thu, 26 Aug 2021 14:31:32 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B82503858413 for ; Thu, 26 Aug 2021 18:31:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B82503858413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1630002691; bh=ce8G1Kkf5UlO6qBsPWWLj+fJ6idJizpUdlF5QHrn3wY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=YAo5FGiq2PaAErp00ppJNIWFSoWlbRZJod3td3rnbeqc0tkxqM+BJT7+IWSIMxj+t XsH09nJm45xZ7g98H5hwxXXCZgrgEtKBxjJsCDURWFTiIaIXQ8TyX/7VAjOCLxFtkm 5UlFHIuq8o+w6vJ/EsjsF7RGaQUZvxDMBEhvtFQw= Received: from vimdzmsp-nwas04.bluewin.ch (vimdzmsp-nwas04.bluewin.ch [195.186.228.51]) by sourceware.org (Postfix) with ESMTPS id 297A53858402 for ; Thu, 26 Aug 2021 18:31:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 297A53858402 Received: from patrick.monnerat ([178.193.82.56]) by vimdzmsp-nwas04.bluewin.ch Swisscom AG with ESMTP id JK9Qm1p7vvCjcJK9VmCb2S; Thu, 26 Aug 2021 20:31:01 +0200 Received: from patrick.monnerat (localhost [127.0.0.1]) by patrick.monnerat (8.16.1/8.16.1) with ESMTPS id 17QIUimY442178 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 26 Aug 2021 20:30:44 +0200 Received: (from patrick@localhost) by patrick.monnerat (8.16.1/8.16.1/Submit) id 17QIUiUt442177; Thu, 26 Aug 2021 20:30:44 +0200 X-Authentication-Warning: patrick.monnerat: patrick set sender to patrick@monnerat.net using -f To: gdb-patches@sourceware.org Subject: [PATCH] Add a timeout parameter to gdb_do_one_event Date: Thu, 26 Aug 2021 20:30:41 +0200 Message-Id: <20210826183041.442130-1-patrick@monnerat.net> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CMAE-Envelope: MS4xfPUa1xInG9tGlA/TS0SbPBh1zYUr/I1fuISNCIgEUXenZLiuFwniGC+E/r0RpeBCXcKKNbyD6XN2OuVy7PW1hUjclscH42zz6MEG/5hgahIec10kCu0X 6zGXn3Jj93/zkkRXt7WhkOipZBqxsz92DnxMskD6a5HmSrPF95O1+Sf35GZvq7qmGoRKmeZPMsaEDn9esWrTM2W75bUmXOUSMnY= 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 | 80 ++++++++++++++++++++++++++++++++++------ gdbsupport/event-loop.h | 2 +- 2 files changed, 69 insertions(+), 13 deletions(-) diff --git a/gdbsupport/event-loop.cc b/gdbsupport/event-loop.cc index 98d1ada52cd..7c99d8ccbb6 100644 --- a/gdbsupport/event-loop.cc +++ b/gdbsupport/event-loop.cc @@ -33,6 +33,7 @@ #include #include "gdbsupport/gdb_sys_time.h" #include "gdbsupport/gdb_select.h" +#include "gdbsupport/gdb_optional.h" /* See event-loop.h. */ @@ -176,17 +177,64 @@ static int gdb_wait_for_event (int); static int update_wait_timeout (void); static int poll_timers (void); +/* RAII class for the gdb_do_one_event timeout timer. */ + +class scoped_event_wait_timeout +{ +public: + scoped_event_wait_timeout () + : m_timer_id () + {} + + ~scoped_event_wait_timeout () + { + this->destroy (); + } + + void reset () + { + m_timer_id.reset (); + } + + void destroy () + { + if (m_timer_id.has_value ()) + delete_timer (*m_timer_id); + this->reset (); + } + + void start_timer (int timeout) + { + this->destroy (); + m_timer_id = create_timer (timeout, + [] (gdb_client_data arg) + { + ((scoped_event_wait_timeout *) arg)->reset (); + }, + this); + } + +private: + gdb::optional m_timer_id; +}; + /* 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; int current = 0; + int res = 0; /* First let's see if there are any asynchronous signal handlers that are ready. These would be the result of invoking any of the @@ -198,8 +246,6 @@ gdb_do_one_event (void) round-robin fashion. */ for (current = 0; current < number_of_sources; current++) { - int res; - switch (event_source_head) { case 0: @@ -232,14 +278,24 @@ gdb_do_one_event (void) /* 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. */ + + res = 0; + if (mstimeout != 0) + { + scoped_event_wait_timeout timer_id; - if (gdb_wait_for_event (1) < 0) - return -1; + if (mstimeout > 0) + timer_id.start_timer (mstimeout); + res = gdb_wait_for_event (1); + } - /* If gdb_wait_for_event has returned 1, it means that one event has - been handled. We break out of the loop. */ - return 1; + return res; } /* See event-loop.h */ diff --git a/gdbsupport/event-loop.h b/gdbsupport/event-loop.h index dc4e4d59f03..cf62f654c1c 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.31.1