libX11 and thread-safety
pma at anderson.fc.hp.com
Tue Jun 7 17:39:50 PDT 2005
> Has anyone else experienced thread safety problems with libX11?
> Specifically, I'm finding that some threaded apps that use libX11 are
> getting hung up on the ConditionWait() in _XReply().
> The apps are calling XInitThreads() as they should.
> I tried commenting-out the ConditionWait() and that seemed to solve my
> problem. Of course, that's not a proper fix.
> Just wondering if anyone else has come across anything like this or
> has any ideas.
Several threads problems have been fixed over the years.
A look at XlibInt.c definitely illustrates that thread
support was added after-the-fact, but I have found things
to be fairly stable recently. (I think the XCB project
dealt with threads from the beginning, so it probably solves
threading in a cleaner fashion.)
There were also some bugs outside of XlibInt.c with incorrect
reply lengths that could cause threaded apps to hang, but I
think fixes for the known bugs have been in the tree for several
years now. Incorrect reply lengths are a problem when threads
are active because Xlib will wait until it has read exactly the
number of reply bytes specified. You can get by with incorrect
reply lengths without threads, but they must be correct when threads
There are also some threading issues in the XIM code that I've
recently been looking at, but that's another can of worms and
unrelated to what you are seeing.
As you noted, XInitThreads() does need to be called. Not only does
it need to be called, but it needs to be called before any
other Xlib call. I think I remember seeing problem a long
time ago where XInitThreads() was called long after XOpenDisplay().
With respect to ConditionWait() in _XReply(), I did come across
a bug in that area once. The bug was in an application that
a customer wouldn't let me have, so I had to use printf()
and some tracing tools just to make a wild guess about what
was going on. I think changing the 'if' around that ConditionWait()
block to 'while' addressed that specific problem. (This is the
ConditionWait() call right after the call to _XFlushInt() in _XReply().)
Unfortunately, I couldn't do any more analysis than that in order
to figure out what was really going on. If I could have, I would
have done so and hopefully been able to gather enough information
to put together a bug report that I had some confidence in. I
would have liked to understand it better, since there are other
places in XlibInt.c that use ConditionWait(), and I would have
liked to see if those needed changes as well.
The change might be worth a try for the problem you're working on.
If you can reproduce the problem reliably and if this change
addresses it, that might increase our confidence that the change
is a reasonable thing to do.
Hope this helps,
More information about the xorg