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 _BOPTools_Parallel_HeaderFile
16 #define _BOPTools_Parallel_HeaderFile
18 #include <Standard_Macro.hxx>
19 #include <Standard_NotImplemented.hxx>
20 #include <OSD_Parallel.hxx>
21 #include <NCollection_DataMap.hxx>
22 #include <Standard_Mutex.hxx>
23 #include <OSD_Thread.hxx>
26 // 1. Implementation of Functors/Starters
31 //=======================================================================
32 //class : BOPTools_Functor
34 //=======================================================================
35 template <class TypeSolver, class TypeSolverVector>
36 class BOPTools_Functor
40 explicit BOPTools_Functor(TypeSolverVector& theSolverVec)
41 : mySolvers(theSolverVec) {}
43 //! Defines functor interface.
44 void operator() (const Standard_Integer theIndex) const
46 TypeSolver& aSolver = mySolvers(theIndex);
51 BOPTools_Functor(const BOPTools_Functor&);
52 BOPTools_Functor& operator= (const BOPTools_Functor&);
55 TypeSolverVector& mySolvers;
58 //=======================================================================
59 //class : BOPTools_Cnt
61 //=======================================================================
62 template <class TypeFunctor, class TypeSolverVector>
66 static void Perform( const Standard_Boolean isRunParallel,
67 TypeSolverVector& theSolverVector )
69 TypeFunctor aFunctor(theSolverVector);
70 OSD_Parallel::For(0, theSolverVector.Length(), aFunctor, !isRunParallel);
75 // 1.2. Context dependent version
78 //=======================================================================
79 //class : BOPTools_ContextFunctor
81 //=======================================================================
82 template <class TypeSolver, class TypeSolverVector,
83 class TypeContext, typename TN>
84 class BOPTools_ContextFunctor
86 //! Auxiliary thread ID hasher.
89 static Standard_Integer HashCode(const Standard_ThreadId theKey,
90 const Standard_Integer Upper)
92 return ::HashCode((Standard_Size)theKey, Upper);
95 static Standard_Boolean IsEqual(const Standard_ThreadId theKey1,
96 const Standard_ThreadId theKey2)
98 return theKey1 == theKey2;
102 typedef NCollection_DataMap<Standard_ThreadId, TypeContext, Hasher> ContextMap;
107 explicit BOPTools_ContextFunctor( TypeSolverVector& theVector )
108 : mySolverVector(theVector) {}
110 //! Binds main thread context
111 void SetContext( TypeContext& theContext )
113 myContexts.Bind(OSD_Thread::Current(), theContext);
116 //! Returns current thread context
117 TypeContext& GetThreadContext() const
119 const Standard_ThreadId aThreadID = OSD_Thread::Current();
120 if ( myContexts.IsBound(aThreadID) )
122 TypeContext& aContext = myContexts(aThreadID);
123 if ( aContext.IsNull() == Standard_False )
127 // Create new context
128 TypeContext aContext = new TN
129 ( NCollection_BaseAllocator::CommonBaseAllocator() );
131 Standard_Mutex::Sentry aLocker(myMutex);
132 myContexts.Bind(aThreadID, aContext);
134 return myContexts(aThreadID);
137 //! Defines functor interface
138 void operator()( const Standard_Integer theIndex ) const
140 TypeContext& aContext = GetThreadContext();
141 TypeSolver& aSolver = mySolverVector(theIndex);
143 aSolver.SetContext(aContext);
148 BOPTools_ContextFunctor(const BOPTools_ContextFunctor&);
149 BOPTools_ContextFunctor& operator= (const BOPTools_ContextFunctor&);
152 TypeSolverVector& mySolverVector;
153 mutable ContextMap myContexts;
154 mutable Standard_Mutex myMutex;
157 //=======================================================================
158 //class : BOPTools_ContextCnt
160 //=======================================================================
161 template <class TypeFunctor, class TypeSolverVector, class TypeContext>
162 class BOPTools_ContextCnt
165 static void Perform( const Standard_Boolean isRunParallel,
166 TypeSolverVector& theSolverVector,
167 TypeContext& theContext )
169 TypeFunctor aFunctor(theSolverVector);
170 aFunctor.SetContext(theContext);
172 OSD_Parallel::For(0, theSolverVector.Length(), aFunctor, !isRunParallel);