Rename gdb exception types
[deliverable/binutils-gdb.git] / gdb / event-loop.c
CommitLineData
b5a0ac70 1/* Event loop machinery for GDB, the GNU debugger.
42a4f53d 2 Copyright (C) 1999-2019 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
b5a0ac70 20#include "defs.h"
9e0b60a8 21#include "event-loop.h"
c2c6d25f 22#include "event-top.h"
4de283e4 23#include "common/queue.h"
5cc3ce8b 24#include "ser-event.h"
409a3f64 25
b5a0ac70 26#ifdef HAVE_POLL
409a3f64 27#if defined (HAVE_POLL_H)
9e0b60a8 28#include <poll.h>
409a3f64
AC
29#elif defined (HAVE_SYS_POLL_H)
30#include <sys/poll.h>
31#endif
44f45770 32#endif
409a3f64 33
9e0b60a8 34#include <sys/types.h>
0747795c 35#include "common/gdb_sys_time.h"
0ea3f30e 36#include "gdb_select.h"
76727919 37#include "observable.h"
7c36c34e 38#include "top.h"
c2c6d25f 39
371d5dec
MS
40/* Tell create_file_handler what events we are interested in.
41 This is used by the select version of the event loop. */
01f69b38
DE
42
43#define GDB_READABLE (1<<1)
44#define GDB_WRITABLE (1<<2)
45#define GDB_EXCEPTION (1<<3)
46
50d01748
PA
47/* Data point to pass to the event handler. */
48typedef union event_data
49{
50 void *ptr;
51 int integer;
52} event_data;
53
c2c6d25f 54typedef struct gdb_event gdb_event;
50d01748 55typedef void (event_handler_func) (event_data);
c2c6d25f
JM
56
57/* Event for the GDB event system. Events are queued by calling
371d5dec 58 async_queue_event and serviced later on by gdb_do_one_event. An
c2c6d25f 59 event can be, for instance, a file descriptor becoming ready to be
50d01748 60 read. Servicing an event simply means that the procedure PROC will
c2c6d25f
JM
61 be called. We have 2 queues, one for file handlers that we listen
62 to in the event loop, and one for the file handlers+events that are
371d5dec 63 ready. The procedure PROC associated with each event is dependant
50d01748
PA
64 of the event source. In the case of monitored file descriptors, it
65 is always the same (handle_file_event). Its duty is to invoke the
66 handler associated with the file descriptor whose state change
67 generated the event, plus doing other cleanups and such. In the
68 case of async signal handlers, it is
69 invoke_async_signal_handler. */
c2c6d25f 70
843b20dc 71typedef struct gdb_event
c2c6d25f 72 {
50d01748
PA
73 /* Procedure to call to service this event. */
74 event_handler_func *proc;
75
76 /* Data to pass to the event handler. */
77 event_data data;
843b20dc 78 } *gdb_event_p;
c2c6d25f
JM
79
80/* Information about each file descriptor we register with the event
371d5dec 81 loop. */
c2c6d25f
JM
82
83typedef struct file_handler
84 {
371d5dec
MS
85 int fd; /* File descriptor. */
86 int mask; /* Events we want to monitor: POLLIN, etc. */
c2c6d25f 87 int ready_mask; /* Events that have been seen since
371d5dec
MS
88 the last time. */
89 handler_func *proc; /* Procedure to call when fd is ready. */
90 gdb_client_data client_data; /* Argument to pass to proc. */
91 int error; /* Was an error detected on this fd? */
92 struct file_handler *next_file; /* Next registered file descriptor. */
c2c6d25f
JM
93 }
94file_handler;
95
371d5dec 96/* PROC is a function to be invoked when the READY flag is set. This
c2c6d25f 97 happens when there has been a signal and the corresponding signal
371d5dec
MS
98 handler has 'triggered' this async_signal_handler for execution.
99 The actual work to be done in response to a signal will be carried
100 out by PROC at a later time, within process_event. This provides a
101 deferred execution of signal handlers.
102
c2c6d25f 103 Async_init_signals takes care of setting up such an
371d5dec
MS
104 async_signal_handler for each interesting signal. */
105
c2c6d25f
JM
106typedef struct async_signal_handler
107 {
371d5dec
MS
108 int ready; /* If ready, call this handler
109 from the main event loop, using
110 invoke_async_handler. */
111 struct async_signal_handler *next_handler; /* Ptr to next handler. */
112 sig_handler_func *proc; /* Function to call to do the work. */
113 gdb_client_data client_data; /* Argument to async_handler_func. */
c2c6d25f
JM
114 }
115async_signal_handler;
116
50d01748
PA
117/* PROC is a function to be invoked when the READY flag is set. This
118 happens when the event has been marked with
119 MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response
120 to an event will be carried out by PROC at a later time, within
121 process_event. This provides a deferred execution of event
122 handlers. */
123typedef struct async_event_handler
124 {
125 /* If ready, call this handler from the main event loop, using
126 invoke_event_handler. */
127 int ready;
128
129 /* Point to next handler. */
130 struct async_event_handler *next_handler;
131
132 /* Function to call to do the work. */
133 async_event_handler_func *proc;
134
135 /* Argument to PROC. */
136 gdb_client_data client_data;
137 }
138async_event_handler;
139
b5a0ac70
SS
140/* Gdb_notifier is just a list of file descriptors gdb is interested in.
141 These are the input file descriptor, and the target file
371d5dec 142 descriptor. We have two flavors of the notifier, one for platforms
b5a0ac70 143 that have the POLL function, the other for those that don't, and
371d5dec 144 only support SELECT. Each of the elements in the gdb_notifier list is
b5a0ac70 145 basically a description of what kind of events gdb is interested
371d5dec 146 in, for each fd. */
b5a0ac70 147
392a587b 148/* As of 1999-04-30 only the input file descriptor is registered with the
371d5dec 149 event loop. */
b5a0ac70 150
44f45770 151/* Do we use poll or select ? */
b5a0ac70 152#ifdef HAVE_POLL
44f45770
EZ
153#define USE_POLL 1
154#else
155#define USE_POLL 0
156#endif /* HAVE_POLL */
157
158static unsigned char use_poll = USE_POLL;
b5a0ac70 159
011825f0
MM
160#ifdef USE_WIN32API
161#include <windows.h>
162#include <io.h>
163#endif
164
b5a0ac70
SS
165static struct
166 {
371d5dec 167 /* Ptr to head of file handler list. */
b5a0ac70
SS
168 file_handler *first_file_handler;
169
4e63d0ac
PA
170 /* Next file handler to handle, for the select variant. To level
171 the fairness across event sources, we serve file handlers in a
172 round-robin-like fashion. The number and order of the polled
173 file handlers may change between invocations, but this is good
174 enough. */
175 file_handler *next_file_handler;
176
44f45770 177#ifdef HAVE_POLL
371d5dec 178 /* Ptr to array of pollfd structures. */
b5a0ac70
SS
179 struct pollfd *poll_fds;
180
4e63d0ac
PA
181 /* Next file descriptor to handle, for the poll variant. To level
182 the fairness across event sources, we poll the file descriptors
183 in a round-robin-like fashion. The number and order of the
184 polled file descriptors may change between invocations, but
185 this is good enough. */
186 int next_poll_fds_index;
187
371d5dec 188 /* Timeout in milliseconds for calls to poll(). */
44f45770
EZ
189 int poll_timeout;
190#endif
b5a0ac70
SS
191
192 /* Masks to be used in the next call to select.
371d5dec 193 Bits are set in response to calls to create_file_handler. */
58a2c44a 194 fd_set check_masks[3];
b5a0ac70 195
371d5dec 196 /* What file descriptors were found ready by select. */
58a2c44a 197 fd_set ready_masks[3];
b5a0ac70 198
371d5dec
MS
199 /* Number of file descriptors to monitor (for poll). */
200 /* Number of valid bits (highest fd value + 1) (for select). */
b5a0ac70
SS
201 int num_fds;
202
371d5dec 203 /* Time structure for calls to select(). */
44f45770 204 struct timeval select_timeout;
c2c6d25f 205
371d5dec 206 /* Flag to tell whether the timeout should be used. */
c2c6d25f 207 int timeout_valid;
6426a772 208 }
b5a0ac70
SS
209gdb_notifier;
210
371d5dec
MS
211/* Structure associated with a timer. PROC will be executed at the
212 first occasion after WHEN. */
c2c6d25f
JM
213struct gdb_timer
214 {
dcb07cfa 215 std::chrono::steady_clock::time_point when;
c2c6d25f
JM
216 int timer_id;
217 struct gdb_timer *next;
371d5dec
MS
218 timer_handler_func *proc; /* Function to call to do the work. */
219 gdb_client_data client_data; /* Argument to async_handler_func. */
ae462839 220 };
c2c6d25f 221
371d5dec
MS
222/* List of currently active timers. It is sorted in order of
223 increasing timers. */
c2c6d25f
JM
224static struct
225 {
371d5dec 226 /* Pointer to first in timer list. */
c2c6d25f
JM
227 struct gdb_timer *first_timer;
228
371d5dec 229 /* Id of the last timer created. */
c2c6d25f
JM
230 int num_timers;
231 }
232timer_list;
233
b5a0ac70 234/* All the async_signal_handlers gdb is interested in are kept onto
371d5dec 235 this list. */
b5a0ac70
SS
236static struct
237 {
371d5dec 238 /* Pointer to first in handler list. */
c5aa993b
JM
239 async_signal_handler *first_handler;
240
371d5dec 241 /* Pointer to last in handler list. */
c5aa993b 242 async_signal_handler *last_handler;
b5a0ac70
SS
243 }
244sighandler_list;
245
50d01748 246/* All the async_event_handlers gdb is interested in are kept onto
371d5dec 247 this list. */
50d01748
PA
248static struct
249 {
371d5dec 250 /* Pointer to first in handler list. */
50d01748
PA
251 async_event_handler *first_handler;
252
371d5dec 253 /* Pointer to last in handler list. */
50d01748
PA
254 async_event_handler *last_handler;
255 }
256async_event_handler_list;
257
258static int invoke_async_signal_handlers (void);
259static void create_file_handler (int fd, int mask, handler_func *proc,
260 gdb_client_data client_data);
70b66289 261static int check_async_event_handlers (void);
50d01748 262static int gdb_wait_for_event (int);
70b66289
PA
263static int update_wait_timeout (void);
264static int poll_timers (void);
b5a0ac70
SS
265\f
266
5cc3ce8b
PA
267/* This event is signalled whenever an asynchronous handler needs to
268 defer an action to the event loop. */
269static struct serial_event *async_signal_handlers_serial_event;
270
271/* Callback registered with ASYNC_SIGNAL_HANDLERS_SERIAL_EVENT. */
272
273static void
274async_signals_handler (int error, gdb_client_data client_data)
275{
276 /* Do nothing. Handlers are run by invoke_async_signal_handlers
277 from instead. */
278}
279
280void
281initialize_async_signal_handlers (void)
282{
283 async_signal_handlers_serial_event = make_serial_event ();
284
285 add_file_handler (serial_event_fd (async_signal_handlers_serial_event),
286 async_signals_handler, NULL);
287}
288
b5a0ac70
SS
289/* Process one high level event. If nothing is ready at this time,
290 wait for something to happen (via gdb_wait_for_event), then process
11cf8741 291 it. Returns >0 if something was done otherwise returns <0 (this
e0dd0826 292 can happen if there are no event sources to wait for). */
11cf8741 293
99656a61 294int
e0dd0826 295gdb_do_one_event (void)
b5a0ac70 296{
50d01748
PA
297 static int event_source_head = 0;
298 const int number_of_sources = 3;
299 int current = 0;
300
70b66289
PA
301 /* First let's see if there are any asynchronous signal handlers
302 that are ready. These would be the result of invoking any of the
303 signal handlers. */
304 if (invoke_async_signal_handlers ())
50d01748
PA
305 return 1;
306
307 /* To level the fairness across event sources, we poll them in a
308 round-robin fashion. */
309 for (current = 0; current < number_of_sources; current++)
11cf8741 310 {
70b66289
PA
311 int res;
312
50d01748
PA
313 switch (event_source_head)
314 {
315 case 0:
70b66289
PA
316 /* Are any timers that are ready? */
317 res = poll_timers ();
50d01748
PA
318 break;
319 case 1:
320 /* Are there events already waiting to be collected on the
321 monitored file descriptors? */
70b66289 322 res = gdb_wait_for_event (0);
50d01748
PA
323 break;
324 case 2:
325 /* Are there any asynchronous event handlers ready? */
70b66289 326 res = check_async_event_handlers ();
50d01748 327 break;
80bd5fab
PA
328 default:
329 internal_error (__FILE__, __LINE__,
330 "unexpected event_source_head %d",
331 event_source_head);
50d01748
PA
332 }
333
334 event_source_head++;
335 if (event_source_head == number_of_sources)
336 event_source_head = 0;
7e5cd2de 337
70b66289
PA
338 if (res > 0)
339 return 1;
340 }
7e5cd2de 341
50d01748
PA
342 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
343 we should get out because this means that there are no event
344 sources left. This will make the event loop stop, and the
345 application exit. */
7e5cd2de 346
50d01748
PA
347 if (gdb_wait_for_event (1) < 0)
348 return -1;
7e5cd2de 349
50d01748
PA
350 /* If gdb_wait_for_event has returned 1, it means that one event has
351 been handled. We break out of the loop. */
11cf8741
JM
352 return 1;
353}
354
371d5dec
MS
355/* Start up the event loop. This is the entry point to the event loop
356 from the command loop. */
b5a0ac70 357
11cf8741
JM
358void
359start_event_loop (void)
360{
e0dd0826
PA
361 /* Loop until there is nothing to do. This is the entry point to
362 the event loop engine. gdb_do_one_event will process one event
363 for each invocation. It blocks waiting for an event and then
364 processes it. */
b5a0ac70
SS
365 while (1)
366 {
e0dd0826 367 int result = 0;
3b8630c3 368
a70b8144 369 try
b5a0ac70 370 {
e0dd0826
PA
371 result = gdb_do_one_event ();
372 }
230d2906 373 catch (const gdb_exception &ex)
e0dd0826
PA
374 {
375 exception_print (gdb_stderr, ex);
376
32c1e744
VP
377 /* If any exception escaped to here, we better enable
378 stdin. Otherwise, any command that calls async_disable_stdin,
379 and then throws, will leave stdin inoperable. */
712af3be 380 async_enable_stdin ();
e0dd0826
PA
381 /* If we long-jumped out of do_one_event, we probably didn't
382 get around to resetting the prompt, which leaves readline
383 in a messed-up state. Reset it here. */
3b12939d 384 current_ui->prompt_state = PROMPT_NEEDED;
76727919 385 gdb::observers::command_error.notify ();
467d8519
TT
386 /* This call looks bizarre, but it is required. If the user
387 entered a command that caused an error,
388 after_char_processing_hook won't be called from
389 rl_callback_read_char_wrapper. Using a cleanup there
390 won't work, since we want this function to be called
391 after a new prompt is printed. */
392 if (after_char_processing_hook)
393 (*after_char_processing_hook) ();
b5a0ac70 394 /* Maybe better to set a flag to be checked somewhere as to
371d5dec 395 whether display the prompt or not. */
b5a0ac70 396 }
492d29ea 397
e0dd0826
PA
398 if (result < 0)
399 break;
b5a0ac70 400 }
085dd6e6 401
371d5dec
MS
402 /* We are done with the event loop. There are no more event sources
403 to listen to. So we exit GDB. */
085dd6e6
JM
404 return;
405}
b5a0ac70
SS
406\f
407
085dd6e6
JM
408/* Wrapper function for create_file_handler, so that the caller
409 doesn't have to know implementation details about the use of poll
371d5dec 410 vs. select. */
c5aa993b 411void
6426a772 412add_file_handler (int fd, handler_func * proc, gdb_client_data client_data)
085dd6e6
JM
413{
414#ifdef HAVE_POLL
44f45770
EZ
415 struct pollfd fds;
416#endif
417
418 if (use_poll)
419 {
420#ifdef HAVE_POLL
371d5dec
MS
421 /* Check to see if poll () is usable. If not, we'll switch to
422 use select. This can happen on systems like
7e5cd2de
EZ
423 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
424 On m68k-motorola-sysv, tty's are not stream-based and not
371d5dec 425 `poll'able. */
7e5cd2de
EZ
426 fds.fd = fd;
427 fds.events = POLLIN;
428 if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
429 use_poll = 0;
44f45770 430#else
8e65ff28 431 internal_error (__FILE__, __LINE__,
e2e0b3e5 432 _("use_poll without HAVE_POLL"));
44f45770
EZ
433#endif /* HAVE_POLL */
434 }
435 if (use_poll)
436 {
437#ifdef HAVE_POLL
438 create_file_handler (fd, POLLIN, proc, client_data);
085dd6e6 439#else
8e65ff28 440 internal_error (__FILE__, __LINE__,
e2e0b3e5 441 _("use_poll without HAVE_POLL"));
085dd6e6 442#endif
44f45770
EZ
443 }
444 else
371d5dec
MS
445 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION,
446 proc, client_data);
085dd6e6
JM
447}
448
b5a0ac70 449/* Add a file handler/descriptor to the list of descriptors we are
371d5dec
MS
450 interested in.
451
452 FD is the file descriptor for the file/stream to be listened to.
453
454 For the poll case, MASK is a combination (OR) of POLLIN,
455 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
456 these are the events we are interested in. If any of them occurs,
457 proc should be called.
458
459 For the select case, MASK is a combination of READABLE, WRITABLE,
460 EXCEPTION. PROC is the procedure that will be called when an event
461 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
462
085dd6e6 463static void
371d5dec
MS
464create_file_handler (int fd, int mask, handler_func * proc,
465 gdb_client_data client_data)
b5a0ac70
SS
466{
467 file_handler *file_ptr;
468
371d5dec
MS
469 /* Do we already have a file handler for this file? (We may be
470 changing its associated procedure). */
b5a0ac70
SS
471 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
472 file_ptr = file_ptr->next_file)
473 {
474 if (file_ptr->fd == fd)
475 break;
476 }
477
371d5dec
MS
478 /* It is a new file descriptor. Add it to the list. Otherwise, just
479 change the data associated with it. */
b5a0ac70
SS
480 if (file_ptr == NULL)
481 {
8d749320 482 file_ptr = XNEW (file_handler);
b5a0ac70
SS
483 file_ptr->fd = fd;
484 file_ptr->ready_mask = 0;
485 file_ptr->next_file = gdb_notifier.first_file_handler;
486 gdb_notifier.first_file_handler = file_ptr;
b5a0ac70 487
05a6c72c
KS
488 if (use_poll)
489 {
b5a0ac70 490#ifdef HAVE_POLL
05a6c72c
KS
491 gdb_notifier.num_fds++;
492 if (gdb_notifier.poll_fds)
493 gdb_notifier.poll_fds =
494 (struct pollfd *) xrealloc (gdb_notifier.poll_fds,
495 (gdb_notifier.num_fds
496 * sizeof (struct pollfd)));
497 else
498 gdb_notifier.poll_fds =
8d749320 499 XNEW (struct pollfd);
05a6c72c
KS
500 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd;
501 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask;
502 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0;
44f45770 503#else
05a6c72c 504 internal_error (__FILE__, __LINE__,
e2e0b3e5 505 _("use_poll without HAVE_POLL"));
44f45770 506#endif /* HAVE_POLL */
05a6c72c 507 }
44f45770 508 else
05a6c72c
KS
509 {
510 if (mask & GDB_READABLE)
511 FD_SET (fd, &gdb_notifier.check_masks[0]);
512 else
513 FD_CLR (fd, &gdb_notifier.check_masks[0]);
514
515 if (mask & GDB_WRITABLE)
516 FD_SET (fd, &gdb_notifier.check_masks[1]);
517 else
518 FD_CLR (fd, &gdb_notifier.check_masks[1]);
519
520 if (mask & GDB_EXCEPTION)
521 FD_SET (fd, &gdb_notifier.check_masks[2]);
522 else
523 FD_CLR (fd, &gdb_notifier.check_masks[2]);
524
525 if (gdb_notifier.num_fds <= fd)
526 gdb_notifier.num_fds = fd + 1;
527 }
44f45770 528 }
05a6c72c
KS
529
530 file_ptr->proc = proc;
531 file_ptr->client_data = client_data;
532 file_ptr->mask = mask;
b5a0ac70
SS
533}
534
4e63d0ac
PA
535/* Return the next file handler to handle, and advance to the next
536 file handler, wrapping around if the end of the list is
537 reached. */
538
539static file_handler *
540get_next_file_handler_to_handle_and_advance (void)
541{
542 file_handler *curr_next;
543
544 /* The first time around, this is still NULL. */
545 if (gdb_notifier.next_file_handler == NULL)
546 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
547
548 curr_next = gdb_notifier.next_file_handler;
549 gdb_assert (curr_next != NULL);
550
551 /* Advance. */
552 gdb_notifier.next_file_handler = curr_next->next_file;
553 /* Wrap around, if necessary. */
554 if (gdb_notifier.next_file_handler == NULL)
555 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
556
557 return curr_next;
558}
559
b5a0ac70 560/* Remove the file descriptor FD from the list of monitored fd's:
371d5dec 561 i.e. we don't care anymore about events on the FD. */
b5a0ac70 562void
c2c6d25f 563delete_file_handler (int fd)
b5a0ac70
SS
564{
565 file_handler *file_ptr, *prev_ptr = NULL;
58a2c44a
EZ
566 int i;
567#ifdef HAVE_POLL
568 int j;
b5a0ac70 569 struct pollfd *new_poll_fds;
b5a0ac70
SS
570#endif
571
371d5dec 572 /* Find the entry for the given file. */
b5a0ac70
SS
573
574 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
575 file_ptr = file_ptr->next_file)
576 {
577 if (file_ptr->fd == fd)
578 break;
579 }
580
581 if (file_ptr == NULL)
582 return;
583
44f45770
EZ
584 if (use_poll)
585 {
b5a0ac70 586#ifdef HAVE_POLL
371d5dec
MS
587 /* Create a new poll_fds array by copying every fd's information
588 but the one we want to get rid of. */
b5a0ac70 589
371d5dec
MS
590 new_poll_fds = (struct pollfd *)
591 xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
b5a0ac70 592
44f45770 593 for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
b5a0ac70 594 {
44f45770
EZ
595 if ((gdb_notifier.poll_fds + i)->fd != fd)
596 {
597 (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd;
598 (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events;
3e43a32a
MS
599 (new_poll_fds + j)->revents
600 = (gdb_notifier.poll_fds + i)->revents;
44f45770
EZ
601 j++;
602 }
b5a0ac70 603 }
b8c9b27d 604 xfree (gdb_notifier.poll_fds);
44f45770
EZ
605 gdb_notifier.poll_fds = new_poll_fds;
606 gdb_notifier.num_fds--;
607#else
8e65ff28 608 internal_error (__FILE__, __LINE__,
e2e0b3e5 609 _("use_poll without HAVE_POLL"));
44f45770 610#endif /* HAVE_POLL */
b5a0ac70 611 }
44f45770
EZ
612 else
613 {
614 if (file_ptr->mask & GDB_READABLE)
615 FD_CLR (fd, &gdb_notifier.check_masks[0]);
616 if (file_ptr->mask & GDB_WRITABLE)
617 FD_CLR (fd, &gdb_notifier.check_masks[1]);
618 if (file_ptr->mask & GDB_EXCEPTION)
619 FD_CLR (fd, &gdb_notifier.check_masks[2]);
b5a0ac70 620
371d5dec 621 /* Find current max fd. */
b5a0ac70 622
44f45770 623 if ((fd + 1) == gdb_notifier.num_fds)
b5a0ac70 624 {
44f45770
EZ
625 gdb_notifier.num_fds--;
626 for (i = gdb_notifier.num_fds; i; i--)
627 {
628 if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
629 || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
630 || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
631 break;
632 }
633 gdb_notifier.num_fds = i;
b5a0ac70
SS
634 }
635 }
b5a0ac70 636
cff3e48b 637 /* Deactivate the file descriptor, by clearing its mask,
371d5dec 638 so that it will not fire again. */
cff3e48b
JM
639
640 file_ptr->mask = 0;
641
4e63d0ac
PA
642 /* If this file handler was going to be the next one to be handled,
643 advance to the next's next, if any. */
644 if (gdb_notifier.next_file_handler == file_ptr)
645 {
646 if (file_ptr->next_file == NULL
647 && file_ptr == gdb_notifier.first_file_handler)
648 gdb_notifier.next_file_handler = NULL;
649 else
650 get_next_file_handler_to_handle_and_advance ();
651 }
652
371d5dec 653 /* Get rid of the file handler in the file handler list. */
b5a0ac70
SS
654 if (file_ptr == gdb_notifier.first_file_handler)
655 gdb_notifier.first_file_handler = file_ptr->next_file;
656 else
657 {
658 for (prev_ptr = gdb_notifier.first_file_handler;
9e0b60a8 659 prev_ptr->next_file != file_ptr;
b5a0ac70
SS
660 prev_ptr = prev_ptr->next_file)
661 ;
662 prev_ptr->next_file = file_ptr->next_file;
663 }
b8c9b27d 664 xfree (file_ptr);
b5a0ac70
SS
665}
666
667/* Handle the given event by calling the procedure associated to the
70b66289
PA
668 corresponding file handler. */
669
b5a0ac70 670static void
70b66289 671handle_file_event (file_handler *file_ptr, int ready_mask)
b5a0ac70 672{
c2c6d25f
JM
673 int mask;
674#ifdef HAVE_POLL
675 int error_mask;
c2c6d25f 676#endif
b5a0ac70 677
b5a0ac70 678 {
b5a0ac70
SS
679 {
680 /* With poll, the ready_mask could have any of three events
371d5dec
MS
681 set to 1: POLLHUP, POLLERR, POLLNVAL. These events
682 cannot be used in the requested event mask (events), but
683 they can be returned in the return mask (revents). We
684 need to check for those event too, and add them to the
685 mask which will be passed to the handler. */
b5a0ac70
SS
686
687 /* See if the desired events (mask) match the received
371d5dec 688 events (ready_mask). */
b5a0ac70 689
44f45770 690 if (use_poll)
c2c6d25f 691 {
44f45770 692#ifdef HAVE_POLL
652c71b4
AS
693 /* POLLHUP means EOF, but can be combined with POLLIN to
694 signal more data to read. */
44f45770 695 error_mask = POLLHUP | POLLERR | POLLNVAL;
70b66289 696 mask = ready_mask & (file_ptr->mask | error_mask);
44f45770 697
652c71b4 698 if ((mask & (POLLERR | POLLNVAL)) != 0)
44f45770 699 {
371d5dec
MS
700 /* Work in progress. We may need to tell somebody
701 what kind of error we had. */
652c71b4 702 if (mask & POLLERR)
3e43a32a
MS
703 printf_unfiltered (_("Error detected on fd %d\n"),
704 file_ptr->fd);
652c71b4 705 if (mask & POLLNVAL)
3e43a32a
MS
706 printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"),
707 file_ptr->fd);
44f45770
EZ
708 file_ptr->error = 1;
709 }
710 else
711 file_ptr->error = 0;
712#else
8e65ff28 713 internal_error (__FILE__, __LINE__,
e2e0b3e5 714 _("use_poll without HAVE_POLL"));
44f45770 715#endif /* HAVE_POLL */
6426a772
JM
716 }
717 else
c2c6d25f 718 {
70b66289 719 if (ready_mask & GDB_EXCEPTION)
44f45770 720 {
3e43a32a
MS
721 printf_unfiltered (_("Exception condition detected "
722 "on fd %d\n"), file_ptr->fd);
44f45770
EZ
723 file_ptr->error = 1;
724 }
725 else
726 file_ptr->error = 0;
70b66289 727 mask = ready_mask & file_ptr->mask;
c2c6d25f 728 }
b5a0ac70 729
371d5dec 730 /* If there was a match, then call the handler. */
b5a0ac70 731 if (mask != 0)
2acceee2 732 (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
b5a0ac70
SS
733 }
734 }
735}
736
70b66289
PA
737/* Wait for new events on the monitored file descriptors. Run the
738 event handler if the first descriptor that is detected by the poll.
739 If BLOCK and if there are no events, this function will block in
740 the call to poll. Return 1 if an event was handled. Return -1 if
741 there are no file descriptors to monitor. Return 1 if an event was
742 handled, otherwise returns 0. */
743
b5a0ac70 744static int
50d01748 745gdb_wait_for_event (int block)
b5a0ac70
SS
746{
747 file_handler *file_ptr;
0f71a2f6 748 int num_found = 0;
b5a0ac70 749
371d5dec 750 /* Make sure all output is done before getting another event. */
7be570e7
JM
751 gdb_flush (gdb_stdout);
752 gdb_flush (gdb_stderr);
753
b5a0ac70
SS
754 if (gdb_notifier.num_fds == 0)
755 return -1;
756
70b66289
PA
757 if (block)
758 update_wait_timeout ();
759
44f45770
EZ
760 if (use_poll)
761 {
b5a0ac70 762#ifdef HAVE_POLL
50d01748
PA
763 int timeout;
764
765 if (block)
766 timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
767 else
768 timeout = 0;
769
770 num_found = poll (gdb_notifier.poll_fds,
771 (unsigned long) gdb_notifier.num_fds, timeout);
44f45770
EZ
772
773 /* Don't print anything if we get out of poll because of a
50d01748 774 signal. */
44f45770 775 if (num_found == -1 && errno != EINTR)
e2e0b3e5 776 perror_with_name (("poll"));
44f45770 777#else
8e65ff28 778 internal_error (__FILE__, __LINE__,
e2e0b3e5 779 _("use_poll without HAVE_POLL"));
44f45770
EZ
780#endif /* HAVE_POLL */
781 }
782 else
c2c6d25f 783 {
50d01748 784 struct timeval select_timeout;
50d01748 785 struct timeval *timeout_p;
d7f9d729 786
50d01748
PA
787 if (block)
788 timeout_p = gdb_notifier.timeout_valid
789 ? &gdb_notifier.select_timeout : NULL;
790 else
791 {
792 memset (&select_timeout, 0, sizeof (select_timeout));
793 timeout_p = &select_timeout;
794 }
795
44f45770
EZ
796 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
797 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
798 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
011825f0
MM
799 num_found = gdb_select (gdb_notifier.num_fds,
800 &gdb_notifier.ready_masks[0],
801 &gdb_notifier.ready_masks[1],
802 &gdb_notifier.ready_masks[2],
50d01748 803 timeout_p);
44f45770 804
371d5dec 805 /* Clear the masks after an error from select. */
44f45770
EZ
806 if (num_found == -1)
807 {
808 FD_ZERO (&gdb_notifier.ready_masks[0]);
809 FD_ZERO (&gdb_notifier.ready_masks[1]);
810 FD_ZERO (&gdb_notifier.ready_masks[2]);
50d01748
PA
811
812 /* Dont print anything if we got a signal, let gdb handle
813 it. */
44f45770 814 if (errno != EINTR)
e2e0b3e5 815 perror_with_name (("select"));
44f45770 816 }
c2c6d25f 817 }
b5a0ac70 818
4e63d0ac
PA
819 /* Avoid looking at poll_fds[i]->revents if no event fired. */
820 if (num_found <= 0)
821 return 0;
822
70b66289
PA
823 /* Run event handlers. We always run just one handler and go back
824 to polling, in case a handler changes the notifier list. Since
825 events for sources we haven't consumed yet wake poll/select
826 immediately, no event is lost. */
b5a0ac70 827
4e63d0ac
PA
828 /* To level the fairness across event descriptors, we handle them in
829 a round-robin-like fashion. The number and order of descriptors
830 may change between invocations, but this is good enough. */
44f45770
EZ
831 if (use_poll)
832 {
b5a0ac70 833#ifdef HAVE_POLL
4e63d0ac
PA
834 int i;
835 int mask;
b5a0ac70 836
4e63d0ac
PA
837 while (1)
838 {
839 if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds)
840 gdb_notifier.next_poll_fds_index = 0;
841 i = gdb_notifier.next_poll_fds_index++;
44f45770 842
4e63d0ac
PA
843 gdb_assert (i < gdb_notifier.num_fds);
844 if ((gdb_notifier.poll_fds + i)->revents)
845 break;
846 }
70b66289 847
4e63d0ac
PA
848 for (file_ptr = gdb_notifier.first_file_handler;
849 file_ptr != NULL;
850 file_ptr = file_ptr->next_file)
851 {
852 if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
853 break;
44f45770 854 }
4e63d0ac
PA
855 gdb_assert (file_ptr != NULL);
856
857 mask = (gdb_notifier.poll_fds + i)->revents;
858 handle_file_event (file_ptr, mask);
859 return 1;
44f45770 860#else
8e65ff28 861 internal_error (__FILE__, __LINE__,
e2e0b3e5 862 _("use_poll without HAVE_POLL"));
44f45770
EZ
863#endif /* HAVE_POLL */
864 }
865 else
866 {
4e63d0ac
PA
867 /* See comment about even source fairness above. */
868 int mask = 0;
869
870 do
b5a0ac70 871 {
4e63d0ac 872 file_ptr = get_next_file_handler_to_handle_and_advance ();
44f45770
EZ
873
874 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
875 mask |= GDB_READABLE;
876 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
877 mask |= GDB_WRITABLE;
878 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
879 mask |= GDB_EXCEPTION;
b5a0ac70 880 }
4e63d0ac
PA
881 while (mask == 0);
882
883 handle_file_event (file_ptr, mask);
884 return 1;
b5a0ac70 885 }
b5a0ac70
SS
886 return 0;
887}
888\f
889
371d5dec 890/* Create an asynchronous handler, allocating memory for it.
b5a0ac70
SS
891 Return a pointer to the newly created handler.
892 This pointer will be used to invoke the handler by
893 invoke_async_signal_handler.
894 PROC is the function to call with CLIENT_DATA argument
371d5dec 895 whenever the handler is invoked. */
b5a0ac70 896async_signal_handler *
3e43a32a
MS
897create_async_signal_handler (sig_handler_func * proc,
898 gdb_client_data client_data)
b5a0ac70
SS
899{
900 async_signal_handler *async_handler_ptr;
901
8d749320 902 async_handler_ptr = XNEW (async_signal_handler);
b5a0ac70
SS
903 async_handler_ptr->ready = 0;
904 async_handler_ptr->next_handler = NULL;
905 async_handler_ptr->proc = proc;
906 async_handler_ptr->client_data = client_data;
907 if (sighandler_list.first_handler == NULL)
908 sighandler_list.first_handler = async_handler_ptr;
909 else
910 sighandler_list.last_handler->next_handler = async_handler_ptr;
911 sighandler_list.last_handler = async_handler_ptr;
912 return async_handler_ptr;
913}
914
371d5dec
MS
915/* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
916 will be used when the handlers are invoked, after we have waited
917 for some event. The caller of this function is the interrupt
918 handler associated with a signal. */
b5a0ac70 919void
6426a772 920mark_async_signal_handler (async_signal_handler * async_handler_ptr)
b5a0ac70 921{
50d01748 922 async_handler_ptr->ready = 1;
5cc3ce8b 923 serial_event_set (async_signal_handlers_serial_event);
b5a0ac70
SS
924}
925
abc56d60
PA
926/* See event-loop.h. */
927
928void
929clear_async_signal_handler (async_signal_handler *async_handler_ptr)
930{
931 async_handler_ptr->ready = 0;
932}
933
934/* See event-loop.h. */
935
936int
937async_signal_handler_is_marked (async_signal_handler *async_handler_ptr)
938{
939 return async_handler_ptr->ready;
940}
941
50d01748
PA
942/* Call all the handlers that are ready. Returns true if any was
943 indeed ready. */
5cc3ce8b 944
50d01748
PA
945static int
946invoke_async_signal_handlers (void)
b5a0ac70
SS
947{
948 async_signal_handler *async_handler_ptr;
50d01748 949 int any_ready = 0;
b5a0ac70 950
5cc3ce8b
PA
951 /* We're going to handle all pending signals, so no need to wake up
952 the event loop again the next time around. Note this must be
953 cleared _before_ calling the callbacks, to avoid races. */
954 serial_event_clear (async_signal_handlers_serial_event);
955
956 /* Invoke all ready handlers. */
b5a0ac70
SS
957
958 while (1)
959 {
c5aa993b 960 for (async_handler_ptr = sighandler_list.first_handler;
b5a0ac70
SS
961 async_handler_ptr != NULL;
962 async_handler_ptr = async_handler_ptr->next_handler)
963 {
964 if (async_handler_ptr->ready)
965 break;
966 }
967 if (async_handler_ptr == NULL)
968 break;
50d01748 969 any_ready = 1;
b5a0ac70 970 async_handler_ptr->ready = 0;
7c36c34e
PA
971 /* Async signal handlers have no connection to whichever was the
972 current UI, and thus always run on the main one. */
973 current_ui = main_ui;
b5a0ac70
SS
974 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
975 }
976
50d01748 977 return any_ready;
b5a0ac70
SS
978}
979
371d5dec 980/* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
b5a0ac70
SS
981 Free the space allocated for it. */
982void
6426a772 983delete_async_signal_handler (async_signal_handler ** async_handler_ptr)
b5a0ac70
SS
984{
985 async_signal_handler *prev_ptr;
986
43ff13b4 987 if (sighandler_list.first_handler == (*async_handler_ptr))
b5a0ac70 988 {
43ff13b4 989 sighandler_list.first_handler = (*async_handler_ptr)->next_handler;
b5a0ac70
SS
990 if (sighandler_list.first_handler == NULL)
991 sighandler_list.last_handler = NULL;
992 }
993 else
994 {
995 prev_ptr = sighandler_list.first_handler;
32107cd5 996 while (prev_ptr && prev_ptr->next_handler != (*async_handler_ptr))
b5a0ac70 997 prev_ptr = prev_ptr->next_handler;
60bc018f 998 gdb_assert (prev_ptr);
43ff13b4
JM
999 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
1000 if (sighandler_list.last_handler == (*async_handler_ptr))
b5a0ac70
SS
1001 sighandler_list.last_handler = prev_ptr;
1002 }
b8c9b27d 1003 xfree ((*async_handler_ptr));
43ff13b4 1004 (*async_handler_ptr) = NULL;
b5a0ac70
SS
1005}
1006
50d01748
PA
1007/* Create an asynchronous event handler, allocating memory for it.
1008 Return a pointer to the newly created handler. PROC is the
1009 function to call with CLIENT_DATA argument whenever the handler is
1010 invoked. */
1011async_event_handler *
1012create_async_event_handler (async_event_handler_func *proc,
1013 gdb_client_data client_data)
1014{
1015 async_event_handler *h;
1016
8d749320 1017 h = XNEW (struct async_event_handler);
50d01748
PA
1018 h->ready = 0;
1019 h->next_handler = NULL;
1020 h->proc = proc;
1021 h->client_data = client_data;
1022 if (async_event_handler_list.first_handler == NULL)
1023 async_event_handler_list.first_handler = h;
1024 else
1025 async_event_handler_list.last_handler->next_handler = h;
1026 async_event_handler_list.last_handler = h;
1027 return h;
1028}
1029
1030/* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
1031 will be used by gdb_do_one_event. The caller will be whoever
1032 created the event source, and wants to signal that the event is
1033 ready to be handled. */
1034void
1035mark_async_event_handler (async_event_handler *async_handler_ptr)
1036{
1037 async_handler_ptr->ready = 1;
1038}
1039
b7d2e916
PA
1040/* See event-loop.h. */
1041
1042void
1043clear_async_event_handler (async_event_handler *async_handler_ptr)
1044{
1045 async_handler_ptr->ready = 0;
1046}
1047
70b66289
PA
1048/* Check if asynchronous event handlers are ready, and call the
1049 handler function for one that is. */
50d01748 1050
70b66289 1051static int
50d01748
PA
1052check_async_event_handlers (void)
1053{
1054 async_event_handler *async_handler_ptr;
50d01748
PA
1055
1056 for (async_handler_ptr = async_event_handler_list.first_handler;
1057 async_handler_ptr != NULL;
1058 async_handler_ptr = async_handler_ptr->next_handler)
1059 {
1060 if (async_handler_ptr->ready)
1061 {
1062 async_handler_ptr->ready = 0;
70b66289
PA
1063 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
1064 return 1;
50d01748
PA
1065 }
1066 }
70b66289
PA
1067
1068 return 0;
50d01748
PA
1069}
1070
1071/* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
1072 Free the space allocated for it. */
1073void
1074delete_async_event_handler (async_event_handler **async_handler_ptr)
b5a0ac70 1075{
50d01748
PA
1076 async_event_handler *prev_ptr;
1077
1078 if (async_event_handler_list.first_handler == *async_handler_ptr)
1079 {
3e43a32a
MS
1080 async_event_handler_list.first_handler
1081 = (*async_handler_ptr)->next_handler;
50d01748
PA
1082 if (async_event_handler_list.first_handler == NULL)
1083 async_event_handler_list.last_handler = NULL;
1084 }
1085 else
1086 {
1087 prev_ptr = async_event_handler_list.first_handler;
1088 while (prev_ptr && prev_ptr->next_handler != *async_handler_ptr)
1089 prev_ptr = prev_ptr->next_handler;
60bc018f 1090 gdb_assert (prev_ptr);
50d01748
PA
1091 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
1092 if (async_event_handler_list.last_handler == (*async_handler_ptr))
1093 async_event_handler_list.last_handler = prev_ptr;
1094 }
1095 xfree (*async_handler_ptr);
1096 *async_handler_ptr = NULL;
b5a0ac70 1097}
c2c6d25f 1098
dcb07cfa
PA
1099/* Create a timer that will expire in MS milliseconds from now. When
1100 the timer is ready, PROC will be executed. At creation, the timer
1101 is added to the timers queue. This queue is kept sorted in order
1102 of increasing timers. Return a handle to the timer struct. */
1103
c2c6d25f 1104int
dcb07cfa 1105create_timer (int ms, timer_handler_func *proc,
371d5dec 1106 gdb_client_data client_data)
c2c6d25f 1107{
dcb07cfa 1108 using namespace std::chrono;
c2c6d25f 1109 struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
6426a772 1110
dcb07cfa 1111 steady_clock::time_point time_now = steady_clock::now ();
c2c6d25f 1112
dcb07cfa
PA
1113 timer_ptr = new gdb_timer ();
1114 timer_ptr->when = time_now + milliseconds (ms);
c2c6d25f
JM
1115 timer_ptr->proc = proc;
1116 timer_ptr->client_data = client_data;
6426a772 1117 timer_list.num_timers++;
c2c6d25f
JM
1118 timer_ptr->timer_id = timer_list.num_timers;
1119
1120 /* Now add the timer to the timer queue, making sure it is sorted in
371d5dec 1121 increasing order of expiration. */
c2c6d25f 1122
6426a772
JM
1123 for (timer_index = timer_list.first_timer;
1124 timer_index != NULL;
c2c6d25f
JM
1125 timer_index = timer_index->next)
1126 {
dcb07cfa 1127 if (timer_index->when > timer_ptr->when)
c2c6d25f
JM
1128 break;
1129 }
6426a772 1130
c2c6d25f
JM
1131 if (timer_index == timer_list.first_timer)
1132 {
1133 timer_ptr->next = timer_list.first_timer;
1134 timer_list.first_timer = timer_ptr;
1135
1136 }
1137 else
1138 {
6426a772
JM
1139 for (prev_timer = timer_list.first_timer;
1140 prev_timer->next != timer_index;
c2c6d25f
JM
1141 prev_timer = prev_timer->next)
1142 ;
6426a772 1143
c2c6d25f
JM
1144 prev_timer->next = timer_ptr;
1145 timer_ptr->next = timer_index;
1146 }
1147
1148 gdb_notifier.timeout_valid = 0;
1149 return timer_ptr->timer_id;
1150}
1151
1152/* There is a chance that the creator of the timer wants to get rid of
371d5dec 1153 it before it expires. */
c2c6d25f
JM
1154void
1155delete_timer (int id)
1156{
1157 struct gdb_timer *timer_ptr, *prev_timer = NULL;
1158
371d5dec 1159 /* Find the entry for the given timer. */
c2c6d25f
JM
1160
1161 for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
1162 timer_ptr = timer_ptr->next)
1163 {
1164 if (timer_ptr->timer_id == id)
1165 break;
1166 }
1167
1168 if (timer_ptr == NULL)
1169 return;
371d5dec 1170 /* Get rid of the timer in the timer list. */
c2c6d25f
JM
1171 if (timer_ptr == timer_list.first_timer)
1172 timer_list.first_timer = timer_ptr->next;
1173 else
1174 {
1175 for (prev_timer = timer_list.first_timer;
1176 prev_timer->next != timer_ptr;
1177 prev_timer = prev_timer->next)
1178 ;
1179 prev_timer->next = timer_ptr->next;
1180 }
dcb07cfa 1181 delete timer_ptr;
c2c6d25f
JM
1182
1183 gdb_notifier.timeout_valid = 0;
1184}
1185
dcb07cfa
PA
1186/* Convert a std::chrono duration to a struct timeval. */
1187
1188template<typename Duration>
1189static struct timeval
1190duration_cast_timeval (const Duration &d)
1191{
1192 using namespace std::chrono;
1193 seconds sec = duration_cast<seconds> (d);
1194 microseconds msec = duration_cast<microseconds> (d - sec);
1195
1196 struct timeval tv;
1197 tv.tv_sec = sec.count ();
1198 tv.tv_usec = msec.count ();
1199 return tv;
1200}
1201
70b66289
PA
1202/* Update the timeout for the select() or poll(). Returns true if the
1203 timer has already expired, false otherwise. */
6426a772 1204
70b66289
PA
1205static int
1206update_wait_timeout (void)
c2c6d25f 1207{
2acceee2 1208 if (timer_list.first_timer != NULL)
c2c6d25f 1209 {
dcb07cfa
PA
1210 using namespace std::chrono;
1211 steady_clock::time_point time_now = steady_clock::now ();
1212 struct timeval timeout;
6426a772 1213
dcb07cfa 1214 if (timer_list.first_timer->when < time_now)
c2c6d25f 1215 {
70b66289 1216 /* It expired already. */
dcb07cfa
PA
1217 timeout.tv_sec = 0;
1218 timeout.tv_usec = 0;
1219 }
1220 else
1221 {
1222 steady_clock::duration d = timer_list.first_timer->when - time_now;
1223 timeout = duration_cast_timeval (d);
c2c6d25f
JM
1224 }
1225
70b66289 1226 /* Update the timeout for select/ poll. */
44f45770
EZ
1227 if (use_poll)
1228 {
c2c6d25f 1229#ifdef HAVE_POLL
dcb07cfa 1230 gdb_notifier.poll_timeout = timeout.tv_sec * 1000;
c2c6d25f 1231#else
8e65ff28 1232 internal_error (__FILE__, __LINE__,
e2e0b3e5 1233 _("use_poll without HAVE_POLL"));
44f45770
EZ
1234#endif /* HAVE_POLL */
1235 }
1236 else
1237 {
dcb07cfa
PA
1238 gdb_notifier.select_timeout.tv_sec = timeout.tv_sec;
1239 gdb_notifier.select_timeout.tv_usec = timeout.tv_usec;
44f45770 1240 }
c2c6d25f 1241 gdb_notifier.timeout_valid = 1;
70b66289 1242
dcb07cfa 1243 if (timer_list.first_timer->when < time_now)
70b66289 1244 return 1;
c2c6d25f 1245 }
6426a772 1246 else
c2c6d25f 1247 gdb_notifier.timeout_valid = 0;
70b66289
PA
1248
1249 return 0;
1250}
1251
1252/* Check whether a timer in the timers queue is ready. If a timer is
1253 ready, call its handler and return. Update the timeout for the
1254 select() or poll() as well. Return 1 if an event was handled,
1255 otherwise returns 0.*/
1256
1257static int
1258poll_timers (void)
1259{
1260 if (update_wait_timeout ())
1261 {
1262 struct gdb_timer *timer_ptr = timer_list.first_timer;
1263 timer_handler_func *proc = timer_ptr->proc;
1264 gdb_client_data client_data = timer_ptr->client_data;
1265
1266 /* Get rid of the timer from the beginning of the list. */
1267 timer_list.first_timer = timer_ptr->next;
1268
1269 /* Delete the timer before calling the callback, not after, in
1270 case the callback itself decides to try deleting the timer
1271 too. */
0e05cf3a 1272 delete timer_ptr;
70b66289
PA
1273
1274 /* Call the procedure associated with that timer. */
1275 (proc) (client_data);
1276
1277 return 1;
1278 }
1279
1280 return 0;
c2c6d25f 1281}
This page took 1.519784 seconds and 4 git commands to generate.