Ignore:
Timestamp:
Apr 11, 2007, 6:12:56 PM (18 years ago)
Author:
ansari
Message:

Sw/SegDataBlock<T> rendu en principe ThreadSafe + changement numero de version SOPHYA et module BaseTools, Reza 11/04/2007

File:
1 edited

Legend:

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

    r2884 r3213  
    1313#include <iomanip>
    1414#include <typeinfo>
     15
     16#include "thsafeop.h"  //  for ThreadSafe operations (Ref.Count/Share)
    1517
    1618/*!
     
    6769  {
    6870    mSRef = NULL;
     71    if (gThsop == NULL) gThsop = new ThSafeOp;
    6972    SetSize(segsz, nbseg);
    7073  }
     
    7275  SegDataBlock(const SegDataBlock<T>& a)
    7376  {
     77    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    7478    mSRef = a.mSRef;
    7579    mSRef->nref++;
     80    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
    7681  }
    7782  //! copy constructor with specification of flags for data sharing and element value copy
     
    7984  {
    8085    if (share) {
     86      gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    8187      mSRef = a.mSRef;
    8288      mSRef->nref++;
     89      gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
    8390    }
    8491    else {
     
    9097  SegDataBlock(const SegDBInterface<T>& a)
    9198  {
     99    if (gThsop == NULL) gThsop = new ThSafeOp;
    92100    SegDataBlock<T> * sdb = dynamic_cast< SegDataBlock<T> *>(&a);
    93101    if (sdb != NULL) {
     102      gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    94103      mSRef = sdb->mSRef;
    95104      mSRef->nref++;
     105      gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
    96106    }
    97107    else Clone(a, true);
     
    101111  {
    102112    //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.
    108120  virtual size_t Extend()
    109121  {
    110122    T * p = new T[mSRef->segsize];
     123    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    111124    mSRef->dseg.push_back(p);
     125    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
    112126    return( mSRef->dseg.size());
    113127  }
     
    117131  virtual void SetSize(size_t segsz, size_t nbseg=0)
    118132  {
     133    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    119134    Delete();
    120135    mSRef = new SDREF;
     
    122137    mSRef->segsize = segsz;
    123138    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
    125141  }
    126142//! Shares the data between two SegDataBlock objects
    127143  void Share(const SegDataBlock<T>& a)
    128144  {
     145    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    129146    Delete();
    130147    mSRef = a.mSRef;
    131148    mSRef->nref++;
     149    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
    132150  }
    133151
     
    135153  void Clone(const SegDBInterface<T> & a, bool cpval=true)
    136154  {
     155    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation
    137156    Delete();
    138157    mSRef = new SDREF;
    139158    mSRef->nref = 1;
    140159    mSRef->segsize = a.SegmentSize();
     160    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
    141161    for(size_t k=0; k<a.NbSegments(); k++) {
    142162      Extend();
     
    230250
    231251protected:
     252  //! NON-thread safe: Decrement the number of reference counts, and free the memory if NRef=0
    232253  void Delete()
    233254  {
     
    243264    mSRef = NULL;
    244265  }
     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
    245274  /*! \cond
    246275    SDREF structure for reference management - for internal use by SegDataBlock
     
    254283  /*! \endcond */
    255284  SDREF *  mSRef;    //!< SDREF structure for reference sharing
     285
     286  static ThSafeOp* gThsop;  //!< Mutex for thread safe operation
    256287};
     288
     289template <class T> ThSafeOp* SegDataBlock<T>::gThsop  = NULL;  // static member initialized to NULL
    257290
    258291//! Definition of operator \<\< for ascii formatted output of SegDataBlock
Note: See TracChangeset for help on using the changeset viewer.