Changeset 20:6a5ce655e1ab in rrlib_thread


Ignore:
Timestamp:
03.03.2014 22:09:48 (6 years ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Message:

Removed boost dependency (replaced boost::thread_specific_ptr with standard thread_local)

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • make.xml

    r14 r20  
    22<targets> 
    33 
    4   <rrlib libs="boost_thread pthread"> 
     4  <rrlib libs="pthread"> 
    55    <sources> 
    66      *.h 
  • tConditionVariable.h

    r15 r20  
    135135  tMutex& mutex; 
    136136 
    137   /*! Wrapped boost condition variable */ 
     137  /*! Wrapped condition variable */ 
    138138  std::condition_variable wrapped; 
    139139 
  • tThread.cpp

    r15 r20  
    3737#include <sys/resource.h> 
    3838#include <sys/syscall.h> 
     39#include <unistd.h> 
     40 
    3941#include "rrlib/design_patterns/singleton.h" 
    4042//---------------------------------------------------------------------- 
     
    7274//---------------------------------------------------------------------- 
    7375 
    74 __thread tThread* tThread::cur_thread = NULL; 
    75  
     76thread_local tThread::tPointer tThread::current_thread; 
    7677 
    7778namespace internal 
    7879{ 
    79 class tThreadCleanup 
    80 { 
    81 public: 
    82   static void Cleanup(tThread* t) 
    83   { 
    84     tLock l(*t); 
    85     t->state = tThread::tState::TERMINATED; 
    86     //t->curThread.reset(); 
    87     l.Unlock(); 
    88     t->self.reset(); // possibly delete thread - important that it's last statement 
    89   } 
    90 }; 
    9180 
    9281/*! 
     
    112101  return oss.str(); 
    113102} 
    114  
    115 template <typename T> 
    116 struct CreateCurThreadLocal 
    117 { 
    118   static T* Create() 
    119   { 
    120     return new T(tThreadCleanup::Cleanup); 
    121   } 
    122   static void Destroy(T* object) 
    123   { 
    124     delete object; 
    125   } 
    126 }; 
    127103 
    128104/*! List of threads currently known and running (== all thread objects created) */ 
     
    285261} 
    286262 
    287 typedef rrlib::design_patterns::tSingletonHolder<boost::thread_specific_ptr<tThread>, rrlib::design_patterns::singleton::Longevity, internal::CreateCurThreadLocal> tCurThreadLocal; 
    288 static inline unsigned int GetLongevity(boost::thread_specific_ptr<tThread>*) 
    289 { 
    290   return 0xFFFFFFFC; // should exist longer than anything using rrlib locks 
    291 } 
    292  
    293 boost::thread_specific_ptr<tThread>& tThread::GetCurThreadLocal() 
    294 { 
    295   return tCurThreadLocal::Instance(); 
    296 } 
    297  
    298263std::string tThread::GetLogDescription() const 
    299264{ 
     
    343308{ 
    344309  //unsafe _FINROC_LOG_MESSAGE(DEBUG_VERBOSE_2, logDomain) << "Entering"; 
    345   cur_thread = this; 
     310  current_thread.pointer = this; 
    346311  tLock l(*this); 
    347312  state = tState::PREPARE_RUNNING; 
    348313  //unsafe _FINROC_LOG_MESSAGE(DEBUG_VERBOSE_2, logDomain) << "Locked"; 
    349   //curThread = threadPtr; 
    350   GetCurThreadLocal().reset(this); 
    351   //unsafe _FINROC_LOG_MESSAGE(DEBUG_VERBOSE_2, logDomain) << "ThreadLocal set"; 
    352314 
    353315  // wait for start signal 
     
    602564} 
    603565 
     566tThread::tPointer::~tPointer() 
     567{ 
     568  tLock l(*pointer); 
     569  pointer->state = tThread::tState::TERMINATED; 
     570  //t->curThread.reset(); 
     571  l.Unlock(); 
     572  pointer->self.reset(); // possibly delete thread - important that it's last statement 
     573} 
     574 
    604575//---------------------------------------------------------------------- 
    605576// End of namespace declaration 
  • tThread.h

    r15 r20  
    4141// External includes (system with <>, local with "") 
    4242//---------------------------------------------------------------------- 
    43 #include <boost/thread/tss.hpp> 
    4443#include <thread> 
    4544#include <atomic> 
     
    126125  inline static tThread& CurrentThread() 
    127126  { 
    128     tThread* result = cur_thread; 
     127    tThread* result = current_thread.pointer; 
    129128    if (result == NULL)   // unknown thread 
    130129    { 
    131       result = GetCurThreadLocal().get(); 
    132       if (result == NULL) 
    133       { 
    134         result = new tThread(true, false); // will be deleted by thread local 
    135         GetCurThreadLocal().reset(result); // safe because it's the same thread 
    136       } 
    137       cur_thread = result; 
     130      result = new tThread(true, false); // will be deleted by thread local 
     131      current_thread.pointer = result; 
    138132    } 
    139133    return *result; 
     
    412406  std::vector<std::shared_ptr<void>> locked_objects; 
    413407 
     408  /*! Holds thread pointer and has destructor for thread cleanup */ 
     409  class tPointer 
     410  { 
     411  public: 
     412    tThread* pointer; 
     413    tPointer() : pointer(NULL) {} 
     414    ~tPointer(); 
     415  }; 
     416 
    414417  /*! Reference to current thread */ 
    415   static __thread tThread* cur_thread; 
     418  static thread_local tPointer current_thread; 
    416419 
    417420  /*! 
     
    446449  void AddToThreadList(); 
    447450 
    448   /*! Stores thread local information on current thread */ 
    449   static boost::thread_specific_ptr<tThread>& GetCurThreadLocal(); 
    450  
    451451  /*! 
    452452   * Newly created threads enter this method 
Note: See TracChangeset for help on using the changeset viewer.