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/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
Note: See TracChangeset for help on using the changeset viewer.