0022939: Make B-Spline internal cache thread-safe to be used in multy-threaded mode
authorabv <abv@opencascade.com>
Fri, 4 May 2012 11:51:20 +0000 (15:51 +0400)
committerazn <azn@opencascade.com>
Sat, 12 May 2012 11:52:34 +0000 (15:52 +0400)
Internal cache in classes implementing b-spline curves and surface in Geom and Geom2d packages is protected from possible concurrency by mutex (added as a class field in each instance).

src/Geom/Geom_BSplineCurve.cdl
src/Geom/Geom_BSplineCurve_1.cxx
src/Geom/Geom_BSplineSurface.cdl
src/Geom/Geom_BSplineSurface_1.cxx
src/Geom2d/Geom2d_BSplineCurve.cdl
src/Geom2d/Geom2d_BSplineCurve_1.cxx

index e75a5f0..83711c3 100755 (executable)
@@ -124,7 +124,8 @@ uses  Array1OfInteger      from TColStd,
       Vec                  from gp,
       BSplKnotDistribution from GeomAbs,
       Geometry             from Geom,
-      Shape                from GeomAbs
+      Shape                from GeomAbs,
+      Mutex                from Standard
 
 
 raises ConstructionError   from Standard,
@@ -1035,4 +1036,6 @@ fields
   maxderivinv   : Real from Standard;
   maxderivinvok : Boolean from Standard;
 
+  myMutex       : Mutex from Standard;
+  -- protected bspline-cache
 end;
index 740e30b..bdd16cc 100755 (executable)
@@ -36,6 +36,7 @@
 #include <Standard_OutOfRange.hxx>
 #include <Standard_DomainError.hxx>
 #include <Standard_RangeError.hxx>
+#include <Standard_Mutex.hxx>
 
 #define  POLES    (poles->Array1())
 #define  KNOTS    (knots->Array1())
@@ -105,36 +106,36 @@ Standard_Integer Geom_BSplineCurve::Degree () const
 //purpose  : 
 //=======================================================================
 
-void Geom_BSplineCurve::D0 ( const Standard_Real U, 
-                                   gp_Pnt& P)  const 
+void Geom_BSplineCurve::D0(const Standard_Real U, gp_Pnt& P) const 
 {
-  Standard_Real  NewU = U ;
-  PeriodicNormalization(NewU) ;
-  if (!IsCacheValid(NewU)) 
-    {
-     Geom_BSplineCurve  * MyCurve = (Geom_BSplineCurve *) this ;
-     MyCurve->ValidateCache(NewU) ;
-    }
-  if (rational) {
+  Standard_Real NewU(U);
+  PeriodicNormalization(NewU);
+
+  Geom_BSplineCurve* MyCurve = (Geom_BSplineCurve *) this;
+  Standard_Mutex::Sentry aSentry(MyCurve->myMutex);
 
+  if(!IsCacheValid(NewU)) 
+    MyCurve->ValidateCache(NewU);
+
+  if(rational)
+  {
     BSplCLib::CacheD0(NewU,
-                     deg,
-                     parametercache,
-                     spanlenghtcache,
-                     (cachepoles->Array1()),
-                     cacheweights->Array1(),
-                     P) ;
-    
+      deg,
+      parametercache,
+      spanlenghtcache,
+      cachepoles->Array1(),
+      cacheweights->Array1(),
+      P);
   }
-  else {
-
+  else
+  {
     BSplCLib::CacheD0(NewU,
-                    deg,
-                    parametercache,
-                    spanlenghtcache,
-                    (cachepoles->Array1()),
-                    *((TColStd_Array1OfReal*) NULL),
-                   P) ;
+      deg,
+      parametercache,
+      spanlenghtcache,
+      cachepoles->Array1(),
+      *((TColStd_Array1OfReal*) NULL),
+      P);
   }
 }
 
