binutils-gdb/gdb/async-event.h
Simon Marchi 6b36ddeb1e gdb: make async event handlers clear themselves
The `ready` flag of async event handlers is cleared by the async event
handler system right before invoking the associated callback, in
check_async_event_handlers.

This is not ideal with how the infrun subsystem consumes events: all
targets' async event handler callbacks essentially just invoke
`inferior_event_handler`, which eventually calls `fetch_inferior_event`
and `do_target_wait`.  `do_target_wait` picks an inferior at random,
and thus a target at random (it could be the target whose `ready` flag
was cleared, or not), and pulls one event from it.

So it's possible that:

- the async event handler for a target A is called
- we end up consuming an event for target B
- all threads of target B are stopped, target_async(0) is called on it,
  so its async event handler is cleared (e.g.
  record_btrace_target::async)

As a result, target A still has events to report while its async event
handler is left unmarked, so these events are not consumed.  To counter
this, at the end of their async event handler callbacks, targets check
if they still have something to report and re-mark their async event
handler (e.g. remote_async_inferior_event_handler).

The linux_nat target does not suffer from this because it doesn't use an
async event handler at the moment.  It only uses a pipe registered with
the event loop.  It is written to in the SIGCHLD handler (and in other
spots that want to get target wait method called) and read from in
the target's wait method.  So if linux_nat happened to be target A in
the example above, the pipe would just stay readable, and the event loop
would wake up again, until linux_nat's wait method is finally called and
consumes the contents of the pipe.

I think it would be nicer if targets using async_event_handler worked in
a similar way, where the flag would stay set until the target's wait
method is actually called.  As a first step towards that, this patch
moves the responsibility of clearing the ready flags of async event
handlers to the invoked callback.

All async event handler callbacks are modified to clear their ready flag
before doing anything else.  So in practice, nothing changes with this
patch.  It's only the responsibility of clearing the flag that is
shifted toward the callee.

gdb/ChangeLog:

	* async-event.h (async_event_handler_func):  Add documentation.
	* async-event.c (check_async_event_handlers): Don't clear
	async_event_handler ready flag.
	* infrun.c (infrun_async_inferior_event_handler): Clear ready
	flag.
	* record-btrace.c (record_btrace_handle_async_inferior_event):
	Likewise.
	* record-full.c (record_full_async_inferior_event_handler):
	Likewise.
	* remote-notif.c (remote_async_get_pending_events_handler):
	Likewise.
	* remote.c (remote_async_inferior_event_handler): Likewise.

Change-Id: I179ef8e99580eae642d332846fd13664dbddc0c1
2021-02-04 13:13:30 -05:00

88 lines
3.1 KiB
C

/* Async events for the GDB event loop.
Copyright (C) 1999-2021 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef ASYNC_EVENT_H
#define ASYNC_EVENT_H
#include "gdbsupport/event-loop.h"
struct async_signal_handler;
struct async_event_handler;
typedef void (sig_handler_func) (gdb_client_data);
/* Type of async event handler callbacks.
DATA is the client data originally passed to create_async_event_handler.
The callback is called when the async event handler is marked. The callback
is responsible for clearing the async event handler if it no longer needs
to be called. */
typedef void (async_event_handler_func) (gdb_client_data);
extern struct async_signal_handler *
create_async_signal_handler (sig_handler_func *proc,
gdb_client_data client_data,
const char *name);
extern void delete_async_signal_handler (struct async_signal_handler **);
/* Call the handler from HANDLER the next time through the event
loop. */
extern void mark_async_signal_handler (struct async_signal_handler *handler);
/* Returns true if HANDLER is marked ready. */
extern int
async_signal_handler_is_marked (struct async_signal_handler *handler);
/* Mark HANDLER as NOT ready. */
extern void clear_async_signal_handler (struct async_signal_handler *handler);
/* Create and register an asynchronous event source in the event loop,
and set PROC as its callback. CLIENT_DATA is passed as argument to
PROC upon its invocation. Returns a pointer to an opaque structure
used to mark as ready and to later delete this event source from
the event loop.
NAME is a user-friendly name for the handler, used in debug statements. The
name is not copied: its lifetime should be at least as long as that of the
handler. */
extern struct async_event_handler *
create_async_event_handler (async_event_handler_func *proc,
gdb_client_data client_data,
const char *name);
/* Remove the event source pointed by HANDLER_PTR created by
CREATE_ASYNC_EVENT_HANDLER from the event loop, and release it. */
extern void
delete_async_event_handler (struct async_event_handler **handler_ptr);
/* Call the handler from HANDLER the next time through the event
loop. */
extern void mark_async_event_handler (struct async_event_handler *handler);
/* Mark the handler (ASYNC_HANDLER_PTR) as NOT ready. */
extern void clear_async_event_handler (struct async_event_handler *handler);
extern void initialize_async_signal_handlers (void);
#endif /* ASYNC_EVENT_H */