[gegl] perlin-noise: fix racy initialization



commit 650f9552c38e6a76af0eda5620e20c4f59f6231a
Author: Øyvind Kolås <pippin gimp org>
Date:   Fri May 24 14:28:00 2019 +0200

    perlin-noise: fix racy initialization
    
    Fixing issue #160, by initializing the tables used by perlin noise during
    class_init of the GeglOperation using it. There is much room for optimization
    by refactoring the gegl-op to directly include inlined versions of the code
    currenly in the perlin folder.

 operations/common/noise-perlin.c  |  2 ++
 operations/common/perlin/perlin.c | 23 +++++------------------
 2 files changed, 7 insertions(+), 18 deletions(-)
---
diff --git a/operations/common/noise-perlin.c b/operations/common/noise-perlin.c
index d25dd1a72..d435f2f48 100644
--- a/operations/common/noise-perlin.c
+++ b/operations/common/noise-perlin.c
@@ -97,6 +97,8 @@ gegl_op_class_init (GeglOpClass *klass)
   GeglOperationClass            *operation_class;
   GeglOperationPointRenderClass *point_render_class;
 
+  perlin_init ();
+
   operation_class = GEGL_OPERATION_CLASS (klass);
   point_render_class = GEGL_OPERATION_POINT_RENDER_CLASS (klass);
 
diff --git a/operations/common/perlin/perlin.c b/operations/common/perlin/perlin.c
index 4e13cb3ca..a1ae3615e 100644
--- a/operations/common/perlin/perlin.c
+++ b/operations/common/perlin/perlin.c
@@ -14,7 +14,6 @@ static int p[B + B + 2];
 static double g3[B + B + 2][3];
 static double g2[B + B + 2][2];
 static double g1[B + B + 2];
-static int start = 1;
 
 double
 noise1 (double arg)
@@ -23,11 +22,6 @@ noise1 (double arg)
   double    rx0, rx1, sx, t, u, v, vec[1];
 
   vec[0] = arg;
-  if (start)
-    {
-      start = 0;
-      perlin_init ();
-    }
 
   setup (0, bx0, bx1, rx0, rx1);
 
@@ -45,12 +39,6 @@ noise2 (double vec[2])
   double    rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v;
   int       i, j;
 
-  if (start)
-    {
-      start = 0;
-      perlin_init ();
-    }
-
   setup (0, bx0, bx1, rx0, rx1);
   setup (1, by0, by1, ry0, ry1);
 
@@ -87,12 +75,6 @@ noise3 (double vec[3])
   double    rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
   int       i, j;
 
-  if (start)
-    {
-      start = 0;
-      perlin_init ();
-    }
-
   setup (0, bx0, bx1, rx0, rx1);
   setup (1, by0, by1, ry0, ry1);
   setup (2, bz0, bz1, rz0, rz1);
@@ -165,6 +147,10 @@ void
 perlin_init (void)
 {
   int       i, j, k;
+  static int initialized = 0;
+  if (initialized)
+    return;
+  /* this is racy - but we call it once when creating our perlin noise op */
 
   g_random_set_seed (1234567890);
 
@@ -198,6 +184,7 @@ perlin_init (void)
       for (j = 0; j < 3; j++)
         g3[B + i][j] = g3[i][j];
     }
+  initialized = 1;
 }
 
 /* --- My harmonic summing functions - PDB --------------------------*/


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