Changeset 3213 in Sophya for trunk/SophyaLib/BaseTools


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

Location:
trunk/SophyaLib/BaseTools
Files:
5 edited

Legend:

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

    r3212 r3213  
    174174  static size_t NallocData; //!< DEBUG: number of allocations (all type<T> classes)
    175175  static size_t NallocSRef; //!< DEBUG: number of references (all type<T> classes)
    176   static ThSafeOp* gThsop;  //!< DEBUG: for thread safe operation
     176  static ThSafeOp* gThsop;  //!< Mutex For thread safe operation
    177177
    178178  size_t       mSz;      //!< size of data structure
  • 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
  • trunk/SophyaLib/BaseTools/sophyainit.cc

    r3203 r3213  
    2727// Module version number - 2.0 , Jul 2006
    2828// Module version number - 2.02 , Fev07 Ajout NDataBlock::RenewObjId()
     29// Module version number - 2.1 ,  Avr07
     30//  - Nettoyage machdefs_mkmf.h
     31//  - Ajout classe ThSafeOp ---> NDataBlock<T> Sw/SegDataBlock<T> ThreadSafe
     32//     
    2933#define MOD_VERS   2.02
    3034
     
    5862  ModListP = new map<string, double>;
    5963 
    60 
    61   #ifdef xx__mac__
    62   //InitToolBox();
    63   //SIOUXSettings.initializeTB = FALSE;
    64   SIOUXSettings.autocloseonquit = FALSE;
    65   SIOUXSettings.asktosaveonclose = FALSE;
    66   SIOUXSettings.showstatusline = TRUE;
    67   #endif
    6864
    6965  // Initialisation des mecanismes PPF I/O
     
    206202  #ifdef __IBMCPP__
    207203  #ifdef SO_ARCH64
    208   compiler = const_cast<char *>("IBM-xlC ") ;
     204  compiler = const_cast<char *>("IBM-xlC (-q64)") ;
    209205  #else
    210   compiler = const_cast<char *>("IBM-xlC (-q64)") ;
     206  compiler = const_cast<char *>("IBM-xlC") ;
    211207  #endif
    212208  #endif
  • trunk/SophyaLib/BaseTools/sversion.h

    r3172 r3213  
    33
    44#define SOPHYA_VERSION   2.1
    5 #define SOPHYA_REVISION  20
    6 #define SOPHYA_TAG       "V_Jan2007"
     5#define SOPHYA_REVISION  30
     6#define SOPHYA_TAG       "V_Avr2007"
    77
    88#endif
  • trunk/SophyaLib/BaseTools/swsegdb.h

    r3172 r3213  
    6969    SetSize(segsz, swpos.size());
    7070    SetSwapper(dsw);
     71    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    7172    mSRef->swp = swpos;
    7273    for(size_t k=0; k<mSRef->fgwp.size(); k++)  mSRef->fgwp[k] = true;
     74    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    7375    }
    7476  //! Constructor - optional specification of segment size and number of segments
     
    8284  SwSegDataBlock(const SwSegDataBlock<T>& a)
    8385  {
     86    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    8487    mSRef = a.mSRef;
    8588    mSRef->nref++;
     89    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    8690  }
    8791
    8892  //! Destructor. The memory is freed when the last object referencing the data segment is destroyed
    89   virtual ~SwSegDataBlock() { Delete(); }
     93  virtual ~SwSegDataBlock()
     94  {
     95    Delete();
     96  }
    9097  //! Adds one segment to the data structure - returns the pointer to the allocated segment.
    9198  virtual size_t Extend()
    9299  {
     100    size_t rs = 0;
     101    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    93102    mSRef->swp.push_back(0);
    94103    mSRef->fgwp.push_back(false);
    95     return mSRef->swp.size();
     104    rs = mSRef->swp.size();
     105    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     106    return rs;
    96107  }
    97108  /*! \brief Changes the data segment size and reallocates the memory segments
     
    102113  {
    103114    Delete();
     115    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    104116    mSRef = new SWSDREF;
    105117    mSRef->nref = 1;
     
    114126    }
    115127    mSRef->swapper = NULL;
     128    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    116129  }
    117130
     
    120133  {
    121134    if (mSRef == NULL) return;
     135    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    122136    if (mSRef->swapper) delete mSRef->swapper; 
    123137    mSRef->swapper = dsw.Clone();
     138    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    124139  }
    125140
     
    133148  virtual T* GetSegment(size_t k)
    134149  {
     150    T* rs = NULL;
     151    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    135152    getSeg(k);
    136153    mSRef->fgcstbuff = false;
    137     return mSRef->buff;
     154    rs = mSRef->buff;
     155    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     156    return rs;
    138157  }
    139158  //! Return the const (read-only) pointer to data segment \b k         
    140159  virtual T const * GetCstSegment(size_t k) const
    141160  {
     161    T const * rs = NULL;
     162    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    142163    if (getSeg(k))  mSRef->fgcstbuff = true;
    143     return mSRef->buff;
     164    rs = mSRef->buff;
     165    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     166    return rs;   
    144167  }
    145168
     
    148171  {
    149172    Delete();
     173    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    150174    mSRef = a.mSRef;
    151175    mSRef->nref++;
     176    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    152177    return *this;
    153178  }
     
    156181  void SwapOutBuffer() const
    157182  {
     183    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    158184    if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
    159185      int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx,
     
    164190      mSRef->fgcstbuff = true; 
    165191   }
     192    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    166193  }
    167194  //! Return the position tag (swap position) table, after call to SwapOutBuffer()
     
    180207  {
    181208    if (mSRef == NULL) return;
     209    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation
    182210    mSRef->nref--;
    183     if (mSRef->nref > 0)  { mSRef = NULL; return; }
     211    if (mSRef->nref > 0)  {
     212      mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     213      mSRef = NULL;
     214      return;
     215    }
    184216    if (mSRef->swapper) delete mSRef->swapper; 
    185217    delete[] mSRef->buff;
     218    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
    186219    delete mSRef;
    187220    mSRef = NULL; 
     
    214247    mutable bool fgcstbuff;            // true : this is a constant T * buff<
    215248    DataSwapperInterface<T> * swapper;   // Data swapper
     249    ThSafeOp gThsop;         // Mutex for thread safe operation one / SWSDREF struct
    216250  } SWSDREF;
    217251  //! \endcond
    218   SWSDREF *  mSRef;    // SWSDREF structure for reference sharing
     252  SWSDREF *  mSRef;    //!< SWSDREF structure for reference sharing
     253
    219254};
    220255
Note: See TracChangeset for help on using the changeset viewer.