Changeset 39:ad7a09181c77 in rrlib_util


Ignore:
Timestamp:
06.01.2012 18:44:11 (8 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Phase:
public
Message:

Made singleton threadsafe and renamed GetInstance() to Instance()

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • patterns/singleton/tSingletonHolder.h

    r38 r39  
    3939//---------------------------------------------------------------------- 
    4040#include <boost/noncopyable.hpp> 
     41#include <mutex> 
    4142 
    4243//---------------------------------------------------------------------- 
     
    8182public: 
    8283 
    83   static T &GetInstance() 
     84  static T &Instance() 
    8485  { 
    85     if (!tSingletonHolder::Instance()) 
     86    if (!tSingletonHolder::InstancePointer()) 
    8687    { 
    87       if (tSingletonHolder::Destroyed()) 
    88       { 
    89         TLifetimePolicy<T>::OnDeadReference(); 
    90         tSingletonHolder::Destroyed() = false; 
    91       } 
    92       tSingletonHolder::Instance() = TCreationPolicy<T>::Create(); 
    93       TLifetimePolicy<T>::ScheduleDestruction(&tSingletonHolder::DestroyInstance); 
     88      tSingletonHolder::CreateInstance(); 
    9489    } 
    95     return *tSingletonHolder::Instance(); 
     90    return *tSingletonHolder::InstancePointer(); 
    9691  } 
    9792 
     
    10398  tSingletonHolder(); 
    10499 
     100  static void CreateInstance() 
     101  { 
     102    static std::mutex mutex; 
     103    std::unique_lock<std::mutex> lock(mutex); 
     104 
     105    if (!tSingletonHolder::InstancePointer()) 
     106    { 
     107      if (tSingletonHolder::Destroyed()) 
     108      { 
     109        TLifetimePolicy<T>::OnDeadReference(); 
     110        tSingletonHolder::Destroyed() = false; 
     111      } 
     112      tSingletonHolder::InstancePointer() = TCreationPolicy<T>::Create(); 
     113      TLifetimePolicy<T>::ScheduleDestruction(&tSingletonHolder::DestroyInstance); 
     114    } 
     115  } 
     116 
    105117  static void DestroyInstance() 
    106118  { 
    107119    assert(!tSingletonHolder::Destroyed()); 
    108     TCreationPolicy<T>::Destroy(tSingletonHolder::Instance()); 
    109     tSingletonHolder::Instance() = 0; 
     120    TCreationPolicy<T>::Destroy(tSingletonHolder::InstancePointer()); 
     121    tSingletonHolder::InstancePointer() = 0; 
    110122    tSingletonHolder::Destroyed() = true; 
    111123  } 
    112124 
    113   static T *&Instance() 
     125  static T *&InstancePointer() 
    114126  { 
    115127    static T *instance = 0; 
    116128    return instance; 
    117129  } 
     130 
    118131  static bool &Destroyed() 
    119132  { 
  • test/test_factory_pattern.cpp

    r34 r39  
    126126  std::cout << "Testing factory" << std::endl; 
    127127 
    128   tShapeFactory::GetInstance().Register<tLine>("line"); 
    129   tShapeFactory::GetInstance().Register<tRectangle>("line"); 
    130   tShapeFactory::GetInstance().Register<tRectangle>("rectangle"); 
    131   tShapeFactory::GetInstance().Register<tCircle>("circle"); 
    132   tShapeFactory::GetInstance().Register<tEllipse>("ellipse"); 
     128  tShapeFactory::Instance().Register<tLine>("line"); 
     129  tShapeFactory::Instance().Register<tRectangle>("line"); 
     130  tShapeFactory::Instance().Register<tRectangle>("rectangle"); 
     131  tShapeFactory::Instance().Register<tCircle>("circle"); 
     132  tShapeFactory::Instance().Register<tEllipse>("ellipse"); 
    133133 
    134   tShape *line = tShapeFactory::GetInstance().Create("line"); 
    135   tShape *rectangle = tShapeFactory::GetInstance().Create("rectangle"); 
    136   tShape *circle = tShapeFactory::GetInstance().Create("circle"); 
    137   tShape *ellipse = tShapeFactory::GetInstance().Create("ellipse"); 
     134  tShape *line = tShapeFactory::Instance().Create("line"); 
     135  tShape *rectangle = tShapeFactory::Instance().Create("rectangle"); 
     136  tShape *circle = tShapeFactory::Instance().Create("circle"); 
     137  tShape *ellipse = tShapeFactory::Instance().Create("ellipse"); 
    138138 
    139139  std::cout << "Testing clone factory" << std::endl; 
    140140 
    141   tShapeCloneFactory::GetInstance().Register(*line, &CloneLine); 
    142   tShapeCloneFactory::GetInstance().Register(rectangle, &CloneRectangle); 
    143   tShapeCloneFactory::GetInstance().Register(*circle, &CloneCircle); 
     141  tShapeCloneFactory::Instance().Register(*line, &CloneLine); 
     142  tShapeCloneFactory::Instance().Register(rectangle, &CloneRectangle); 
     143  tShapeCloneFactory::Instance().Register(*circle, &CloneCircle); 
    144144 
    145   tShapeCloneFactory::GetInstance().Create(*line); 
    146   tShapeCloneFactory::GetInstance().Create(*rectangle); 
    147   tShapeCloneFactory::GetInstance().Create(*circle); 
    148 //  tShapeCloneFactory::GetInstance().Create(*ellipse); 
     145  tShapeCloneFactory::Instance().Create(*line); 
     146  tShapeCloneFactory::Instance().Create(*rectangle); 
     147  tShapeCloneFactory::Instance().Create(*circle); 
     148//  tShapeCloneFactory::Instance().Create(*ellipse); 
    149149 
    150   tShapeCloneFactory::GetInstance().Create(line); 
    151   tShapeCloneFactory::GetInstance().Create(rectangle); 
    152   tShapeCloneFactory::GetInstance().Create(circle); 
     150  tShapeCloneFactory::Instance().Create(line); 
     151  tShapeCloneFactory::Instance().Create(rectangle); 
     152  tShapeCloneFactory::Instance().Create(circle); 
    153153 
    154154 
  • test/test_singleton_pattern.cpp

    r38 r39  
    9191  { 
    9292    std::cout << "Keyboard dtor" << std::endl; 
    93     tLog::GetInstance().Print("Keyboard destroyed."); 
     93    tLog::Instance().Print("Keyboard destroyed."); 
    9494  } 
    9595 
     
    110110  { 
    111111    std::cout << "Display dtor" << std::endl; 
    112     tLog::GetInstance().Print("Display destroyed."); 
     112    tLog::Instance().Print("Display destroyed."); 
    113113  } 
    114114 
     
    122122int main(int argc, char **argv) 
    123123{ 
    124   tKeyboard::GetInstance().Type("foo"); 
    125   tDisplay::GetInstance().Show("bar"); 
     124  tKeyboard::Instance().Type("foo"); 
     125  tDisplay::Instance().Show("bar"); 
    126126 
    127127  return EXIT_SUCCESS; 
Note: See TracChangeset for help on using the changeset viewer.