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