Changeset 3214 in Sophya for trunk/SophyaLib/BaseTools/swsegdb.h


Ignore:
Timestamp:
Apr 12, 2007, 12:19:19 AM (18 years ago)
Author:
ansari
Message:

Debug protections thread-safe pour SegDataBlock<T> et SwSegDataBlock<T>, Reza 12/04/2007

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/BaseTools/swsegdb.h

    r3213 r3214  
    8484  SwSegDataBlock(const SwSegDataBlock<T>& a)
    8585  {
    86     mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
     86    a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    8787    mSRef = a.mSRef;
    8888    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;
    9094  }
    9195
     
    113117  {
    114118    Delete();
    115     mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    116119    mSRef = new SWSDREF;
     120    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    117121    mSRef->nref = 1;
    118122    mSRef->segsize = segsz;
    119123    mSRef->dsid = AnyDataObj::getUniqueId();
    120     mSRef->buff = new T[segsz];
    121     mSRef->bidx = -1;       
    122     mSRef->fgcstbuff = true;
    123124    for(size_t k=0; k<nbseg; k++) {
    124125      mSRef->swp.push_back(0);
     
    127128    mSRef->swapper = NULL;
    128129    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     130    buff = new T[segsz];
     131    bidx = -1;       
     132    fgcstbuff = true;
    129133  }
    130134
     
    148152  virtual T* GetSegment(size_t k)
    149153  {
    150     T* rs = NULL;
    151     mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    152154    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;
    157157  }
    158158  //! Return the const (read-only) pointer to data segment \b k         
    159159  virtual T const * GetCstSegment(size_t k) const
    160160  {
    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;   
    167163  }
    168164
     
    171167  {
    172168    Delete();
    173     mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
     169    a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    174170    mSRef = a.mSRef;
    175171    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;
    177177    return *this;
    178178  }
     
    182182  {
    183183    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; 
    191191   }
    192192    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     
    211211    if (mSRef->nref > 0)  {
    212212      mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     213      delete[] buff;
    213214      mSRef = NULL;
    214215      return;
    215216    }
    216217    if (mSRef->swapper) delete mSRef->swapper; 
    217     delete[] mSRef->buff;
    218218    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    219219    delete mSRef;
     220    delete[] buff;
    220221    mSRef = NULL; 
    221222  }
    222223  bool getSeg(size_t k) const
    223224  {
    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;
    230232    }
    231233    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);
    233235    //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;
    235238    return true;
    236239  }
     
    243246    mutable std::vector< int_8 > swp;  // swap position tag for each segment
    244247    mutable std::vector< bool > fgwp;  // swap flag (true = already swapped)  for each segment
    245     mutable T * buff;                // Data buffer   
    246     mutable int_8 bidx;              // segment index (number) corresponding to buffer
    247     mutable bool fgcstbuff;            // true : this is a constant T * buff<
    248248    DataSwapperInterface<T> * swapper;   // Data swapper
    249249    ThSafeOp gThsop;         // Mutex for thread safe operation one / SWSDREF struct
     
    251251  //! \endcond
    252252  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
    253256
    254257};
Note: See TracChangeset for help on using the changeset viewer.