From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id EKQEKfN8J2HRKAAAWB0awg (envelope-from ) for ; Thu, 26 Aug 2021 07:37:23 -0400 Received: by simark.ca (Postfix, from userid 112) id A434B1EE1B; Thu, 26 Aug 2021 07:37:23 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: * X-Spam-Status: No, score=1.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED, URIBL_DBL_SPAM autolearn=no 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 2209B1EDEE for ; Thu, 26 Aug 2021 07:37:20 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 865A9385843B for ; Thu, 26 Aug 2021 11:37:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 865A9385843B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1629977839; bh=/aZcDB8v9Bn/mAENdKrxpo4mV2VW8+Oc2GR2LFU2ELs=; h=Subject:To:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ARqO5zSLMOjv89DRy0PM2auN/hnnREa+4mfJmgzmiiHeSzR5yMLlgJpOfGZq8ohyu ZUL9ZMchTi/5eTnwttsSRl31wkmSgAyxoceUBvL8En13pdBVZNHCfpywBwSN0vnvTi azO+D1x8fLg/U1DWV0Gw8NR97rOvGxptYB7MfTSg= Received: from jupiter.monnerat.net (jupiter.monnerat.net [46.226.111.226]) by sourceware.org (Postfix) with ESMTPS id D43493858400 for ; Thu, 26 Aug 2021 11:36:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D43493858400 Received: from patrick.monnerat ([192.168.0.128]) by jupiter.monnerat.net (8.14.8/8.14.8) with ESMTP id 17QBaVop010314 (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256 verify=OK); Thu, 26 Aug 2021 13:36:37 +0200 DKIM-Filter: OpenDKIM Filter v2.10.3 jupiter.monnerat.net 17QBaVop010314 Subject: Re: [PATCH] Add a timeout parameter to gdb_do_one_event To: Simon Marchi , gdb-patches@sourceware.org References: <20210823182359.104456-1-patrick@monnerat.net> <4e3085bb-af40-e0dc-73aa-991f97243e06@polymtl.ca> Message-ID: <60a52fef-89f0-62f1-1a45-5e5a40f47df6@monnerat.net> Date: Thu, 26 Aug 2021 13:36:31 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <4e3085bb-af40-e0dc-73aa-991f97243e06@polymtl.ca> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US 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" On 8/26/21 5:24 AM, Simon Marchi wrote: > On 2021-08-23 2:23 p.m., Patrick Monnerat via Gdb-patches wrote: >> 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 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. > So, Insight's main loop looks like: > > while True: > call gdb_do_one_event with a timeout > call gui_do_one_event with a timeout > > ? Not exactly, although this is the first idea that emerges. But this approach is not reactive enough and consumes CPU uselessly. The real implementation makes the GUI event loop call gdb_do_one_event and recursively. The actual event waiting is performed by gdb_do_one_event, but the GUI may define a timeout for it. The hard task here is to avoid infinite recursion. As Insight GUI is Tcl/Tk, a Tcl C API feature called a notifier (https://www.tcl.tk/man/tcl8.4/TclLib/Notifier.html) allowed me to design such a strategy. See https://sourceware.org/git/?p=insight.git;a=blob;f=gdbtk/generic/gdbtk.c line 247 and under to satisfy your curiosity! But it is a quite large part of the interface between the 2 subsystems that was not needed before gdb commit b2d8657. Note that an additional patch (unsubmitted yet) is needed to map Tcl file events into gdb file handlers. The alternate solution would have been to run the GUI in a separate thread, but that's even a bigger work! > >> --- >> gdbsupport/event-loop.cc | 45 ++++++++++++++++++++++++++++------------ >> gdbsupport/event-loop.h | 2 +- >> 2 files changed, 33 insertions(+), 14 deletions(-) >> >> diff --git a/gdbsupport/event-loop.cc b/gdbsupport/event-loop.cc >> index 98d1ada52cd..72c64dcdb72 100644 >> --- a/gdbsupport/event-loop.cc >> +++ b/gdbsupport/event-loop.cc >> @@ -177,16 +177,21 @@ 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 > mstimeout -> MSTIMEOUT Even if not a constant but the name of a parameter? > >> + 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. >> + Setting the timeout to a negative value disables it. > Does setting the timeout to 0 mean return immediately if nothing is > available? Yes, >> + The timeout is never used by gdb itself, it is however needed to >> + integrate gdb event handling within some external (i.e.: GUI) event >> + loop. */ > Err, you can probably say "Insight" here. It's not like we want to > support programs doing this in general. We make an exception for > Insight because of historical reasons, I suppose. OK. > >> 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 +203,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 +235,30 @@ 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. */ >> - >> - if (gdb_wait_for_event (1) < 0) >> - return -1; >> + 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 (mstimeout != 0) >> + { >> + int timerid = 0; >> + >> + if (mstimeout > 0) >> + timerid = create_timer (mstimeout, >> + [] (gdb_client_data timeridp) >> + { >> + *((int *) timeridp) = 0; >> + }, >> + &timerid); >> + res = gdb_wait_for_event (1); >> + if (timerid) >> + delete_timer (timerid); > Probably not a practical concern, but by creating timers over and over, > for a reaaaaaaally long GDB session, the timer id will eventually wrap > and create_timer might return 0. I don't know what will happen then. Yes, you're right. I'll change it. Maybe an RAII class here too? Merci, Patrick