Discussion:
XDrawPoint(s) etc MT safe?
Dennis Clarke
2018-10-17 01:58:14 UTC
Permalink
Dear Xorg :

Something I had not thought of came up today. Could multiple threads
call XDrawPoint() and then XFlush() ? Suppose sixteen threads are
dispatched to do some foo and each of them utters some XDrawPoint()
calls and then XFlush()? Is that remotely thread safe?

Dennis
_______________________________________________
***@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/li
Dennis Clarke
2018-10-17 02:04:15 UTC
Permalink
    Something I had not thought of came up today. Could multiple threads
call XDrawPoint() and then XFlush() ?  Suppose sixteen threads are
dispatched to do some foo and each of them utters some XDrawPoint()
calls and then XFlush()?  Is that remotely thread safe?
Sorry, assume XInitThreads() is in place. Am I stuck with using
XLockDisplay() and XUnlockDisplay() and really multiple threads can
not really do work simultaneously. That is the question.

Dennis
_______________________________________________
***@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/listinfo/xorg
Your subscripti
Carsten Haitzler (The Rasterman)
2018-10-17 09:37:10 UTC
Permalink
Post by Dennis Clarke
    Something I had not thought of came up today. Could multiple threads
call XDrawPoint() and then XFlush() ?  Suppose sixteen threads are
dispatched to do some foo and each of them utters some XDrawPoint()
calls and then XFlush()?  Is that remotely thread safe?
Sorry, assume XInitThreads() is in place. Am I stuck with using
XLockDisplay() and XUnlockDisplay() and really multiple threads can
not really do work simultaneously. That is the question.
xinitthreads should make anything in xlib "mt safe".. but only in as much as
doing the calls will not mess up xlib state or the socket/protocol pipeline.
(note that if this does happen it'd be an xlib/xcb bug).

but ... how can you sensibly have 2 threads draw to the same target in any
way ... unless you very specifically avoid ever overdrawing on the same areas
(e.g. use 4 threads and split region into a 2x2 grid of target areas to draw
into), or you always draw the same color points so that it doesn't matter which
gets drawn first etc. ... ? xflush will flush whatever is queued in the xlib
buffer irrespective of which thread put it there... :)
--
------------- Codito, ergo sum - "I code, therefore I am" --------------
Carsten Haitzler - ***@rasterman.com

_______________________________________________
***@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/listinfo/xor
Dennis Clarke
2018-10-17 17:59:59 UTC
Permalink
Post by Carsten Haitzler (The Rasterman)
Post by Dennis Clarke
    Something I had not thought of came up today. Could multiple threads
call XDrawPoint() and then XFlush() ?  Suppose sixteen threads are
dispatched to do some foo and each of them utters some XDrawPoint()
calls and then XFlush()?  Is that remotely thread safe?
Sorry, assume XInitThreads() is in place. Am I stuck with using
XLockDisplay() and XUnlockDisplay() and really multiple threads can
not really do work simultaneously. That is the question.
xinitthreads should make anything in xlib "mt safe"..
OKay .. good enough for me .. for now at least.
Post by Carsten Haitzler (The Rasterman)
but ... how can you sensibly have 2 threads draw to the same target in any
way ... unless you very specifically avoid ever overdrawing ...
Exactly that way.

Dennis
_______________________________________________
***@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/listinfo/xorg
Your subscription ad
Adam Jackson
2018-10-17 15:09:32 UTC
Permalink
Post by Dennis Clarke
Post by Dennis Clarke
Something I had not thought of came up today. Could multiple threads
call XDrawPoint() and then XFlush() ? Suppose sixteen threads are
dispatched to do some foo and each of them utters some XDrawPoint()
calls and then XFlush()? Is that remotely thread safe?
Sorry, assume XInitThreads() is in place. Am I stuck with using
XLockDisplay() and XUnlockDisplay() and really multiple threads can
not really do work simultaneously. That is the question.
Individual Xlib function calls are thread-safe, in that they internally
lock the Display while they're running to avoid multiple threads
modifying the Display state. Note how the first thing XDrawPoint does
is call LockDisplay:

https://gitlab.freedesktop.org/xorg/lib/libx11/blob/master/src/DrPoint.c#L36

So if you had two threads calling XDrawPoint in parallel, the second
one would block at that LockDisplay until the first one was done.

You only need XLockDisplay() if you're trying to establish an atomic
sequence of actions with respect to your sibling threads. For example,
if one thread was doing XQueryTree in a loop, and another was creating
and destroying windows, you could end up with a sequence like:

Thread A creates window 1
Thread B queries for the root window's children, learns window 1
Thread A destroys window 1
Thread B queries for window 1's children, gets BadWindow

- ajax

_______________________________________________
***@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/listinfo/xorg
Your subscription
Dennis Clarke
2018-10-17 19:53:23 UTC
Permalink
Post by Adam Jackson
Post by Dennis Clarke
Post by Dennis Clarke
Something I had not thought of came up today. Could multiple threads
call XDrawPoint() and then XFlush() ? Suppose sixteen threads are
dispatched to do some foo and each of them utters some XDrawPoint()
calls and then XFlush()? Is that remotely thread safe?
Sorry, assume XInitThreads() is in place. Am I stuck with using
XLockDisplay() and XUnlockDisplay() and really multiple threads can
not really do work simultaneously. That is the question.
Individual Xlib function calls are thread-safe, in that they internally
lock the Display while they're running to avoid multiple threads
modifying the Display state. Note how the first thing XDrawPoint does
https://gitlab.freedesktop.org/xorg/lib/libx11/blob/master/src/DrPoint.c#L36
Beauty. So I don't really need to call XLockDisplay or XUnlockDisplay
inside any given thread. Looks redundant. That helps.
Post by Adam Jackson
So if you had two threads calling XDrawPoint in parallel, the second
one would block at that LockDisplay until the first one was done.
Perfect ... thank you!
Post by Adam Jackson
You only need XLockDisplay() if you're trying to establish an atomic
sequence of actions with respect to your sibling threads. For example,
if one thread was doing XQueryTree in a loop, and another was creating
Thread A creates window 1
Thread B queries for the root window's children, learns window 1
Thread A destroys window 1
Thread B queries for window 1's children, gets BadWindow
ah yes .. well I hope to never have a thread do anything like that.

Thank you.

Dennis




_______________________________________________
***@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/listinfo/xorg
Your subscription address: %(use

Loading...