From 57ffcdf014d3a1be3fc2ee6b85aa03b9a813dbdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Tue, 17 Feb 2009 12:00:08 +0000 Subject: [PATCH] Added a mutex for clutter asynchronous textures threads. Bug #1453 - Asynchronous texture loading can starve cpu. Add a mutex that is held in the loader threads during the image decoding. We were spawning and starting a thread for each asynchronously loaded texture. This can cause cpu / memory starvation when many pixbuf loaders allocate their temporary memory at the same time. Also added -fno-strict-aliasing to MAINTAINER_CFLAGS in configure.ac to avoid incorrect warnings caused by the static mutex code. --- clutter/clutter-texture.c | 18 +++++++++++++++++- configure.ac | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/clutter/clutter-texture.c b/clutter/clutter-texture.c index aabb99ed6..3e3cec618 100644 --- a/clutter/clutter-texture.c +++ b/clutter/clutter-texture.c @@ -104,6 +104,7 @@ struct _ClutterTexturePrivate gchar *load_filename; CoglBitmap *load_bitmap; GError *load_error; + gboolean abort; }; enum @@ -639,6 +640,7 @@ clutter_texture_async_load_cancel (ClutterTexture *texture) if (priv->load_thread) { + priv->abort = TRUE; g_thread_join (priv->load_thread); priv->load_thread = NULL; } @@ -1609,12 +1611,25 @@ clutter_texture_thread_cb (gpointer data) static gpointer clutter_texture_thread_func (gpointer data) { - ClutterTexture *self = data; + static GStaticMutex thread_load_mutex = G_STATIC_MUTEX_INIT; + ClutterTexture *self = data; ClutterTexturePrivate *priv = self->priv; + /* we aquire the shared lock, only one thread is allowed to + * be loading at a time + */ + g_static_mutex_lock (&thread_load_mutex); + if (priv->abort) + { + g_static_mutex_unlock (&thread_load_mutex); + g_free (priv->load_filename); + priv->load_filename = NULL; + return NULL; + } /* Try loading with imaging backend */ priv->load_bitmap = cogl_bitmap_new_from_file (priv->load_filename, &priv->load_error); + g_static_mutex_unlock (&thread_load_mutex); g_free (priv->load_filename); priv->load_filename = NULL; @@ -1649,6 +1664,7 @@ clutter_texture_idle_func (gpointer data) return FALSE; } + /* * clutter_texture_async_load: * @self: a #ClutterTexture diff --git a/configure.ac b/configure.ac index 53d00ffd0..503ecd9d9 100644 --- a/configure.ac +++ b/configure.ac @@ -590,7 +590,7 @@ AC_ARG_ENABLE([maintainer-flags], enable_maintainer_flags=maintainer_flags_default) if test "x$enable_maintainer_flags" = "xyes"; then - MAINTAINER_CFLAGS="-Werror -Wall -Wshadow -Wcast-align -Wno-uninitialized -Wempty-body -Wformat-security -Winit-self" + MAINTAINER_CFLAGS="-Werror -Wall -Wshadow -Wcast-align -Wno-strict-aliasing -Wno-uninitialized -Wempty-body -Wformat-security -Winit-self" fi AC_SUBST(MAINTAINER_CFLAGS)