Changeset 3392 in Sophya for trunk/SophyaLib/HiStats/ntuple.cc


Ignore:
Timestamp:
Nov 22, 2007, 7:25:40 PM (18 years ago)
Author:
ansari
Message:

Implementation NTuple::Fill(), BaseDataTable:;AddRow()/GetRow() thread-safe - Reza 22/11/2007

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/HiStats/ntuple.cc

    r3236 r3392  
    55#include "perrors.h"
    66#include "ntuple.h"
     7#include "thsafeop.h"
    78
    89namespace SOPHYA {
     
    1718   Simple NTuple class (a Table or 2-D data set) with double or
    1819   single precision floating point value columns.
     20   \warning
     21   Thread safe fill operation can be activated using the method SetThreadSafe()
     22   Default mode is NON thread-safe fill.
    1923   \sa SOPHYA::ObjFileIO<NTuple>
    2024
     
    6569mFgDouble = true;
    6670mInfo = NULL;
     71mThS = NULL;
    6772}
    6873
     
    8994mVarD = NULL;
    9095mInfo = NULL;
     96mThS = NULL;
    9197if (nvar <= 0)  throw ParmError("NTuple::NTuple(nvar<=0) ");
    9298mNVar = nvar;
     
    126132mVarD = NULL;
    127133mInfo = NULL;
     134mThS = NULL;
    128135int nvar = noms.size();
    129136if (nvar <= 0)  throw ParmError("NTuple::NTuple(nvar<=0) ");
     
    150157}
    151158
    152 //! Copy constructor - Copies the table definition and associated data
     159/*!
     160  Copy constructor - Copies the table definition and associated data,
     161  as well as the tread safety state
     162*/
    153163//                                       cmv 8/10/99
    154164//++
     
    158168//--
    159169: mNVar(0), mNEnt(0), mBlk(0), mNBlk(0)
    160 , mVar(NULL), mVarD(NULL), mInfo(NULL)
     170 , mVar(NULL), mVarD(NULL), mInfo(NULL),mThS(NULL)
    161171{
    162172if(NT.mNVar<=0) return; // cas ou NT est cree par defaut
     
    185195
    186196if(NT.mInfo!=NULL) {mInfo = new DVList; *mInfo = *(NT.mInfo);}
    187 
     197mThS = NULL;
     198if(NT.mThS!=NULL) SetThreadSafe(true);
    188199return;
    189200}
    190201
    191 /* --Methode--
    192 //! Constructor with table initialized from a PPF file
    193 //++
    194 NTuple::NTuple(char *flnm)
    195 //
    196 //      Createur lecture fichier ppersist.
    197 //--
    198 {
    199 mNVar = mNEnt = mBlk = mNBlk = 0;
    200 mVar = NULL;
    201 mVarD = NULL;
    202 mNames = NULL;
    203 mInfo = NULL;
    204 PInPersist s(flnm);
    205 ObjFileIO<NTuple> fiont(this);
    206 fiont.Read(s);
    207 }
     202
     203/* --Methode-- */
     204NTuple::~NTuple()
     205{
     206Clean();
     207}
     208/* --Methode-- */
     209/*!
     210  \brief Activate or deactivate thread-safe \b Fill() operations
     211  If fg==true, create an ThSafeOp object in order to insure atomic Fill()
     212  operations. if fg==false, the ThSafeOp object (mThS) of the target NTuple
     213  is destroyed.
     214  \warning The default Fill() operation mode for NTuples is NOT thread-safe.
     215  Please note also that the thread-safety state is NOt saved to PPF (or FITS) streams.
    208216*/
    209 
    210 /* --Methode-- */
    211 NTuple::~NTuple()
    212 {
    213 Clean();
     217void NTuple::SetThreadSafe(bool fg)
     218{
     219if (fg) {
     220  if (mThS) return;
     221  else mThS = new ThSafeOp();
     222}
     223else {
     224  if (mThS) delete mThS;
     225  mThS = NULL;
     226}
    214227}
    215228
     
    221234if (mVarD) delete[] mVarD;
    222235if (mInfo) delete mInfo;
     236if (mThS) delete mThS;
    223237int i;
    224238if(mNBlk>0) {
     
    232246mVarD = NULL;
    233247mInfo = NULL;
     248mThS = NULL;
    234249return;
    235250}
     
    283298//--
    284299{
     300if (mThS)  mThS->lock();   // Thread-safe operation
    285301int numb = mNEnt/mBlk;
    286302if (numb >= mNBlk) {
     
    302318else memcpy((mPtr[numb]+offb*mNVar), x, mNVar*sizeof(r_4));
    303319mNEnt++;
     320if (mThS)  mThS->unlock();   // Thread-safe operation
    304321return;
    305322}
     
    316333//--
    317334{
     335if (mThS)  mThS->lock();   // Thread-safe operation
    318336int numb = mNEnt/mBlk;
    319337if (numb >= mNBlk) {
     
    337355
    338356mNEnt++;
     357if (mThS)  mThS->unlock();   // Thread-safe operation
    339358return;
    340359}
Note: See TracChangeset for help on using the changeset viewer.