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