Changeset 4:46a4150b6fd3 in rrlib_thread


Ignore:
Timestamp:
22.07.2012 13:28:38 (7 years ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Message:

tLoopThread now allows to set whether to use application or system time after construction.
Cleaned up thread class code.

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • internal/tLockStack.cpp

    r1 r4  
    102102static std::string GetLogDescription() 
    103103{ 
    104   return std::string("Lock stack for thread '") + tThread::CurrentThreadRaw()->GetName() + "'"; 
     104  return std::string("Lock stack for thread '") + tThread::CurrentThread().GetName() + "'"; 
    105105} 
    106106 
     
    143143  { 
    144144    stack_data = new tLockStackData(); 
    145     tThread* t = tThread::CurrentThreadRaw(); 
    146     assert(t); 
     145    tThread& t = tThread::CurrentThread(); 
    147146    // tLock l(*t); no thread-safety issue, since this is always the current thread itself 
    148     t->lock_stack = std::shared_ptr<tLockStackData>(stack_data); // make sure it will be deleted with thread 
     147    t.lock_stack = std::shared_ptr<tLockStackData>(stack_data); // make sure it will be deleted with thread 
    149148  } 
    150149 
  • tLoopThread.cpp

    r2 r4  
    8888void tLoopThread::MainLoop() 
    8989{ 
    90   while (!stop_signal) 
     90  while (!IsStopSignalSet()) 
    9191  { 
    9292    if (pause_signal.load(std::memory_order_relaxed)) 
     
    100100    if (last_cycle_start != rrlib::time::cNO_TIME) 
    101101    { 
     102      // copy atomics to local variables 
    102103      rrlib::time::tDuration cycle_time = this->cycle_time.Load(); 
     104      bool local_use_application_time = use_application_time.load(std::memory_order_relaxed); 
    103105 
    104106      // wait 
     
    118120        { 
    119121          RRLIB_LOG_PRINT(rrlib::logging::eLL_WARNING, "Waiting for ", rrlib::time::ToString(last_wait), ", as in last cycle, instead."); 
    120           Sleep(last_wait, use_application_time); 
     122          Sleep(last_wait, local_use_application_time); 
    121123        } 
    122124        else 
     
    128130      { 
    129131        last_wait = wait_for_x; 
    130         Sleep(wait_for_x, use_application_time, last_cycle_start + cycle_time); 
     132        Sleep(wait_for_x, local_use_application_time, last_cycle_start + cycle_time); 
    131133      } 
    132134      last_cycle_start += cycle_time; 
     
    156158  catch (const std::exception& e) 
    157159  { 
    158     RRLIB_LOG_PRINT(rrlib::logging::eLL_DEBUG, "Uncaught Thread Exception - ", e); 
     160    RRLIB_LOG_PRINT(rrlib::logging::eLL_DEBUG_WARNING, "Uncaught Exception: ", e); 
    159161  } 
    160162} 
     163 
     164void tLoopThread::SetUseApplicationTime(bool use_application_time) 
     165{ 
     166  //assert(&CurrentThread() == this && "Please only call from this thread"); 
     167  bool last_value = this->use_application_time.exchange(use_application_time); 
     168  if (last_value != use_application_time) 
     169  { 
     170    last_cycle_start != rrlib::time::cNO_TIME; 
     171  } 
     172} 
     173 
    161174 
    162175//---------------------------------------------------------------------- 
  • tLoopThread.h

    r0 r4  
    9090   * \return Start time of current cycle (always smaller than rrlib::time::Now()) 
    9191   */ 
    92   inline rrlib::time::tTimestamp GetCurrentCycleStartTime() 
    93   { 
    94     assert(CurrentThreadRaw() == this && "Please only call from this thread"); 
     92  inline rrlib::time::tTimestamp GetCurrentCycleStartTime() const 
     93  { 
     94    assert(&CurrentThread() == this && "Please only call from this thread"); 
    9595    return last_cycle_start; 
    9696  } 
     
    9999   * \return Current Cycle time with which callback function is called 
    100100   */ 
    101   inline rrlib::time::tDuration GetCycleTime() 
     101  inline rrlib::time::tDuration GetCycleTime() const 
    102102  { 
    103103    return cycle_time.Load(); 
     
    107107   * \return Time spent in last call to MainLoopCallback() 
    108108   */ 
    109   inline rrlib::time::tDuration GetLastCycleTime() 
     109  inline rrlib::time::tDuration GetLastCycleTime() const 
    110110  { 
    111111    return last_cycle_time.Load(); 
     
    115115   * \return Is Thread currently paused? 
    116116   */ 
    117   inline bool IsPausing() 
     117  inline bool IsPausing() const 
    118118  { 
    119119    return pause_signal; 
     
    123123   * \return Is thread currently running? (and not paused) 
    124124   */ 
    125   inline bool IsRunning() 
     125  inline bool IsRunning() const 
    126126  { 
    127127    return IsAlive() && !IsPausing(); 
     
    129129 
    130130  /*! 
    131    * \return Is the stop signal set in order to stop the thread? 
    132    */ 
    133   inline bool IsStopSignalSet() 
    134   { 
    135     return stop_signal; 
     131   * \return Is this thread using "application time" instead of system time (see rrlib/util/time.h)? 
     132   */ 
     133  inline bool IsUsingApplicationTime() const 
     134  { 
     135    return use_application_time; 
    136136  } 
    137137 
     
    159159  } 
    160160 
     161  /*! 
     162   * \param use_application_time Use "application time" (see rrlib/util/time.h) instead of system time? 
     163   * 
     164   * This will not have any effect on Sleep() and Wait() operations this thread might be executing. 
     165   * 
     166   * (This method is preferably called from this thread - to avoid issues in case 
     167   *  thread is currently performing operations with another time base) 
     168   */ 
     169  void SetUseApplicationTime(bool use_application_time); 
    161170 
    162171//---------------------------------------------------------------------- 
     
    164173//---------------------------------------------------------------------- 
    165174private: 
     175 
     176  // TODO: For optimization, we could put the following three atomic variables in a single 64-bit atomic 
    166177 
    167178  /*! Thread pauses if this flag is set */ 
     
    172183 
    173184  /*! Use "application time" (see rrlib/util/time.h) instead of system time? */ 
    174   const bool use_application_time; 
     185  std::atomic<bool> use_application_time; 
    175186 
    176187  /*! Display warning, if cycle time is exceeded? */ 
  • tThread.cpp

    r3 r4  
    224224  if (!unknown_thread) 
    225225  { 
    226     if (tThread::CurrentThreadRaw() != this) 
     226    if (&tThread::CurrentThread() != this) 
    227227    { 
    228228      Join(); // we shouldn't delete anything while thread is still running 
     
    278278    return; 
    279279  } 
    280   if (CurrentThreadRaw() == this) 
     280  if (&CurrentThread() == this) 
    281281  { 
    282282    RRLIB_LOG_PRINT(rrlib::logging::eLL_DEBUG_WARNING, "Thread cannot join itself"); 
     
    397397{ 
    398398  rrlib::time::tTimeMode time_mode = rrlib::time::GetTimeMode(); 
    399   tThread& t = *CurrentThreadRaw(); 
     399  tThread& t = CurrentThread(); 
    400400  if (time_mode == rrlib::time::tTimeMode::SYSTEM_TIME || (!use_application_time)) 
    401401  { 
     
    510510    std::weak_ptr<tThread> thread = current_threads[i]; 
    511511    std::shared_ptr<tThread> t = thread.lock(); 
    512     if (t && t.get() != CurrentThreadRaw()) 
     512    if (t && t.get() != &CurrentThread()) 
    513513    { 
    514514      if (t->unknown_thread) 
  • tThread.h

    r0 r4  
    107107 
    108108  /*! 
    109    * Note: To merely get current thread's id, Util.currentThreadId() is much faster. 
    110    * 
    111    * \return Shared Pointer to the thread that is currently executing. 
    112    */ 
    113   static std::shared_ptr<tThread> CurrentThread() 
    114   { 
    115     return CurrentThreadRaw()->self; 
    116   } 
    117  
    118   /*! 
    119109   * (convenience function) 
    120110   * 
     
    123113  inline static int64_t CurrentThreadId() 
    124114  { 
    125     return CurrentThreadRaw()->GetId(); 
    126   } 
    127  
    128   /*! 
    129    * (This is faster than CurrentThread()... However, it's only safe as long 
    130    *  as the returned thread pointer is only used by the thread itself. 
    131    *  Otherwise the thread might have already been deleted.) 
    132    * 
    133    * \return Raw Pointer to the thread that is currently executing. 
    134    */ 
    135   inline static tThread* CurrentThreadRaw() 
     115    return CurrentThread().GetId(); 
     116  } 
     117 
     118  /*! 
     119   * \return The thread that is currently executing. 
     120   * 
     121   * (Using the returned reference is only safe as long it is only used by the thread itself. 
     122   *  Otherwise the thread might have already been deleted. 
     123   *  Using CurrentThread().GetSharedPtr() is the safe alternative in this respect.) 
     124   */ 
     125  inline static tThread& CurrentThread() 
    136126  { 
    137127    tThread* result = cur_thread; 
     
    146136      cur_thread = result; 
    147137    } 
    148     return result; 
     138    return *result; 
    149139  } 
    150140 
     
    221211  { 
    222212    return state == tState::RUNNING || state == tState::PREPARE_RUNNING; 
     213  } 
     214 
     215  /*! 
     216   * \return Is the stop signal in order to stop this thread set? 
     217   */ 
     218  inline bool IsStopSignalSet() const 
     219  { 
     220    return stop_signal; 
    223221  } 
    224222 
     
    337335protected: 
    338336 
    339   //! Signal for stopping thread 
    340   std::atomic<bool> stop_signal; 
    341  
    342337  /*! 
    343338   * Default constructor for derived classes. 
     
    346341   */ 
    347342  tThread(const std::string& name = ""); 
     343 
     344  /*! 
     345   * \param value New value for signal for stopping thread 
     346   */ 
     347  void SetStopSignal(bool value) 
     348  { 
     349    stop_signal = value; 
     350  } 
    348351 
    349352//---------------------------------------------------------------------- 
     
    369372  static const int cNO_THREAD_ID = -1; 
    370373 
     374  /*! Signal for stopping thread */ 
     375  std::atomic<bool> stop_signal; 
     376 
    371377  /*! Holds on to lock stack as long as thread exists */ 
    372378  std::shared_ptr<void> lock_stack; 
Note: See TracChangeset for help on using the changeset viewer.