Changeset 3213 in Sophya for trunk/SophyaLib/BaseTools/segdatablock.h
- Timestamp:
- Apr 11, 2007, 6:12:56 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/segdatablock.h
r2884 r3213 13 13 #include <iomanip> 14 14 #include <typeinfo> 15 16 #include "thsafeop.h" // for ThreadSafe operations (Ref.Count/Share) 15 17 16 18 /*! … … 67 69 { 68 70 mSRef = NULL; 71 if (gThsop == NULL) gThsop = new ThSafeOp; 69 72 SetSize(segsz, nbseg); 70 73 } … … 72 75 SegDataBlock(const SegDataBlock<T>& a) 73 76 { 77 gThsop->lock(); // (ThreadSafe) - Start of atomic operation 74 78 mSRef = a.mSRef; 75 79 mSRef->nref++; 80 gThsop->unlock(); // (ThreadSafe) - End of atomic operation 76 81 } 77 82 //! copy constructor with specification of flags for data sharing and element value copy … … 79 84 { 80 85 if (share) { 86 gThsop->lock(); // (ThreadSafe) - Start of atomic operation 81 87 mSRef = a.mSRef; 82 88 mSRef->nref++; 89 gThsop->unlock(); // (ThreadSafe) - End of atomic operation 83 90 } 84 91 else { … … 90 97 SegDataBlock(const SegDBInterface<T>& a) 91 98 { 99 if (gThsop == NULL) gThsop = new ThSafeOp; 92 100 SegDataBlock<T> * sdb = dynamic_cast< SegDataBlock<T> *>(&a); 93 101 if (sdb != NULL) { 102 gThsop->lock(); // (ThreadSafe) - Start of atomic operation 94 103 mSRef = sdb->mSRef; 95 104 mSRef->nref++; 105 gThsop->unlock(); // (ThreadSafe) - End of atomic operation 96 106 } 97 107 else Clone(a, true); … … 101 111 { 102 112 //DEL cout << " DEBUG-~SegDataBlock() " << hex << mSRef << dec << " NRef()= " << NRef() << endl; 103 Delete(); 104 } 105 106 107 //! Adds one segment to the data structure - returns the pointer to the allocated segment. 113 gThsop->lock(); // (ThreadSafe) - Start of atomic operation 114 Delete(); 115 gThsop->unlock(); // (ThreadSafe) - End of atomic operation 116 } 117 118 119 //! Adds one segment to the data structure - returns the number of allocated segments. 108 120 virtual size_t Extend() 109 121 { 110 122 T * p = new T[mSRef->segsize]; 123 gThsop->lock(); // (ThreadSafe) - Start of atomic operation 111 124 mSRef->dseg.push_back(p); 125 gThsop->unlock(); // (ThreadSafe) - End of atomic operation 112 126 return( mSRef->dseg.size()); 113 127 } … … 117 131 virtual void SetSize(size_t segsz, size_t nbseg=0) 118 132 { 133 gThsop->lock(); // (ThreadSafe) - Start of atomic operation 119 134 Delete(); 120 135 mSRef = new SDREF; … … 122 137 mSRef->segsize = segsz; 123 138 mSRef->dsid = AnyDataObj::getUniqueId(); 124 for(size_t k=0; k<nbseg; k++) Extend(); 139 for(size_t k=0; k<nbseg; k++) Extend_P(); 140 gThsop->unlock(); // (ThreadSafe) - End of atomic operation 125 141 } 126 142 //! Shares the data between two SegDataBlock objects 127 143 void Share(const SegDataBlock<T>& a) 128 144 { 145 gThsop->lock(); // (ThreadSafe) - Start of atomic operation 129 146 Delete(); 130 147 mSRef = a.mSRef; 131 148 mSRef->nref++; 149 gThsop->unlock(); // (ThreadSafe) - End of atomic operation 132 150 } 133 151 … … 135 153 void Clone(const SegDBInterface<T> & a, bool cpval=true) 136 154 { 155 gThsop->lock(); // (ThreadSafe) - Start of atomic operation 137 156 Delete(); 138 157 mSRef = new SDREF; 139 158 mSRef->nref = 1; 140 159 mSRef->segsize = a.SegmentSize(); 160 gThsop->unlock(); // (ThreadSafe) - End of atomic operation 141 161 for(size_t k=0; k<a.NbSegments(); k++) { 142 162 Extend(); … … 230 250 231 251 protected: 252 //! NON-thread safe: Decrement the number of reference counts, and free the memory if NRef=0 232 253 void Delete() 233 254 { … … 243 264 mSRef = NULL; 244 265 } 266 //! NON-thread safe, version of Extend() : Adds one segment to the data structure 267 size_t Extend_P() 268 { 269 T * p = new T[mSRef->segsize]; 270 mSRef->dseg.push_back(p); 271 return( mSRef->dseg.size()); 272 } 273 245 274 /*! \cond 246 275 SDREF structure for reference management - for internal use by SegDataBlock … … 254 283 /*! \endcond */ 255 284 SDREF * mSRef; //!< SDREF structure for reference sharing 285 286 static ThSafeOp* gThsop; //!< Mutex for thread safe operation 256 287 }; 288 289 template <class T> ThSafeOp* SegDataBlock<T>::gThsop = NULL; // static member initialized to NULL 257 290 258 291 //! Definition of operator \<\< for ascii formatted output of SegDataBlock
Note:
See TracChangeset
for help on using the changeset viewer.