@@ -147,32 +148,36 @@ void Geom_BSplineCurve::D1 (const Standard_Real U,
                                   gp_Pnt& P,
                                   gp_Vec& V1) const
 {
-Standard_Real  NewU = U ;
-  PeriodicNormalization(NewU) ;
-  if (!IsCacheValid(NewU)) 
-    {
-     Geom_BSplineCurve  * MyCurve = (Geom_BSplineCurve *) this ;
-     MyCurve->ValidateCache(NewU) ;
-    }
-  if (rational) {
+  Standard_Real NewU(U);
+  PeriodicNormalization(NewU);
+
+  Geom_BSplineCurve* MyCurve = (Geom_BSplineCurve *) this;
+  Standard_Mutex::Sentry aSentry(MyCurve->myMutex);
+
+  if(!IsCacheValid(NewU)) 
+    MyCurve->ValidateCache(NewU);
+
+  if(rational)
+  {
     BSplCLib::CacheD1(NewU,
-                     deg,
-                     parametercache,
-                     spanlenghtcache,
-                     (cachepoles->Array1()),
-                     cacheweights->Array1(),
-                     P,
-                     V1) ;
+      deg,
+      parametercache,
+      spanlenghtcache,
+      cachepoles->Array1(),
+      cacheweights->Array1(),
+      P,
+      V1);
   }
-  else {
+  else
+  {
     BSplCLib::CacheD1(NewU,
-                    deg, 
-                    parametercache,
-                    spanlenghtcache,
-                    (cachepoles->Array1()),
-                    *((TColStd_Array1OfReal*) NULL),
-                    P,
-                    V1) ;
+      deg,
+      parametercache,
+      spanlenghtcache,
+      cachepoles->Array1(),
+      *((TColStd_Array1OfReal*) NULL),
+      P,
+      V1);
   }
 }
 
@@ -181,20 +186,22 @@ Standard_Real  NewU = U ;
 //purpose  : 
 //=======================================================================
 
