From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1551) id 1C887385840C; Mon, 16 May 2022 18:54:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1C887385840C Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Pedro Alves To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdbsupport/event-loop.cc: simplify !HAVE_POLL paths X-Act-Checkin: binutils-gdb X-Git-Author: Pedro Alves X-Git-Refname: refs/heads/master X-Git-Oldrev: e90601a4f1af82ff5350797ddc54e24efd731535 X-Git-Newrev: 36a5b3705352f228cdd14a7f9d5e85177fad034c Message-Id: <20220516185434.1C887385840C@sourceware.org> Date: Mon, 16 May 2022 18:54:34 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 16 May 2022 18:54:34 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D36a5b3705352= f228cdd14a7f9d5e85177fad034c commit 36a5b3705352f228cdd14a7f9d5e85177fad034c Author: Pedro Alves Date: Mon May 16 10:11:15 2022 +0100 gdbsupport/event-loop.cc: simplify !HAVE_POLL paths =20 gdbsupport/event-loop.cc throughout handles the case of use_poll being true on a system where HAVE_POLL is not defined, by calling internal_error if that situation ever happens. =20 Simplify this by moving the "use_poll" global itself under HAVE_POLL, so that it's way more unlikely to ever end up in such a situation. Then, move the code that checks the value of use_poll under HAVE_POLL too, and remove the internal_error calls. Like, from: =20 if (use_poll) { #ifdef HAVE_POLL // poll code #else internal_error (....); #endif /* HAVE_POLL */ } else { // select code } =20 to =20 #ifdef HAVE_POLL if (use_poll) { // poll code } else #endif /* HAVE_POLL */ { // select code } =20 While at it, make use_poll be a bool. The current code is using unsigned char most probably to save space, but I don't think it really matters here. =20 Co-Authored-By: Youling Tang Change-Id: I0dd74fdd4d393ccd057906df4cd75e8e83c1cdb4 Diff: --- gdbsupport/event-loop.cc | 88 +++++++++++++++-----------------------------= ---- 1 file changed, 27 insertions(+), 61 deletions(-) diff --git a/gdbsupport/event-loop.cc b/gdbsupport/event-loop.cc index 385b45b2de1..18f57ef8d61 100644 --- a/gdbsupport/event-loop.cc +++ b/gdbsupport/event-loop.cc @@ -78,14 +78,12 @@ struct file_handler struct file_handler *next_file; }; =20 -/* Do we use poll or select ? */ #ifdef HAVE_POLL -#define USE_POLL 1 -#else -#define USE_POLL 0 -#endif /* HAVE_POLL */ - -static unsigned char use_poll =3D USE_POLL; +/* Do we use poll or select? Some systems have poll, but then it's + not useable with all kinds of files. We probe that whenever a new + file handler is added. */ +static bool use_poll =3D true; +#endif =20 #ifdef USE_WIN32API #include @@ -249,12 +247,10 @@ add_file_handler (int fd, handler_func *proc, gdb_cli= ent_data client_data, std::string &&name, bool is_ui) { #ifdef HAVE_POLL - struct pollfd fds; -#endif - if (use_poll) { -#ifdef HAVE_POLL + struct pollfd fds; + /* Check to see if poll () is usable. If not, we'll switch to use select. This can happen on systems like m68k-motorola-sys, `poll' cannot be used to wait for `stdin'. @@ -263,23 +259,15 @@ add_file_handler (int fd, handler_func *proc, gdb_cli= ent_data client_data, fds.fd =3D fd; fds.events =3D POLLIN; if (poll (&fds, 1, 0) =3D=3D 1 && (fds.revents & POLLNVAL)) - use_poll =3D 0; -#else - internal_error (__FILE__, __LINE__, - _("use_poll without HAVE_POLL")); -#endif /* HAVE_POLL */ + use_poll =3D false; } if (use_poll) { -#ifdef HAVE_POLL create_file_handler (fd, POLLIN, proc, client_data, std::move (name), is_ui); -#else - internal_error (__FILE__, __LINE__, - _("use_poll without HAVE_POLL")); -#endif } else +#endif /* HAVE_POLL */ create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, proc, client_data, std::move (name), is_ui); } @@ -321,9 +309,9 @@ create_file_handler (int fd, int mask, handler_func * p= roc, file_ptr->next_file =3D gdb_notifier.first_file_handler; gdb_notifier.first_file_handler =3D file_ptr; =20 +#ifdef HAVE_POLL if (use_poll) { -#ifdef HAVE_POLL gdb_notifier.num_fds++; if (gdb_notifier.poll_fds) gdb_notifier.poll_fds =3D @@ -336,12 +324,9 @@ create_file_handler (int fd, int mask, handler_func * = proc, (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd =3D fd; (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events =3D mask; (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents =3D 0; -#else - internal_error (__FILE__, __LINE__, - _("use_poll without HAVE_POLL")); -#endif /* HAVE_POLL */ } else +#endif /* HAVE_POLL */ { if (mask & GDB_READABLE) FD_SET (fd, &gdb_notifier.check_masks[0]); @@ -402,10 +387,6 @@ delete_file_handler (int fd) { file_handler *file_ptr, *prev_ptr =3D NULL; int i; -#ifdef HAVE_POLL - int j; - struct pollfd *new_poll_fds; -#endif =20 /* Find the entry for the given file. */ =20 @@ -419,9 +400,12 @@ delete_file_handler (int fd) if (file_ptr =3D=3D NULL) return; =20 +#ifdef HAVE_POLL if (use_poll) { -#ifdef HAVE_POLL + int j; + struct pollfd *new_poll_fds; + /* Create a new poll_fds array by copying every fd's information but the one we want to get rid of. */ =20 @@ -442,12 +426,9 @@ delete_file_handler (int fd) xfree (gdb_notifier.poll_fds); gdb_notifier.poll_fds =3D new_poll_fds; gdb_notifier.num_fds--; -#else - internal_error (__FILE__, __LINE__, - _("use_poll without HAVE_POLL")); -#endif /* HAVE_POLL */ } else +#endif /* HAVE_POLL */ { if (file_ptr->mask & GDB_READABLE) FD_CLR (fd, &gdb_notifier.check_masks[0]); @@ -510,9 +491,6 @@ static void handle_file_event (file_handler *file_ptr, int ready_mask) { int mask; -#ifdef HAVE_POLL - int error_mask; -#endif =20 { { @@ -526,9 +504,11 @@ handle_file_event (file_handler *file_ptr, int ready_m= ask) /* See if the desired events (mask) match the received events (ready_mask). */ =20 +#ifdef HAVE_POLL if (use_poll) { -#ifdef HAVE_POLL + int error_mask; + /* POLLHUP means EOF, but can be combined with POLLIN to signal more data to read. */ error_mask =3D POLLHUP | POLLERR | POLLNVAL; @@ -547,12 +527,9 @@ handle_file_event (file_handler *file_ptr, int ready_m= ask) } else file_ptr->error =3D 0; -#else - internal_error (__FILE__, __LINE__, - _("use_poll without HAVE_POLL")); -#endif /* HAVE_POLL */ } else +#endif /* HAVE_POLL */ { if (ready_mask & GDB_EXCEPTION) { @@ -599,9 +576,9 @@ gdb_wait_for_event (int block) if (block) update_wait_timeout (); =20 +#ifdef HAVE_POLL if (use_poll) { -#ifdef HAVE_POLL int timeout; =20 if (block) @@ -616,12 +593,9 @@ gdb_wait_for_event (int block) signal. */ if (num_found =3D=3D -1 && errno !=3D EINTR) perror_with_name (("poll")); -#else - internal_error (__FILE__, __LINE__, - _("use_poll without HAVE_POLL")); -#endif /* HAVE_POLL */ } else +#endif /* HAVE_POLL */ { struct timeval select_timeout; struct timeval *timeout_p; @@ -670,9 +644,9 @@ gdb_wait_for_event (int block) /* To level the fairness across event descriptors, we handle them in a round-robin-like fashion. The number and order of descriptors may change between invocations, but this is good enough. */ +#ifdef HAVE_POLL if (use_poll) { -#ifdef HAVE_POLL int i; int mask; =20 @@ -699,12 +673,9 @@ gdb_wait_for_event (int block) mask =3D (gdb_notifier.poll_fds + i)->revents; handle_file_event (file_ptr, mask); return 1; -#else - internal_error (__FILE__, __LINE__, - _("use_poll without HAVE_POLL")); -#endif /* HAVE_POLL */ } else +#endif /* HAVE_POLL */ { /* See comment about even source fairness above. */ int mask =3D 0; @@ -856,16 +827,11 @@ update_wait_timeout (void) } =20 /* Update the timeout for select/ poll. */ - if (use_poll) - { #ifdef HAVE_POLL - gdb_notifier.poll_timeout =3D timeout.tv_sec * 1000; -#else - internal_error (__FILE__, __LINE__, - _("use_poll without HAVE_POLL")); -#endif /* HAVE_POLL */ - } + if (use_poll) + gdb_notifier.poll_timeout =3D timeout.tv_sec * 1000; else +#endif /* HAVE_POLL */ { gdb_notifier.select_timeout.tv_sec =3D timeout.tv_sec; gdb_notifier.select_timeout.tv_usec =3D timeout.tv_usec;