Changeset 30:fa354798480f in rrlib_thread


Ignore:
Timestamp:
20.06.2014 16:42:18 (5 years ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Message:

Dependency to pthread library is now optional. Added RRLIB_SINGLE_THREADED #ifndefs to all places where they were still missing.

Files:
8 edited

Legend:

Unmodified
Added
Removed
  • make.xml

    r29 r30  
    33<targets> 
    44 
    5   <library libs="pthread"> 
     5  <library optionallibs="pthread"> 
    66    <sources> 
    77      *.h 
  • tConditionVariable.cpp

    r19 r30  
    7070//---------------------------------------------------------------------- 
    7171 
     72#ifndef RRLIB_SINGLE_THREADED 
    7273typedef rrlib::design_patterns::tSingletonHolder<std::vector<tConditionVariable*>> tConditionVariableListSingleton; 
    7374static std::vector<tConditionVariable*>* GetConditionVariableList() 
     
    352353  } 
    353354} 
     355#else // RRLIB_SINGLE_THREADED 
     356 
     357tConditionVariable::tConditionVariable(tMutex& mutex) 
     358{} 
     359 
     360tConditionVariable::~tConditionVariable() 
     361{} 
     362 
     363void tConditionVariable::Notify(tLock& l) 
     364{} 
     365 
     366void tConditionVariable::NotifyAll(tLock& l) 
     367{} 
     368 
     369void tConditionVariable::Wait(tLock& l) 
     370{} 
     371 
     372void tConditionVariable::Wait(tLock& l, const rrlib::time::tDuration& wait_for, bool use_application_time, rrlib::time::tTimestamp wait_until) 
     373{} 
     374 
     375#endif // RRLIB_SINGLE_THREADED 
    354376 
    355377//---------------------------------------------------------------------- 
  • tConditionVariable.h

    r20 r30  
    4242//---------------------------------------------------------------------- 
    4343#include "rrlib/time/time.h" 
     44 
     45#ifndef RRLIB_SINGLE_THREADED 
    4446#include <condition_variable> 
     47#endif 
    4548 
    4649//---------------------------------------------------------------------- 
     
    130133private: 
    131134 
     135#ifndef RRLIB_SINGLE_THREADED 
    132136  friend class tTimeStretchingListenerImpl; 
    133137 
     
    160164   */ 
    161165  bool ConditionVariableLockCorrectlyAcquired(const tLock& l) const; 
     166#endif 
    162167}; 
    163168 
  • tLock.h

    r15 r30  
    240240 
    241241  template <typename T1, typename T2> 
    242   tLock(const T& mutex, T2 parameter) {} 
     242  tLock(const T1& mutex, T2 parameter) {} 
    243243 
    244244  ~tLock() {} 
     245 
     246  inline void Lock() 
     247  {} 
     248 
     249  inline bool TryLock() 
     250  { 
     251    return true; 
     252  } 
     253 
     254  void Unlock() 
     255  {} 
    245256 
    246257#endif 
  • tLoopThread.h

    r19 r30  
    9292  inline rrlib::time::tTimestamp GetCurrentCycleStartTime() const 
    9393  { 
     94#ifndef RRLIB_SINGLE_THREADED 
    9495    assert(&CurrentThread() == this && "Please only call from this thread"); 
     96#endif 
    9597    return last_cycle_start; 
    9698  } 
  • tOrderedMutexBaseClass.h

    r15 r30  
    8585  const char* GetDescription() const 
    8686  { 
     87#ifndef RRLIB_SINGLE_THREADED 
    8788    return description; 
     89#else 
     90    return ""; 
     91#endif 
    8892  } 
    8993 
  • tThread.cpp

    r20 r30  
    3535#include <sstream> 
    3636#include <cstring> 
     37 
     38#ifndef RRLIB_SINGLE_THREADED 
    3739#include <sys/resource.h> 
    3840#include <sys/syscall.h> 
    3941#include <unistd.h> 
     42#endif 
    4043 
    4144#include "rrlib/design_patterns/singleton.h" 
     
    6972// Const values 
    7073//---------------------------------------------------------------------- 
     74#ifdef _PTHREAD_H 
     75#define RRLIB_THREAD_USING_PTHREADS 
     76#endif 
    7177 
    7278//---------------------------------------------------------------------- 
     
    7480//---------------------------------------------------------------------- 
    7581 
     82#ifndef RRLIB_SINGLE_THREADED 
    7683thread_local tThread::tPointer tThread::current_thread; 
     84#endif 
    7785 
    7886namespace internal 
     
    128136  tLock lock2(GetThreadList()->obj_mutex); 
    129137  // hmm... we start at id 1024 - as the former threads may be more long-lived 
    130   counter = std::max(1023u, counter); 
     138  counter = std::max<uint32_t>(1023u, counter); 
    131139  std::vector<std::weak_ptr<tThread>>& current_threads = GetThreadList()->vec; 
    132140  while (true) 
     
    168176  longevity(0), 
    169177  unknown_thread(true), 
     178#ifndef RRLIB_SINGLE_THREADED 
    170179  wrapped_thread(), 
    171180  handle(pthread_self()), 
     181#endif 
    172182  joining_threads(0) 
    173183{ 
    174184  AddToThreadList(); 
    175185 
     186#ifdef RRLIB_THREAD_USING_PTHREADS 
    176187  // see if we can obtain a thread name 
    177188  char name_buffer[1024]; 
     
    184195    } 
    185196  } 
     197#endif 
    186198} 
    187199 
     
    201213  longevity(0), 
    202214  unknown_thread(false), 
     215#ifndef RRLIB_SINGLE_THREADED 
    203216  wrapped_thread(&Launch, this), 
    204217  handle(wrapped_thread.native_handle()), 
     218#endif 
    205219  joining_threads(0) 
    206220{ 
     
    236250  if (!unknown_thread) 
    237251  { 
     252#ifndef RRLIB_SINGLE_THREADED 
    238253    if (&tThread::CurrentThread() != this) 
    239254    { 
     
    244259      wrapped_thread.detach(); 
    245260    } 
     261#endif 
    246262  } 
    247263 
     
    275291    return; 
    276292  } 
     293 
     294#ifndef RRLIB_SINGLE_THREADED 
    277295  if (!wrapped_thread.joinable()) 
    278296  { 
     
    298316  } 
    299317  RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "Joined Thread"); 
     318#endif 
    300319} 
    301320 
     
    307326void tThread::Launcher() 
    308327{ 
     328#ifndef RRLIB_SINGLE_THREADED 
    309329  //unsafe _FINROC_LOG_MESSAGE(DEBUG_VERBOSE_2, logDomain) << "Entering"; 
    310330  current_thread.pointer = this; 
     
    344364    RRLIB_LOG_PRINT(ERROR, "Thread encountered exception during cleanup: ", e.what()); 
    345365  } 
     366#endif 
    346367} 
    347368 
     
    370391{ 
    371392  this->name = name; 
     393#ifdef RRLIB_THREAD_USING_PTHREADS 
    372394  pthread_setname_np(handle, name.substr(0, 15).c_str()); 
     395#endif 
    373396} 
    374397 
    375398void tThread::SetPriority(int new_priority) 
    376399{ 
     400#ifdef RRLIB_THREAD_USING_PTHREADS 
    377401  //if (new_priority < sched_get_priority_min(SCHED_OTHER) || new_priority > sched_get_priority_max(SCHED_OTHER)) 
    378402  if (new_priority < -20 || new_priority > 19) 
     
    400424  RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "Set niceness to ", new_priority); 
    401425  priority = new_priority; 
     426#endif 
    402427} 
    403428 
    404429void tThread::SetRealtime() 
    405430{ 
     431#ifdef RRLIB_THREAD_USING_PTHREADS 
    406432  struct sched_param param; 
    407433  param.sched_priority = 49; 
     
    412438    RRLIB_LOG_PRINT(ERROR, "Failed making thread a real-time thread.", (error_code == EPERM ? " Caller does not have appropriate privileges." : "")); 
    413439  } 
     440#endif 
    414441} 
    415442 
     
    417444void tThread::Sleep(const rrlib::time::tDuration& sleep_for, bool use_application_time, rrlib::time::tTimestamp wait_until) 
    418445{ 
     446#ifndef RRLIB_SINGLE_THREADED 
    419447  rrlib::time::tTimeMode time_mode = rrlib::time::GetTimeMode(); 
    420448  tThread& t = CurrentThread(); 
     
    451479    } 
    452480  } 
     481#else 
     482  std::this_thread::sleep_for(sleep_for); 
     483#endif 
    453484} 
    454485 
     
    480511bool tThread::StopThreads(bool query_only) 
    481512{ 
     513#ifndef RRLIB_SINGLE_THREADED 
    482514  volatile static bool stopping_threadz = false; 
    483515  if (stopping_threadz || query_only)   // We don't do this twice 
     
    555587    } 
    556588  } 
     589#endif 
    557590 
    558591  return true; 
  • tThread.h

    r20 r30  
    9494public: 
    9595 
    96   typedef std::thread tWrappedThread; 
    9796  typedef uint32_t tThreadId; 
     97 
     98#ifndef RRLIB_SINGLE_THREADED 
     99  typedef std::thread::native_handle_type tNativeHandle; 
     100#else 
     101  typedef tThreadId tNativeHandle; 
     102#endif 
    98103 
    99104  /*! 
     
    106111  virtual ~tThread(); 
    107112 
     113#ifndef RRLIB_SINGLE_THREADED 
    108114  /*! 
    109115   * (convenience function) 
     
    133139    return *result; 
    134140  } 
     141#endif 
    135142 
    136143  bool GetDeleteOnCompletion() const 
     
    184191   * \return Native thread handle 
    185192   */ 
    186   inline std::thread::native_handle_type GetNativeHandle() const 
    187   { 
     193  inline tNativeHandle GetNativeHandle() const 
     194  { 
     195#ifndef RRLIB_SINGLE_THREADED 
    188196    return handle; 
     197#else 
     198    return GetId(); 
     199#endif 
    189200  } 
    190201 
     
    415426  }; 
    416427 
     428#ifndef RRLIB_SINGLE_THREADED 
    417429  /*! Reference to current thread */ 
    418430  static thread_local tPointer current_thread; 
     431#endif 
    419432 
    420433  /*! 
     
    428441  const bool unknown_thread; 
    429442 
     443#ifndef RRLIB_SINGLE_THREADED 
    430444  /*! wrapped thread */ 
    431   tWrappedThread wrapped_thread; 
    432  
    433   /*! pthread/whatever handle */ 
    434   std::thread::native_handle_type handle; 
     445  std::thread wrapped_thread; 
     446 
     447  /*! native thread handle */ 
     448  tNativeHandle handle; 
     449#endif 
    435450 
    436451  /*! Number of threads that are joining */ 
Note: See TracChangeset for help on using the changeset viewer.