1 // Copyright (c) 2020 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #ifndef _SelectMgr_BVHThreadPool_HeaderFile
15 #define _SelectMgr_BVHThreadPool_HeaderFile
17 #include <Standard_Transient.hxx>
18 #include <OSD_Thread.hxx>
19 #include <Standard_Mutex.hxx>
20 #include <Select3D_SensitiveEntity.hxx>
21 #include <Standard_Condition.hxx>
22 #include <Message_Messenger.hxx>
24 //! Class defining a thread pool for building BVH for the list of Select3D_SensitiveEntity within background thread(s).
25 class SelectMgr_BVHThreadPool : public Standard_Transient
27 DEFINE_STANDARD_RTTIEXT(SelectMgr_BVHThreadPool, Standard_Transient)
30 Standard_EXPORT SelectMgr_BVHThreadPool (Standard_Integer theNbThreads);
33 Standard_EXPORT virtual ~SelectMgr_BVHThreadPool();
37 //! Thread with back reference to thread pool and thread mutex in it.
38 class BVHThread : public OSD_Thread
40 friend class SelectMgr_BVHThreadPool;
45 myToCatchFpe (Standard_False)
50 //! Returns mutex used for BVH building
51 Standard_Mutex& BVHMutex()
56 //! Assignment operator.
57 BVHThread& operator= (const BVHThread& theCopy)
63 //! Assignment operator.
64 void Assign (const BVHThread& theCopy)
66 OSD_Thread::Assign (theCopy);
67 myPool = theCopy.myPool;
68 myToCatchFpe = theCopy.myToCatchFpe;
72 //! Method is executed in the context of thread.
75 //! Method is executed in the context of thread.
76 static Standard_Address runThread (Standard_Address theTask);
80 SelectMgr_BVHThreadPool* myPool;
81 Standard_Mutex myMutex;
86 //! Queue a sensitive entity to build its BVH
87 Standard_EXPORT void AddEntity (const Handle(Select3D_SensitiveEntity)& theEntity);
90 Standard_EXPORT void StopThreads();
92 //! Waits for all threads finish their jobs
93 Standard_EXPORT void WaitThreads();
95 //! Returns array of threads
96 NCollection_Array1<BVHThread>& Threads()
103 //! Class providing a simple interface to mutexes for list of BVHThread
108 //! Constructor - initializes the sentry object and locks list of mutexes immediately
109 Sentry (const Handle(SelectMgr_BVHThreadPool)& thePool)
115 //! Destructor - unlocks list of mutexes if already locked.
121 //! Lock list of mutexes
124 if (!myPool.IsNull())
126 for (Standard_Integer i = myPool->Threads().Lower(); i <= myPool->Threads().Upper(); ++i)
128 myPool->Threads().ChangeValue(i).BVHMutex().Lock();
133 //! Unlock list of mutexes
136 if (!myPool.IsNull())
138 for (Standard_Integer i = myPool->Threads().Lower(); i <= myPool->Threads().Upper(); ++i)
140 myPool->Threads().ChangeValue(i).BVHMutex().Unlock();
145 //! This method should not be called (prohibited).
146 Sentry (const Sentry &);
147 //! This method should not be called (prohibited).
148 Sentry& operator = (const Sentry &);
151 Handle(SelectMgr_BVHThreadPool) myPool;
156 NCollection_List<Handle(Select3D_SensitiveEntity)> myBVHToBuildList; //!< list of queued sensitive entities
157 NCollection_Array1<BVHThread> myBVHThreads; //!< threads to build BVH
158 Standard_Boolean myToStopBVHThread; //!< flag to stop BVH threads
159 Standard_Mutex myBVHListMutex; //!< mutex for interaction with myBVHToBuildList
160 Standard_Condition myWakeEvent; //!< raises when any sensitive is added to the BVH list
161 Standard_Condition myIdleEvent; //!< raises when BVH list become empty
162 Standard_Boolean myIsStarted; //!< indicates that threads are running