[babl] babl: add a lock around creation of new fishes



commit ee4a8ba6399eb9864f5bccabef02f6a7b3ea6974
Author: Øyvind Kolås <pippin gimp org>
Date:   Mon Aug 19 12:44:19 2019 +0200

    babl: add a lock around creation of new fishes
    
    This prevents multiple threads from racing in creation of a missing fish.

 babl/babl-fish-path.c | 12 ++++++++----
 babl/babl-fish.c      | 21 ++++++++++++++++++++-
 babl/babl-internal.c  |  3 +++
 babl/babl-internal.h  |  1 +
 4 files changed, 32 insertions(+), 5 deletions(-)
---
diff --git a/babl/babl-fish-path.c b/babl/babl-fish-path.c
index 9d6a0c6bb..82b728ed4 100644
--- a/babl/babl-fish-path.c
+++ b/babl/babl-fish-path.c
@@ -26,15 +26,14 @@
 #define BABL_HARD_MAX_PATH_LENGTH  8
 #define BABL_MAX_NAME_LEN          1024
 
-#define BABL_TEST_ITER             4
+#define BABL_TEST_ITER             16
 
 #ifndef MIN
 #define MIN(a, b) (((a) > (b)) ? (b) : (a))
 #endif
 
-#define NUM_TEST_PIXELS            (babl_get_num_path_test_pixels ())
 #define MAX_BUFFER_SIZE            512
-
+#define ITERATIONS                 4
 
 int   babl_in_fish_path = 0;
 
@@ -622,12 +621,17 @@ babl_fish_path2 (const Babl *source,
         max_path_length () + 2 <= BABL_HARD_MAX_PATH_LENGTH)
     {
       get_conversion_path (&pc, (Babl *) source, 0, max_path_length () + 2, tolerance);
-#if 0
+#if 1
       if (babl->fish_path.conversion_list->count)
       {
         fprintf (stderr, "babl is using very long chain, should be optimized\n");
         babl_list_each (babl->fish_path.conversion_list, show_item, NULL);
       }
+      else
+      {
+         fprintf (stderr, "babl is lacking conversion for %s to %s\n",
+               babl_get_name (source), babl_get_name (destination));
+      }
 #endif
     }
 
diff --git a/babl/babl-fish.c b/babl/babl-fish.c
index 8ce21442f..ce22b6be0 100644
--- a/babl/babl-fish.c
+++ b/babl/babl-fish.c
@@ -276,6 +276,21 @@ babl_fish (const void *source,
             return ffish.fish_path;
           }
 
+        babl_mutex_lock (babl_fish_mutex);
+        /* do a second look in the database, in case another thread held the
+           mutex and made the fish
+         */
+        if (!ffish.fish_fish)
+        {
+        babl_hash_table_find (id_htable, hashval, find_fish_path, (void *) &ffish);
+        if (ffish.fish_path)
+          {
+            /* we have found suitable fish path in the database */
+            babl_mutex_unlock (babl_fish_mutex);
+            return ffish.fish_path;
+          }
+        }
+
         if (!ffish.fish_fish)
           {
             const Babl *src_space = (void*)source_format->format.space;
@@ -289,6 +304,7 @@ babl_fish (const void *source,
 
                 if (fish_path)
                   {
+                    babl_mutex_unlock (babl_fish_mutex);
                     return fish_path;
                   }
 #if 1
@@ -330,12 +346,15 @@ babl_fish (const void *source,
     if (ffish.fish_ref)
       {
         /* we have already found suitable reference fish */
+        babl_mutex_unlock (babl_fish_mutex);
         return ffish.fish_ref;
       }
     else
       {
         /* we have to create new reference fish */
-        return babl_fish_reference (source_format, destination_format);
+        Babl *ret = babl_fish_reference (source_format, destination_format);
+        babl_mutex_unlock (babl_fish_mutex);
+        return ret;
       }
   }
 }
diff --git a/babl/babl-internal.c b/babl/babl-internal.c
index b0c1831c8..f7939a1b3 100644
--- a/babl/babl-internal.c
+++ b/babl/babl-internal.c
@@ -77,6 +77,7 @@ babl_die (void)
   exit (-1);
 }
 
+BablMutex *babl_fish_mutex;
 
 BablMutex *babl_format_mutex;
 #if BABL_DEBUG_MEM
@@ -89,6 +90,7 @@ babl_internal_init (void)
 {
   babl_set_malloc (malloc);
   babl_set_free (free);
+  babl_fish_mutex = babl_mutex_new ();
   babl_format_mutex = babl_mutex_new ();
   babl_reference_mutex = babl_mutex_new ();
 #if BABL_DEBUG_MEM
@@ -99,6 +101,7 @@ babl_internal_init (void)
 void
 babl_internal_destroy (void)
 {
+  babl_mutex_destroy (babl_fish_mutex);
   babl_mutex_destroy (babl_format_mutex);
   babl_mutex_destroy (babl_reference_mutex);
 #if BABL_DEBUG_MEM
diff --git a/babl/babl-internal.h b/babl/babl-internal.h
index c47682721..b3c97854f 100644
--- a/babl/babl-internal.h
+++ b/babl/babl-internal.h
@@ -247,6 +247,7 @@ babl_fatal (const char *format, ...)
 extern int   babl_hmpf_on_name_lookups;
 extern int   babl_in_fish_path;
 extern BablMutex *babl_format_mutex;
+extern BablMutex *babl_fish_mutex;
 extern BablMutex *babl_reference_mutex;
 
 #define BABL_DEBUG_MEM 0


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