Changeset 346


Ignore:
Timestamp:
04/07/15 04:20:14 (10 years ago)
Author:
Maciej Komosinski
Message:

Each thread can use its own simulator's GenMan?

Location:
cpp
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/threads.h

    r286 r346  
    6666};
    6767
     68template<class T> class ThreadSingletonPtr
     69{
     70pthread_key_t mt_key;
     71
     72  public:
     73
     74ThreadSingletonPtr()
     75 {
     76 pthread_key_create(&mt_key,NULL);
     77 }
     78
     79T* setptr(T* new_o)
     80 {
     81 T* o=(T*)pthread_getspecific(mt_key);
     82 pthread_setspecific(mt_key,new_o);
     83 return o;
     84 }
     85
     86T* get()
     87 {
     88 return (T*)pthread_getspecific(mt_key);
     89 }
     90};
     91
    6892#else
    6993
     
    82106////////////////////////////////////
    83107
     108// THREAD_LOCAL(cls) - behaves like object of class cls (automatic creation/destruction)
     109// THREAD_LOCAL(cls)..._PTR - behaves like pointer to cls (initial NULL, no autocreation/destruction)
     110// _PTR can only be accessed using tls...Ptr() variant of Get/Set, _ptr suffix is internally used in variable name to avoid mistakes
    84111#ifdef USE_CPP_TLS
    85112
     
    88115#define THREAD_LOCAL_DECL(cls,var) CPP_TLS cls* var
    89116#define THREAD_LOCAL_DEF(cls,var) CPP_TLS cls* var=NULL
     117#define THREAD_LOCAL_DECL_PTR(cls,var) CPP_TLS cls* var ## _ptr
     118#define THREAD_LOCAL_DEF_PTR(cls,var) CPP_TLS cls* var ## _ptr=NULL
    90119
    91120template<class T> T* tlsGet(T*& var)
     
    93122if (!var)
    94123        var=new T();
     124return var;
     125}
     126
     127template<class T> T* tlsGetPtr(T*& var)
     128{
    95129return var;
    96130}
     
    105139}
    106140
     141#define tlsGetSetPtr(var,o) tlsSet(var ## _ptr,o)
     142
    107143#else
    108144
     
    115151#define tlsSet(var,o) var.set(o)
    116152
     153#define THREAD_LOCAL_DECL_PTR(cls,var) ThreadSingletonPtr<cls> var ## _ptr
     154#define THREAD_LOCAL_DEF_PTR(cls,var) ThreadSingletonPtr<cls> var ## _ptr
     155#define tlsGetPtr(var) var ## _ptr.get()
     156#define tlsSetPtr(var,o) var ## _ptr.setptr(o)
     157
    117158#endif
    118159
  • cpp/frams/genetics/geno.cpp

    r286 r346  
    77#include <frams/model/model.h>
    88
    9 SListTempl<GenoValidator*> Geno::validators;
    109GenoConvManager *Geno::converters = NULL;
     10
     11THREAD_LOCAL_DEF(Geno::Validators, geno_validators);
     12
     13Geno::Validators& Geno::getValidators() {return tlsGetRef(geno_validators);}
    1114
    1215void Geno::init(const SString& genstring, char genformat, const SString& genname, const SString& comment)
     
    234237        if (isvalid >= 0) return;
    235238        if (gen.len() == 0) { isvalid = 0; return; }
    236         FOREACH(GenoValidator*, v, validators)
     239        Validators& vals=getValidators();
     240        FOREACH(GenoValidator*, v, vals)
    237241                if ((isvalid = v->testGenoValidity(*this)) >= 0)
    238242                        return;
  • cpp/frams/genetics/geno.h

    r286 r346  
    5454
    5555public:
     56        typedef SListTempl<GenoValidator*> Validators;
     57
    5658        /// create a genotype object from primitives
    5759        /// @param genstring pure genotype, without any comments
     
    116118
    117119        // managing global Geno-related objects (used for validation and conversion)
    118         static void addValidator(GenoValidator* gv) { validators += gv; }
    119         static void removeValidator(GenoValidator* gv) { validators -= gv; }
     120        static Validators& getValidators();
     121        static void addValidator(GenoValidator* gv,int at_position=9999) { getValidators().insert(at_position,gv); }
     122        static void removeValidator(GenoValidator* gv) { getValidators() -= gv; }
    120123        static void useConverters(GenoConvManager& gcm) { converters = &gcm; }
    121124        static GenoConvManager &getConverters() { return *converters; }
    122125protected:
    123126        static GenoConvManager *converters;
    124         static SListTempl<GenoValidator*> validators;
    125127};
    126128
  • cpp/frams/genetics/preconfigured.h

    r286 r346  
    3030};
    3131
     32/** Initialization procedure for applications adding their own validators */
     33class PreconfiguredGenetics_NoValidators
     34{
     35  public:
     36DefaultGenoConvManager gcm;
     37
     38PreconfiguredGenetics_NoValidators()
     39        {
     40        gcm.addDefaultConverters(); //without converters, the application would only handle "format 0" genotypes
     41        Geno::useConverters(gcm);
     42        }
     43};
     44
    3245#endif
    3346
Note: See TracChangeset for help on using the changeset viewer.