- Timestamp:
- Apr 12, 2007, 12:19:19 AM (18 years ago)
- Location:
- trunk/SophyaLib/BaseTools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/segdatablock.h
r3213 r3214 69 69 { 70 70 mSRef = NULL; 71 if (gThsop == NULL) gThsop = new ThSafeOp;72 71 SetSize(segsz, nbseg); 73 72 } … … 75 74 SegDataBlock(const SegDataBlock<T>& a) 76 75 { 77 gThsop->lock(); // (ThreadSafe) - Start of atomic operation76 a.mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 78 77 mSRef = a.mSRef; 79 78 mSRef->nref++; 80 gThsop->unlock(); // (ThreadSafe) - End of atomic operation79 a.mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 81 80 } 82 81 //! copy constructor with specification of flags for data sharing and element value copy … … 84 83 { 85 84 if (share) { 86 gThsop->lock(); // (ThreadSafe) - Start of atomic operation85 a.mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 87 86 mSRef = a.mSRef; 88 87 mSRef->nref++; 89 gThsop->unlock(); // (ThreadSafe) - End of atomic operation88 a.mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 90 89 } 91 90 else { … … 97 96 SegDataBlock(const SegDBInterface<T>& a) 98 97 { 99 if (gThsop == NULL) gThsop = new ThSafeOp;100 98 SegDataBlock<T> * sdb = dynamic_cast< SegDataBlock<T> *>(&a); 101 99 if (sdb != NULL) { 102 gThsop->lock(); // (ThreadSafe) - Start of atomic operation100 sdb->mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 103 101 mSRef = sdb->mSRef; 104 102 mSRef->nref++; 105 gThsop->unlock(); // (ThreadSafe) - End of atomic operation103 sdb->mSRefg->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 106 104 } 107 105 else Clone(a, true); … … 111 109 { 112 110 //DEL cout << " DEBUG-~SegDataBlock() " << hex << mSRef << dec << " NRef()= " << NRef() << endl; 113 gThsop->lock(); // (ThreadSafe) - Start of atomic operation114 111 Delete(); 115 gThsop->unlock(); // (ThreadSafe) - End of atomic operation116 112 } 117 113 … … 121 117 { 122 118 T * p = new T[mSRef->segsize]; 123 gThsop->lock(); // (ThreadSafe) - Start of atomic operation119 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 124 120 mSRef->dseg.push_back(p); 125 gThsop->unlock(); // (ThreadSafe) - End of atomic operation 126 return( mSRef->dseg.size()); 121 size_t rs = mSRef->dseg.size(); 122 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 123 return( rs ); 127 124 } 128 125 … … 131 128 virtual void SetSize(size_t segsz, size_t nbseg=0) 132 129 { 133 gThsop->lock(); // (ThreadSafe) - Start of atomic operation134 130 Delete(); 135 131 mSRef = new SDREF; 132 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 136 133 mSRef->nref = 1; 137 134 mSRef->segsize = segsz; 138 135 mSRef->dsid = AnyDataObj::getUniqueId(); 139 136 for(size_t k=0; k<nbseg; k++) Extend_P(); 140 gThsop->unlock(); // (ThreadSafe) - End of atomic operation137 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 141 138 } 142 139 //! Shares the data between two SegDataBlock objects 143 140 void Share(const SegDataBlock<T>& a) 144 141 { 145 gThsop->lock(); // (ThreadSafe) - Start of atomic operation146 142 Delete(); 143 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 147 144 mSRef = a.mSRef; 148 145 mSRef->nref++; 149 gThsop->unlock(); // (ThreadSafe) - End of atomic operation146 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 150 147 } 151 148 … … 153 150 void Clone(const SegDBInterface<T> & a, bool cpval=true) 154 151 { 155 gThsop->lock(); // (ThreadSafe) - Start of atomic operation156 152 Delete(); 157 153 mSRef = new SDREF; 154 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 158 155 mSRef->nref = 1; 159 156 mSRef->segsize = a.SegmentSize(); 160 gThsop->unlock(); // (ThreadSafe) - End of atomic operation157 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 161 158 for(size_t k=0; k<a.NbSegments(); k++) { 162 159 Extend(); … … 254 251 { 255 252 if (mSRef == NULL) return; 253 mSRef->gThsop.lock(); 256 254 mSRef->nref--; 257 if (mSRef->nref > 0) { mSRef = NULL; return; } 255 if (mSRef->nref > 0) { 256 mSRef->gThsop.unlock(); 257 mSRef = NULL; 258 return; 259 } 258 260 //DEL cout << " DEBUG-SegDataBlock::Delete() NbSegments() = " << NbSegments() << endl; 259 261 for(size_t k=0; k<NbSegments(); k++) { … … 261 263 mSRef->dseg[k] = NULL; 262 264 } 265 mSRef->gThsop.unlock(); 263 266 delete mSRef; 264 267 mSRef = NULL; … … 280 283 std::vector<T *> dseg; 281 284 size_t segsize; 285 ThSafeOp gThsop; // Mutex for thread safe operation 282 286 } SDREF; 283 287 /*! \endcond */ 284 288 SDREF * mSRef; //!< SDREF structure for reference sharing 285 289 286 static ThSafeOp* gThsop; //!< Mutex for thread safe operation287 290 }; 288 291 289 template <class T> ThSafeOp* SegDataBlock<T>::gThsop = NULL; // static member initialized to NULL290 292 291 293 //! Definition of operator \<\< for ascii formatted output of SegDataBlock -
trunk/SophyaLib/BaseTools/swsegdb.h
r3213 r3214 84 84 SwSegDataBlock(const SwSegDataBlock<T>& a) 85 85 { 86 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation86 a.mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 87 87 mSRef = a.mSRef; 88 88 mSRef->nref++; 89 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 89 size_t segsz = mSRef->segsize; 90 a.mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 91 buff = new T[segsz]; 92 bidx = -1; 93 fgcstbuff = true; 90 94 } 91 95 … … 113 117 { 114 118 Delete(); 115 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation116 119 mSRef = new SWSDREF; 120 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 117 121 mSRef->nref = 1; 118 122 mSRef->segsize = segsz; 119 123 mSRef->dsid = AnyDataObj::getUniqueId(); 120 mSRef->buff = new T[segsz];121 mSRef->bidx = -1;122 mSRef->fgcstbuff = true;123 124 for(size_t k=0; k<nbseg; k++) { 124 125 mSRef->swp.push_back(0); … … 127 128 mSRef->swapper = NULL; 128 129 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 130 buff = new T[segsz]; 131 bidx = -1; 132 fgcstbuff = true; 129 133 } 130 134 … … 148 152 virtual T* GetSegment(size_t k) 149 153 { 150 T* rs = NULL;151 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation152 154 getSeg(k); 153 mSRef->fgcstbuff = false; 154 rs = mSRef->buff; 155 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 156 return rs; 155 fgcstbuff = false; 156 return buff; 157 157 } 158 158 //! Return the const (read-only) pointer to data segment \b k 159 159 virtual T const * GetCstSegment(size_t k) const 160 160 { 161 T const * rs = NULL; 162 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 163 if (getSeg(k)) mSRef->fgcstbuff = true; 164 rs = mSRef->buff; 165 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 166 return rs; 161 if (getSeg(k)) fgcstbuff = true; 162 return buff; 167 163 } 168 164 … … 171 167 { 172 168 Delete(); 173 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation169 a.mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 174 170 mSRef = a.mSRef; 175 171 mSRef->nref++; 176 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 172 size_t segsz = mSRef->segsize; 173 a.mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 174 buff = new T[segsz]; 175 bidx = -1; 176 fgcstbuff = true; 177 177 return *this; 178 178 } … … 182 182 { 183 183 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 184 if (( mSRef->bidx >= 0) && !mSRef->fgcstbuff) {185 int_8 nswp = mSRef->swapper->WriteToSwap( mSRef->buff, mSRef->segsize, mSRef->bidx,186 mSRef->swp[ mSRef->bidx], mSRef->fgwp[mSRef->bidx]);187 mSRef->swp[ mSRef->bidx] = nswp;188 mSRef->fgwp[ mSRef->bidx] = true;189 mSRef->bidx = -1;190 mSRef->fgcstbuff = true;184 if ((bidx >= 0) && !fgcstbuff) { 185 int_8 nswp = mSRef->swapper->WriteToSwap(buff, mSRef->segsize, bidx, 186 mSRef->swp[bidx], mSRef->fgwp[bidx]); 187 mSRef->swp[bidx] = nswp; 188 mSRef->fgwp[bidx] = true; 189 bidx = -1; 190 fgcstbuff = true; 191 191 } 192 192 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation … … 211 211 if (mSRef->nref > 0) { 212 212 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 213 delete[] buff; 213 214 mSRef = NULL; 214 215 return; 215 216 } 216 217 if (mSRef->swapper) delete mSRef->swapper; 217 delete[] mSRef->buff;218 218 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 219 219 delete mSRef; 220 delete[] buff; 220 221 mSRef = NULL; 221 222 } 222 223 bool getSeg(size_t k) const 223 224 { 224 if (k == mSRef->bidx) return false; 225 if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) { 226 int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx, 227 mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]); 228 mSRef->swp[mSRef->bidx] = nswp; 229 mSRef->fgwp[mSRef->bidx] = true; 225 if (k == bidx) return false; 226 mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation 227 if ((bidx >= 0) && !fgcstbuff) { 228 int_8 nswp = mSRef->swapper->WriteToSwap(buff, mSRef->segsize, bidx, 229 mSRef->swp[bidx], mSRef->fgwp[bidx]); 230 mSRef->swp[bidx] = nswp; 231 mSRef->fgwp[bidx] = true; 230 232 } 231 233 if (mSRef->fgwp[k]) 232 mSRef->swapper->ReadFromSwap(k, mSRef->swp[k], mSRef->buff, mSRef->segsize);234 mSRef->swapper->ReadFromSwap(k, mSRef->swp[k], buff, mSRef->segsize); 233 235 //DEL-02022007 else { delete[] mSRef->buff; mSRef->buff = new T[mSRef->segsize]; } 234 mSRef->bidx = k; 236 mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation 237 bidx = k; 235 238 return true; 236 239 } … … 243 246 mutable std::vector< int_8 > swp; // swap position tag for each segment 244 247 mutable std::vector< bool > fgwp; // swap flag (true = already swapped) for each segment 245 mutable T * buff; // Data buffer246 mutable int_8 bidx; // segment index (number) corresponding to buffer247 mutable bool fgcstbuff; // true : this is a constant T * buff<248 248 DataSwapperInterface<T> * swapper; // Data swapper 249 249 ThSafeOp gThsop; // Mutex for thread safe operation one / SWSDREF struct … … 251 251 //! \endcond 252 252 SWSDREF * mSRef; //!< SWSDREF structure for reference sharing 253 mutable T * buff; // Data buffer 254 mutable int_8 bidx; // segment index (number) corresponding to buffer 255 mutable bool fgcstbuff; // true : this is a constant T * buff 253 256 254 257 };
Note:
See TracChangeset
for help on using the changeset viewer.