Move event-loop.[ch] to gdbsupport/
[deliverable/binutils-gdb.git] / gdbsupport / event-loop.cc
CommitLineData
b5a0ac70 1/* Event loop machinery for GDB, the GNU debugger.
b811d2c2 2 Copyright (C) 1999-2020 Free Software Foundation, Inc.
b5a0ac70
SS
3 Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
b5a0ac70
SS
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
371d5dec 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
b5a0ac70 19
400b5eca
TT
20#include "gdbsupport/common-defs.h"
21#include "gdbsupport/event-loop.h"
409a3f64 22
98029d02
TT
23#include <chrono>
24
b5a0ac70 25#ifdef HAVE_POLL
409a3f64 26#if defined (HAVE_POLL_H)
9e0b60a8 27#include <poll.h>
409a3f64
AC
28#elif defined (HAVE_SYS_POLL_H)
29#include <sys/poll.h>
30#endif
44f45770 31#endif
409a3f64 32
9e0b60a8 33#include <sys/types.h>
268a13a5 34#include "gdbsupport/gdb_sys_time.h"
06cc9596 35#include "gdbsupport/gdb_select.h"
c2c6d25f 36
371d5dec
MS
37/* Tell create_file_handler what events we are interested in.
38 This is used by the select version of the event loop. */
01f69b38
DE
39
40#define GDB_READABLE (1<<1)
41#define GDB_WRITABLE (1<<2)
42#define GDB_EXCEPTION (1<<3)
43
c2c6d25f 44/* Information about each file descriptor we register with the event
371d5dec 45 loop. */
c2c6d25f
JM
46
47typedef struct file_handler
48 {
371d5dec
MS
49 int fd; /* File descriptor. */
50 int mask; /* Events we want to monitor: POLLIN, etc. */
c2c6d25f 51 int ready_mask; /* Events that have been seen since
371d5dec
MS
52 the last time. */
53 handler_func *proc; /* Procedure to call when fd is ready. */
54 gdb_client_data client_data; /* Argument to pass to proc. */
55 int error; /* Was an error detected on this fd? */
56 struct file_handler *next_file; /* Next registered file descriptor. */
c2c6d25f
JM
57 }
58file_handler;
59
b5a0ac70
SS
60/* Gdb_notifier is just a list of file descriptors gdb is interested in.
61 These are the input file descriptor, and the target file
371d5dec 62 descriptor. We have two flavors of the notifier, one for platforms
b5a0ac70 63 that have the POLL function, the other for those that don't, and
371d5dec 64 only support SELECT. Each of the elements in the gdb_notifier list is
b5a0ac70 65 basically a description of what kind of events gdb is interested
371d5dec 66 in, for each fd. */
b5a0ac70 67
392a587b 68/* As of 1999-04-30 only the input file descriptor is registered with the
371d5dec 69 event loop. */
b5a0ac70 70
44f45770 71/* Do we use poll or select ? */
b5a0ac70 72#ifdef HAVE_POLL
44f45770
EZ
73#define USE_POLL 1
74#else
75#define USE_POLL 0
76#endif /* HAVE_POLL */
77
78static unsigned char use_poll = USE_POLL;
b5a0ac70 79
011825f0
MM
80#ifdef USE_WIN32API
81#include <windows.h>
82#include <io.h>
83#endif
84
b5a0ac70
SS
85static struct
86 {
371d5dec 87 /* Ptr to head of file handler list. */
b5a0ac70
SS
88 file_handler *first_file_handler;
89
4e63d0ac
PA
90 /* Next file handler to handle, for the select variant. To level
91 the fairness across event sources, we serve file handlers in a
92 round-robin-like fashion. The number and order of the polled
93 file handlers may change between invocations, but this is good
94 enough. */
95 file_handler *next_file_handler;
96
44f45770 97#ifdef HAVE_POLL
371d5dec 98 /* Ptr to array of pollfd structures. */
b5a0ac70
SS
99 struct pollfd *poll_fds;
100
4e63d0ac
PA
101 /* Next file descriptor to handle, for the poll variant. To level
102 the fairness across event sources, we poll the file descriptors
103 in a round-robin-like fashion. The number and order of the
104 polled file descriptors may change between invocations, but
105 this is good enough. */
106 int next_poll_fds_index;
107
371d5dec 108 /* Timeout in milliseconds for calls to poll(). */
44f45770
EZ
109 int poll_timeout;
110#endif
b5a0ac70
SS
111
112 /* Masks to be used in the next call to select.
371d5dec 113 Bits are set in response to calls to create_file_handler. */
58a2c44a 114 fd_set check_masks[3];
b5a0ac70 115
371d5dec 116 /* What file descriptors were found ready by select. */
58a2c44a 117 fd_set ready_masks[3];
b5a0ac70 118
371d5dec
MS
119 /* Number of file descriptors to monitor (for poll). */
120 /* Number of valid bits (highest fd value + 1) (for select). */
b5a0ac70
SS
121 int num_fds;
122
371d5dec 123 /* Time structure for calls to select(). */
44f45770 124 struct timeval select_timeout;
c2c6d25f 125
371d5dec 126 /* Flag to tell whether the timeout should be used. */
c2c6d25f 127 int timeout_valid;
6426a772 128 }
b5a0ac70
SS
129gdb_notifier;
130
371d5dec
MS
131/* Structure associated with a timer. PROC will be executed at the
132 first occasion after WHEN. */
c2c6d25f
JM
133struct gdb_timer
134 {
dcb07cfa 135 std::chrono::steady_clock::time_point when;
c2c6d25f
JM
136 int timer_id;
137 struct gdb_timer *next;
371d5dec
MS
138 timer_handler_func *proc; /* Function to call to do the work. */
139 gdb_client_data client_data; /* Argument to async_handler_func. */
ae462839 140 };
c2c6d25f 141
371d5dec
MS
142/* List of currently active timers. It is sorted in order of
143 increasing timers. */
c2c6d25f
JM
144static struct
145 {
371d5dec 146 /* Pointer to first in timer list. */
c2c6d25f
JM
147 struct gdb_timer *first_timer;
148
371d5dec 149 /* Id of the last timer created. */
c2c6d25f
JM
150 int num_timers;
151 }
152timer_list;
153
50d01748
PA
154static void create_file_handler (int fd, int mask, handler_func *proc,
155 gdb_client_data client_data);
50d01748 156static int gdb_wait_for_event (int);
70b66289
PA
157static int update_wait_timeout (void);
158static int poll_timers (void);
b5a0ac70 159\f
b5a0ac70
SS
160/* Process one high level event. If nothing is ready at this time,
161 wait for something to happen (via gdb_wait_for_event), then process
11cf8741 162 it. Returns >0 if something was done otherwise returns <0 (this
e0dd0826 163 can happen if there are no event sources to wait for). */
11cf8741 164
99656a61 165int
e0dd0826 166gdb_do_one_event (void)
b5a0ac70 167{
50d01748
PA
168 static int event_source_head = 0;
169 const int number_of_sources = 3;
170 int current = 0;
171
70b66289
PA
172 /* First let's see if there are any asynchronous signal handlers
173 that are ready. These would be the result of invoking any of the
174 signal handlers. */
175 if (invoke_async_signal_handlers ())
50d01748
PA
176 return 1;
177
178 /* To level the fairness across event sources, we poll them in a
179 round-robin fashion. */
180 for (current = 0; current < number_of_sources; current++)
11cf8741 181 {
70b66289
PA
182 int res;
183
50d01748
PA
184 switch (event_source_head)
185 {
186 case 0:
70b66289
PA
187 /* Are any timers that are ready? */
188 res = poll_timers ();
50d01748
PA
189 break;
190 case 1:
191 /* Are there events already waiting to be collected on the
192 monitored file descriptors? */
70b66289 193 res = gdb_wait_for_event (0);
50d01748
PA
194 break;
195 case 2:
196 /* Are there any asynchronous event handlers ready? */
70b66289 197 res = check_async_event_handlers ();
50d01748 198 break;
80bd5fab
PA
199 default:
200 internal_error (__FILE__, __LINE__,
201 "unexpected event_source_head %d",
202 event_source_head);
50d01748
PA
203 }
204
205 event_source_head++;
206 if (event_source_head == number_of_sources)
207 event_source_head = 0;
7e5cd2de 208
70b66289
PA
209 if (res > 0)
210 return 1;
211 }
7e5cd2de 212
50d01748
PA
213 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
214 we should get out because this means that there are no event
215 sources left. This will make the event loop stop, and the
216 application exit. */
7e5cd2de 217
50d01748
PA
218 if (gdb_wait_for_event (1) < 0)
219 return -1;
7e5cd2de 220
50d01748
PA
221 /* If gdb_wait_for_event has returned 1, it means that one event has
222 been handled. We break out of the loop. */
11cf8741
JM
223 return 1;
224}
225
b5a0ac70
SS
226\f
227
085dd6e6
JM
228/* Wrapper function for create_file_handler, so that the caller
229 doesn't have to know implementation details about the use of poll
371d5dec 230 vs. select. */
c5aa993b 231void
6426a772 232add_file_handler (int fd, handler_func * proc, gdb_client_data client_data)
085dd6e6
JM
233{
234#ifdef HAVE_POLL
44f45770
EZ
235 struct pollfd fds;
236#endif
237
238 if (use_poll)
239 {
240#ifdef HAVE_POLL
371d5dec
MS
241 /* Check to see if poll () is usable. If not, we'll switch to
242 use select. This can happen on systems like
7e5cd2de
EZ
243 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
244 On m68k-motorola-sysv, tty's are not stream-based and not
371d5dec 245 `poll'able. */
7e5cd2de
EZ
246 fds.fd = fd;
247 fds.events = POLLIN;
248 if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
249 use_poll = 0;
44f45770 250#else
8e65ff28 251 internal_error (__FILE__, __LINE__,
e2e0b3e5 252 _("use_poll without HAVE_POLL"));
44f45770
EZ
253#endif /* HAVE_POLL */
254 }
255 if (use_poll)
256 {
257#ifdef HAVE_POLL
258 create_file_handler (fd, POLLIN, proc, client_data);
085dd6e6 259#else
8e65ff28 260 internal_error (__FILE__, __LINE__,
e2e0b3e5 261 _("use_poll without HAVE_POLL"));
085dd6e6 262#endif
44f45770
EZ
263 }
264 else
371d5dec
MS
265 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION,
266 proc, client_data);
085dd6e6
JM
267}
268
b5a0ac70 269/* Add a file handler/descriptor to the list of descriptors we are
371d5dec
MS
270 interested in.
271
272 FD is the file descriptor for the file/stream to be listened to.
273
274 For the poll case, MASK is a combination (OR) of POLLIN,
275 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
276 these are the events we are interested in. If any of them occurs,
277 proc should be called.
278
279 For the select case, MASK is a combination of READABLE, WRITABLE,
280 EXCEPTION. PROC is the procedure that will be called when an event
281 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
282
085dd6e6 283static void
371d5dec
MS
284create_file_handler (int fd, int mask, handler_func * proc,
285 gdb_client_data client_data)
b5a0ac70
SS
286{
287 file_handler *file_ptr;
288
371d5dec
MS
289 /* Do we already have a file handler for this file? (We may be
290 changing its associated procedure). */
b5a0ac70
SS
291 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
292 file_ptr = file_ptr->next_file)
293 {
294 if (file_ptr->fd == fd)
295 break;
296 }
297
371d5dec
MS
298 /* It is a new file descriptor. Add it to the list. Otherwise, just
299 change the data associated with it. */
b5a0ac70
SS
300 if (file_ptr == NULL)
301 {
8d749320 302 file_ptr = XNEW (file_handler);
b5a0ac70
SS
303 file_ptr->fd = fd;
304 file_ptr->ready_mask = 0;
305 file_ptr->next_file = gdb_notifier.first_file_handler;
306 gdb_notifier.first_file_handler = file_ptr;
b5a0ac70 307
05a6c72c
KS
308 if (use_poll)
309 {
b5a0ac70 310#ifdef HAVE_POLL
05a6c72c
KS
311 gdb_notifier.num_fds++;
312 if (gdb_notifier.poll_fds)
313 gdb_notifier.poll_fds =
314 (struct pollfd *) xrealloc (gdb_notifier.poll_fds,
315 (gdb_notifier.num_fds
316 * sizeof (struct pollfd)));
317 else
318 gdb_notifier.poll_fds =
8d749320 319 XNEW (struct pollfd);
05a6c72c
KS
320 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd;
321 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask;
322 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0;
44f45770 323#else
05a6c72c 324 internal_error (__FILE__, __LINE__,
e2e0b3e5 325 _("use_poll without HAVE_POLL"));
44f45770 326#endif /* HAVE_POLL */
05a6c72c 327 }
44f45770 328 else
05a6c72c
KS
329 {
330 if (mask & GDB_READABLE)
331 FD_SET (fd, &gdb_notifier.check_masks[0]);
332 else
333 FD_CLR (fd, &gdb_notifier.check_masks[0]);
334
335 if (mask & GDB_WRITABLE)
336 FD_SET (fd, &gdb_notifier.check_masks[1]);
337 else
338 FD_CLR (fd, &gdb_notifier.check_masks[1]);
339
340 if (mask & GDB_EXCEPTION)
341 FD_SET (fd, &gdb_notifier.check_masks[2]);
342 else
343 FD_CLR (fd, &gdb_notifier.check_masks[2]);
344
345 if (gdb_notifier.num_fds <= fd)
346 gdb_notifier.num_fds = fd + 1;
347 }
44f45770 348 }
05a6c72c
KS
349
350 file_ptr->proc = proc;
351 file_ptr->client_data = client_data;
352 file_ptr->mask = mask;
b5a0ac70
SS
353}
354
4e63d0ac
PA
355/* Return the next file handler to handle, and advance to the next
356 file handler, wrapping around if the end of the list is
357 reached. */
358
359static file_handler *
360get_next_file_handler_to_handle_and_advance (void)
361{
362 file_handler *curr_next;
363
364 /* The first time around, this is still NULL. */
365 if (gdb_notifier.next_file_handler == NULL)
366 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
367
368 curr_next = gdb_notifier.next_file_handler;
369 gdb_assert (curr_next != NULL);
370
371 /* Advance. */
372 gdb_notifier.next_file_handler = curr_next->next_file;
373 /* Wrap around, if necessary. */
374 if (gdb_notifier.next_file_handler == NULL)
375 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
376
377 return curr_next;
378}
379
b5a0ac70 380/* Remove the file descriptor FD from the list of monitored fd's:
371d5dec 381 i.e. we don't care anymore about events on the FD. */
b5a0ac70 382void
c2c6d25f 383delete_file_handler (int fd)
b5a0ac70
SS
384{
385 file_handler *file_ptr, *prev_ptr = NULL;
58a2c44a
EZ
386 int i;
387#ifdef HAVE_POLL
388 int j;
b5a0ac70 389 struct pollfd *new_poll_fds;
b5a0ac70
SS
390#endif
391
371d5dec 392 /* Find the entry for the given file. */
b5a0ac70
SS
393
394 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
395 file_ptr = file_ptr->next_file)
396 {
397 if (file_ptr->fd == fd)
398 break;
399 }
400
401 if (file_ptr == NULL)
402 return;
403
44f45770
EZ
404 if (use_poll)
405 {
b5a0ac70 406#ifdef HAVE_POLL
371d5dec
MS
407 /* Create a new poll_fds array by copying every fd's information
408 but the one we want to get rid of. */
b5a0ac70 409
371d5dec
MS
410 new_poll_fds = (struct pollfd *)
411 xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
b5a0ac70 412
44f45770 413 for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
b5a0ac70 414 {
44f45770
EZ
415 if ((gdb_notifier.poll_fds + i)->fd != fd)
416 {
417 (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd;
418 (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events;
3e43a32a
MS
419 (new_poll_fds + j)->revents
420 = (gdb_notifier.poll_fds + i)->revents;
44f45770
EZ
421 j++;
422 }
b5a0ac70 423 }
b8c9b27d 424 xfree (gdb_notifier.poll_fds);
44f45770
EZ
425 gdb_notifier.poll_fds = new_poll_fds;
426 gdb_notifier.num_fds--;
427#else
8e65ff28 428 internal_error (__FILE__, __LINE__,
e2e0b3e5 429 _("use_poll without HAVE_POLL"));
44f45770 430#endif /* HAVE_POLL */
b5a0ac70 431 }
44f45770
EZ
432 else
433 {
434 if (file_ptr->mask & GDB_READABLE)
435 FD_CLR (fd, &gdb_notifier.check_masks[0]);
436 if (file_ptr->mask & GDB_WRITABLE)
437 FD_CLR (fd, &gdb_notifier.check_masks[1]);
438 if (file_ptr->mask & GDB_EXCEPTION)
439 FD_CLR (fd, &gdb_notifier.check_masks[2]);
b5a0ac70 440
371d5dec 441 /* Find current max fd. */
b5a0ac70 442
44f45770 443 if ((fd + 1) == gdb_notifier.num_fds)
b5a0ac70 444 {
44f45770
EZ
445 gdb_notifier.num_fds--;
446 for (i = gdb_notifier.num_fds; i; i--)
447 {
448 if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
449 || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
450 || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
451 break;
452 }
453 gdb_notifier.num_fds = i;
b5a0ac70
SS
454 }
455 }
b5a0ac70 456
cff3e48b 457 /* Deactivate the file descriptor, by clearing its mask,
371d5dec 458 so that it will not fire again. */
cff3e48b
JM
459
460 file_ptr->mask = 0;
461
4e63d0ac
PA
462 /* If this file handler was going to be the next one to be handled,
463 advance to the next's next, if any. */
464 if (gdb_notifier.next_file_handler == file_ptr)
465 {
466 if (file_ptr->next_file == NULL
467 && file_ptr == gdb_notifier.first_file_handler)
468 gdb_notifier.next_file_handler = NULL;
469 else
470 get_next_file_handler_to_handle_and_advance ();
471 }
472
371d5dec 473 /* Get rid of the file handler in the file handler list. */
b5a0ac70
SS
474 if (file_ptr == gdb_notifier.first_file_handler)
475 gdb_notifier.first_file_handler = file_ptr->next_file;
476 else
477 {
478 for (prev_ptr = gdb_notifier.first_file_handler;
9e0b60a8 479 prev_ptr->next_file != file_ptr;
b5a0ac70
SS
480 prev_ptr = prev_ptr->next_file)
481 ;
482 prev_ptr->next_file = file_ptr->next_file;
483 }
b8c9b27d 484 xfree (file_ptr);
b5a0ac70
SS
485}
486
487/* Handle the given event by calling the procedure associated to the
70b66289
PA
488 corresponding file handler. */
489
b5a0ac70 490static void
70b66289 491handle_file_event (file_handler *file_ptr, int ready_mask)
b5a0ac70 492{
c2c6d25f
JM
493 int mask;
494#ifdef HAVE_POLL
495 int error_mask;
c2c6d25f 496#endif
b5a0ac70 497
b5a0ac70 498 {
b5a0ac70
SS
499 {
500 /* With poll, the ready_mask could have any of three events
371d5dec
MS
501 set to 1: POLLHUP, POLLERR, POLLNVAL. These events
502 cannot be used in the requested event mask (events), but
503 they can be returned in the return mask (revents). We
504 need to check for those event too, and add them to the
505 mask which will be passed to the handler. */
b5a0ac70
SS
506
507 /* See if the desired events (mask) match the received
371d5dec 508 events (ready_mask). */
b5a0ac70 509
44f45770 510 if (use_poll)
c2c6d25f 511 {
44f45770 512#ifdef HAVE_POLL
652c71b4
AS
513 /* POLLHUP means EOF, but can be combined with POLLIN to
514 signal more data to read. */
44f45770 515 error_mask = POLLHUP | POLLERR | POLLNVAL;
70b66289 516 mask = ready_mask & (file_ptr->mask | error_mask);
44f45770 517
652c71b4 518 if ((mask & (POLLERR | POLLNVAL)) != 0)
44f45770 519 {
371d5dec
MS
520 /* Work in progress. We may need to tell somebody
521 what kind of error we had. */
652c71b4 522 if (mask & POLLERR)
29f2bf4f 523 warning (_("Error detected on fd %d"), file_ptr->fd);
652c71b4 524 if (mask & POLLNVAL)
29f2bf4f
TT
525 warning (_("Invalid or non-`poll'able fd %d"),
526 file_ptr->fd);
44f45770
EZ
527 file_ptr->error = 1;
528 }
529 else
530 file_ptr->error = 0;
531#else
8e65ff28 532 internal_error (__FILE__, __LINE__,
e2e0b3e5 533 _("use_poll without HAVE_POLL"));
44f45770 534#endif /* HAVE_POLL */
6426a772
JM
535 }
536 else
c2c6d25f 537 {
70b66289 538 if (ready_mask & GDB_EXCEPTION)
44f45770 539 {
29f2bf4f
TT
540 warning (_("Exception condition detected on fd %d"),
541 file_ptr->fd);
44f45770
EZ
542 file_ptr->error = 1;
543 }
544 else
545 file_ptr->error = 0;
70b66289 546 mask = ready_mask & file_ptr->mask;
c2c6d25f 547 }
b5a0ac70 548
371d5dec 549 /* If there was a match, then call the handler. */
b5a0ac70 550 if (mask != 0)
2acceee2 551 (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
b5a0ac70
SS
552 }
553 }
554}
555
70b66289
PA
556/* Wait for new events on the monitored file descriptors. Run the
557 event handler if the first descriptor that is detected by the poll.
558 If BLOCK and if there are no events, this function will block in
559 the call to poll. Return 1 if an event was handled. Return -1 if
560 there are no file descriptors to monitor. Return 1 if an event was
561 handled, otherwise returns 0. */
562
b5a0ac70 563static int
50d01748 564gdb_wait_for_event (int block)
b5a0ac70
SS
565{
566 file_handler *file_ptr;
0f71a2f6 567 int num_found = 0;
b5a0ac70 568
371d5dec 569 /* Make sure all output is done before getting another event. */
c1cd3163 570 flush_streams ();
7be570e7 571
b5a0ac70
SS
572 if (gdb_notifier.num_fds == 0)
573 return -1;
574
70b66289
PA
575 if (block)
576 update_wait_timeout ();
577
44f45770
EZ
578 if (use_poll)
579 {
b5a0ac70 580#ifdef HAVE_POLL
50d01748
PA
581 int timeout;
582
583 if (block)
584 timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
585 else
586 timeout = 0;
587
588 num_found = poll (gdb_notifier.poll_fds,
589 (unsigned long) gdb_notifier.num_fds, timeout);
44f45770
EZ
590
591 /* Don't print anything if we get out of poll because of a
50d01748 592 signal. */
44f45770 593 if (num_found == -1 && errno != EINTR)
e2e0b3e5 594 perror_with_name (("poll"));
44f45770 595#else
8e65ff28 596 internal_error (__FILE__, __LINE__,
e2e0b3e5 597 _("use_poll without HAVE_POLL"));
44f45770
EZ
598#endif /* HAVE_POLL */
599 }
600 else
c2c6d25f 601 {
50d01748 602 struct timeval select_timeout;
50d01748 603 struct timeval *timeout_p;
d7f9d729 604
50d01748
PA
605 if (block)
606 timeout_p = gdb_notifier.timeout_valid
607 ? &gdb_notifier.select_timeout : NULL;
608 else
609 {
610 memset (&select_timeout, 0, sizeof (select_timeout));
611 timeout_p = &select_timeout;
612 }
613
44f45770
EZ
614 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
615 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
616 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
011825f0
MM
617 num_found = gdb_select (gdb_notifier.num_fds,
618 &gdb_notifier.ready_masks[0],
619 &gdb_notifier.ready_masks[1],
620 &gdb_notifier.ready_masks[2],
50d01748 621 timeout_p);
44f45770 622
371d5dec 623 /* Clear the masks after an error from select. */
44f45770
EZ
624 if (num_found == -1)
625 {
626 FD_ZERO (&gdb_notifier.ready_masks[0]);
627 FD_ZERO (&gdb_notifier.ready_masks[1]);
628 FD_ZERO (&gdb_notifier.ready_masks[2]);
50d01748
PA
629
630 /* Dont print anything if we got a signal, let gdb handle
631 it. */
44f45770 632 if (errno != EINTR)
e2e0b3e5 633 perror_with_name (("select"));
44f45770 634 }
c2c6d25f 635 }
b5a0ac70 636
4e63d0ac
PA
637 /* Avoid looking at poll_fds[i]->revents if no event fired. */
638 if (num_found <= 0)
639 return 0;
640
70b66289
PA
641 /* Run event handlers. We always run just one handler and go back
642 to polling, in case a handler changes the notifier list. Since
643 events for sources we haven't consumed yet wake poll/select
644 immediately, no event is lost. */
b5a0ac70 645
4e63d0ac
PA
646 /* To level the fairness across event descriptors, we handle them in
647 a round-robin-like fashion. The number and order of descriptors
648 may change between invocations, but this is good enough. */
44f45770
EZ
649 if (use_poll)
650 {
b5a0ac70 651#ifdef HAVE_POLL
4e63d0ac
PA
652 int i;
653 int mask;
b5a0ac70 654
4e63d0ac
PA
655 while (1)
656 {
657 if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds)
658 gdb_notifier.next_poll_fds_index = 0;
659 i = gdb_notifier.next_poll_fds_index++;
44f45770 660
4e63d0ac
PA
661 gdb_assert (i < gdb_notifier.num_fds);
662 if ((gdb_notifier.poll_fds + i)->revents)
663 break;
664 }
70b66289 665
4e63d0ac
PA
666 for (file_ptr = gdb_notifier.first_file_handler;
667 file_ptr != NULL;
668 file_ptr = file_ptr->next_file)
669 {
670 if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
671 break;
44f45770 672 }
4e63d0ac
PA
673 gdb_assert (file_ptr != NULL);
674
675 mask = (gdb_notifier.poll_fds + i)->revents;
676 handle_file_event (file_ptr, mask);
677 return 1;
44f45770 678#else
8e65ff28 679 internal_error (__FILE__, __LINE__,
e2e0b3e5 680 _("use_poll without HAVE_POLL"));
44f45770
EZ
681#endif /* HAVE_POLL */
682 }
683 else
684 {
4e63d0ac
PA
685 /* See comment about even source fairness above. */
686 int mask = 0;
687
688 do
b5a0ac70 689 {
4e63d0ac 690 file_ptr = get_next_file_handler_to_handle_and_advance ();
44f45770
EZ
691
692 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
693 mask |= GDB_READABLE;
694 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
695 mask |= GDB_WRITABLE;
696 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
697 mask |= GDB_EXCEPTION;
b5a0ac70 698 }
4e63d0ac
PA
699 while (mask == 0);
700
701 handle_file_event (file_ptr, mask);
702 return 1;
b5a0ac70 703 }
b5a0ac70
SS
704 return 0;
705}
706\f
dcb07cfa
PA
707/* Create a timer that will expire in MS milliseconds from now. When
708 the timer is ready, PROC will be executed. At creation, the timer
709 is added to the timers queue. This queue is kept sorted in order
710 of increasing timers. Return a handle to the timer struct. */
711
c2c6d25f 712int
dcb07cfa 713create_timer (int ms, timer_handler_func *proc,
371d5dec 714 gdb_client_data client_data)
c2c6d25f 715{
dcb07cfa 716 using namespace std::chrono;
c2c6d25f 717 struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
6426a772 718
dcb07cfa 719 steady_clock::time_point time_now = steady_clock::now ();
c2c6d25f 720
dcb07cfa
PA
721 timer_ptr = new gdb_timer ();
722 timer_ptr->when = time_now + milliseconds (ms);
c2c6d25f
JM
723 timer_ptr->proc = proc;
724 timer_ptr->client_data = client_data;
6426a772 725 timer_list.num_timers++;
c2c6d25f
JM
726 timer_ptr->timer_id = timer_list.num_timers;
727
728 /* Now add the timer to the timer queue, making sure it is sorted in
371d5dec 729 increasing order of expiration. */
c2c6d25f 730
6426a772
JM
731 for (timer_index = timer_list.first_timer;
732 timer_index != NULL;
c2c6d25f
JM
733 timer_index = timer_index->next)
734 {
dcb07cfa 735 if (timer_index->when > timer_ptr->when)
c2c6d25f
JM
736 break;
737 }
6426a772 738
c2c6d25f
JM
739 if (timer_index == timer_list.first_timer)
740 {
741 timer_ptr->next = timer_list.first_timer;
742 timer_list.first_timer = timer_ptr;
743
744 }
745 else
746 {
6426a772
JM
747 for (prev_timer = timer_list.first_timer;
748 prev_timer->next != timer_index;
c2c6d25f
JM
749 prev_timer = prev_timer->next)
750 ;
6426a772 751
c2c6d25f
JM
752 prev_timer->next = timer_ptr;
753 timer_ptr->next = timer_index;
754 }
755
756 gdb_notifier.timeout_valid = 0;
757 return timer_ptr->timer_id;
758}
759
760/* There is a chance that the creator of the timer wants to get rid of
371d5dec 761 it before it expires. */
c2c6d25f
JM
762void
763delete_timer (int id)
764{
765 struct gdb_timer *timer_ptr, *prev_timer = NULL;
766
371d5dec 767 /* Find the entry for the given timer. */
c2c6d25f
JM
768
769 for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
770 timer_ptr = timer_ptr->next)
771 {
772 if (timer_ptr->timer_id == id)
773 break;
774 }
775
776 if (timer_ptr == NULL)
777 return;
371d5dec 778 /* Get rid of the timer in the timer list. */
c2c6d25f
JM
779 if (timer_ptr == timer_list.first_timer)
780 timer_list.first_timer = timer_ptr->next;
781 else
782 {
783 for (prev_timer = timer_list.first_timer;
784 prev_timer->next != timer_ptr;
785 prev_timer = prev_timer->next)
786 ;
787 prev_timer->next = timer_ptr->next;
788 }
dcb07cfa 789 delete timer_ptr;
c2c6d25f
JM
790
791 gdb_notifier.timeout_valid = 0;
792}
793
dcb07cfa
PA
794/* Convert a std::chrono duration to a struct timeval. */
795
796template<typename Duration>
797static struct timeval
798duration_cast_timeval (const Duration &d)
799{
800 using namespace std::chrono;
801 seconds sec = duration_cast<seconds> (d);
802 microseconds msec = duration_cast<microseconds> (d - sec);
803
804 struct timeval tv;
805 tv.tv_sec = sec.count ();
806 tv.tv_usec = msec.count ();
807 return tv;
808}
809
70b66289
PA
810/* Update the timeout for the select() or poll(). Returns true if the
811 timer has already expired, false otherwise. */
6426a772 812
70b66289
PA
813static int
814update_wait_timeout (void)
c2c6d25f 815{
2acceee2 816 if (timer_list.first_timer != NULL)
c2c6d25f 817 {
dcb07cfa
PA
818 using namespace std::chrono;
819 steady_clock::time_point time_now = steady_clock::now ();
820 struct timeval timeout;
6426a772 821
dcb07cfa 822 if (timer_list.first_timer->when < time_now)
c2c6d25f 823 {
70b66289 824 /* It expired already. */
dcb07cfa
PA
825 timeout.tv_sec = 0;
826 timeout.tv_usec = 0;
827 }
828 else
829 {
830 steady_clock::duration d = timer_list.first_timer->when - time_now;
831 timeout = duration_cast_timeval (d);
c2c6d25f
JM
832 }
833
70b66289 834 /* Update the timeout for select/ poll. */
44f45770
EZ
835 if (use_poll)
836 {
c2c6d25f 837#ifdef HAVE_POLL
dcb07cfa 838 gdb_notifier.poll_timeout = timeout.tv_sec * 1000;
c2c6d25f 839#else
8e65ff28 840 internal_error (__FILE__, __LINE__,
e2e0b3e5 841 _("use_poll without HAVE_POLL"));
44f45770
EZ
842#endif /* HAVE_POLL */
843 }
844 else
845 {
dcb07cfa
PA
846 gdb_notifier.select_timeout.tv_sec = timeout.tv_sec;
847 gdb_notifier.select_timeout.tv_usec = timeout.tv_usec;
44f45770 848 }
c2c6d25f 849 gdb_notifier.timeout_valid = 1;
70b66289 850
dcb07cfa 851 if (timer_list.first_timer->when < time_now)
70b66289 852 return 1;
c2c6d25f 853 }
6426a772 854 else
c2c6d25f 855 gdb_notifier.timeout_valid = 0;
70b66289
PA
856
857 return 0;
858}
859
860/* Check whether a timer in the timers queue is ready. If a timer is
861 ready, call its handler and return. Update the timeout for the
862 select() or poll() as well. Return 1 if an event was handled,
863 otherwise returns 0.*/
864
865static int
866poll_timers (void)
867{
868 if (update_wait_timeout ())
869 {
870 struct gdb_timer *timer_ptr = timer_list.first_timer;
871 timer_handler_func *proc = timer_ptr->proc;
872 gdb_client_data client_data = timer_ptr->client_data;
873
874 /* Get rid of the timer from the beginning of the list. */
875 timer_list.first_timer = timer_ptr->next;
876
877 /* Delete the timer before calling the callback, not after, in
878 case the callback itself decides to try deleting the timer
879 too. */
0e05cf3a 880 delete timer_ptr;
70b66289
PA
881
882 /* Call the procedure associated with that timer. */
883 (proc) (client_data);
884
885 return 1;
886 }
887
888 return 0;
c2c6d25f 889}
This page took 2.36526 seconds and 4 git commands to generate.