[cairo] [PATCH] Do not hold mutexes when calling destructors.

Behdad Esfahbod behdad at behdad.org
Thu May 15 13:32:38 PDT 2008

On Thu, 2008-05-15 at 16:23 -0400, Behdad Esfahbod wrote:
> On Thu, 2008-05-15 at 16:17 -0400, Jeff Muizelaar wrote:
> > You've lost me. Both implementations (mine and krh's) lock when ever
> > they modify the cache...
> Humm, right.  Anyhoo...

I know I thought of a problem before... Here it is.  In your loop:

+    while (solid_surface_cache.size) {
+	cairo_surface_t *surface = solid_surface_cache.cache[solid_surface_cache.size-1].surface;
+	solid_surface_cache.size--;
+	/* release the lock to avoid the possibility of a recursive
+	 * deadlock when the scaled font destroy closure gets called */
+	CAIRO_MUTEX_UNLOCK (_cairo_pattern_solid_surface_cache_lock);
+	cairo_surface_destroy (surface);
+	CAIRO_MUTEX_LOCK (_cairo_pattern_solid_surface_cache_lock);
+    }

solid_surface_cache.size is not declared volatile, so compiler is free
to replace that with a simple countdown loop, but since it may be
modified from other threads, you may end up destroying an entry twice.
Or things like that.


"Those who would give up Essential Liberty to purchase a little
 Temporary Safety, deserve neither Liberty nor Safety."
        -- Benjamin Franklin, 1759

More information about the cairo mailing list