1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2013 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _BOPDS_Col_HeaderFile
16 #define _BOPDS_Col_HeaderFile
18 #include <Standard_Macro.hxx>
19 #include <Standard_NotImplemented.hxx>
20 #include <OSD_Parallel.hxx>
21 #include <NCollection_DataMap.hxx>
24 // 1. Implementation of Functors/Starters
29 //=======================================================================
30 //class : BOPCol_Functor
32 //=======================================================================
33 template <class TypeSolver, class TypeSolverVector>
38 explicit BOPCol_Functor(TypeSolverVector& theSolverVec)
39 : mySolvers(theSolverVec) {}
41 //! Defines functor interface.
42 void operator() (const Standard_Integer theIndex) const
44 TypeSolver& aSolver = mySolvers(theIndex);
49 BOPCol_Functor(const BOPCol_Functor&);
50 BOPCol_Functor& operator= (const BOPCol_Functor&);
53 TypeSolverVector& mySolvers;
56 //=======================================================================
59 //=======================================================================
60 template <class TypeFunctor, class TypeSolverVector>
64 static void Perform( const Standard_Boolean isRunParallel,
65 TypeSolverVector& theSolverVector )
67 TypeFunctor aFunctor(theSolverVector);
68 OSD_Parallel::For(0, theSolverVector.Extent(), aFunctor, !isRunParallel);
73 // 1.2. Context dependent version
76 //=======================================================================
77 //class : BOPCol_ContextFunctor
79 //=======================================================================
80 template <class TypeSolver, class TypeSolverVector,
81 class TypeContext, typename TN>
82 class BOPCol_ContextFunctor
84 //! Auxiliary thread ID hasher.
87 static Standard_Integer HashCode(const Standard_ThreadId theKey,
88 const Standard_Integer Upper)
90 return ::HashCode((Standard_Size)theKey, Upper);
93 static Standard_Boolean IsEqual(const Standard_ThreadId theKey1,
94 const Standard_ThreadId theKey2)
96 return theKey1 == theKey2;
100 typedef NCollection_DataMap<Standard_ThreadId, TypeContext, Hasher> ContextMap;
105 explicit BOPCol_ContextFunctor( TypeSolverVector& theVector )
106 : mySolverVector(theVector) {}
108 //! Binds main thread context
109 void SetContext( TypeContext& theContext )
111 myContexts.Bind(OSD_Thread::Current(), theContext);
114 //! Returns current thread context
115 TypeContext& GetThreadContext() const
117 const Standard_ThreadId aThreadID = OSD_Thread::Current();
118 if ( myContexts.IsBound(aThreadID) )
120 TypeContext& aContext = myContexts(aThreadID);
121 if ( aContext.IsNull() == Standard_False )
125 // Create new context
126 TypeContext aContext = new TN
127 ( NCollection_BaseAllocator::CommonBaseAllocator() );
129 Standard_Mutex::Sentry aLocker(myMutex);
130 myContexts.Bind(aThreadID, aContext);
132 return myContexts(aThreadID);
135 //! Defines functor interface
136 void operator()( const Standard_Integer theIndex ) const
138 TypeContext& aContext = GetThreadContext();
139 TypeSolver& aSolver = mySolverVector(theIndex);
141 aSolver.SetContext(aContext);
146 BOPCol_ContextFunctor(const BOPCol_ContextFunctor&);
147 BOPCol_ContextFunctor& operator= (const BOPCol_ContextFunctor&);
150 TypeSolverVector& mySolverVector;
151 mutable ContextMap myContexts;
152 mutable Standard_Mutex myMutex;
155 //=======================================================================
156 //class : BOPCol_ContextCnt
158 //=======================================================================
159 template <class TypeFunctor, class TypeSolverVector, class TypeContext>
160 class BOPCol_ContextCnt
163 static void Perform( const Standard_Boolean isRunParallel,
164 TypeSolverVector& theSolverVector,
165 TypeContext& theContext )
167 TypeFunctor aFunctor(theSolverVector);
168 aFunctor.SetContext(theContext);
170 OSD_Parallel::For(0, theSolverVector.Extent(), aFunctor, !isRunParallel);