gprofng: fix build with -mx32
[binutils-gdb/blckswan.git] / gdbsupport / event-loop.cc
blobf078c1278a85da76435ea113ca36773212d2e219
1 /* Event loop machinery for GDB, the GNU debugger.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbsupport/common-defs.h"
21 #include "gdbsupport/event-loop.h"
23 #include <chrono>
25 #ifdef HAVE_POLL
26 #if defined (HAVE_POLL_H)
27 #include <poll.h>
28 #elif defined (HAVE_SYS_POLL_H)
29 #include <sys/poll.h>
30 #endif
31 #endif
33 #include <sys/types.h>
34 #include "gdbsupport/gdb_sys_time.h"
35 #include "gdbsupport/gdb_select.h"
37 /* See event-loop.h. */
39 debug_event_loop_kind debug_event_loop;
41 /* Tell create_file_handler what events we are interested in.
42 This is used by the select version of the event loop. */
44 #define GDB_READABLE (1<<1)
45 #define GDB_WRITABLE (1<<2)
46 #define GDB_EXCEPTION (1<<3)
48 /* Information about each file descriptor we register with the event
49 loop. */
51 struct file_handler
53 /* File descriptor. */
54 int fd;
56 /* Events we want to monitor: POLLIN, etc. */
57 int mask;
59 /* Events that have been seen since the last time. */
60 int ready_mask;
62 /* Procedure to call when fd is ready. */
63 handler_func *proc;
65 /* Argument to pass to proc. */
66 gdb_client_data client_data;
68 /* User-friendly name of this handler. */
69 std::string name;
71 /* If set, this file descriptor is used for a user interface. */
72 bool is_ui;
74 /* Was an error detected on this fd? */
75 int error;
77 /* Next registered file descriptor. */
78 struct file_handler *next_file;
81 #ifdef HAVE_POLL
82 /* Do we use poll or select? Some systems have poll, but then it's
83 not useable with all kinds of files. We probe that whenever a new
84 file handler is added. */
85 static bool use_poll = true;
86 #endif
88 #ifdef USE_WIN32API
89 #include <windows.h>
90 #include <io.h>
91 #endif
93 /* Gdb_notifier is just a list of file descriptors gdb is interested in.
94 These are the input file descriptor, and the target file
95 descriptor. We have two flavors of the notifier, one for platforms
96 that have the POLL function, the other for those that don't, and
97 only support SELECT. Each of the elements in the gdb_notifier list is
98 basically a description of what kind of events gdb is interested
99 in, for each fd. */
101 static struct
103 /* Ptr to head of file handler list. */
104 file_handler *first_file_handler;
106 /* Next file handler to handle, for the select variant. To level
107 the fairness across event sources, we serve file handlers in a
108 round-robin-like fashion. The number and order of the polled
109 file handlers may change between invocations, but this is good
110 enough. */
111 file_handler *next_file_handler;
113 #ifdef HAVE_POLL
114 /* Ptr to array of pollfd structures. */
115 struct pollfd *poll_fds;
117 /* Next file descriptor to handle, for the poll variant. To level
118 the fairness across event sources, we poll the file descriptors
119 in a round-robin-like fashion. The number and order of the
120 polled file descriptors may change between invocations, but
121 this is good enough. */
122 int next_poll_fds_index;
124 /* Timeout in milliseconds for calls to poll(). */
125 int poll_timeout;
126 #endif
128 /* Masks to be used in the next call to select.
129 Bits are set in response to calls to create_file_handler. */
130 fd_set check_masks[3];
132 /* What file descriptors were found ready by select. */
133 fd_set ready_masks[3];
135 /* Number of file descriptors to monitor (for poll). */
136 /* Number of valid bits (highest fd value + 1) (for select). */
137 int num_fds;
139 /* Time structure for calls to select(). */
140 struct timeval select_timeout;
142 /* Flag to tell whether the timeout should be used. */
143 int timeout_valid;
145 gdb_notifier;
147 /* Structure associated with a timer. PROC will be executed at the
148 first occasion after WHEN. */
149 struct gdb_timer
151 std::chrono::steady_clock::time_point when;
152 int timer_id;
153 struct gdb_timer *next;
154 timer_handler_func *proc; /* Function to call to do the work. */
155 gdb_client_data client_data; /* Argument to async_handler_func. */
158 /* List of currently active timers. It is sorted in order of
159 increasing timers. */
160 static struct
162 /* Pointer to first in timer list. */
163 struct gdb_timer *first_timer;
165 /* Id of the last timer created. */
166 int num_timers;
168 timer_list;
170 static void create_file_handler (int fd, int mask, handler_func *proc,
171 gdb_client_data client_data,
172 std::string &&name, bool is_ui);
173 static int gdb_wait_for_event (int);
174 static int update_wait_timeout (void);
175 static int poll_timers (void);
177 /* Process one high level event. If nothing is ready at this time,
178 wait for something to happen (via gdb_wait_for_event), then process
179 it. Returns >0 if something was done otherwise returns <0 (this
180 can happen if there are no event sources to wait for). */
183 gdb_do_one_event (void)
185 static int event_source_head = 0;
186 const int number_of_sources = 3;
187 int current = 0;
189 /* First let's see if there are any asynchronous signal handlers
190 that are ready. These would be the result of invoking any of the
191 signal handlers. */
192 if (invoke_async_signal_handlers ())
193 return 1;
195 /* To level the fairness across event sources, we poll them in a
196 round-robin fashion. */
197 for (current = 0; current < number_of_sources; current++)
199 int res;
201 switch (event_source_head)
203 case 0:
204 /* Are any timers that are ready? */
205 res = poll_timers ();
206 break;
207 case 1:
208 /* Are there events already waiting to be collected on the
209 monitored file descriptors? */
210 res = gdb_wait_for_event (0);
211 break;
212 case 2:
213 /* Are there any asynchronous event handlers ready? */
214 res = check_async_event_handlers ();
215 break;
216 default:
217 internal_error (__FILE__, __LINE__,
218 "unexpected event_source_head %d",
219 event_source_head);
222 event_source_head++;
223 if (event_source_head == number_of_sources)
224 event_source_head = 0;
226 if (res > 0)
227 return 1;
230 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
231 we should get out because this means that there are no event
232 sources left. This will make the event loop stop, and the
233 application exit. */
235 if (gdb_wait_for_event (1) < 0)
236 return -1;
238 /* If gdb_wait_for_event has returned 1, it means that one event has
239 been handled. We break out of the loop. */
240 return 1;
243 /* See event-loop.h */
245 void
246 add_file_handler (int fd, handler_func *proc, gdb_client_data client_data,
247 std::string &&name, bool is_ui)
249 #ifdef HAVE_POLL
250 if (use_poll)
252 struct pollfd fds;
254 /* Check to see if poll () is usable. If not, we'll switch to
255 use select. This can happen on systems like
256 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
257 On m68k-motorola-sysv, tty's are not stream-based and not
258 `poll'able. */
259 fds.fd = fd;
260 fds.events = POLLIN;
261 if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
262 use_poll = false;
264 if (use_poll)
266 create_file_handler (fd, POLLIN, proc, client_data, std::move (name),
267 is_ui);
269 else
270 #endif /* HAVE_POLL */
271 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION,
272 proc, client_data, std::move (name), is_ui);
275 /* Helper for add_file_handler.
277 For the poll case, MASK is a combination (OR) of POLLIN,
278 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
279 these are the events we are interested in. If any of them occurs,
280 proc should be called.
282 For the select case, MASK is a combination of READABLE, WRITABLE,
283 EXCEPTION. PROC is the procedure that will be called when an event
284 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
286 static void
287 create_file_handler (int fd, int mask, handler_func * proc,
288 gdb_client_data client_data, std::string &&name,
289 bool is_ui)
291 file_handler *file_ptr;
293 /* Do we already have a file handler for this file? (We may be
294 changing its associated procedure). */
295 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
296 file_ptr = file_ptr->next_file)
298 if (file_ptr->fd == fd)
299 break;
302 /* It is a new file descriptor. Add it to the list. Otherwise, just
303 change the data associated with it. */
304 if (file_ptr == NULL)
306 file_ptr = new file_handler;
307 file_ptr->fd = fd;
308 file_ptr->ready_mask = 0;
309 file_ptr->next_file = gdb_notifier.first_file_handler;
310 gdb_notifier.first_file_handler = file_ptr;
312 #ifdef HAVE_POLL
313 if (use_poll)
315 gdb_notifier.num_fds++;
316 if (gdb_notifier.poll_fds)
317 gdb_notifier.poll_fds =
318 (struct pollfd *) xrealloc (gdb_notifier.poll_fds,
319 (gdb_notifier.num_fds
320 * sizeof (struct pollfd)));
321 else
322 gdb_notifier.poll_fds =
323 XNEW (struct pollfd);
324 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd;
325 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask;
326 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0;
328 else
329 #endif /* HAVE_POLL */
331 if (mask & GDB_READABLE)
332 FD_SET (fd, &gdb_notifier.check_masks[0]);
333 else
334 FD_CLR (fd, &gdb_notifier.check_masks[0]);
336 if (mask & GDB_WRITABLE)
337 FD_SET (fd, &gdb_notifier.check_masks[1]);
338 else
339 FD_CLR (fd, &gdb_notifier.check_masks[1]);
341 if (mask & GDB_EXCEPTION)
342 FD_SET (fd, &gdb_notifier.check_masks[2]);
343 else
344 FD_CLR (fd, &gdb_notifier.check_masks[2]);
346 if (gdb_notifier.num_fds <= fd)
347 gdb_notifier.num_fds = fd + 1;
351 file_ptr->proc = proc;
352 file_ptr->client_data = client_data;
353 file_ptr->mask = mask;
354 file_ptr->name = std::move (name);
355 file_ptr->is_ui = is_ui;
358 /* Return the next file handler to handle, and advance to the next
359 file handler, wrapping around if the end of the list is
360 reached. */
362 static file_handler *
363 get_next_file_handler_to_handle_and_advance (void)
365 file_handler *curr_next;
367 /* The first time around, this is still NULL. */
368 if (gdb_notifier.next_file_handler == NULL)
369 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
371 curr_next = gdb_notifier.next_file_handler;
372 gdb_assert (curr_next != NULL);
374 /* Advance. */
375 gdb_notifier.next_file_handler = curr_next->next_file;
376 /* Wrap around, if necessary. */
377 if (gdb_notifier.next_file_handler == NULL)
378 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
380 return curr_next;
383 /* Remove the file descriptor FD from the list of monitored fd's:
384 i.e. we don't care anymore about events on the FD. */
385 void
386 delete_file_handler (int fd)
388 file_handler *file_ptr, *prev_ptr = NULL;
389 int i;
391 /* Find the entry for the given file. */
393 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
394 file_ptr = file_ptr->next_file)
396 if (file_ptr->fd == fd)
397 break;
400 if (file_ptr == NULL)
401 return;
403 #ifdef HAVE_POLL
404 if (use_poll)
406 int j;
407 struct pollfd *new_poll_fds;
409 /* Create a new poll_fds array by copying every fd's information
410 but the one we want to get rid of. */
412 new_poll_fds = (struct pollfd *)
413 xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
415 for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
417 if ((gdb_notifier.poll_fds + i)->fd != fd)
419 (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd;
420 (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events;
421 (new_poll_fds + j)->revents
422 = (gdb_notifier.poll_fds + i)->revents;
423 j++;
426 xfree (gdb_notifier.poll_fds);
427 gdb_notifier.poll_fds = new_poll_fds;
428 gdb_notifier.num_fds--;
430 else
431 #endif /* HAVE_POLL */
433 if (file_ptr->mask & GDB_READABLE)
434 FD_CLR (fd, &gdb_notifier.check_masks[0]);
435 if (file_ptr->mask & GDB_WRITABLE)
436 FD_CLR (fd, &gdb_notifier.check_masks[1]);
437 if (file_ptr->mask & GDB_EXCEPTION)
438 FD_CLR (fd, &gdb_notifier.check_masks[2]);
440 /* Find current max fd. */
442 if ((fd + 1) == gdb_notifier.num_fds)
444 gdb_notifier.num_fds--;
445 for (i = gdb_notifier.num_fds; i; i--)
447 if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
448 || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
449 || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
450 break;
452 gdb_notifier.num_fds = i;
456 /* Deactivate the file descriptor, by clearing its mask,
457 so that it will not fire again. */
459 file_ptr->mask = 0;
461 /* If this file handler was going to be the next one to be handled,
462 advance to the next's next, if any. */
463 if (gdb_notifier.next_file_handler == file_ptr)
465 if (file_ptr->next_file == NULL
466 && file_ptr == gdb_notifier.first_file_handler)
467 gdb_notifier.next_file_handler = NULL;
468 else
469 get_next_file_handler_to_handle_and_advance ();
472 /* Get rid of the file handler in the file handler list. */
473 if (file_ptr == gdb_notifier.first_file_handler)
474 gdb_notifier.first_file_handler = file_ptr->next_file;
475 else
477 for (prev_ptr = gdb_notifier.first_file_handler;
478 prev_ptr->next_file != file_ptr;
479 prev_ptr = prev_ptr->next_file)
481 prev_ptr->next_file = file_ptr->next_file;
484 delete file_ptr;
487 /* Handle the given event by calling the procedure associated to the
488 corresponding file handler. */
490 static void
491 handle_file_event (file_handler *file_ptr, int ready_mask)
493 int mask;
495 /* See if the desired events (mask) match the received events
496 (ready_mask). */
498 #ifdef HAVE_POLL
499 if (use_poll)
501 int error_mask;
503 /* With poll, the ready_mask could have any of three events set
504 to 1: POLLHUP, POLLERR, POLLNVAL. These events cannot be
505 used in the requested event mask (events), but they can be
506 returned in the return mask (revents). We need to check for
507 those event too, and add them to the mask which will be
508 passed to the handler. */
510 /* POLLHUP means EOF, but can be combined with POLLIN to
511 signal more data to read. */
512 error_mask = POLLHUP | POLLERR | POLLNVAL;
513 mask = ready_mask & (file_ptr->mask | error_mask);
515 if ((mask & (POLLERR | POLLNVAL)) != 0)
517 /* Work in progress. We may need to tell somebody
518 what kind of error we had. */
519 if (mask & POLLERR)
520 warning (_("Error detected on fd %d"), file_ptr->fd);
521 if (mask & POLLNVAL)
522 warning (_("Invalid or non-`poll'able fd %d"),
523 file_ptr->fd);
524 file_ptr->error = 1;
526 else
527 file_ptr->error = 0;
529 else
530 #endif /* HAVE_POLL */
532 if (ready_mask & GDB_EXCEPTION)
534 warning (_("Exception condition detected on fd %d"),
535 file_ptr->fd);
536 file_ptr->error = 1;
538 else
539 file_ptr->error = 0;
540 mask = ready_mask & file_ptr->mask;
543 /* If there was a match, then call the handler. */
544 if (mask != 0)
546 event_loop_ui_debug_printf (file_ptr->is_ui,
547 "invoking fd file handler `%s`",
548 file_ptr->name.c_str ());
549 file_ptr->proc (file_ptr->error, file_ptr->client_data);
553 /* Wait for new events on the monitored file descriptors. Run the
554 event handler if the first descriptor that is detected by the poll.
555 If BLOCK and if there are no events, this function will block in
556 the call to poll. Return 1 if an event was handled. Return -1 if
557 there are no file descriptors to monitor. Return 1 if an event was
558 handled, otherwise returns 0. */
560 static int
561 gdb_wait_for_event (int block)
563 file_handler *file_ptr;
564 int num_found = 0;
566 /* Make sure all output is done before getting another event. */
567 flush_streams ();
569 if (gdb_notifier.num_fds == 0)
570 return -1;
572 if (block)
573 update_wait_timeout ();
575 #ifdef HAVE_POLL
576 if (use_poll)
578 int timeout;
580 if (block)
581 timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
582 else
583 timeout = 0;
585 num_found = poll (gdb_notifier.poll_fds,
586 (unsigned long) gdb_notifier.num_fds, timeout);
588 /* Don't print anything if we get out of poll because of a
589 signal. */
590 if (num_found == -1 && errno != EINTR)
591 perror_with_name (("poll"));
593 else
594 #endif /* HAVE_POLL */
596 struct timeval select_timeout;
597 struct timeval *timeout_p;
599 if (block)
600 timeout_p = gdb_notifier.timeout_valid
601 ? &gdb_notifier.select_timeout : NULL;
602 else
604 memset (&select_timeout, 0, sizeof (select_timeout));
605 timeout_p = &select_timeout;
608 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
609 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
610 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
611 num_found = gdb_select (gdb_notifier.num_fds,
612 &gdb_notifier.ready_masks[0],
613 &gdb_notifier.ready_masks[1],
614 &gdb_notifier.ready_masks[2],
615 timeout_p);
617 /* Clear the masks after an error from select. */
618 if (num_found == -1)
620 FD_ZERO (&gdb_notifier.ready_masks[0]);
621 FD_ZERO (&gdb_notifier.ready_masks[1]);
622 FD_ZERO (&gdb_notifier.ready_masks[2]);
624 /* Dont print anything if we got a signal, let gdb handle
625 it. */
626 if (errno != EINTR)
627 perror_with_name (("select"));
631 /* Avoid looking at poll_fds[i]->revents if no event fired. */
632 if (num_found <= 0)
633 return 0;
635 /* Run event handlers. We always run just one handler and go back
636 to polling, in case a handler changes the notifier list. Since
637 events for sources we haven't consumed yet wake poll/select
638 immediately, no event is lost. */
640 /* To level the fairness across event descriptors, we handle them in
641 a round-robin-like fashion. The number and order of descriptors
642 may change between invocations, but this is good enough. */
643 #ifdef HAVE_POLL
644 if (use_poll)
646 int i;
647 int mask;
649 while (1)
651 if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds)
652 gdb_notifier.next_poll_fds_index = 0;
653 i = gdb_notifier.next_poll_fds_index++;
655 gdb_assert (i < gdb_notifier.num_fds);
656 if ((gdb_notifier.poll_fds + i)->revents)
657 break;
660 for (file_ptr = gdb_notifier.first_file_handler;
661 file_ptr != NULL;
662 file_ptr = file_ptr->next_file)
664 if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
665 break;
667 gdb_assert (file_ptr != NULL);
669 mask = (gdb_notifier.poll_fds + i)->revents;
670 handle_file_event (file_ptr, mask);
671 return 1;
673 else
674 #endif /* HAVE_POLL */
676 /* See comment about even source fairness above. */
677 int mask = 0;
681 file_ptr = get_next_file_handler_to_handle_and_advance ();
683 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
684 mask |= GDB_READABLE;
685 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
686 mask |= GDB_WRITABLE;
687 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
688 mask |= GDB_EXCEPTION;
690 while (mask == 0);
692 handle_file_event (file_ptr, mask);
693 return 1;
695 return 0;
698 /* Create a timer that will expire in MS milliseconds from now. When
699 the timer is ready, PROC will be executed. At creation, the timer
700 is added to the timers queue. This queue is kept sorted in order
701 of increasing timers. Return a handle to the timer struct. */
704 create_timer (int ms, timer_handler_func *proc,
705 gdb_client_data client_data)
707 using namespace std::chrono;
708 struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
710 steady_clock::time_point time_now = steady_clock::now ();
712 timer_ptr = new gdb_timer ();
713 timer_ptr->when = time_now + milliseconds (ms);
714 timer_ptr->proc = proc;
715 timer_ptr->client_data = client_data;
716 timer_list.num_timers++;
717 timer_ptr->timer_id = timer_list.num_timers;
719 /* Now add the timer to the timer queue, making sure it is sorted in
720 increasing order of expiration. */
722 for (timer_index = timer_list.first_timer;
723 timer_index != NULL;
724 timer_index = timer_index->next)
726 if (timer_index->when > timer_ptr->when)
727 break;
730 if (timer_index == timer_list.first_timer)
732 timer_ptr->next = timer_list.first_timer;
733 timer_list.first_timer = timer_ptr;
736 else
738 for (prev_timer = timer_list.first_timer;
739 prev_timer->next != timer_index;
740 prev_timer = prev_timer->next)
743 prev_timer->next = timer_ptr;
744 timer_ptr->next = timer_index;
747 gdb_notifier.timeout_valid = 0;
748 return timer_ptr->timer_id;
751 /* There is a chance that the creator of the timer wants to get rid of
752 it before it expires. */
753 void
754 delete_timer (int id)
756 struct gdb_timer *timer_ptr, *prev_timer = NULL;
758 /* Find the entry for the given timer. */
760 for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
761 timer_ptr = timer_ptr->next)
763 if (timer_ptr->timer_id == id)
764 break;
767 if (timer_ptr == NULL)
768 return;
769 /* Get rid of the timer in the timer list. */
770 if (timer_ptr == timer_list.first_timer)
771 timer_list.first_timer = timer_ptr->next;
772 else
774 for (prev_timer = timer_list.first_timer;
775 prev_timer->next != timer_ptr;
776 prev_timer = prev_timer->next)
778 prev_timer->next = timer_ptr->next;
780 delete timer_ptr;
782 gdb_notifier.timeout_valid = 0;
785 /* Convert a std::chrono duration to a struct timeval. */
787 template<typename Duration>
788 static struct timeval
789 duration_cast_timeval (const Duration &d)
791 using namespace std::chrono;
792 seconds sec = duration_cast<seconds> (d);
793 microseconds msec = duration_cast<microseconds> (d - sec);
795 struct timeval tv;
796 tv.tv_sec = sec.count ();
797 tv.tv_usec = msec.count ();
798 return tv;
801 /* Update the timeout for the select() or poll(). Returns true if the
802 timer has already expired, false otherwise. */
804 static int
805 update_wait_timeout (void)
807 if (timer_list.first_timer != NULL)
809 using namespace std::chrono;
810 steady_clock::time_point time_now = steady_clock::now ();
811 struct timeval timeout;
813 if (timer_list.first_timer->when < time_now)
815 /* It expired already. */
816 timeout.tv_sec = 0;
817 timeout.tv_usec = 0;
819 else
821 steady_clock::duration d = timer_list.first_timer->when - time_now;
822 timeout = duration_cast_timeval (d);
825 /* Update the timeout for select/ poll. */
826 #ifdef HAVE_POLL
827 if (use_poll)
828 gdb_notifier.poll_timeout = timeout.tv_sec * 1000;
829 else
830 #endif /* HAVE_POLL */
832 gdb_notifier.select_timeout.tv_sec = timeout.tv_sec;
833 gdb_notifier.select_timeout.tv_usec = timeout.tv_usec;
835 gdb_notifier.timeout_valid = 1;
837 if (timer_list.first_timer->when < time_now)
838 return 1;
840 else
841 gdb_notifier.timeout_valid = 0;
843 return 0;
846 /* Check whether a timer in the timers queue is ready. If a timer is
847 ready, call its handler and return. Update the timeout for the
848 select() or poll() as well. Return 1 if an event was handled,
849 otherwise returns 0.*/
851 static int
852 poll_timers (void)
854 if (update_wait_timeout ())
856 struct gdb_timer *timer_ptr = timer_list.first_timer;
857 timer_handler_func *proc = timer_ptr->proc;
858 gdb_client_data client_data = timer_ptr->client_data;
860 /* Get rid of the timer from the beginning of the list. */
861 timer_list.first_timer = timer_ptr->next;
863 /* Delete the timer before calling the callback, not after, in
864 case the callback itself decides to try deleting the timer
865 too. */
866 delete timer_ptr;
868 /* Call the procedure associated with that timer. */
869 (proc) (client_data);
871 return 1;
874 return 0;