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