ADTF  3.18.2
thread_parameter.h
Go to the documentation of this file.
1 
7 #ifndef _THREAD_CLASS_HEADER_
8 #define _THREAD_CLASS_HEADER_
9 
10 #include <thread>
11 #include <functional>
12 
13 namespace A_UTILS_NS
14 {
15 
17 #ifndef __STDCALL
18 #ifdef WIN32
19 #define __STDCALL __stdcall
20 #else
21 #define __STDCALL
22 #endif
23 #endif
24 
26 #ifndef __THREAD_RESULT
27 #ifdef WIN32
28 #define __THREAD_RESULT unsigned long
29 #else
30 #define __THREAD_RESULT void*
31 #endif
32 #endif
33 
34 #ifdef _DEBUG
36 #define _DEBUG_NAME_SIZE 512
37 #endif
38 
40 #ifdef WIN32
41 #define __THREADCALL __stdcall
42 #else
43 #define __THREADCALL
44 #endif
45 
50 {
51  Default = 0,
52  Normal = 1,
53  RealTime = 2,
54 };
55 
57 #define A_UTILS_THREAD_PRIORITY_LOWEST 0.0
59 #define A_UTILS_THREAD_PRIORITY_LOW 0.25
61 #define A_UTILS_THREAD_PRIORITY_NORMAL 0.5
63 #define A_UTILS_THREAD_PRIORITY_HIGH 0.625
65 #define A_UTILS_THREAD_PRIORITY_HIGHEST 0.75
67 #define A_UTILS_THREAD_PRIORITY_TIME_CRITICAL 1.0
68 
70 typedef std::vector<bool> tCpuAffinityMask;
71 
76 {
82  fPriority{0.5}
83  {
84  }
85 
94  {
95  }
96 
108  {
109  }
110 
115 
120 
127  inline bool operator==(const tSchedulingParameters& sOther) const
128  {
129  return (eSchedulingClass == sOther.eSchedulingClass) &&
130  (fPriority == sOther.fPriority) &&
131  (oCpuAffinity == sOther.oCpuAffinity);
132  }
133 
137 };
138 
143 std::thread::native_handle_type get_native_handle_of_this_thread();
144 
152 tResult set_priority(std::thread::native_handle_type oThreadHandle,
153  tSchedulingClass eSchedulingClass, tFloat64 fPriority);
154 
162 tResult get_priority(std::thread::native_handle_type oThreadHandle,
163  tSchedulingClass& eSchedulingClass, tFloat64& fPriority);
164 
171 tResult set_cpu_affinity(std::thread::native_handle_type oThreadHandle,
172  const tCpuAffinityMask& oCpuAffinity);
173 
180 tResult get_cpu_affinity(std::thread::native_handle_type oThreadHandle,
181  tCpuAffinityMask& oCpuAffinity);
182 
189 tResult set_scheduling(std::thread::native_handle_type oThreadHandle,
190  const tSchedulingParameters& sScheduling);
191 
198 tResult get_scheduling(const std::thread::native_handle_type& oThreadHandle,
199  tSchedulingParameters& sScheduling);
200 
207 template <typename Callable, typename ...Args>
208 void run_with_scheduling(tSchedulingParameters sScheduling, Callable&& pFunc, Args&&... args)
209 {
211  pFunc(std::forward<Args>(args)...);
212 }
213 
221 template <typename Callable, typename ...Args>
222 void run_with_checked_scheduling(tSchedulingParameters sScheduling, Callable&& pFunc, Args&&... args)
223 {
225  {
226  pFunc(std::forward<Args>(args)...);
227  }
228 }
229 
235  const tSchedulingParameters& sScheduling,
236  std::function<void()> pFunc);
237 
246 template <typename Callable, typename ...Args>
248  const tSchedulingParameters& sScheduling,
249  Callable&& pFunc, Args&&... args)
250 {
251  // a lambda is not usable here, gcc 4.8 has a bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=41933
252  // so we use this bind as a workaround
253  auto pFuncCallable = std::bind(std::forward<Callable>(pFunc), std::forward<Args>(args)...);
254  return create_thread_with_scheduling_impl(oThread, sScheduling, pFuncCallable);
255 }
256 
264 template <typename Callable, typename ...Args>
266  Callable&& pFunc, Args&&... args)
267 {
268  std::thread oThread;
269  create_thread_with_scheduling(oThread, sScheduling,
270  std::forward<Callable>(pFunc),
271  std::forward<Args>(args)...);
272  return oThread;
273 }
274 
275 } // namespace A_UTILS_NS
276 
277 #endif // _THREAD_CLASS_HEADER_
double tFloat64
type definition for Float64 (64bit double values) (platform and compiler independent type).
uint32_t tUInt32
type definition for unsigned integer values (32bit) (platform and compiler independent type).
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11
tResult get_priority(std::thread::native_handle_type oThreadHandle, tSchedulingClass &eSchedulingClass, tFloat64 &fPriority)
Gets the priority of a thread.
std::thread::native_handle_type get_native_handle_of_this_thread()
Get the native handle for the current thread.
tSchedulingClass
Enum for different scheduling schemes.
@ Default
Choose whatever is applicable.
@ Normal
Basic non-realtime scheduling.
@ RealTime
Realtime scheduling.
tResult create_thread_with_scheduling_impl(std::thread &oThread, const tSchedulingParameters &sScheduling, std::function< void()> pFunc)
For internal use only.
tResult get_cpu_affinity(std::thread::native_handle_type oThreadHandle, tCpuAffinityMask &oCpuAffinity)
Gets the CPU affinity of a thread.
tResult get_scheduling(const std::thread::native_handle_type &oThreadHandle, tSchedulingParameters &sScheduling)
Gets the scheduling parameters of a thread.
void run_with_scheduling(tSchedulingParameters sScheduling, Callable &&pFunc, Args &&... args)
Sets the given scheduling paremeters and executes the given method afterwards.
tResult set_cpu_affinity(std::thread::native_handle_type oThreadHandle, const tCpuAffinityMask &oCpuAffinity)
Sets the CPU affinity of a thread.
std::vector< bool > tCpuAffinityMask
Container for a CPU Affinity mask.
void run_with_checked_scheduling(tSchedulingParameters sScheduling, Callable &&pFunc, Args &&... args)
Sets the given scheduling paremeters and executes the given method afterwards only if the scheduling ...
tResult create_thread_with_scheduling(std::thread &oThread, const tSchedulingParameters &sScheduling, Callable &&pFunc, Args &&... args)
Creates a new thread that has the given scheduling parameters.
tResult set_priority(std::thread::native_handle_type oThreadHandle, tSchedulingClass eSchedulingClass, tFloat64 fPriority)
Sets the priority of a thread.
tResult set_scheduling(std::thread::native_handle_type oThreadHandle, const tSchedulingParameters &sScheduling)
Sets the scheduling parameters of a thread.
Compound class, that groups scheduling parameters.
tCpuAffinityMask oCpuAffinity
The CPU affinity. Empty -> run on all CPUs.
tSchedulingClass eSchedulingClass
The scheduling class.
bool operator==(const tSchedulingParameters &sOther) const
Equals operator.
tSchedulingParameters(const tSchedulingParameters &)=default
Copy constructor.
tSchedulingParameters(tSchedulingParameters &&)=default
Move constructor.
tSchedulingParameters(tSchedulingClass eSchedulingClass, tFloat64 fPriority)
Convenience constructor.
tFloat64 fPriority
The priority in the range [0.0, 1.0].
tSchedulingParameters(tSchedulingClass eSchedulingClass, tFloat64 fPriority, const tCpuAffinityMask &oCpuAffinity)
Convenience constructor.