PeriodicThread.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00021 //----------------------------------------------------------------------
00022 #include "Logging.h"
00023 #include "PeriodicThread.h"
00024 #include "PeriodicThreadImpl.h"
00025 
00026 #if defined _SYSTEM_QNX_
00027 # include "PeriodicThreadImplQNX.h"
00028 #elif defined _SYSTEM_POSIX_
00029 # if defined _SYSTEM_LXRT_
00030 #  include "PeriodicThreadImplLxrt.h"
00031 # endif
00032 # if defined _SYSTEM_LINUX_
00033 #  include <linux/version.h>
00034 #  if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
00035 #   include "PeriodicThreadImplTimerfd.h"
00036 #  endif
00037 # endif
00038 # include "PeriodicThreadImplEmulate.h"
00039 #elif defined _SYSTEM_WIN32_
00040 # include "PeriodicThreadImplEmulate.h"
00041 #else
00042 # error "No implementation specified for System dependent components"
00043 #endif
00044 
00045 using icl_core::logging::endl;
00046 
00047 namespace icl_core {
00048 namespace thread {
00049 
00050 PeriodicThread::PeriodicThread(const icl_core::String& description,
00051                                const icl_core::TimeSpan& period,
00052                                ThreadPriority priority)
00053   : Thread(description, priority)
00054 {
00055 #if defined (_SYSTEM_QNX_)
00056   LOGGING_DEBUG_CO(Thread, PeriodicThread, threadInfo(),
00057                    "Creating QNX periodic thread implementation." << endl);
00058   m_impl = new PeriodicThreadImplQNX(period);
00059 
00060 #elif defined (_SYSTEM_POSIX_)
00061 # if defined (_SYSTEM_LXRT_)
00062   // Only create an LXRT implementation if the LXRT runtime system is
00063   // really available. Otherwise create an ACE or POSIX
00064   // implementation, depending on the system configuration.
00065   // Remark: This allows us to compile programs with LXRT support but
00066   // run them on systems on which no LXRT is installed and to disable
00067   // LXRT support at program startup on systems with installed LXRT!
00068   if (icl_core::os::isLxrtAvailable())
00069   {
00070     LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
00071                      "Creating LXRT periodic thread implementation." << endl);
00072     m_impl = new PeriodicThreadImplLxrt(period);
00073   }
00074   else
00075   {
00076 # endif
00077 # if defined (_SYSTEM_LINUX_)
00078 #  if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
00079     // Linux system with timerfd in non LXRT mode.
00080     LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
00081                      "Creating timerfd periodic thread implementation." << endl);
00082     m_impl = new PeriodicThreadImplTimerfd(period);
00083 #  else
00084     // Older Linux system in non LXRT mode.
00085     LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
00086                      "Creating emulate periodic thread implementation." << endl);
00087     m_impl = new PeriodicThreadImplEmulate(period);
00088 #  endif
00089 # else
00090     // Generic POSIX system.
00091     LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
00092                      "Creating emulate periodic thread implementation." << endl);
00093     m_impl = new PeriodicThreadImplEmulate(period);
00094 # endif
00095 # if defined(_SYSTEM_LXRT_)
00096   }
00097 # endif
00098 
00099 #elif defined (_SYSTEM_WIN32_)
00100   LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
00101                    "Creating emulate periodic thread implementation." << endl);
00102   m_impl = new PeriodicThreadImplEmulate(period);
00103 #endif
00104 }
00105 
00106 PeriodicThread::~PeriodicThread()
00107 {
00108   delete m_impl; m_impl = NULL;
00109 }
00110 
00111 icl_core::TimeSpan PeriodicThread::period() const
00112 {
00113   return m_impl->period();
00114 }
00115 
00116 bool PeriodicThread::setPeriod(const icl_core::TimeSpan& period)
00117 {
00118   return m_impl->setPeriod(period);
00119 }
00120 
00122 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
00123 
00127 icl_core::TimeSpan PeriodicThread::Period() const
00128 {
00129   return period();
00130 }
00131 
00135 bool PeriodicThread::SetPeriod(const icl_core::TimeSpan& period)
00136 {
00137   return setPeriod(period);
00138 }
00139 
00140 #endif
00141 
00142 
00143 void PeriodicThread::waitPeriod()
00144 {
00145   LOGGING_TRACE_CO(IclCoreThread, PeriodicThread, threadInfo(), "Begin." << endl);
00146 
00147   // Reset to hard or soft realtime mode, if necessary.
00148   if (isHardRealtime() && !executesHardRealtime())
00149   {
00150     if (setHardRealtime(true))
00151     {
00152       LOGGING_INFO_CO(IclCoreThread, PeriodicThread, threadInfo(),
00153                       "Resetted to hard realtime mode." << endl);
00154     }
00155     else
00156     {
00157       LOGGING_ERROR_CO(IclCoreThread, PeriodicThread, threadInfo(),
00158                        "Resetting to hard realtime mode failed!" << endl);
00159     }
00160   }
00161   else if (!isHardRealtime() && executesHardRealtime())
00162   {
00163     if (setHardRealtime(false))
00164     {
00165       LOGGING_INFO_CO(IclCoreThread, PeriodicThread, threadInfo(),
00166                       "Resetted to soft realtime mode." << endl);
00167     }
00168     else
00169     {
00170       LOGGING_ERROR_CO(IclCoreThread, PeriodicThread, threadInfo(),
00171                        "Resetting to soft realtime mode failed!" << endl);
00172     }
00173   }
00174 
00175   // Wait!
00176   m_impl->waitPeriod();
00177 
00178   LOGGING_TRACE_CO(IclCoreThread, PeriodicThread, threadInfo(), "Done." << endl);
00179 }
00180 
00181 void PeriodicThread::makePeriodic()
00182 {
00183   LOGGING_TRACE_CO(IclCoreThread, PeriodicThread, threadInfo(), "Begin." << endl);
00184 
00185   m_impl->makePeriodic();
00186 
00187   LOGGING_TRACE_CO(IclCoreThread, PeriodicThread, threadInfo(), "Done." << endl);
00188 }
00189 
00191 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
00192 
00196 void PeriodicThread::WaitPeriod()
00197 {
00198   waitPeriod();
00199 }
00200 
00201 #endif
00202 
00203 
00204 }
00205 }


fzi_icl_core
Author(s):
autogenerated on Thu Jun 6 2019 20:22:24