Changeset c6a59b


Ignore:
Timestamp:
May 5, 2016, 1:06:20 PM (9 years ago)
Author:
Frederik Heber <heber@…>
Children:
c1e104
Parents:
039e15
git-author:
Frederik Heber <heber@…> (05/05/16 09:20:08)
git-committer:
Frederik Heber <heber@…> (05/05/16 13:06:20)
Message:

Singleton extended to also return AtomicInstance-wrapped instance.

Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/CodePatterns/Singleton.hpp

    r039e15 rc6a59b  
    1717
    1818#include "CodePatterns/Assert.hpp"
     19#include "CodePatterns/AtomicInstance.hpp"
    1920
    2021/**
     
    145146   * when only the Singleton pattern is friend with the class
    146147   *
    147    * All methods have similar sematics to auto_ptr
     148   * All methods have similar semantics to auto_ptr
    148149   */
    149150  class ptr_t {
     
    235236
    236237  /**
     238   * returns the instance of this Singleton as a reference inside a mutex-locked
     239   * unique ptr.
     240   *
     241   * If no Singleton exists at this point and we are allowed to create one
     242   * a new one is created and stored inside the singleton. If the mutex is locked,
     243   * the function will wait until the mutex is released. This can be used to
     244   * ensure atomic access to the static instance.
     245   */
     246  static AtomicInstance<T> getLockedInstance();
     247
     248  /**
    237249   * returns the instance of this singleton as a pointer
    238250   *
  • src/CodePatterns/Singleton_impl.hpp

    r039e15 rc6a59b  
    4545  }
    4646  return *theInstance;
     47}
     48
     49template <class T,bool _may_create>
     50AtomicInstance<T> Singleton<T,_may_create>::getLockedInstance(){
     51  // boost supports RAII-Style locking, so we don't need to unlock
     52  boost::recursive_mutex::scoped_lock guard(instanceLock);
     53  if(!theInstance.get()) {
     54    theInstance.reset(creator::make());
     55  }
     56  T *ptr = theInstance.get();
     57  return AtomicInstance<T>(ptr);
    4758}
    4859
     
    120131    template name& Singleton< name , name::may_create >::getInstance(); \
    121132    template const name& Singleton< name , name::may_create >::getConstInstance(); \
     133    template AtomicInstance<name> Singleton< name , name::may_create >::getLockedInstance(); \
    122134    template name* Singleton< name , name::may_create >::getPointer();  \
    123135    template const name* Singleton< name , name::may_create >::getConstPointer();  \
  • src/Patterns/Makefile.am

    r039e15 rc6a59b  
    2323        $(top_srcdir)/src/CodePatterns/ManipulablePrototypeFactory_impl.hpp \
    2424        $(top_srcdir)/src/CodePatterns/ObservedValue.hpp \
    25         $(top_srcdir)/src/CodePatterns/Registry_impl.hpp \
    26         $(top_srcdir)/src/CodePatterns/Singleton_impl.hpp \
    2725        $(top_srcdir)/src/CodePatterns/PrototypeFactory.hpp \
    2826        $(top_srcdir)/src/CodePatterns/PrototypeFactory_impl.hpp \
    2927        $(top_srcdir)/src/CodePatterns/Registry.hpp \
    30         $(top_srcdir)/src/CodePatterns/Singleton.hpp
     28        $(top_srcdir)/src/CodePatterns/Registry_impl.hpp \
     29        $(top_srcdir)/src/CodePatterns/Singleton.hpp \
     30        $(top_srcdir)/src/CodePatterns/Singleton_impl.hpp
    3131
    3232PATTERNDEBUGHEADER =
  • src/Patterns/unittests/SingletonUnitTest.cpp

    r039e15 rc6a59b  
    8181CONSTRUCT_SINGLETON(SingletonStub2);
    8282
    83 void SingletonTest::setUp(){}
     83void SingletonTest::setUp()
     84{
     85  ASSERT_DO(Assert::Throw);
     86}
     87
    8488void SingletonTest::tearDown(){}
    8589
     
    152156  // mechanism. Check with Valgrind to see if memory-leak occurs
    153157  std::cout << "Not purging Singleton!\n Check with Valgrind to see if automatic purgins is working!" << std::endl;
     158}
    154159
     160void SingletonTest::LockedTest(){
     161  const AtomicInstance<SingletonStub1> ptr_1_1(SingletonStub1::getLockedInstance());
     162  const SingletonStub1 &test_1 = *ptr_1_1;
     163
     164  // this deadlocks
     165//  AtomicInstance<SingletonStub1> ptr_1_2(SingletonStub1::getLockedInstance());
     166
     167  AtomicInstance<SingletonStub2> ptr_2_1(SingletonStub2::getLockedInstance());
     168  SingletonStub2 &test_2 = *ptr_2_1;
     169
     170  // this deadlocks
     171//  AtomicInstance<SingletonStub2> ptr_2_2(SingletonStub2::getLockedInstance());
    155172}
  • src/Patterns/unittests/SingletonUnitTest.hpp

    r039e15 rc6a59b  
    2020  CPPUNIT_TEST_SUITE( SingletonTest );
    2121  CPPUNIT_TEST ( ConstructionTest );
     22  CPPUNIT_TEST ( LockedTest );
    2223  CPPUNIT_TEST_SUITE_END();
    2324
     
    2728
    2829  void ConstructionTest();
     30  void LockedTest();
    2931};
    3032
Note: See TracChangeset for help on using the changeset viewer.