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