event loop and timers

Steven J Abner pheonix.sja at att.net
Mon Sep 2 17:21:17 UTC 2024


Again, thank you. I worked out the pseudocode to this:

  do {
    int timeout;
    xcb_generic_event_t *event;
    while ((event = xcb_poll_for_event(connection)) != NULL)
        /* _process_event() returns 'running' for main loop. */
      if (_process_event(event) == false) goto cleanup;
    do {
        /* _process_timers() runs any timers that are immediately 
pending,
           and returns the number of milliseconds until the next timer
           in the queue is ready.
           _process_timers() returns INT_MAX if there are no timers. */
        /* _process_timers() also removes from timeout its run time. */
      timeout = _process_timers();
      if ((event = xcb_poll_for_queued_event(connection)) == NULL) 
break;
        if (_process_event(event) == false) goto cleanup;
    } while (1);
    if (timeout > 20) {
      struct pollfd pfd[1] = {
        xcb_get_file_descriptor(connection), POLLRDNORM, POLLWRNORM
      };
      poll(pfd, 1, timeout);
    }
  } while (1);
    /* received XCB_DESTROY_NOTIFY */
cleanup:

 queued_event() hasn't occurred, and _process_timer() is 2ms at most, 
with rounding.
I love it!
 But I also, besides thank you, wish to pick your brain one more time, 
please.
The timeout value, while just sitting idle after timer kicks in, can 
vary more than I expected. The timer is for 10000ms and timeout range 
from  9999 to 900 with only a single printf() just before poll(). I had 
expected a flatter response of maybe 8000.
Is this due to the xcb_poll_* calls? Do you have a guess where or what 
I might do to improve? Or is this normal for a poll() loop on x86_64?
 Steve




More information about the Xcb mailing list