PeriodicThread.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
21 //----------------------------------------------------------------------
22 #include "Logging.h"
23 #include "PeriodicThread.h"
24 #include "PeriodicThreadImpl.h"
25 
26 #if defined _SYSTEM_QNX_
27 # include "PeriodicThreadImplQNX.h"
28 #elif defined _SYSTEM_POSIX_
29 # if defined _SYSTEM_LXRT_
30 # include "PeriodicThreadImplLxrt.h"
31 # endif
32 # if defined _SYSTEM_LINUX_
33 # include <linux/version.h>
34 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
36 # endif
37 # endif
39 #elif defined _SYSTEM_WIN32_
41 #else
42 # error "No implementation specified for System dependent components"
43 #endif
44 
46 
47 namespace icl_core {
48 namespace thread {
49 
51  const icl_core::TimeSpan& period,
52  ThreadPriority priority)
53  : Thread(description, priority)
54 {
55 #if defined (_SYSTEM_QNX_)
57  "Creating QNX periodic thread implementation." << endl);
58  m_impl = new PeriodicThreadImplQNX(period);
59 
60 #elif defined (_SYSTEM_POSIX_)
61 # if defined (_SYSTEM_LXRT_)
62  // Only create an LXRT implementation if the LXRT runtime system is
63  // really available. Otherwise create an ACE or POSIX
64  // implementation, depending on the system configuration.
65  // Remark: This allows us to compile programs with LXRT support but
66  // run them on systems on which no LXRT is installed and to disable
67  // LXRT support at program startup on systems with installed LXRT!
69  {
70  LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
71  "Creating LXRT periodic thread implementation." << endl);
72  m_impl = new PeriodicThreadImplLxrt(period);
73  }
74  else
75  {
76 # endif
77 # if defined (_SYSTEM_LINUX_)
78 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
79  // Linux system with timerfd in non LXRT mode.
80  LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
81  "Creating timerfd periodic thread implementation." << endl);
82  m_impl = new PeriodicThreadImplTimerfd(period);
83 # else
84  // Older Linux system in non LXRT mode.
85  LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
86  "Creating emulate periodic thread implementation." << endl);
87  m_impl = new PeriodicThreadImplEmulate(period);
88 # endif
89 # else
90  // Generic POSIX system.
91  LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
92  "Creating emulate periodic thread implementation." << endl);
93  m_impl = new PeriodicThreadImplEmulate(period);
94 # endif
95 # if defined(_SYSTEM_LXRT_)
96  }
97 # endif
98 
99 #elif defined (_SYSTEM_WIN32_)
100  LOGGING_DEBUG_CO(IclCoreThread, PeriodicThread, threadInfo(),
101  "Creating emulate periodic thread implementation." << endl);
102  m_impl = new PeriodicThreadImplEmulate(period);
103 #endif
104 }
105 
107 {
108  delete m_impl; m_impl = NULL;
109 }
110 
112 {
113  return m_impl->period();
114 }
115 
117 {
118  return m_impl->setPeriod(period);
119 }
120 
122 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
123 
127 icl_core::TimeSpan PeriodicThread::Period() const
128 {
129  return period();
130 }
131 
135 bool PeriodicThread::SetPeriod(const icl_core::TimeSpan& period)
136 {
137  return setPeriod(period);
138 }
139 
140 #endif
141 
144 {
145  LOGGING_TRACE_CO(IclCoreThread, PeriodicThread, threadInfo(), "Begin." << endl);
146 
147  // Reset to hard or soft realtime mode, if necessary.
149  {
150  if (setHardRealtime(true))
151  {
152  LOGGING_INFO_CO(IclCoreThread, PeriodicThread, threadInfo(),
153  "Resetted to hard realtime mode." << endl);
154  }
155  else
156  {
157  LOGGING_ERROR_CO(IclCoreThread, PeriodicThread, threadInfo(),
158  "Resetting to hard realtime mode failed!" << endl);
159  }
160  }
161  else if (!isHardRealtime() && executesHardRealtime())
162  {
163  if (setHardRealtime(false))
164  {
165  LOGGING_INFO_CO(IclCoreThread, PeriodicThread, threadInfo(),
166  "Resetted to soft realtime mode." << endl);
167  }
168  else
169  {
170  LOGGING_ERROR_CO(IclCoreThread, PeriodicThread, threadInfo(),
171  "Resetting to soft realtime mode failed!" << endl);
172  }
173  }
174 
175  // Wait!
176  m_impl->waitPeriod();
177 
178  LOGGING_TRACE_CO(IclCoreThread, PeriodicThread, threadInfo(), "Done." << endl);
179 }
180 
182 {
183  LOGGING_TRACE_CO(IclCoreThread, PeriodicThread, threadInfo(), "Begin." << endl);
184 
185  m_impl->makePeriodic();
186 
187  LOGGING_TRACE_CO(IclCoreThread, PeriodicThread, threadInfo(), "Done." << endl);
188 }
189 
191 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
192 
196 void PeriodicThread::WaitPeriod()
197 {
198  waitPeriod();
199 }
200 
201 #endif
202 
204 }
205 }
Contains icl_core::thread::PeriodicThread.
virtual icl_core::TimeSpan period() const =0
Contains icl_core::thread::PeriodicThreadImpl.
#define LOGGING_ERROR_CO(stream, classname, objectname, arg)
Contains icl_core::thread::PeriodicThreadImplQNX.
Contains icl_core::thread::PeriodicThread.
bool isLxrtAvailable()
Definition: os_lxrt.cpp:141
Contains logging definitions for the icl_core_thread library.
bool setPeriod(const icl_core::TimeSpan &period)
ThreadStream & endl(ThreadStream &stream)
Definition: ThreadStream.h:249
#define LOGGING_TRACE_CO(stream, classname, objectname, arg)
#define LOGGING_DEBUG_CO(stream, classname, objectname, arg)
bool setHardRealtime(bool hard_realtime=true)
Repesents absolute times.
Definition: TimeSpan.h:46
#define LOGGING_INFO_CO(stream, classname, objectname, arg)
std::string String
Definition: BaseTypes.h:43
Contains icl_core::thread::PeriodicThreadImplTimerfd.
const char * threadInfo() const
PeriodicThread(const icl_core::String &description, const icl_core::TimeSpan &period, ThreadPriority priority=0)
icl_core::TimeSpan period() const
int32_t ThreadPriority
Definition: os_thread.h:50
Defines icl_core::thread::PeriodicThreadImplLxrt.
virtual bool setPeriod(const icl_core::TimeSpan &period)=0


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58