[Pixman] Making ref counting thread safe

Bill Spitzak spitzak at gmail.com
Fri Jan 7 11:19:23 PST 2011

Siarhei Siamashka wrote:

> I would suggest to generally forbid using the same pixman_image_t from multiple
> threads, but also add a function for creating pixman_image_t clone, which
> would share the pixel buffer with the original image and track pixel buffer
> lifetime via thread-safe refcounting.

I don't see any usefulness in that. All it will do is force a 
multithreaded application to make their own pixman_image_t that checks 
if the thread is different and encapsulates the ref.

You should be able to use the same source surface from any number of 
cairo_t in any number of threads. Without it programmers will just be 
forced to make a per-thread copy of all their source surfaces. However 
there is NO need for more than one thread or more than one cairo_t to 
write to a surface at a time.

The problem is that this is likely to work in many cases, producing 
unreliable code. Enforcing it may make cairo programs work better. What 
might work is any attempt to create a cairo_t to draw on a surface will 
fail if there is another one drawing to that surface or using it as a 
source, and any attempt to use a surface as a source will fail if there 
is a cairo_t to draw into it.

If this would break existing single-threaded software then perhaps it 
could not fail if the conflicting cairo_t's all belong to the same thread.

I would not do blocking (ie attempts to use a cairo_t as a source would 
block until the writing cairo_t is destroyed).

I also think the testing should be put on the code where a cairo_t is 
attached to a surface as a source or destination, not on the drawing 
operations themselves, to reduce the overhead of the testing.

Using the same cairo_t in different threads is not allowed but probably 
should not be tested for. It is almost certain to crash soon enough that 
the programmer will realize and fix it.

More information about the Pixman mailing list