00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
00063
00064
00065
00066
00067
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
00080 LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
00081 "Creating timerfd periodic thread implementation." << endl);
00082 m_impl = new PeriodicThreadImplTimerfd(period);
00083 # else
00084
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
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
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
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 }