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