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