[gegl/gsoc2011-opencl] Locking for Tiles



commit 4724a2e493ed7e4fec10c98285c8735852c9a711
Author: Victor Oliveira <victormatheus gmail com>
Date:   Thu Jun 9 18:42:06 2011 -0300

    Locking for Tiles
    
    I'm basing this code on the gsoc2009 branch.
    The changes create 4 locks in GeglTile, read and write for cpu and opencl, instead of
    the old general one.
    this is necessary because we can't guarantee anymore reading is in sync, because
    we can change data in the gpu without updating the correspondent data in cpu (and vice-versa).

 gegl/buffer/gegl-buffer-private.h |   30 ++++++++-
 gegl/buffer/gegl-tile.c           |  128 +++++++++++++++++++++++++++---------
 gegl/buffer/gegl-tile.h           |    3 +-
 3 files changed, 127 insertions(+), 34 deletions(-)
---
diff --git a/gegl/buffer/gegl-buffer-private.h b/gegl/buffer/gegl-buffer-private.h
index fcf11ce..53d8e3b 100644
--- a/gegl/buffer/gegl-buffer-private.h
+++ b/gegl/buffer/gegl-buffer-private.h
@@ -137,6 +137,25 @@ void            gegl_buffer_sampler           (GeglBuffer     *buffer,
 
 
 
+typedef enum
+{
+  GEGL_TILE_LOCK_NONE         = 0,
+  GEGL_TILE_LOCK_READ         = (1 << 0),
+  GEGL_TILE_LOCK_WRITE        = (1 << 1),
+  GEGL_TILE_LOCK_CL_READ      = (1 << 2),
+  GEGL_TILE_LOCK_CL_WRITE     = (1 << 3),
+  GEGL_TILE_LOCK_READWRITE    = GEGL_TILE_LOCK_READ
+                                | GEGL_TILE_LOCK_WRITE,
+  GEGL_TILE_LOCK_CL_READWRITE = GEGL_TILE_LOCK_CL_READ
+                                | GEGL_TILE_LOCK_CL_WRITE,
+  GEGL_TILE_LOCK_ALL_READ     = GEGL_TILE_LOCK_READ
+                                | GEGL_TILE_LOCK_CL_READ,
+  GEGL_TILE_LOCK_ALL_WRITE    = GEGL_TILE_LOCK_WRITE
+                                | GEGL_TILE_LOCK_CL_WRITE,
+  GEGL_TILE_LOCK_ALL          = GEGL_TILE_LOCK_READWRITE
+                                | GEGL_TILE_LOCK_CL_READWRITE,
+} GeglTileLockMode;
+
 /* the instance size of a GeglTile is a bit large, and should if possible be
  * trimmed down
  */
@@ -163,9 +182,17 @@ struct _GeglTile
   guint            stored_rev;  /* what revision was we when we from tile_storage?
                                    (currently set to 1 when loaded from disk */
 
-  gchar            lock;        /* number of times the tile is write locked
+  guint            read_locks;  /* number of times the tile is read locked   
+                                 * if lock/unlock are correctly done, it should
+                                   be 0/1, but there is no problem with shared reads
+                                 */
+
+  gchar            write_locks; /* number of times the tile is write locked
                                  * should in theory just have the values 0/1
                                  */
+
+  GeglTileLockMode lock_mode;
+
   GMutex          *mutex;
 
   /* the shared list is a doubly linked circular list */
@@ -181,6 +208,7 @@ struct _GeglTile
 
 #ifndef __GEGL_TILE_C
 #define gegl_tile_get_data(tile)  ((guchar*)((tile)->data))
+#define gegl_tile_get_cl_data(tile)  ((tile)->cl_data))
 #endif // __GEGL_TILE_C
 
 
diff --git a/gegl/buffer/gegl-tile.c b/gegl/buffer/gegl-tile.c
index 4ed9005..f48f5b1 100644
--- a/gegl/buffer/gegl-tile.c
+++ b/gegl/buffer/gegl-tile.c
@@ -103,7 +103,9 @@ gegl_tile_new_bare (void)
   tile->stored_rev = 1;
   tile->rev        = 1;
   tile->cl_rev     = 0;
-  tile->lock       = 0;
+  tile->read_locks  = 0;
+  tile->write_locks = 0;
+  tile->lock_mode   = GEGL_TILE_LOCK_NONE;
   tile->data       = NULL;
   tile->cl_data    = NULL;
 
@@ -205,37 +207,76 @@ static gint total_unlocks = 0;
 void gegl_bt (void);
 
 void
-gegl_tile_lock (GeglTile *tile)
+gegl_tile_lock (GeglTile *tile,
+                GeglTileLockMode lock_mode)
 {
-  g_mutex_lock (tile->mutex);
+  if (lock_mode != GEGL_TILE_LOCK_NONE)
+    g_mutex_lock (tile->mutex);
+  else
+    g_warning ("%s called with lock_mode GEGL_TILE_LOCK_NONE", G_STRFUNC);
 
-  if (tile->lock != 0)
+  if (!gegl_cl_is_accelerated ())
+    lock_mode &= ~GEGL_TILE_LOCK_CL_READWRITE;
+
+  if (tile->write_locks != 0)
     {
-      g_warning ("strange tile lock count: %i", tile->lock);
-      gegl_bt ();
+      if (lock_mode & GEGL_TILE_LOCK_ALL_WRITE)
+        {
+          g_warning ("strange tile lock count: %i", tile->write_lock);
+          gegl_bt ();
+        }
+      if (lock_mode & GEGL_TILE_LOCK_ALL_READ)
+        {
+          g_warning ("shouldn't lock for reading while write-lock (%i) is active",
+                     tile->write_locks);
+          gegl_bt ();
+        }
     }
+
+  if (tile->read_locks > 0)
+    {
+      if (lock_mode & GEGL_TILE_LOCK_ALL_READ)
+        {
+          g_warning ("strange tile lock count: %i", tile->read_lock);
+          gegl_bt ();
+        }
+      if (lock_mode & GEGL_TILE_LOCK_ALL_WRITE)
+        {
+          g_warning ("shouldn't lock for writing while read-lock (%i) is active",
+                     tile->read_locks);
+          gegl_bt ();
+        }
+    }
+
+
 #if 0
   total_locks++;
 #endif
 
-  tile->lock++;
+  if (lock_mode & GEGL_TILE_LOCK_ALL_READ) 
+    tile->read_lock++;
+
+  if (lock_mode & GEGL_TILE_LOCK_ALL_WRITE) 
+    tile->write_lock++;
+
   /*fprintf (stderr, "global tile locking: %i %i\n", locks, unlocks);*/
 
   gegl_tile_unclone (tile);
 
-  if (gegl_cl_is_accelerated ())
+  if (lock_mode & GEGL_TILE_LOCK_CL_READ && lock_mode & GEGL_TILE_LOCK_WRITE
+     && tile->rev > tile->cl_rev)
     {
-      if (tile->rev > tile->cl_rev)
-        {
-          gegl_cl_texture_set (tile->cl_data, tile->data, tile->size);
-          tile->cl_rev = tile->rev;
-        }
-      else if (tile->cl_rev > tile->rev)
-        {
-          gegl_cl_texture_get (tile->cl_data, tile->data, tile->size);
-          tile->rev = tile->cl_rev;
-        }
+      gegl_cl_texture_set (tile->cl_data, tile->data, tile->size);
+      tile->cl_rev = tile->rev;
     }
+  else if (lock_mode & GEGL_TILE_LOCK_READ && lock_mode & GEGL_TILE_LOCK_CL_WRITE
+    && tile->cl_rev > tile->rev)
+    {
+      gegl_cl_texture_get (tile->cl_data, tile->data, tile->size);
+      tile->rev = tile->cl_rev;
+    }
+
+  tile->lock_mode = lock_mode;
 }
 
 static void
@@ -271,27 +312,50 @@ gegl_tile_unlock (GeglTile *tile)
 #if 0
   total_unlocks++;
 #endif
-  if (tile->lock == 0)
+  if (tile->lock_mode & GEGL_TILE_LOCK_ALL_WRITE)
     {
-      g_warning ("unlocked a tile with lock count == 0");
-    }
-  tile->lock--;
+      if (tile->write_locks == 0)
+        {
+          g_warning ("unlocked a tile with write-lock count == 0");
+          gegl_bt ();
+        }
+      tile->write_locks--;
 
-  if (tile->lock == 0 &&
-      tile->z == 0)
-    {
-      gegl_tile_void_pyramid (tile);
+      if (tile->write_locks == 0
+        && tile->z == 0)
+        {
+          gegl_tile_void_pyramid (tile);
+        }
+
+      if (tile->write_locks==0)
+        {
+          if (tile->lock_mode & GEGL_TILE_LOCK_CL_WRITE)
+            {
+              tile->cl_rev = MAX(tile->rev, tile->cl_rev)+1; //cl is updated
+              if (tile->lock_mode & GEGL_TILE_LOCK_WRITE)
+                tile->rev = cl_rev; //cl and cpu are in sync
+            }
+          else 
+            tile->rev++;
+        }
     }
 
-  if (tile->lock==0)
+  if (tile->lock_mode & GEGL_TILE_LOCK_ALL_READ)
     {
-      if (gegl_cl_is_accelerated ())
-          tile->rev = tile->cl_rev = MAX(tile->rev, tile->cl_rev)+1;
-      else
-          tile->rev++;
+      if (tile->read_locks == 0)
+        {
+          g_warning ("unlocked a tile with read-lock count == 0");
+          gegl_bt ();
+        }
+      tile->read_locks--;
     }
 
-  g_mutex_unlock (tile->mutex);
+  if (tile->lock_mode != GEGL_TILE_LOCK_NONE)
+    g_mutex_unlock (tile->mutex);
+  else
+    g_warning ("%s called with lock_mode GEGL_TILE_LOCK_NONE", G_STRFUNC);
+
+  tile->lock_mode = GEGL_TILE_LOCK_NONE;
 }
 
 
diff --git a/gegl/buffer/gegl-tile.h b/gegl/buffer/gegl-tile.h
index 05d5469..7235fa9 100644
--- a/gegl/buffer/gegl-tile.h
+++ b/gegl/buffer/gegl-tile.h
@@ -30,7 +30,8 @@ void         gegl_tile_unref          (GeglTile *tile);
 /* lock a tile for writing, this would allow writing to buffers
  * later gotten with get_data()
  */
-void         gegl_tile_lock           (GeglTile *tile);
+void         gegl_tile_lock           (GeglTile *tile,
+                                       GeglTileLockMode lock_mode);
 
 /* unlock the tile notifying the tile that we're done manipulating
  * the data.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]