Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Patrick Monnerat via Gdb-patches <gdb-patches@sourceware.org>
To: Simon Marchi <simon.marchi@polymtl.ca>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Add a timeout parameter to gdb_do_one_event
Date: Thu, 26 Aug 2021 17:14:36 +0200	[thread overview]
Message-ID: <0442ef6e-bb22-924d-a1ef-05d6e29cf5f0@monnerat.net> (raw)
In-Reply-To: <12e53aee-05ae-8060-df57-0fc722d4a82c@polymtl.ca>


On 8/26/21 3:47 PM, Simon Marchi wrote:
>
> On 2021-08-26 7:36 a.m., Patrick Monnerat wrote:
>> 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.
> Indeed, happy to know it's not that.
>
>> 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.
> Ok, so you use GDB's event loop, registering some additional events in
> it.  For example, the user clicking a button will generate an event,
> wake up gdb_wait_for_event and call your handler to go do the GUI work.
> Is that it?

To be short, yes. But a mouse click is a bad example as it is not 
handled exactly the same way in Unixes and W$.

Tcl "*events" are also timed calls (with their own queue) and "idle" 
(something that should be executed whenever no event is pending). The 
Tcl notifier is informed by the interpreter of the max time to wait, 
taking into account these 2 event sources.

I tried to implement this timeout with a gdb timer created before 
calling gdb_wait_for_event, but this failed as, if 0, it may be 
triggered before any other pending event because of the round-robin 
strategy of gdb_do_one_event initial loop.

>
> And the timeout is needed to call into the GUI subsystem to do some
> periodic work?
That's it. And as noted above, the actual timeout value is determined by 
Tcl.
>
>>>> ---
>>>>    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);
>>>>    \f
>>>>    /* 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?
> Yes, if you look around, this is how we refer to parameter names in
> comment.  Like it or hate it, that's how it is.
No problem: I just wanted to be sure it was not a misunderstanding.
>
>>>> +   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,
> Ok, can you mention it in the comment?  I don't think it is mentioned.
Will do!
>
>>> 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?
> Not sure, event with an RAII class you need a way to flag whether a
> timer was created or not.  Maybe optional?
>
>    gdb::optional<int> timer_id;
>
>    if (mstimeout > 0)
>      timer_id = create_timer (...);
>
>    res = gdb_wait_for_event (1);
>
>    if (timer_id.has_value ())
>      delete_timer (*timer_id);
>
Thanks for the hint. I think both together would be optimal.

Patrick


  reply	other threads:[~2021-08-26 15:15 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-23 18:23 Patrick Monnerat via Gdb-patches
2021-08-26  3:24 ` Simon Marchi via Gdb-patches
2021-08-26 11:36   ` Patrick Monnerat via Gdb-patches
2021-08-26 13:47     ` Simon Marchi via Gdb-patches
2021-08-26 15:14       ` Patrick Monnerat via Gdb-patches [this message]
2022-03-14 14:49         ` [PING] " Patrick Monnerat via Gdb-patches
2021-08-27 18:08     ` [PATCH] " Tom Tromey
2021-08-28  0:07       ` Patrick Monnerat via Gdb-patches
2021-08-26 18:30 Patrick Monnerat via Gdb-patches
2022-03-14 14:49 Patrick Monnerat via Gdb-patches
2022-03-14 16:17 ` Pedro Alves
2022-03-17 13:08 Patrick Monnerat via Gdb-patches
2022-04-15 16:21 ` Tom Tromey
2022-04-16  0:38   ` Patrick Monnerat via Gdb-patches
2022-07-22 13:41 ` Simon Marchi via Gdb-patches
2022-07-22 22:45   ` Patrick Monnerat via Gdb-patches
2022-07-25  1:07     ` Simon Marchi via Gdb-patches
2022-08-18 11:16 ` Andrew Burgess via Gdb-patches
2022-08-19 11:29   ` Patrick Monnerat via Gdb-patches
2022-08-23 18:38     ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0442ef6e-bb22-924d-a1ef-05d6e29cf5f0@monnerat.net \
    --to=gdb-patches@sourceware.org \
    --cc=patrick@monnerat.net \
    --cc=simon.marchi@polymtl.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox