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