File indexing completed on 2025-01-18 10:04:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _SelectMgr_BVHThreadPool_HeaderFile
0015 #define _SelectMgr_BVHThreadPool_HeaderFile
0016
0017 #include <Standard_Transient.hxx>
0018 #include <OSD_Thread.hxx>
0019 #include <Standard_Mutex.hxx>
0020 #include <Select3D_SensitiveEntity.hxx>
0021 #include <Standard_Condition.hxx>
0022 #include <Message_Messenger.hxx>
0023
0024
0025 class SelectMgr_BVHThreadPool : public Standard_Transient
0026 {
0027 DEFINE_STANDARD_RTTIEXT(SelectMgr_BVHThreadPool, Standard_Transient)
0028 public:
0029
0030 Standard_EXPORT SelectMgr_BVHThreadPool (Standard_Integer theNbThreads);
0031
0032
0033 Standard_EXPORT virtual ~SelectMgr_BVHThreadPool();
0034
0035 public:
0036
0037
0038 class BVHThread : public OSD_Thread
0039 {
0040 friend class SelectMgr_BVHThreadPool;
0041 public:
0042
0043 BVHThread()
0044 : OSD_Thread(),
0045 myPool(nullptr),
0046 myMutex(),
0047 myToCatchFpe (Standard_False)
0048 {}
0049
0050
0051 BVHThread(const BVHThread& theOther)
0052 : OSD_Thread(theOther),
0053 myPool(theOther.myPool),
0054 myMutex(),
0055 myToCatchFpe(theOther.myToCatchFpe)
0056 {}
0057
0058
0059 Standard_Mutex& BVHMutex()
0060 {
0061 return myMutex;
0062 }
0063
0064
0065 BVHThread& operator= (const BVHThread& theCopy)
0066 {
0067 Assign (theCopy);
0068 return *this;
0069 }
0070
0071
0072 void Assign (const BVHThread& theCopy)
0073 {
0074 OSD_Thread::Assign (theCopy);
0075 myPool = theCopy.myPool;
0076 myToCatchFpe = theCopy.myToCatchFpe;
0077 }
0078
0079 private:
0080
0081 void performThread();
0082
0083
0084 static Standard_Address runThread (Standard_Address theTask);
0085
0086 private:
0087
0088 SelectMgr_BVHThreadPool* myPool;
0089 Standard_Mutex myMutex;
0090 bool myToCatchFpe;
0091 };
0092
0093 public:
0094
0095 Standard_EXPORT void AddEntity (const Handle(Select3D_SensitiveEntity)& theEntity);
0096
0097
0098 Standard_EXPORT void StopThreads();
0099
0100
0101 Standard_EXPORT void WaitThreads();
0102
0103
0104 NCollection_Array1<BVHThread>& Threads()
0105 {
0106 return myBVHThreads;
0107 }
0108
0109 public:
0110
0111
0112 class Sentry
0113 {
0114 public:
0115
0116
0117 Sentry (const Handle(SelectMgr_BVHThreadPool)& thePool)
0118 : myPool (thePool)
0119 {
0120 Lock();
0121 }
0122
0123
0124 ~Sentry()
0125 {
0126 Unlock();
0127 }
0128
0129
0130 void Lock()
0131 {
0132 if (!myPool.IsNull())
0133 {
0134 for (Standard_Integer i = myPool->Threads().Lower(); i <= myPool->Threads().Upper(); ++i)
0135 {
0136 myPool->Threads().ChangeValue(i).BVHMutex().Lock();
0137 }
0138 }
0139 }
0140
0141
0142 void Unlock()
0143 {
0144 if (!myPool.IsNull())
0145 {
0146 for (Standard_Integer i = myPool->Threads().Lower(); i <= myPool->Threads().Upper(); ++i)
0147 {
0148 myPool->Threads().ChangeValue(i).BVHMutex().Unlock();
0149 }
0150 }
0151 }
0152
0153
0154 Sentry (const Sentry &);
0155
0156 Sentry& operator = (const Sentry &);
0157
0158 private:
0159 Handle(SelectMgr_BVHThreadPool) myPool;
0160 };
0161
0162 protected:
0163
0164 NCollection_List<Handle(Select3D_SensitiveEntity)> myBVHToBuildList;
0165 NCollection_Array1<BVHThread> myBVHThreads;
0166 Standard_Boolean myToStopBVHThread;
0167 Standard_Mutex myBVHListMutex;
0168 Standard_Condition myWakeEvent;
0169 Standard_Condition myIdleEvent;
0170 Standard_Boolean myIsStarted;
0171 };
0172
0173 #endif