Commit | Line | Data |
---|---|---|
b5a0ac70 | 1 | /* Event loop machinery for GDB, the GNU debugger. |
b811d2c2 | 2 | Copyright (C) 1999-2020 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" |
5cc3ce8b | 23 | #include "ser-event.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> |
268a13a5 | 34 | #include "gdbsupport/gdb_sys_time.h" |
06cc9596 | 35 | #include "gdbsupport/gdb_select.h" |
7c36c34e | 36 | #include "top.h" |
c2c6d25f | 37 | |
371d5dec MS |
38 | /* Tell create_file_handler what events we are interested in. |
39 | This is used by the select version of the event loop. */ | |
01f69b38 DE |
40 | |
41 | #define GDB_READABLE (1<<1) | |
42 | #define GDB_WRITABLE (1<<2) | |
43 | #define GDB_EXCEPTION (1<<3) | |
44 | ||
c2c6d25f | 45 | /* Information about each file descriptor we register with the event |
371d5dec | 46 | loop. */ |
c2c6d25f JM |
47 | |
48 | typedef struct file_handler | |
49 | { | |
371d5dec MS |
50 | int fd; /* File descriptor. */ |
51 | int mask; /* Events we want to monitor: POLLIN, etc. */ | |
c2c6d25f | 52 | int ready_mask; /* Events that have been seen since |
371d5dec MS |
53 | the last time. */ |
54 | handler_func *proc; /* Procedure to call when fd is ready. */ | |
55 | gdb_client_data client_data; /* Argument to pass to proc. */ | |
56 | int error; /* Was an error detected on this fd? */ | |
57 | struct file_handler *next_file; /* Next registered file descriptor. */ | |
c2c6d25f JM |
58 | } |
59 | file_handler; | |
60 | ||
371d5dec | 61 | /* PROC is a function to be invoked when the READY flag is set. This |
c2c6d25f | 62 | happens when there has been a signal and the corresponding signal |
371d5dec MS |
63 | handler has 'triggered' this async_signal_handler for execution. |
64 | The actual work to be done in response to a signal will be carried | |
65 | out by PROC at a later time, within process_event. This provides a | |
66 | deferred execution of signal handlers. | |
67 | ||
c2c6d25f | 68 | Async_init_signals takes care of setting up such an |
371d5dec MS |
69 | async_signal_handler for each interesting signal. */ |
70 | ||
c2c6d25f JM |
71 | typedef struct async_signal_handler |
72 | { | |
371d5dec MS |
73 | int ready; /* If ready, call this handler |
74 | from the main event loop, using | |
75 | invoke_async_handler. */ | |
76 | struct async_signal_handler *next_handler; /* Ptr to next handler. */ | |
77 | sig_handler_func *proc; /* Function to call to do the work. */ | |
78 | gdb_client_data client_data; /* Argument to async_handler_func. */ | |
c2c6d25f JM |
79 | } |
80 | async_signal_handler; | |
81 | ||
50d01748 PA |
82 | /* PROC is a function to be invoked when the READY flag is set. This |
83 | happens when the event has been marked with | |
84 | MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response | |
85 | to an event will be carried out by PROC at a later time, within | |
86 | process_event. This provides a deferred execution of event | |
87 | handlers. */ | |
88 | typedef struct async_event_handler | |
89 | { | |
90 | /* If ready, call this handler from the main event loop, using | |
91 | invoke_event_handler. */ | |
92 | int ready; | |
93 | ||
94 | /* Point to next handler. */ | |
95 | struct async_event_handler *next_handler; | |
96 | ||
97 | /* Function to call to do the work. */ | |
98 | async_event_handler_func *proc; | |
99 | ||
100 | /* Argument to PROC. */ | |
101 | gdb_client_data client_data; | |
102 | } | |
103 | async_event_handler; | |
104 | ||
b5a0ac70 SS |
105 | /* Gdb_notifier is just a list of file descriptors gdb is interested in. |
106 | These are the input file descriptor, and the target file | |
371d5dec | 107 | descriptor. We have two flavors of the notifier, one for platforms |
b5a0ac70 | 108 | that have the POLL function, the other for those that don't, and |
371d5dec | 109 | only support SELECT. Each of the elements in the gdb_notifier list is |
b5a0ac70 | 110 | basically a description of what kind of events gdb is interested |
371d5dec | 111 | in, for each fd. */ |
b5a0ac70 | 112 | |
392a587b | 113 | /* As of 1999-04-30 only the input file descriptor is registered with the |
371d5dec | 114 | event loop. */ |
b5a0ac70 | 115 | |
44f45770 | 116 | /* Do we use poll or select ? */ |
b5a0ac70 | 117 | #ifdef HAVE_POLL |
44f45770 EZ |
118 | #define USE_POLL 1 |
119 | #else | |
120 | #define USE_POLL 0 | |
121 | #endif /* HAVE_POLL */ | |
122 | ||
123 | static unsigned char use_poll = USE_POLL; | |
b5a0ac70 | 124 | |
011825f0 MM |
125 | #ifdef USE_WIN32API |
126 | #include <windows.h> | |
127 | #include <io.h> | |
128 | #endif | |
129 | ||
b5a0ac70 SS |
130 | static struct |
131 | { | |
371d5dec | 132 | /* Ptr to head of file handler list. */ |
b5a0ac70 SS |
133 | file_handler *first_file_handler; |
134 | ||
4e63d0ac PA |
135 | /* Next file handler to handle, for the select variant. To level |
136 | the fairness across event sources, we serve file handlers in a | |
137 | round-robin-like fashion. The number and order of the polled | |
138 | file handlers may change between invocations, but this is good | |
139 | enough. */ | |
140 | file_handler *next_file_handler; | |
141 | ||
44f45770 | 142 | #ifdef HAVE_POLL |
371d5dec | 143 | /* Ptr to array of pollfd structures. */ |
b5a0ac70 SS |
144 | struct pollfd *poll_fds; |
145 | ||
4e63d0ac PA |
146 | /* Next file descriptor to handle, for the poll variant. To level |
147 | the fairness across event sources, we poll the file descriptors | |
148 | in a round-robin-like fashion. The number and order of the | |
149 | polled file descriptors may change between invocations, but | |
150 | this is good enough. */ | |
151 | int next_poll_fds_index; | |
152 | ||
371d5dec | 153 | /* Timeout in milliseconds for calls to poll(). */ |
44f45770 EZ |
154 | int poll_timeout; |
155 | #endif | |
b5a0ac70 SS |
156 | |
157 | /* Masks to be used in the next call to select. | |
371d5dec | 158 | Bits are set in response to calls to create_file_handler. */ |
58a2c44a | 159 | fd_set check_masks[3]; |
b5a0ac70 | 160 | |
371d5dec | 161 | /* What file descriptors were found ready by select. */ |
58a2c44a | 162 | fd_set ready_masks[3]; |
b5a0ac70 | 163 | |
371d5dec MS |
164 | /* Number of file descriptors to monitor (for poll). */ |
165 | /* Number of valid bits (highest fd value + 1) (for select). */ | |
b5a0ac70 SS |
166 | int num_fds; |
167 | ||
371d5dec | 168 | /* Time structure for calls to select(). */ |
44f45770 | 169 | struct timeval select_timeout; |
c2c6d25f | 170 | |
371d5dec | 171 | /* Flag to tell whether the timeout should be used. */ |
c2c6d25f | 172 | int timeout_valid; |
6426a772 | 173 | } |
b5a0ac70 SS |
174 | gdb_notifier; |
175 | ||
371d5dec MS |
176 | /* Structure associated with a timer. PROC will be executed at the |
177 | first occasion after WHEN. */ | |
c2c6d25f JM |
178 | struct gdb_timer |
179 | { | |
dcb07cfa | 180 | std::chrono::steady_clock::time_point when; |
c2c6d25f JM |
181 | int timer_id; |
182 | struct gdb_timer *next; | |
371d5dec MS |
183 | timer_handler_func *proc; /* Function to call to do the work. */ |
184 | gdb_client_data client_data; /* Argument to async_handler_func. */ | |
ae462839 | 185 | }; |
c2c6d25f | 186 | |
371d5dec MS |
187 | /* List of currently active timers. It is sorted in order of |
188 | increasing timers. */ | |
c2c6d25f JM |
189 | static struct |
190 | { | |
371d5dec | 191 | /* Pointer to first in timer list. */ |
c2c6d25f JM |
192 | struct gdb_timer *first_timer; |
193 | ||
371d5dec | 194 | /* Id of the last timer created. */ |
c2c6d25f JM |
195 | int num_timers; |
196 | } | |
197 | timer_list; | |
198 | ||
b5a0ac70 | 199 | /* All the async_signal_handlers gdb is interested in are kept onto |
371d5dec | 200 | this list. */ |
b5a0ac70 SS |
201 | static struct |
202 | { | |
371d5dec | 203 | /* Pointer to first in handler list. */ |
c5aa993b JM |
204 | async_signal_handler *first_handler; |
205 | ||
371d5dec | 206 | /* Pointer to last in handler list. */ |
c5aa993b | 207 | async_signal_handler *last_handler; |
b5a0ac70 SS |
208 | } |
209 | sighandler_list; | |
210 | ||
50d01748 | 211 | /* All the async_event_handlers gdb is interested in are kept onto |
371d5dec | 212 | this list. */ |
50d01748 PA |
213 | static struct |
214 | { | |
371d5dec | 215 | /* Pointer to first in handler list. */ |
50d01748 PA |
216 | async_event_handler *first_handler; |
217 | ||
371d5dec | 218 | /* Pointer to last in handler list. */ |
50d01748 PA |
219 | async_event_handler *last_handler; |
220 | } | |
221 | async_event_handler_list; | |
222 | ||
223 | static int invoke_async_signal_handlers (void); | |
224 | static void create_file_handler (int fd, int mask, handler_func *proc, | |
225 | gdb_client_data client_data); | |
70b66289 | 226 | static int check_async_event_handlers (void); |
50d01748 | 227 | static int gdb_wait_for_event (int); |
70b66289 PA |
228 | static int update_wait_timeout (void); |
229 | static int poll_timers (void); | |
b5a0ac70 SS |
230 | \f |
231 | ||
5cc3ce8b PA |
232 | /* This event is signalled whenever an asynchronous handler needs to |
233 | defer an action to the event loop. */ | |
234 | static struct serial_event *async_signal_handlers_serial_event; | |
235 | ||
236 | /* Callback registered with ASYNC_SIGNAL_HANDLERS_SERIAL_EVENT. */ | |
237 | ||
238 | static void | |
239 | async_signals_handler (int error, gdb_client_data client_data) | |
240 | { | |
241 | /* Do nothing. Handlers are run by invoke_async_signal_handlers | |
242 | from instead. */ | |
243 | } | |
244 | ||
245 | void | |
246 | initialize_async_signal_handlers (void) | |
247 | { | |
248 | async_signal_handlers_serial_event = make_serial_event (); | |
249 | ||
250 | add_file_handler (serial_event_fd (async_signal_handlers_serial_event), | |
251 | async_signals_handler, NULL); | |
252 | } | |
253 | ||
b5a0ac70 SS |
254 | /* Process one high level event. If nothing is ready at this time, |
255 | wait for something to happen (via gdb_wait_for_event), then process | |
11cf8741 | 256 | it. Returns >0 if something was done otherwise returns <0 (this |
e0dd0826 | 257 | can happen if there are no event sources to wait for). */ |
11cf8741 | 258 | |
99656a61 | 259 | int |
e0dd0826 | 260 | gdb_do_one_event (void) |
b5a0ac70 | 261 | { |
50d01748 PA |
262 | static int event_source_head = 0; |
263 | const int number_of_sources = 3; | |
264 | int current = 0; | |
265 | ||
70b66289 PA |
266 | /* First let's see if there are any asynchronous signal handlers |
267 | that are ready. These would be the result of invoking any of the | |
268 | signal handlers. */ | |
269 | if (invoke_async_signal_handlers ()) | |
50d01748 PA |
270 | return 1; |
271 | ||
272 | /* To level the fairness across event sources, we poll them in a | |
273 | round-robin fashion. */ | |
274 | for (current = 0; current < number_of_sources; current++) | |
11cf8741 | 275 | { |
70b66289 PA |
276 | int res; |
277 | ||
50d01748 PA |
278 | switch (event_source_head) |
279 | { | |
280 | case 0: | |
70b66289 PA |
281 | /* Are any timers that are ready? */ |
282 | res = poll_timers (); | |
50d01748 PA |
283 | break; |
284 | case 1: | |
285 | /* Are there events already waiting to be collected on the | |
286 | monitored file descriptors? */ | |
70b66289 | 287 | res = gdb_wait_for_event (0); |
50d01748 PA |
288 | break; |
289 | case 2: | |
290 | /* Are there any asynchronous event handlers ready? */ | |
70b66289 | 291 | res = check_async_event_handlers (); |
50d01748 | 292 | break; |
80bd5fab PA |
293 | default: |
294 | internal_error (__FILE__, __LINE__, | |
295 | "unexpected event_source_head %d", | |
296 | event_source_head); | |
50d01748 PA |
297 | } |
298 | ||
299 | event_source_head++; | |
300 | if (event_source_head == number_of_sources) | |
301 | event_source_head = 0; | |
7e5cd2de | 302 | |
70b66289 PA |
303 | if (res > 0) |
304 | return 1; | |
305 | } | |
7e5cd2de | 306 | |
50d01748 PA |
307 | /* Block waiting for a new event. If gdb_wait_for_event returns -1, |
308 | we should get out because this means that there are no event | |
309 | sources left. This will make the event loop stop, and the | |
310 | application exit. */ | |
7e5cd2de | 311 | |
50d01748 PA |
312 | if (gdb_wait_for_event (1) < 0) |
313 | return -1; | |
7e5cd2de | 314 | |
50d01748 PA |
315 | /* If gdb_wait_for_event has returned 1, it means that one event has |
316 | been handled. We break out of the loop. */ | |
11cf8741 JM |
317 | return 1; |
318 | } | |
319 | ||
b5a0ac70 SS |
320 | \f |
321 | ||
085dd6e6 JM |
322 | /* Wrapper function for create_file_handler, so that the caller |
323 | doesn't have to know implementation details about the use of poll | |
371d5dec | 324 | vs. select. */ |
c5aa993b | 325 | void |
6426a772 | 326 | add_file_handler (int fd, handler_func * proc, gdb_client_data client_data) |
085dd6e6 JM |
327 | { |
328 | #ifdef HAVE_POLL | |
44f45770 EZ |
329 | struct pollfd fds; |
330 | #endif | |
331 | ||
332 | if (use_poll) | |
333 | { | |
334 | #ifdef HAVE_POLL | |
371d5dec MS |
335 | /* Check to see if poll () is usable. If not, we'll switch to |
336 | use select. This can happen on systems like | |
7e5cd2de EZ |
337 | m68k-motorola-sys, `poll' cannot be used to wait for `stdin'. |
338 | On m68k-motorola-sysv, tty's are not stream-based and not | |
371d5dec | 339 | `poll'able. */ |
7e5cd2de EZ |
340 | fds.fd = fd; |
341 | fds.events = POLLIN; | |
342 | if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL)) | |
343 | use_poll = 0; | |
44f45770 | 344 | #else |
8e65ff28 | 345 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 346 | _("use_poll without HAVE_POLL")); |
44f45770 EZ |
347 | #endif /* HAVE_POLL */ |
348 | } | |
349 | if (use_poll) | |
350 | { | |
351 | #ifdef HAVE_POLL | |
352 | create_file_handler (fd, POLLIN, proc, client_data); | |
085dd6e6 | 353 | #else |
8e65ff28 | 354 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 355 | _("use_poll without HAVE_POLL")); |
085dd6e6 | 356 | #endif |
44f45770 EZ |
357 | } |
358 | else | |
371d5dec MS |
359 | create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, |
360 | proc, client_data); | |
085dd6e6 JM |
361 | } |
362 | ||
b5a0ac70 | 363 | /* Add a file handler/descriptor to the list of descriptors we are |
371d5dec MS |
364 | interested in. |
365 | ||
366 | FD is the file descriptor for the file/stream to be listened to. | |
367 | ||
368 | For the poll case, MASK is a combination (OR) of POLLIN, | |
369 | POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND: | |
370 | these are the events we are interested in. If any of them occurs, | |
371 | proc should be called. | |
372 | ||
373 | For the select case, MASK is a combination of READABLE, WRITABLE, | |
374 | EXCEPTION. PROC is the procedure that will be called when an event | |
375 | occurs for FD. CLIENT_DATA is the argument to pass to PROC. */ | |
376 | ||
085dd6e6 | 377 | static void |
371d5dec MS |
378 | create_file_handler (int fd, int mask, handler_func * proc, |
379 | gdb_client_data client_data) | |
b5a0ac70 SS |
380 | { |
381 | file_handler *file_ptr; | |
382 | ||
371d5dec MS |
383 | /* Do we already have a file handler for this file? (We may be |
384 | changing its associated procedure). */ | |
b5a0ac70 SS |
385 | for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; |
386 | file_ptr = file_ptr->next_file) | |
387 | { | |
388 | if (file_ptr->fd == fd) | |
389 | break; | |
390 | } | |
391 | ||
371d5dec MS |
392 | /* It is a new file descriptor. Add it to the list. Otherwise, just |
393 | change the data associated with it. */ | |
b5a0ac70 SS |
394 | if (file_ptr == NULL) |
395 | { | |
8d749320 | 396 | file_ptr = XNEW (file_handler); |
b5a0ac70 SS |
397 | file_ptr->fd = fd; |
398 | file_ptr->ready_mask = 0; | |
399 | file_ptr->next_file = gdb_notifier.first_file_handler; | |
400 | gdb_notifier.first_file_handler = file_ptr; | |
b5a0ac70 | 401 | |
05a6c72c KS |
402 | if (use_poll) |
403 | { | |
b5a0ac70 | 404 | #ifdef HAVE_POLL |
05a6c72c KS |
405 | gdb_notifier.num_fds++; |
406 | if (gdb_notifier.poll_fds) | |
407 | gdb_notifier.poll_fds = | |
408 | (struct pollfd *) xrealloc (gdb_notifier.poll_fds, | |
409 | (gdb_notifier.num_fds | |
410 | * sizeof (struct pollfd))); | |
411 | else | |
412 | gdb_notifier.poll_fds = | |
8d749320 | 413 | XNEW (struct pollfd); |
05a6c72c KS |
414 | (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd; |
415 | (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask; | |
416 | (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0; | |
44f45770 | 417 | #else |
05a6c72c | 418 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 419 | _("use_poll without HAVE_POLL")); |
44f45770 | 420 | #endif /* HAVE_POLL */ |
05a6c72c | 421 | } |
44f45770 | 422 | else |
05a6c72c KS |
423 | { |
424 | if (mask & GDB_READABLE) | |
425 | FD_SET (fd, &gdb_notifier.check_masks[0]); | |
426 | else | |
427 | FD_CLR (fd, &gdb_notifier.check_masks[0]); | |
428 | ||
429 | if (mask & GDB_WRITABLE) | |
430 | FD_SET (fd, &gdb_notifier.check_masks[1]); | |
431 | else | |
432 | FD_CLR (fd, &gdb_notifier.check_masks[1]); | |
433 | ||
434 | if (mask & GDB_EXCEPTION) | |
435 | FD_SET (fd, &gdb_notifier.check_masks[2]); | |
436 | else | |
437 | FD_CLR (fd, &gdb_notifier.check_masks[2]); | |
438 | ||
439 | if (gdb_notifier.num_fds <= fd) | |
440 | gdb_notifier.num_fds = fd + 1; | |
441 | } | |
44f45770 | 442 | } |
05a6c72c KS |
443 | |
444 | file_ptr->proc = proc; | |
445 | file_ptr->client_data = client_data; | |
446 | file_ptr->mask = mask; | |
b5a0ac70 SS |
447 | } |
448 | ||
4e63d0ac PA |
449 | /* Return the next file handler to handle, and advance to the next |
450 | file handler, wrapping around if the end of the list is | |
451 | reached. */ | |
452 | ||
453 | static file_handler * | |
454 | get_next_file_handler_to_handle_and_advance (void) | |
455 | { | |
456 | file_handler *curr_next; | |
457 | ||
458 | /* The first time around, this is still NULL. */ | |
459 | if (gdb_notifier.next_file_handler == NULL) | |
460 | gdb_notifier.next_file_handler = gdb_notifier.first_file_handler; | |
461 | ||
462 | curr_next = gdb_notifier.next_file_handler; | |
463 | gdb_assert (curr_next != NULL); | |
464 | ||
465 | /* Advance. */ | |
466 | gdb_notifier.next_file_handler = curr_next->next_file; | |
467 | /* Wrap around, if necessary. */ | |
468 | if (gdb_notifier.next_file_handler == NULL) | |
469 | gdb_notifier.next_file_handler = gdb_notifier.first_file_handler; | |
470 | ||
471 | return curr_next; | |
472 | } | |
473 | ||
b5a0ac70 | 474 | /* Remove the file descriptor FD from the list of monitored fd's: |
371d5dec | 475 | i.e. we don't care anymore about events on the FD. */ |
b5a0ac70 | 476 | void |
c2c6d25f | 477 | delete_file_handler (int fd) |
b5a0ac70 SS |
478 | { |
479 | file_handler *file_ptr, *prev_ptr = NULL; | |
58a2c44a EZ |
480 | int i; |
481 | #ifdef HAVE_POLL | |
482 | int j; | |
b5a0ac70 | 483 | struct pollfd *new_poll_fds; |
b5a0ac70 SS |
484 | #endif |
485 | ||
371d5dec | 486 | /* Find the entry for the given file. */ |
b5a0ac70 SS |
487 | |
488 | for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; | |
489 | file_ptr = file_ptr->next_file) | |
490 | { | |
491 | if (file_ptr->fd == fd) | |
492 | break; | |
493 | } | |
494 | ||
495 | if (file_ptr == NULL) | |
496 | return; | |
497 | ||
44f45770 EZ |
498 | if (use_poll) |
499 | { | |
b5a0ac70 | 500 | #ifdef HAVE_POLL |
371d5dec MS |
501 | /* Create a new poll_fds array by copying every fd's information |
502 | but the one we want to get rid of. */ | |
b5a0ac70 | 503 | |
371d5dec MS |
504 | new_poll_fds = (struct pollfd *) |
505 | xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd)); | |
b5a0ac70 | 506 | |
44f45770 | 507 | for (i = 0, j = 0; i < gdb_notifier.num_fds; i++) |
b5a0ac70 | 508 | { |
44f45770 EZ |
509 | if ((gdb_notifier.poll_fds + i)->fd != fd) |
510 | { | |
511 | (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd; | |
512 | (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events; | |
3e43a32a MS |
513 | (new_poll_fds + j)->revents |
514 | = (gdb_notifier.poll_fds + i)->revents; | |
44f45770 EZ |
515 | j++; |
516 | } | |
b5a0ac70 | 517 | } |
b8c9b27d | 518 | xfree (gdb_notifier.poll_fds); |
44f45770 EZ |
519 | gdb_notifier.poll_fds = new_poll_fds; |
520 | gdb_notifier.num_fds--; | |
521 | #else | |
8e65ff28 | 522 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 523 | _("use_poll without HAVE_POLL")); |
44f45770 | 524 | #endif /* HAVE_POLL */ |
b5a0ac70 | 525 | } |
44f45770 EZ |
526 | else |
527 | { | |
528 | if (file_ptr->mask & GDB_READABLE) | |
529 | FD_CLR (fd, &gdb_notifier.check_masks[0]); | |
530 | if (file_ptr->mask & GDB_WRITABLE) | |
531 | FD_CLR (fd, &gdb_notifier.check_masks[1]); | |
532 | if (file_ptr->mask & GDB_EXCEPTION) | |
533 | FD_CLR (fd, &gdb_notifier.check_masks[2]); | |
b5a0ac70 | 534 | |
371d5dec | 535 | /* Find current max fd. */ |
b5a0ac70 | 536 | |
44f45770 | 537 | if ((fd + 1) == gdb_notifier.num_fds) |
b5a0ac70 | 538 | { |
44f45770 EZ |
539 | gdb_notifier.num_fds--; |
540 | for (i = gdb_notifier.num_fds; i; i--) | |
541 | { | |
542 | if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0]) | |
543 | || FD_ISSET (i - 1, &gdb_notifier.check_masks[1]) | |
544 | || FD_ISSET (i - 1, &gdb_notifier.check_masks[2])) | |
545 | break; | |
546 | } | |
547 | gdb_notifier.num_fds = i; | |
b5a0ac70 SS |
548 | } |
549 | } | |
b5a0ac70 | 550 | |
cff3e48b | 551 | /* Deactivate the file descriptor, by clearing its mask, |
371d5dec | 552 | so that it will not fire again. */ |
cff3e48b JM |
553 | |
554 | file_ptr->mask = 0; | |
555 | ||
4e63d0ac PA |
556 | /* If this file handler was going to be the next one to be handled, |
557 | advance to the next's next, if any. */ | |
558 | if (gdb_notifier.next_file_handler == file_ptr) | |
559 | { | |
560 | if (file_ptr->next_file == NULL | |
561 | && file_ptr == gdb_notifier.first_file_handler) | |
562 | gdb_notifier.next_file_handler = NULL; | |
563 | else | |
564 | get_next_file_handler_to_handle_and_advance (); | |
565 | } | |
566 | ||
371d5dec | 567 | /* Get rid of the file handler in the file handler list. */ |
b5a0ac70 SS |
568 | if (file_ptr == gdb_notifier.first_file_handler) |
569 | gdb_notifier.first_file_handler = file_ptr->next_file; | |
570 | else | |
571 | { | |
572 | for (prev_ptr = gdb_notifier.first_file_handler; | |
9e0b60a8 | 573 | prev_ptr->next_file != file_ptr; |
b5a0ac70 SS |
574 | prev_ptr = prev_ptr->next_file) |
575 | ; | |
576 | prev_ptr->next_file = file_ptr->next_file; | |
577 | } | |
b8c9b27d | 578 | xfree (file_ptr); |
b5a0ac70 SS |
579 | } |
580 | ||
581 | /* Handle the given event by calling the procedure associated to the | |
70b66289 PA |
582 | corresponding file handler. */ |
583 | ||
b5a0ac70 | 584 | static void |
70b66289 | 585 | handle_file_event (file_handler *file_ptr, int ready_mask) |
b5a0ac70 | 586 | { |
c2c6d25f JM |
587 | int mask; |
588 | #ifdef HAVE_POLL | |
589 | int error_mask; | |
c2c6d25f | 590 | #endif |
b5a0ac70 | 591 | |
b5a0ac70 | 592 | { |
b5a0ac70 SS |
593 | { |
594 | /* With poll, the ready_mask could have any of three events | |
371d5dec MS |
595 | set to 1: POLLHUP, POLLERR, POLLNVAL. These events |
596 | cannot be used in the requested event mask (events), but | |
597 | they can be returned in the return mask (revents). We | |
598 | need to check for those event too, and add them to the | |
599 | mask which will be passed to the handler. */ | |
b5a0ac70 SS |
600 | |
601 | /* See if the desired events (mask) match the received | |
371d5dec | 602 | events (ready_mask). */ |
b5a0ac70 | 603 | |
44f45770 | 604 | if (use_poll) |
c2c6d25f | 605 | { |
44f45770 | 606 | #ifdef HAVE_POLL |
652c71b4 AS |
607 | /* POLLHUP means EOF, but can be combined with POLLIN to |
608 | signal more data to read. */ | |
44f45770 | 609 | error_mask = POLLHUP | POLLERR | POLLNVAL; |
70b66289 | 610 | mask = ready_mask & (file_ptr->mask | error_mask); |
44f45770 | 611 | |
652c71b4 | 612 | if ((mask & (POLLERR | POLLNVAL)) != 0) |
44f45770 | 613 | { |
371d5dec MS |
614 | /* Work in progress. We may need to tell somebody |
615 | what kind of error we had. */ | |
652c71b4 | 616 | if (mask & POLLERR) |
3e43a32a MS |
617 | printf_unfiltered (_("Error detected on fd %d\n"), |
618 | file_ptr->fd); | |
652c71b4 | 619 | if (mask & POLLNVAL) |
3e43a32a MS |
620 | printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"), |
621 | file_ptr->fd); | |
44f45770 EZ |
622 | file_ptr->error = 1; |
623 | } | |
624 | else | |
625 | file_ptr->error = 0; | |
626 | #else | |
8e65ff28 | 627 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 628 | _("use_poll without HAVE_POLL")); |
44f45770 | 629 | #endif /* HAVE_POLL */ |
6426a772 JM |
630 | } |
631 | else | |
c2c6d25f | 632 | { |
70b66289 | 633 | if (ready_mask & GDB_EXCEPTION) |
44f45770 | 634 | { |
3e43a32a MS |
635 | printf_unfiltered (_("Exception condition detected " |
636 | "on fd %d\n"), file_ptr->fd); | |
44f45770 EZ |
637 | file_ptr->error = 1; |
638 | } | |
639 | else | |
640 | file_ptr->error = 0; | |
70b66289 | 641 | mask = ready_mask & file_ptr->mask; |
c2c6d25f | 642 | } |
b5a0ac70 | 643 | |
371d5dec | 644 | /* If there was a match, then call the handler. */ |
b5a0ac70 | 645 | if (mask != 0) |
2acceee2 | 646 | (*file_ptr->proc) (file_ptr->error, file_ptr->client_data); |
b5a0ac70 SS |
647 | } |
648 | } | |
649 | } | |
650 | ||
70b66289 PA |
651 | /* Wait for new events on the monitored file descriptors. Run the |
652 | event handler if the first descriptor that is detected by the poll. | |
653 | If BLOCK and if there are no events, this function will block in | |
654 | the call to poll. Return 1 if an event was handled. Return -1 if | |
655 | there are no file descriptors to monitor. Return 1 if an event was | |
656 | handled, otherwise returns 0. */ | |
657 | ||
b5a0ac70 | 658 | static int |
50d01748 | 659 | gdb_wait_for_event (int block) |
b5a0ac70 SS |
660 | { |
661 | file_handler *file_ptr; | |
0f71a2f6 | 662 | int num_found = 0; |
b5a0ac70 | 663 | |
371d5dec | 664 | /* Make sure all output is done before getting another event. */ |
da5bd37e TT |
665 | gdb_stdout->flush (); |
666 | gdb_stderr->flush (); | |
7be570e7 | 667 | |
b5a0ac70 SS |
668 | if (gdb_notifier.num_fds == 0) |
669 | return -1; | |
670 | ||
70b66289 PA |
671 | if (block) |
672 | update_wait_timeout (); | |
673 | ||
44f45770 EZ |
674 | if (use_poll) |
675 | { | |
b5a0ac70 | 676 | #ifdef HAVE_POLL |
50d01748 PA |
677 | int timeout; |
678 | ||
679 | if (block) | |
680 | timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1; | |
681 | else | |
682 | timeout = 0; | |
683 | ||
684 | num_found = poll (gdb_notifier.poll_fds, | |
685 | (unsigned long) gdb_notifier.num_fds, timeout); | |
44f45770 EZ |
686 | |
687 | /* Don't print anything if we get out of poll because of a | |
50d01748 | 688 | signal. */ |
44f45770 | 689 | if (num_found == -1 && errno != EINTR) |
e2e0b3e5 | 690 | perror_with_name (("poll")); |
44f45770 | 691 | #else |
8e65ff28 | 692 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 693 | _("use_poll without HAVE_POLL")); |
44f45770 EZ |
694 | #endif /* HAVE_POLL */ |
695 | } | |
696 | else | |
c2c6d25f | 697 | { |
50d01748 | 698 | struct timeval select_timeout; |
50d01748 | 699 | struct timeval *timeout_p; |
d7f9d729 | 700 | |
50d01748 PA |
701 | if (block) |
702 | timeout_p = gdb_notifier.timeout_valid | |
703 | ? &gdb_notifier.select_timeout : NULL; | |
704 | else | |
705 | { | |
706 | memset (&select_timeout, 0, sizeof (select_timeout)); | |
707 | timeout_p = &select_timeout; | |
708 | } | |
709 | ||
44f45770 EZ |
710 | gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0]; |
711 | gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1]; | |
712 | gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2]; | |
011825f0 MM |
713 | num_found = gdb_select (gdb_notifier.num_fds, |
714 | &gdb_notifier.ready_masks[0], | |
715 | &gdb_notifier.ready_masks[1], | |
716 | &gdb_notifier.ready_masks[2], | |
50d01748 | 717 | timeout_p); |
44f45770 | 718 | |
371d5dec | 719 | /* Clear the masks after an error from select. */ |
44f45770 EZ |
720 | if (num_found == -1) |
721 | { | |
722 | FD_ZERO (&gdb_notifier.ready_masks[0]); | |
723 | FD_ZERO (&gdb_notifier.ready_masks[1]); | |
724 | FD_ZERO (&gdb_notifier.ready_masks[2]); | |
50d01748 PA |
725 | |
726 | /* Dont print anything if we got a signal, let gdb handle | |
727 | it. */ | |
44f45770 | 728 | if (errno != EINTR) |
e2e0b3e5 | 729 | perror_with_name (("select")); |
44f45770 | 730 | } |
c2c6d25f | 731 | } |
b5a0ac70 | 732 | |
4e63d0ac PA |
733 | /* Avoid looking at poll_fds[i]->revents if no event fired. */ |
734 | if (num_found <= 0) | |
735 | return 0; | |
736 | ||
70b66289 PA |
737 | /* Run event handlers. We always run just one handler and go back |
738 | to polling, in case a handler changes the notifier list. Since | |
739 | events for sources we haven't consumed yet wake poll/select | |
740 | immediately, no event is lost. */ | |
b5a0ac70 | 741 | |
4e63d0ac PA |
742 | /* To level the fairness across event descriptors, we handle them in |
743 | a round-robin-like fashion. The number and order of descriptors | |
744 | may change between invocations, but this is good enough. */ | |
44f45770 EZ |
745 | if (use_poll) |
746 | { | |
b5a0ac70 | 747 | #ifdef HAVE_POLL |
4e63d0ac PA |
748 | int i; |
749 | int mask; | |
b5a0ac70 | 750 | |
4e63d0ac PA |
751 | while (1) |
752 | { | |
753 | if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds) | |
754 | gdb_notifier.next_poll_fds_index = 0; | |
755 | i = gdb_notifier.next_poll_fds_index++; | |
44f45770 | 756 | |
4e63d0ac PA |
757 | gdb_assert (i < gdb_notifier.num_fds); |
758 | if ((gdb_notifier.poll_fds + i)->revents) | |
759 | break; | |
760 | } | |
70b66289 | 761 | |
4e63d0ac PA |
762 | for (file_ptr = gdb_notifier.first_file_handler; |
763 | file_ptr != NULL; | |
764 | file_ptr = file_ptr->next_file) | |
765 | { | |
766 | if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd) | |
767 | break; | |
44f45770 | 768 | } |
4e63d0ac PA |
769 | gdb_assert (file_ptr != NULL); |
770 | ||
771 | mask = (gdb_notifier.poll_fds + i)->revents; | |
772 | handle_file_event (file_ptr, mask); | |
773 | return 1; | |
44f45770 | 774 | #else |
8e65ff28 | 775 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 776 | _("use_poll without HAVE_POLL")); |
44f45770 EZ |
777 | #endif /* HAVE_POLL */ |
778 | } | |
779 | else | |
780 | { | |
4e63d0ac PA |
781 | /* See comment about even source fairness above. */ |
782 | int mask = 0; | |
783 | ||
784 | do | |
b5a0ac70 | 785 | { |
4e63d0ac | 786 | file_ptr = get_next_file_handler_to_handle_and_advance (); |
44f45770 EZ |
787 | |
788 | if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0])) | |
789 | mask |= GDB_READABLE; | |
790 | if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1])) | |
791 | mask |= GDB_WRITABLE; | |
792 | if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2])) | |
793 | mask |= GDB_EXCEPTION; | |
b5a0ac70 | 794 | } |
4e63d0ac PA |
795 | while (mask == 0); |
796 | ||
797 | handle_file_event (file_ptr, mask); | |
798 | return 1; | |
b5a0ac70 | 799 | } |
b5a0ac70 SS |
800 | return 0; |
801 | } | |
802 | \f | |
803 | ||
371d5dec | 804 | /* Create an asynchronous handler, allocating memory for it. |
b5a0ac70 SS |
805 | Return a pointer to the newly created handler. |
806 | This pointer will be used to invoke the handler by | |
807 | invoke_async_signal_handler. | |
808 | PROC is the function to call with CLIENT_DATA argument | |
371d5dec | 809 | whenever the handler is invoked. */ |
b5a0ac70 | 810 | async_signal_handler * |
3e43a32a MS |
811 | create_async_signal_handler (sig_handler_func * proc, |
812 | gdb_client_data client_data) | |
b5a0ac70 SS |
813 | { |
814 | async_signal_handler *async_handler_ptr; | |
815 | ||
8d749320 | 816 | async_handler_ptr = XNEW (async_signal_handler); |
b5a0ac70 SS |
817 | async_handler_ptr->ready = 0; |
818 | async_handler_ptr->next_handler = NULL; | |
819 | async_handler_ptr->proc = proc; | |
820 | async_handler_ptr->client_data = client_data; | |
821 | if (sighandler_list.first_handler == NULL) | |
822 | sighandler_list.first_handler = async_handler_ptr; | |
823 | else | |
824 | sighandler_list.last_handler->next_handler = async_handler_ptr; | |
825 | sighandler_list.last_handler = async_handler_ptr; | |
826 | return async_handler_ptr; | |
827 | } | |
828 | ||
371d5dec MS |
829 | /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information |
830 | will be used when the handlers are invoked, after we have waited | |
831 | for some event. The caller of this function is the interrupt | |
832 | handler associated with a signal. */ | |
b5a0ac70 | 833 | void |
6426a772 | 834 | mark_async_signal_handler (async_signal_handler * async_handler_ptr) |
b5a0ac70 | 835 | { |
50d01748 | 836 | async_handler_ptr->ready = 1; |
5cc3ce8b | 837 | serial_event_set (async_signal_handlers_serial_event); |
b5a0ac70 SS |
838 | } |
839 | ||
abc56d60 PA |
840 | /* See event-loop.h. */ |
841 | ||
842 | void | |
843 | clear_async_signal_handler (async_signal_handler *async_handler_ptr) | |
844 | { | |
845 | async_handler_ptr->ready = 0; | |
846 | } | |
847 | ||
848 | /* See event-loop.h. */ | |
849 | ||
850 | int | |
851 | async_signal_handler_is_marked (async_signal_handler *async_handler_ptr) | |
852 | { | |
853 | return async_handler_ptr->ready; | |
854 | } | |
855 | ||
50d01748 PA |
856 | /* Call all the handlers that are ready. Returns true if any was |
857 | indeed ready. */ | |
5cc3ce8b | 858 | |
50d01748 PA |
859 | static int |
860 | invoke_async_signal_handlers (void) | |
b5a0ac70 SS |
861 | { |
862 | async_signal_handler *async_handler_ptr; | |
50d01748 | 863 | int any_ready = 0; |
b5a0ac70 | 864 | |
5cc3ce8b PA |
865 | /* We're going to handle all pending signals, so no need to wake up |
866 | the event loop again the next time around. Note this must be | |
867 | cleared _before_ calling the callbacks, to avoid races. */ | |
868 | serial_event_clear (async_signal_handlers_serial_event); | |
869 | ||
870 | /* Invoke all ready handlers. */ | |
b5a0ac70 SS |
871 | |
872 | while (1) | |
873 | { | |
c5aa993b | 874 | for (async_handler_ptr = sighandler_list.first_handler; |
b5a0ac70 SS |
875 | async_handler_ptr != NULL; |
876 | async_handler_ptr = async_handler_ptr->next_handler) | |
877 | { | |
878 | if (async_handler_ptr->ready) | |
879 | break; | |
880 | } | |
881 | if (async_handler_ptr == NULL) | |
882 | break; | |
50d01748 | 883 | any_ready = 1; |
b5a0ac70 | 884 | async_handler_ptr->ready = 0; |
7c36c34e PA |
885 | /* Async signal handlers have no connection to whichever was the |
886 | current UI, and thus always run on the main one. */ | |
887 | current_ui = main_ui; | |
b5a0ac70 SS |
888 | (*async_handler_ptr->proc) (async_handler_ptr->client_data); |
889 | } | |
890 | ||
50d01748 | 891 | return any_ready; |
b5a0ac70 SS |
892 | } |
893 | ||
371d5dec | 894 | /* Delete an asynchronous handler (ASYNC_HANDLER_PTR). |
b5a0ac70 SS |
895 | Free the space allocated for it. */ |
896 | void | |
6426a772 | 897 | delete_async_signal_handler (async_signal_handler ** async_handler_ptr) |
b5a0ac70 SS |
898 | { |
899 | async_signal_handler *prev_ptr; | |
900 | ||
43ff13b4 | 901 | if (sighandler_list.first_handler == (*async_handler_ptr)) |
b5a0ac70 | 902 | { |
43ff13b4 | 903 | sighandler_list.first_handler = (*async_handler_ptr)->next_handler; |
b5a0ac70 SS |
904 | if (sighandler_list.first_handler == NULL) |
905 | sighandler_list.last_handler = NULL; | |
906 | } | |
907 | else | |
908 | { | |
909 | prev_ptr = sighandler_list.first_handler; | |
32107cd5 | 910 | while (prev_ptr && prev_ptr->next_handler != (*async_handler_ptr)) |
b5a0ac70 | 911 | prev_ptr = prev_ptr->next_handler; |
60bc018f | 912 | gdb_assert (prev_ptr); |
43ff13b4 JM |
913 | prev_ptr->next_handler = (*async_handler_ptr)->next_handler; |
914 | if (sighandler_list.last_handler == (*async_handler_ptr)) | |
b5a0ac70 SS |
915 | sighandler_list.last_handler = prev_ptr; |
916 | } | |
b8c9b27d | 917 | xfree ((*async_handler_ptr)); |
43ff13b4 | 918 | (*async_handler_ptr) = NULL; |
b5a0ac70 SS |
919 | } |
920 | ||
50d01748 PA |
921 | /* Create an asynchronous event handler, allocating memory for it. |
922 | Return a pointer to the newly created handler. PROC is the | |
923 | function to call with CLIENT_DATA argument whenever the handler is | |
924 | invoked. */ | |
925 | async_event_handler * | |
926 | create_async_event_handler (async_event_handler_func *proc, | |
927 | gdb_client_data client_data) | |
928 | { | |
929 | async_event_handler *h; | |
930 | ||
8d749320 | 931 | h = XNEW (struct async_event_handler); |
50d01748 PA |
932 | h->ready = 0; |
933 | h->next_handler = NULL; | |
934 | h->proc = proc; | |
935 | h->client_data = client_data; | |
936 | if (async_event_handler_list.first_handler == NULL) | |
937 | async_event_handler_list.first_handler = h; | |
938 | else | |
939 | async_event_handler_list.last_handler->next_handler = h; | |
940 | async_event_handler_list.last_handler = h; | |
941 | return h; | |
942 | } | |
943 | ||
944 | /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information | |
945 | will be used by gdb_do_one_event. The caller will be whoever | |
946 | created the event source, and wants to signal that the event is | |
947 | ready to be handled. */ | |
948 | void | |
949 | mark_async_event_handler (async_event_handler *async_handler_ptr) | |
950 | { | |
951 | async_handler_ptr->ready = 1; | |
952 | } | |
953 | ||
b7d2e916 PA |
954 | /* See event-loop.h. */ |
955 | ||
956 | void | |
957 | clear_async_event_handler (async_event_handler *async_handler_ptr) | |
958 | { | |
959 | async_handler_ptr->ready = 0; | |
960 | } | |
961 | ||
70b66289 PA |
962 | /* Check if asynchronous event handlers are ready, and call the |
963 | handler function for one that is. */ | |
50d01748 | 964 | |
70b66289 | 965 | static int |
50d01748 PA |
966 | check_async_event_handlers (void) |
967 | { | |
968 | async_event_handler *async_handler_ptr; | |
50d01748 PA |
969 | |
970 | for (async_handler_ptr = async_event_handler_list.first_handler; | |
971 | async_handler_ptr != NULL; | |
972 | async_handler_ptr = async_handler_ptr->next_handler) | |
973 | { | |
974 | if (async_handler_ptr->ready) | |
975 | { | |
976 | async_handler_ptr->ready = 0; | |
70b66289 PA |
977 | (*async_handler_ptr->proc) (async_handler_ptr->client_data); |
978 | return 1; | |
50d01748 PA |
979 | } |
980 | } | |
70b66289 PA |
981 | |
982 | return 0; | |
50d01748 PA |
983 | } |
984 | ||
985 | /* Delete an asynchronous handler (ASYNC_HANDLER_PTR). | |
986 | Free the space allocated for it. */ | |
987 | void | |
988 | delete_async_event_handler (async_event_handler **async_handler_ptr) | |
b5a0ac70 | 989 | { |
50d01748 PA |
990 | async_event_handler *prev_ptr; |
991 | ||
992 | if (async_event_handler_list.first_handler == *async_handler_ptr) | |
993 | { | |
3e43a32a MS |
994 | async_event_handler_list.first_handler |
995 | = (*async_handler_ptr)->next_handler; | |
50d01748 PA |
996 | if (async_event_handler_list.first_handler == NULL) |
997 | async_event_handler_list.last_handler = NULL; | |
998 | } | |
999 | else | |
1000 | { | |
1001 | prev_ptr = async_event_handler_list.first_handler; | |
1002 | while (prev_ptr && prev_ptr->next_handler != *async_handler_ptr) | |
1003 | prev_ptr = prev_ptr->next_handler; | |
60bc018f | 1004 | gdb_assert (prev_ptr); |
50d01748 PA |
1005 | prev_ptr->next_handler = (*async_handler_ptr)->next_handler; |
1006 | if (async_event_handler_list.last_handler == (*async_handler_ptr)) | |
1007 | async_event_handler_list.last_handler = prev_ptr; | |
1008 | } | |
1009 | xfree (*async_handler_ptr); | |
1010 | *async_handler_ptr = NULL; | |
b5a0ac70 | 1011 | } |
c2c6d25f | 1012 | |
dcb07cfa PA |
1013 | /* Create a timer that will expire in MS milliseconds from now. When |
1014 | the timer is ready, PROC will be executed. At creation, the timer | |
1015 | is added to the timers queue. This queue is kept sorted in order | |
1016 | of increasing timers. Return a handle to the timer struct. */ | |
1017 | ||
c2c6d25f | 1018 | int |
dcb07cfa | 1019 | create_timer (int ms, timer_handler_func *proc, |
371d5dec | 1020 | gdb_client_data client_data) |
c2c6d25f | 1021 | { |
dcb07cfa | 1022 | using namespace std::chrono; |
c2c6d25f | 1023 | struct gdb_timer *timer_ptr, *timer_index, *prev_timer; |
6426a772 | 1024 | |
dcb07cfa | 1025 | steady_clock::time_point time_now = steady_clock::now (); |
c2c6d25f | 1026 | |
dcb07cfa PA |
1027 | timer_ptr = new gdb_timer (); |
1028 | timer_ptr->when = time_now + milliseconds (ms); | |
c2c6d25f JM |
1029 | timer_ptr->proc = proc; |
1030 | timer_ptr->client_data = client_data; | |
6426a772 | 1031 | timer_list.num_timers++; |
c2c6d25f JM |
1032 | timer_ptr->timer_id = timer_list.num_timers; |
1033 | ||
1034 | /* Now add the timer to the timer queue, making sure it is sorted in | |
371d5dec | 1035 | increasing order of expiration. */ |
c2c6d25f | 1036 | |
6426a772 JM |
1037 | for (timer_index = timer_list.first_timer; |
1038 | timer_index != NULL; | |
c2c6d25f JM |
1039 | timer_index = timer_index->next) |
1040 | { | |
dcb07cfa | 1041 | if (timer_index->when > timer_ptr->when) |
c2c6d25f JM |
1042 | break; |
1043 | } | |
6426a772 | 1044 | |
c2c6d25f JM |
1045 | if (timer_index == timer_list.first_timer) |
1046 | { | |
1047 | timer_ptr->next = timer_list.first_timer; | |
1048 | timer_list.first_timer = timer_ptr; | |
1049 | ||
1050 | } | |
1051 | else | |
1052 | { | |
6426a772 JM |
1053 | for (prev_timer = timer_list.first_timer; |
1054 | prev_timer->next != timer_index; | |
c2c6d25f JM |
1055 | prev_timer = prev_timer->next) |
1056 | ; | |
6426a772 | 1057 | |
c2c6d25f JM |
1058 | prev_timer->next = timer_ptr; |
1059 | timer_ptr->next = timer_index; | |
1060 | } | |
1061 | ||
1062 | gdb_notifier.timeout_valid = 0; | |
1063 | return timer_ptr->timer_id; | |
1064 | } | |
1065 | ||
1066 | /* There is a chance that the creator of the timer wants to get rid of | |
371d5dec | 1067 | it before it expires. */ |
c2c6d25f JM |
1068 | void |
1069 | delete_timer (int id) | |
1070 | { | |
1071 | struct gdb_timer *timer_ptr, *prev_timer = NULL; | |
1072 | ||
371d5dec | 1073 | /* Find the entry for the given timer. */ |
c2c6d25f JM |
1074 | |
1075 | for (timer_ptr = timer_list.first_timer; timer_ptr != NULL; | |
1076 | timer_ptr = timer_ptr->next) | |
1077 | { | |
1078 | if (timer_ptr->timer_id == id) | |
1079 | break; | |
1080 | } | |
1081 | ||
1082 | if (timer_ptr == NULL) | |
1083 | return; | |
371d5dec | 1084 | /* Get rid of the timer in the timer list. */ |
c2c6d25f JM |
1085 | if (timer_ptr == timer_list.first_timer) |
1086 | timer_list.first_timer = timer_ptr->next; | |
1087 | else | |
1088 | { | |
1089 | for (prev_timer = timer_list.first_timer; | |
1090 | prev_timer->next != timer_ptr; | |
1091 | prev_timer = prev_timer->next) | |
1092 | ; | |
1093 | prev_timer->next = timer_ptr->next; | |
1094 | } | |
dcb07cfa | 1095 | delete timer_ptr; |
c2c6d25f JM |
1096 | |
1097 | gdb_notifier.timeout_valid = 0; | |
1098 | } | |
1099 | ||
dcb07cfa PA |
1100 | /* Convert a std::chrono duration to a struct timeval. */ |
1101 | ||
1102 | template<typename Duration> | |
1103 | static struct timeval | |
1104 | duration_cast_timeval (const Duration &d) | |
1105 | { | |
1106 | using namespace std::chrono; | |
1107 | seconds sec = duration_cast<seconds> (d); | |
1108 | microseconds msec = duration_cast<microseconds> (d - sec); | |
1109 | ||
1110 | struct timeval tv; | |
1111 | tv.tv_sec = sec.count (); | |
1112 | tv.tv_usec = msec.count (); | |
1113 | return tv; | |
1114 | } | |
1115 | ||
70b66289 PA |
1116 | /* Update the timeout for the select() or poll(). Returns true if the |
1117 | timer has already expired, false otherwise. */ | |
6426a772 | 1118 | |
70b66289 PA |
1119 | static int |
1120 | update_wait_timeout (void) | |
c2c6d25f | 1121 | { |
2acceee2 | 1122 | if (timer_list.first_timer != NULL) |
c2c6d25f | 1123 | { |
dcb07cfa PA |
1124 | using namespace std::chrono; |
1125 | steady_clock::time_point time_now = steady_clock::now (); | |
1126 | struct timeval timeout; | |
6426a772 | 1127 | |
dcb07cfa | 1128 | if (timer_list.first_timer->when < time_now) |
c2c6d25f | 1129 | { |
70b66289 | 1130 | /* It expired already. */ |
dcb07cfa PA |
1131 | timeout.tv_sec = 0; |
1132 | timeout.tv_usec = 0; | |
1133 | } | |
1134 | else | |
1135 | { | |
1136 | steady_clock::duration d = timer_list.first_timer->when - time_now; | |
1137 | timeout = duration_cast_timeval (d); | |
c2c6d25f JM |
1138 | } |
1139 | ||
70b66289 | 1140 | /* Update the timeout for select/ poll. */ |
44f45770 EZ |
1141 | if (use_poll) |
1142 | { | |
c2c6d25f | 1143 | #ifdef HAVE_POLL |
dcb07cfa | 1144 | gdb_notifier.poll_timeout = timeout.tv_sec * 1000; |
c2c6d25f | 1145 | #else |
8e65ff28 | 1146 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 1147 | _("use_poll without HAVE_POLL")); |
44f45770 EZ |
1148 | #endif /* HAVE_POLL */ |
1149 | } | |
1150 | else | |
1151 | { | |
dcb07cfa PA |
1152 | gdb_notifier.select_timeout.tv_sec = timeout.tv_sec; |
1153 | gdb_notifier.select_timeout.tv_usec = timeout.tv_usec; | |
44f45770 | 1154 | } |
c2c6d25f | 1155 | gdb_notifier.timeout_valid = 1; |
70b66289 | 1156 | |
dcb07cfa | 1157 | if (timer_list.first_timer->when < time_now) |
70b66289 | 1158 | return 1; |
c2c6d25f | 1159 | } |
6426a772 | 1160 | else |
c2c6d25f | 1161 | gdb_notifier.timeout_valid = 0; |
70b66289 PA |
1162 | |
1163 | return 0; | |
1164 | } | |
1165 | ||
1166 | /* Check whether a timer in the timers queue is ready. If a timer is | |
1167 | ready, call its handler and return. Update the timeout for the | |
1168 | select() or poll() as well. Return 1 if an event was handled, | |
1169 | otherwise returns 0.*/ | |
1170 | ||
1171 | static int | |
1172 | poll_timers (void) | |
1173 | { | |
1174 | if (update_wait_timeout ()) | |
1175 | { | |
1176 | struct gdb_timer *timer_ptr = timer_list.first_timer; | |
1177 | timer_handler_func *proc = timer_ptr->proc; | |
1178 | gdb_client_data client_data = timer_ptr->client_data; | |
1179 | ||
1180 | /* Get rid of the timer from the beginning of the list. */ | |
1181 | timer_list.first_timer = timer_ptr->next; | |
1182 | ||
1183 | /* Delete the timer before calling the callback, not after, in | |
1184 | case the callback itself decides to try deleting the timer | |
1185 | too. */ | |
0e05cf3a | 1186 | delete timer_ptr; |
70b66289 PA |
1187 | |
1188 | /* Call the procedure associated with that timer. */ | |
1189 | (proc) (client_data); | |
1190 | ||
1191 | return 1; | |
1192 | } | |
1193 | ||
1194 | return 0; | |
c2c6d25f | 1195 | } |