[Xcb] questions about proper event loop and timers

Barton C Massey bart at cs.pdx.edu
Sun Aug 13 18:35:16 PDT 2006


Greetings!

As you noted, timer_create() and timer_settime() set up a
timer that will deliver a signal to the calling thread when
it expires.  As you and Ian noted, you probably want to
block rather than busy-waiting for events until the timer
expires.  You have basically two choices:

  * If the callback action you want to take upon timer
    expiration is safe to execute in a signal handler,
    you can simply register it as the signal handler.  I
    believe that timers send SIG_ALARM by default.

  * Otherwise, you can spawn a separate thread that waits
    for the timer to expire and then calls back.  (Looking
    at it the other way, you can spawn a separate thread to
    handle X events.  This is an encouraged idiom in XCB.)
    sigwait() will be useful here.

An XCB utility library that handles timed callbacks and
event processing would be a quite sensible thing to write.
I think it can be written transparently atop XCB and run
single-threaded with a little thought.

Most toolkits can run atop Xlib/XCB these days, and atop
pure XCB shortly.  Maybe programming to a toolkit that has
the functionality you want already present would be a better
approach?  While standalone apps are quite writable using
XCB directly, it really is intended more as a building block
for layering atop.

We could consider putting the timeout value of the select()s
inside XCB under user control.  I'm not very excited about
this, but perhaps others on the list feel differently?

I would not recommend polling for events, nor trying to
select on the XCB connection fd.  Both of these approaches
have a high potential for problems, IMHO.

Good luck, and please let us know if we can be of further
help in this matter!

    Bart Massey
    Assoc. Prof. Computer Science
    Portland State University
    bart at cs.pdx.edu

In message <e88138500608120907n5e658258tf03eee5d6be62431 at mail.gmail.com> you wrote:
> Hi,
> I'm new to xcb usage and running into a bit of problem.
> 
> I'm trying to figure out the best way to put together an event loop
> that uses both timers and x events in a single thread.
> 
> If I was just catching/dispatching x events I'd do something like
> 
> XCBGenericEvent* xcbEvent = XCBWaitForEvent( connection );
> while ( NULL != xcbEvent ) {
>     //process the event..
>     xcbEvent = XCBWaitForEvent( connection );
> }
> 
> This works fine, no busy waiting, no CPU spiking
> 
> 
> However if I want to add support for timers, i.e. registering some
> callback function that gets invoked every x milliseconds, then I'm a
> bit stuck.
> 
> As I understand it, on linux, timers are handled with timer_create() and
> timer_settime(). If I use these API's then how do I set up the XCB
> event loop such that I don't have to poll all the time? Won't
> XCBWaitForEvent() block, and then the callback for the timer not get
> triggered (because the thread is blocked in the XCBWaitForEvent()
> code)?
> 
> Is there a standard way to handle timers with XCB? Alternately, would
> anyone consider adding some extra functions that allow for add timer
> support into the XCB API's?
> 
> 
> Thanks
> 
> Jim Crafton
> _______________________________________________
> Xcb mailing list
> Xcb at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/xcb


More information about the Xcb mailing list