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