0022939: Make B-Spline internal cache thread-safe to be used in multy-threaded mode
[occt.git] / src / Geom / Geom_BSplineCurve_1.cxx
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,