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