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