Changeset 3214 in Sophya for trunk


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

Location:
trunk/SophyaLib/BaseTools
Files:
2 edited

Legend:

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

    r3213 r3214  
    6969  {
    7070    mSRef = NULL;
    71     if (gThsop == NULL) gThsop = new ThSafeOp;
    7271    SetSize(segsz, nbseg);
    7372  }
     
    7574  SegDataBlock(const SegDataBlock<T>& a)
    7675  {
    77     gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
     76    a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    7877    mSRef = a.mSRef;
    7978    mSRef->nref++;
    80     gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
     79    a.mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    8180  }
    8281  //! copy constructor with specification of flags for data sharing and element value copy
     
    8483  {
    8584    if (share) {
    86       gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
     85      a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    8786      mSRef = a.mSRef;
    8887      mSRef->nref++;
    89       gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
     88      a.mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    9089    }
    9190    else {
     
    9796  SegDataBlock(const SegDBInterface<T>& a)
    9897  {
    99     if (gThsop == NULL) gThsop = new ThSafeOp;
    10098    SegDataBlock<T> * sdb = dynamic_cast< SegDataBlock<T> *>(&a);
    10199    if (sdb != NULL) {
    102       gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
     100      sdb->mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    103101      mSRef = sdb->mSRef;
    104102      mSRef->nref++;
    105       gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
     103      sdb->mSRefg->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    106104    }
    107105    else Clone(a, true);
     
    111109  {
    112110    //DEL    cout << " DEBUG-~SegDataBlock() " << hex << mSRef << dec << " NRef()= " << NRef() << endl;
    113     gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    114111    Delete();
    115     gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
    116112  }
    117113
     
    121117  {
    122118    T * p = new T[mSRef->segsize];
    123     gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
     119    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    124120    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 );
    127124  }
    128125
     
    131128  virtual void SetSize(size_t segsz, size_t nbseg=0)
    132129  {
    133     gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    134130    Delete();
    135131    mSRef = new SDREF;
     132    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    136133    mSRef->nref = 1;
    137134    mSRef->segsize = segsz;
    138135    mSRef->dsid = AnyDataObj::getUniqueId();
    139136    for(size_t k=0; k<nbseg; k++) Extend_P();
    140     gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
     137    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    141138  }
    142139//! Shares the data between two SegDataBlock objects
    143140  void Share(const SegDataBlock<T>& a)
    144141  {
    145     gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    146142    Delete();
     143    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    147144    mSRef = a.mSRef;
    148145    mSRef->nref++;
    149     gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
     146    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    150147  }
    151148
     
    153150  void Clone(const SegDBInterface<T> & a, bool cpval=true)
    154151  {
    155     gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    156152    Delete();
    157153    mSRef = new SDREF;
     154    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    158155    mSRef->nref = 1;
    159156    mSRef->segsize = a.SegmentSize();
    160     gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
     157    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    161158    for(size_t k=0; k<a.NbSegments(); k++) {
    162159      Extend();
     
    254251  {
    255252    if (mSRef == NULL) return;
     253    mSRef->gThsop.lock();
    256254    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    }
    258260    //DEL cout << " DEBUG-SegDataBlock::Delete() NbSegments() = " << NbSegments() << endl;
    259261    for(size_t k=0; k<NbSegments(); k++) {
     
    261263      mSRef->dseg[k] = NULL;
    262264    }
     265    mSRef->gThsop.unlock();
    263266    delete mSRef;
    264267    mSRef = NULL;
     
    280283    std::vector<T *> dseg;
    281284    size_t segsize;
     285    ThSafeOp gThsop;  // Mutex for thread safe operation
    282286  } SDREF;
    283287  /*! \endcond */
    284288  SDREF *  mSRef;    //!< SDREF structure for reference sharing
    285289
    286   static ThSafeOp* gThsop;  //!< Mutex for thread safe operation
    287290};
    288291
    289 template <class T> ThSafeOp* SegDataBlock<T>::gThsop  = NULL;  // static member initialized to NULL
    290292
    291293//! Definition of operator \<\< for ascii formatted output of SegDataBlock
  • 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.