-void Geom_BSplineCurve::D2 (const Standard_Real U ,
-                                  gp_Pnt& P ,
-                                  gp_Vec& V1,
-                                  gp_Vec& V2 ) const
+void Geom_BSplineCurve::D2(const Standard_Real U,
+                           gp_Pnt& P,
+                           gp_Vec& V1,
+                           gp_Vec& V2) const
 {
-  Standard_Real  NewU = U ;
-  PeriodicNormalization(NewU) ;
-  if (!IsCacheValid(NewU)) 
-    {
-     Geom_BSplineCurve  * MyCurve = (Geom_BSplineCurve *) this ;
-     MyCurve->ValidateCache(NewU) ;
-    }
-  if (rational) {
+  Standard_Real NewU(U);
+  PeriodicNormalization(NewU);
+
+  Geom_BSplineCurve* MyCurve = (Geom_BSplineCurve *) this;
+  Standard_Mutex::Sentry aSentry(MyCurve->myMutex);
+
+  if(!IsCacheValid(NewU)) 
+    MyCurve->ValidateCache(NewU);
+
+  if(rational)
+  {
     BSplCLib::CacheD2(NewU,
                      deg,
                      parametercache,
@@ -203,7 +210,7 @@ void Geom_BSplineCurve::D2 (const Standard_Real U ,
                      cacheweights->Array1(),
                      P,
                      V1,
-                     V2) ;
+                     V2);
   }
   else {
     BSplCLib::CacheD2(NewU,
@@ -214,7 +221,7 @@ void Geom_BSplineCurve::D2 (const Standard_Real U ,
                      *((TColStd_Array1OfReal*) NULL),
                      P,
                      V1,
-                     V2) ;
+                     V2);
   }
 }
 
@@ -223,21 +230,24 @@ void Geom_BSplineCurve::D2 (const Standard_Real U ,
 //purpose  : 
 //=======================================================================
 
-void Geom_BSplineCurve::D3  (const Standard_Real U ,
-                            gp_Pnt& P ,
-                            gp_Vec& V1,
-                            gp_Vec& V2,
-                            gp_Vec& V3 ) const
+void Geom_BSplineCurve::D3(const Standard_Real U,
+                           gp_Pnt& P,
+                           gp_Vec& V1,
+                           gp_Vec& V2,
+                           gp_Vec& V3) const
 {
   
-Standard_Real  NewU = U ;
-  PeriodicNormalization(NewU) ;
-  if (!IsCacheValid(NewU)) 
-    {
-     Geom_BSplineCurve  * MyCurve = (Geom_BSplineCurve *) this ;
-     MyCurve->ValidateCache(NewU) ;
-    }
-  if (rational) {
+  Standard_Real NewU(U);
+  PeriodicNormalization(NewU);
+
+  Geom_BSplineCurve* MyCurve = (Geom_BSplineCurve *) this;
+  Standard_Mutex::Sentry aSentry(MyCurve->myMutex);
+
+  if(!IsCacheValid(NewU)) 
+    MyCurve->ValidateCache(NewU);
+
+  if(rational)
+  {
     BSplCLib::CacheD3(NewU,
                      deg,
                      parametercache,
@@ -249,12 +259,13 @@ Standard_Real  NewU = U ;
                      V2,
                      V3) ;
   }
-  else {
+  else
+  {
     BSplCLib::CacheD3(NewU,
                      deg,
                      parametercache,
                      spanlenghtcache,
-                     (cachepoles->Array1()),
+                     cachepoles->Array1(),
                      *((TColStd_Array1OfReal*) NULL),
                      P,
                      V1,
index d3c9149..102802f 100755 (executable)
@@ -153,8 +153,8 @@ uses  Array1OfInteger      from TColStd,
       BSplKnotDistribution from GeomAbs,
       Curve                from Geom,
       Geometry             from Geom,
-      Shape                from GeomAbs
-
+      Shape                from GeomAbs,
+      Mutex                from Standard
 
 raises ConstructionError   from Standard,
        DimensionError      from Standard,
@@ -1487,4 +1487,7 @@ fields
     vmaxderivinv  : Real from Standard;
     maxderivinvok : Boolean from Standard;
 
+    myMutex       : Mutex from Standard;
+    -- protected bsplinesurface-cache
+
 end;
index e36e065..9ffc3a1 100755 (executable)
@@ -48,6 +48,7 @@
 #include <Standard_DimensionError.hxx>
 #include <Standard_ConstructionError.hxx>
 #include <Standard_NotImplemented.hxx>
+#include <Standard_Mutex.hxx>
 
 #define  POLES    (poles->Array2())
 #define  WEIGHTS  (weights->Array2())
@@ -111,21 +112,19 @@ Standard_Boolean Geom_BSplineSurface::IsCNv
 //purpose  : 
 //=======================================================================
 
-void Geom_BSplineSurface::D0 (const Standard_Real U, 
-                             const Standard_Real V, 
-                                   gp_Pnt&       P ) const 
+void Geom_BSplineSurface::D0(const Standard_Real U,
+                             const Standard_Real V,
+                             gp_Pnt& P) const 
 {
-  Standard_Real  new_u = U,
-                 new_v = V ;
-  PeriodicNormalization(new_u,
-                       new_v) ;
-  if (!IsCacheValid(new_u,
-                    new_v))
-    {
-     Geom_BSplineSurface * my_surface = (Geom_BSplineSurface *) this ;
-     my_surface->ValidateCache(new_u,
-                              new_v) ;
-   }
+  Standard_Real  new_u(U), new_v(V);
+  PeriodicNormalization(new_u, new_v);
+
+  Geom_BSplineSurface* MySurface = (Geom_BSplineSurface *) this;
+  Standard_Mutex::Sentry aSentry(MySurface->myMutex);
+
+  if(!IsCacheValid(new_u, new_v))
+     MySurface->ValidateCache(new_u, new_v);
+
  Standard_Real uparameter_11 = (2*ucacheparameter + ucachespanlenght)/2,
                uspanlenght_11 = ucachespanlenght/2,
                vparameter_11 = (2*vcacheparameter + vcachespanlenght)/2,
@@ -164,23 +163,20 @@ void Geom_BSplineSurface::D0 (const Standard_Real U,
 //purpose  : 
 //=======================================================================
 
-void Geom_BSplineSurface::D1 (const Standard_Real U, 
-                             const Standard_Real V, 
-                                   gp_Pnt&       P,
-                                   gp_Vec&       D1U,
-                                   gp_Vec&       D1V) const
-{
-  Standard_Real  new_u = U,
-                 new_v = V ;
-  PeriodicNormalization(new_u,
-                       new_v) ;
-  if (!IsCacheValid(new_u,
-                    new_v))
-    {
-     Geom_BSplineSurface * my_surface = (Geom_BSplineSurface *) this ;
-     my_surface->ValidateCache(new_u,
-                              new_v) ;
-   }
+void Geom_BSplineSurface::D1(const Standard_Real U,
+                             const Standard_Real V,
+                             gp_Pnt& P,
+                             gp_Vec& D1U,
+                             gp_Vec& D1V) const
+{
+  Standard_Real  new_u(U), new_v(V);
+  PeriodicNormalization(new_u, new_v);
+
+  Geom_BSplineSurface* MySurface = (Geom_BSplineSurface *) this;
+  Standard_Mutex::Sentry aSentry(MySurface->myMutex);
+
+  if(!IsCacheValid(new_u, new_v))
+     MySurface->ValidateCache(new_u, new_v);
 
   Standard_Real uparameter_11 = (2*ucacheparameter + ucachespanlenght)/2,
                 uspanlenght_11 = ucachespanlenght/2,
@@ -235,18 +231,14 @@ void Geom_BSplineSurface::D2 (const Standard_Real U,
                                    gp_Vec&       D2V,
                                    gp_Vec&       D2UV) const
 {
+  Standard_Real  new_u(U), new_v(V);
+  PeriodicNormalization(new_u, new_v);
+
+  Geom_BSplineSurface* MySurface = (Geom_BSplineSurface *) this;
+  Standard_Mutex::Sentry aSentry(MySurface->myMutex);
 
-    Standard_Real  new_u = U,
-                   new_v = V ;
-  PeriodicNormalization(new_u,
-                       new_v) ;
-  if (!IsCacheValid(new_u,
-                    new_v))
-    {
-     Geom_BSplineSurface * my_surface = (Geom_BSplineSurface *) this ;
-     my_surface->ValidateCache(new_u,
-                              new_v) ;
-   }
+  if(!IsCacheValid(new_u, new_v))
+     MySurface->ValidateCache(new_u, new_v);
 
   Standard_Real uparameter_11 = (2*ucacheparameter + ucachespanlenght)/2,
                 uspanlenght_11 = ucachespanlenght/2,
index 530840b..3354595 100755 (executable)
@@ -129,8 +129,8 @@ uses  Array1OfInteger      from TColStd,
       Vec2d                from gp,
       BSplKnotDistribution from GeomAbs,
       Geometry             from Geom2d,
-      Shape                from GeomAbs
-
+      Shape                from GeomAbs,
+      Mutex                from Standard
 
 raises ConstructionError   from Standard,
        DimensionError      from Standard,
@@ -1044,4 +1044,7 @@ fields
   maxderivinv   : Real from Standard;
   maxderivinvok : Boolean from Standard;
 
+  myMutex       : Mutex from Standard;
+  -- protected bspline-cache
+
 end;
index f1c0f0a..7122ccd 100755 (executable)
@@ -35,6 +35,7 @@
 #include <Standard_OutOfRange.hxx>
 #include <Standard_DomainError.hxx>
 #include <Standard_RangeError.hxx>
+#include <Standard_Mutex.hxx>
 
 #define  POLES    (poles->Array1())
 #define  KNOTS    (knots->Array1())
@@ -109,14 +110,17 @@ Standard_Integer Geom2d_BSplineCurve::Degree () const
 void Geom2d_BSplineCurve::D0 ( const Standard_Real U, 
                              gp_Pnt2d& P) const
 {
-  Standard_Real  NewU = U ;
-  PeriodicNormalization(NewU) ;
-  if (!IsCacheValid(NewU)) {
-    Geom2d_BSplineCurve  * MyCurve = (Geom2d_BSplineCurve *) this ;
-    MyCurve->ValidateCache(NewU) ;
-  }
+  Standard_Real NewU(U);
+  PeriodicNormalization(NewU);
+
+  Geom2d_BSplineCurve* MyCurve = (Geom2d_BSplineCurve *) this;
+  Standard_Mutex::Sentry aSentry(MyCurve->myMutex);
+
+  if (!IsCacheValid(NewU))
+    MyCurve->ValidateCache(NewU);
   
-  if ( rational ) {
+  if(rational)
+  {
     BSplCLib::CacheD0(NewU,
                      deg,
                      parametercache,
@@ -146,14 +150,17 @@ void Geom2d_BSplineCurve::D1 (const Standard_Real U,
                              gp_Pnt2d& P,
                              gp_Vec2d& V1) const
 {
-  Standard_Real  NewU = U ;
-  PeriodicNormalization(NewU) ;
-  if (!IsCacheValid(NewU)) {
-    Geom2d_BSplineCurve  * MyCurve = (Geom2d_BSplineCurve *) this ;
-    MyCurve->ValidateCache(NewU) ;
-  }
+  Standard_Real NewU(U);
+  PeriodicNormalization(NewU);
 
-  if ( rational ) {
+  Geom2d_BSplineCurve* MyCurve = (Geom2d_BSplineCurve *) this;
+  Standard_Mutex::Sentry aSentry(MyCurve->myMutex);
+
+  if (!IsCacheValid(NewU))
+    MyCurve->ValidateCache(NewU);
+  
+  if(rational)
+  {
     BSplCLib::CacheD1(NewU,
                      deg,
                      parametercache,
@@ -185,14 +192,17 @@ void Geom2d_BSplineCurve::D2 (const Standard_Real U ,
                              gp_Vec2d& V1,
                              gp_Vec2d& V2 ) const
 {
-  Standard_Real  NewU = U ;
-  PeriodicNormalization(NewU) ;
-  if (!IsCacheValid(NewU)) {
-    Geom2d_BSplineCurve  * MyCurve = (Geom2d_BSplineCurve *) this ;
-    MyCurve->ValidateCache(NewU) ;
-  }
+  Standard_Real NewU(U);
+  PeriodicNormalization(NewU);
+
+  Geom2d_BSplineCurve* MyCurve = (Geom2d_BSplineCurve *) this;
+  Standard_Mutex::Sentry aSentry(MyCurve->myMutex);
+
+  if (!IsCacheValid(NewU))
+    MyCurve->ValidateCache(NewU);
   
-  if ( rational ) {
+  if(rational)
+  {
     BSplCLib::CacheD2(NewU,
                      deg,
                      parametercache,
@@ -227,14 +237,17 @@ void Geom2d_BSplineCurve::D3  (const Standard_Real U ,
                               gp_Vec2d& V2,
                               gp_Vec2d& V3 ) const
 {
-  Standard_Real  NewU = U ;
-  PeriodicNormalization(NewU) ;
-  if (!IsCacheValid(NewU)) {
-    Geom2d_BSplineCurve  * MyCurve = (Geom2d_BSplineCurve *) this ;
-    MyCurve->ValidateCache(NewU) ;
-  }
+  Standard_Real NewU(U);
+  PeriodicNormalization(NewU);
 
-  if ( rational ) {
+  Geom2d_BSplineCurve* MyCurve = (Geom2d_BSplineCurve *) this;
+  Standard_Mutex::Sentry aSentry(MyCurve->myMutex);
+
+  if (!IsCacheValid(NewU))
+    MyCurve->ValidateCache(NewU);
+  
+  if(rational)
+  {
     BSplCLib::CacheD3(NewU,
                      deg,
                      parametercache,