Changeset 3208 in Sophya


Ignore:
Timestamp:
Apr 10, 2007, 5:57:02 PM (18 years ago)
Author:
ansari
Message:

Ajout classe ThSafeOp (thsafeop.h) pour rendre NDataBlock<T> (et SegDataBlock) thread-safe, ajout methode DataTypeInfo::getTypeId() , implementation ThSafeOp ds ndatablock.cc (.h), Reza 10/04/2007

Location:
trunk/SophyaLib/BaseTools
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/BaseTools/datatype.cc

    r2884 r3208  
    1414{
    1515}
    16 #if defined(__SGICC__) || defined(__IBMCPP__)
    17 template <class T>
    18 string DataTypeInfo<T>::getTypeName()
    19 {
    20   if (typeid(T) == typeid(uint_1)) return("uint_1");
    21   else if (typeid(T) == typeid(uint_2)) return("uint_2");
    22   else if (typeid(T) == typeid(int_2)) return("int_2");
    23   else if (typeid(T) == typeid(int_4)) return("int_4");
    24   else if (typeid(T) == typeid(int_8)) return("int_8");
    25   else if (typeid(T) == typeid(uint_4)) return("uint_4");
    26   else if (typeid(T) == typeid(uint_8)) return("uint_8");
    27   else if (typeid(T) == typeid(r_4)) return("r_4");
    28   else if (typeid(T) == typeid(r_8)) return("r_8");
    29   else if (typeid(T) == typeid(complex<r_4>)) return(" complex<r_4> ");
    30   else if (typeid(T) == typeid(complex<r_8>)) return(" complex<r_8> ");
    31   else return("???unknowntype???");
    32 }
    33 #else
     16//! Return the type name of T, as a string, (Example: T=uint_8 --> "uint_8")
    3417template <class T>
    3518string DataTypeInfo<T>::getTypeName() { return("???unknowntype???"); }
     
    3720string DataTypeInfo<uint_1>::getTypeName() { return("uint_1"); }
    3821DECL_TEMP_SPEC
     22string DataTypeInfo<int_1>::getTypeName() { return("int_1"); }
     23DECL_TEMP_SPEC
    3924string DataTypeInfo<uint_2>::getTypeName() { return("uint_2"); }
    4025DECL_TEMP_SPEC
    4126string DataTypeInfo<int_2>::getTypeName() { return("int_2"); }
    4227DECL_TEMP_SPEC
     28string DataTypeInfo<uint_4>::getTypeName() { return("uint_4"); }
     29DECL_TEMP_SPEC
    4330string DataTypeInfo<int_4>::getTypeName() { return("int_4"); }
    4431DECL_TEMP_SPEC
     32string DataTypeInfo<uint_8>::getTypeName() { return("uint_8"); }
     33DECL_TEMP_SPEC
    4534string DataTypeInfo<int_8>::getTypeName() { return("int_8"); }
    46 DECL_TEMP_SPEC
    47 string DataTypeInfo<uint_4>::getTypeName() { return("uint_4"); }
    48 DECL_TEMP_SPEC
    49 string DataTypeInfo<uint_8>::getTypeName() { return("uint_8"); }
    5035DECL_TEMP_SPEC
    5136string DataTypeInfo<r_4>::getTypeName() { return("r_4"); }
     
    5641DECL_TEMP_SPEC
    5742string DataTypeInfo< complex<r_8> >::getTypeName() { return(" complex<r_8> "); }
    58 #endif
     43
     44/*!
     45  \ brief Return an identifier associated to T
     46  uint_1 -> 1  , int_1 -> 2
     47  uint_2 -> 3  , int_2 -> 4
     48  uint_4 -> 5  , int_4 -> 6
     49  uint_8 -> 7  , int_8 -> 8
     50  r_4    -> 9  , r_8   -> 10
     51  complex<r_4> -> 11 , complex<r_8> -> 12
     52*/
     53template <class T>
     54int DataTypeInfo<T>::getTypeId() { return(0); }
     55DECL_TEMP_SPEC
     56int DataTypeInfo<uint_1>::getTypeId() { return(1); }
     57DECL_TEMP_SPEC
     58int DataTypeInfo<int_1>::getTypeId() { return(2); }
     59DECL_TEMP_SPEC
     60int DataTypeInfo<uint_2>::getTypeId() { return(3); }
     61DECL_TEMP_SPEC
     62int DataTypeInfo<int_2>::getTypeId() { return(4); }
     63DECL_TEMP_SPEC
     64int DataTypeInfo<uint_4>::getTypeId() { return(5); }
     65DECL_TEMP_SPEC
     66int DataTypeInfo<int_4>::getTypeId() { return(6); }
     67DECL_TEMP_SPEC
     68int DataTypeInfo<uint_8>::getTypeId() { return(7); }
     69DECL_TEMP_SPEC
     70int DataTypeInfo<int_8>::getTypeId() { return(8); }
     71DECL_TEMP_SPEC
     72int DataTypeInfo<r_4>::getTypeId() { return(9); }
     73DECL_TEMP_SPEC
     74int DataTypeInfo<r_8>::getTypeId() { return(10); }
     75DECL_TEMP_SPEC
     76int DataTypeInfo< complex<r_4> >::getTypeId() { return(11); }
     77DECL_TEMP_SPEC
     78int DataTypeInfo< complex<r_8> >::getTypeId() { return(12); }
     79
    5980
    6081
     
    115136#ifdef __CXX_PRAGMA_TEMPLATES__
    116137#pragma define_template DataTypeInfo<uint_1>
     138#pragma define_template DataTypeInfo<int_1>
    117139#pragma define_template DataTypeInfo<uint_2>
    118140#pragma define_template DataTypeInfo<int_2>
     
    130152namespace SOPHYA {
    131153template class DataTypeInfo<uint_1>;
     154template class DataTypeInfo<int_1>;
    132155template class DataTypeInfo<uint_2>;
    133156template class DataTypeInfo<int_2>;
  • trunk/SophyaLib/BaseTools/datatype.h

    r1238 r3208  
    1313namespace SOPHYA {
    1414
     15//! class returning type name for usual numrical types (int_2,int_4,r_4,r_8 ...)
    1516template <class T>
    1617class DataTypeInfo {
    1718public:
    1819  static string getTypeName();
     20  static int    getTypeId();
    1921  DataTypeInfo();
    2022};
  • trunk/SophyaLib/BaseTools/ndatablock.cc

    r3113 r3208  
    3232template <class T> size_t NDataBlock<T>::NallocData       = 0;
    3333template <class T> size_t NDataBlock<T>::NallocSRef       = 0;
     34template <class T> ThSafeOp* NDataBlock<T>::gThsop        = NULL;
    3435
    3536//! Set debug (and level print) for allocation and references debug.
     
    8889if(Debug_NDataBlock>1)
    8990  cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
     91if (gThsop == NULL) gThsop = new ThSafeOp;
    9092
    9193Alloc(n, NULL, NULL, fzero);   // allocation et mise a zero
     
    107109  cout<<"?_NDataBlock::NDataBlock("<<this
    108110      <<",data="<<data<<",br="<<br<<")"<<endl;
     111if (gThsop == NULL) gThsop = new ThSafeOp;
    109112
    110113Alloc(n,data,br);
     
    119122if(Debug_NDataBlock>1)
    120123  cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
     124if (gThsop == NULL) gThsop = new ThSafeOp;
    121125}
    122126
     
    160164  cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
    161165
    162 Delete();
     166Dealloc();  // ThreadSafe version of Delete()
    163167}
    164168
     
    197201if(&a==NULL) throw(NullPtrError("NDataBlock::Share  &a==NULL\n"));
    198202if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n"));
     203//--- Start of atomic (in one block) operation for thread safety
     204gThsop->lock();   //  (ThreadSafe)
    199205if(mSRef) Delete();
    200206mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
     207gThsop->unlock();   //  (ThreadSafe)
     208//--- End of atomic operation
    201209
    202210if(Debug_NDataBlock>1)
     
    330338  throw(NullPtrError("NDataBlock::Alloc br!=NULL && data==NULL\n"));
    331339if(n==0) throw(SzMismatchError("NDataBlock::Alloc n==0\n"));
     340//--- Start of atomic (in one block) operation for thread safety (ThreadSafe)
     341gThsop->lock();   //  (ThreadSafe)
    332342if(mSRef) Delete();
    333343mSz = n;
     
    338348else {mSRef->data = new T[n]; if (zero) memset(mSRef->data,0,n*sizeof(T));}
    339349mSRef->bridge = br;
     350gThsop->unlock();   //  (ThreadSafe)
     351//--- End of atomic operation (ThreadSafe)
    340352
    341353if(Debug_NDataBlock>0) {
     
    354366}
    355367
    356 //! Management of de-allocation
     368//! Management of de-allocation (NOT thread-safe)
    357369template <class T>
    358370void NDataBlock<T>::Delete(void)
     
    460472}
    461473
     474/*!
     475  \brief Calls the protected Delete() method to set the size to zero
     476  This is the public - thread safe version - of the Delete() method
     477  The memory is freed if last referenced structure.
     478*/
     479template <class T>
     480void NDataBlock<T>::Dealloc()
     481{
     482  gThsop->lock();
     483  Delete();
     484  gThsop->unlock();
     485}
     486
    462487////////////////
    463488// Impression //
  • trunk/SophyaLib/BaseTools/ndatablock.h

    r3172 r3208  
    1010#include "anydataobj.h"
    1111#include <iostream>
     12
     13#include "thsafeop.h"      /* For thread safe operation (Ref.Count/Share) */
    1214
    1315namespace SOPHYA {
     
    8890  void Realloc(size_t nnew,bool force=false);
    8991 
    90   //! Calls Delete() to set the size to zero (The memory is freed if last referenced structure)
    91   inline void Dealloc() { Delete(); }
     92  //  Public, thread safe interface to Delete() (set the size to zero)
     93  void Dealloc();
    9294
    9395  // Informations pointeur/data
     
    172174  static size_t NallocData; //!< DEBUG: number of allocations (all type<T> classes)
    173175  static size_t NallocSRef; //!< DEBUG: number of references (all type<T> classes)
     176  static ThSafeOp* gThsop;  //!< DEBUG: for thread safe operation
    174177
    175178  size_t       mSz;      //!< size of data structure
Note: See TracChangeset for help on using the changeset viewer.