1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | // created: 2012/10/04
|
---|
6 | // filename: Thread.cpp
|
---|
7 | //
|
---|
8 | // author: Guillaume Sanahuja
|
---|
9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
10 | //
|
---|
11 | // version: $Id: $
|
---|
12 | //
|
---|
13 | // purpose: Abstract class for a thread
|
---|
14 | //
|
---|
15 | //
|
---|
16 | /*********************************************************************/
|
---|
17 |
|
---|
18 | #include "Thread.h"
|
---|
19 | #include "Thread_impl.h"
|
---|
20 | #ifdef __XENO__
|
---|
21 | #include <native/timer.h>
|
---|
22 | #else
|
---|
23 | #include <sys/time.h>
|
---|
24 | #include <unistd.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | using std::string;
|
---|
28 |
|
---|
29 | namespace flair {
|
---|
30 | namespace core {
|
---|
31 |
|
---|
32 | Thread::Thread(const Object *parent, string name, uint8_t priority)
|
---|
33 | : Object(parent, name, "Thread") {
|
---|
34 | pimpl_ = new Thread_impl(this, priority);
|
---|
35 | }
|
---|
36 |
|
---|
37 | Thread::~Thread() { delete pimpl_; }
|
---|
38 |
|
---|
39 | void Thread::Start(void) { pimpl_->Start(); }
|
---|
40 |
|
---|
41 | void Thread::SafeStop(void) { pimpl_->SafeStop(); }
|
---|
42 |
|
---|
43 | bool Thread::ToBeStopped(void) const { return pimpl_->ToBeStopped(); }
|
---|
44 |
|
---|
45 | #ifdef __XENO__
|
---|
46 | void Thread::WarnUponSwitches(bool value) {
|
---|
47 | // Ask Xenomai to warn us upon switches to secondary mode.
|
---|
48 | if (value == true) {
|
---|
49 | rt_task_set_mode(0, T_WARNSW, NULL);
|
---|
50 | } else {
|
---|
51 | rt_task_set_mode(T_WARNSW, 0, NULL);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | #else
|
---|
55 | void Thread::WarnUponSwitches(bool value) {
|
---|
56 | // Warn("Not applicable in non real time\n");
|
---|
57 | }
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | void Thread::Join(void) { pimpl_->Join(); }
|
---|
61 |
|
---|
62 | void Thread::SetPeriodUS(uint32_t period) { pimpl_->SetPeriodUS(period); }
|
---|
63 |
|
---|
64 | uint32_t Thread::GetPeriodUS(void) const { return pimpl_->GetPeriodUS(); }
|
---|
65 |
|
---|
66 | void Thread::SetPeriodMS(uint32_t period) { pimpl_->SetPeriodMS(period); }
|
---|
67 |
|
---|
68 | uint32_t Thread::GetPeriodMS(void) const { return pimpl_->GetPeriodMS(); }
|
---|
69 |
|
---|
70 | bool Thread::IsPeriodSet(void) { return pimpl_->period_set; }
|
---|
71 |
|
---|
72 | void Thread::WaitPeriod(void) const { pimpl_->WaitPeriod(); }
|
---|
73 |
|
---|
74 | void Thread::Suspend(void) { pimpl_->Suspend(); }
|
---|
75 |
|
---|
76 | bool Thread::SuspendUntil(Time date) { return pimpl_->SuspendUntil(date); }
|
---|
77 |
|
---|
78 | bool Thread::IsSuspended(void) const { return pimpl_->IsSuspended(); }
|
---|
79 |
|
---|
80 | bool Thread::IsRunning(void) const { return pimpl_->isRunning; }
|
---|
81 |
|
---|
82 | void Thread::Resume(void) { pimpl_->Resume(); }
|
---|
83 |
|
---|
84 | int Thread::WaitUpdate(const IODevice *device) {
|
---|
85 | return pimpl_->WaitUpdate(device);
|
---|
86 | }
|
---|
87 |
|
---|
88 | void Thread::SleepUntil(Time time) const {
|
---|
89 | #ifdef __XENO__
|
---|
90 | int status = rt_task_sleep_until(time);
|
---|
91 | if (status != 0) {
|
---|
92 | char errorMsg[256];
|
---|
93 | Err("%s, error rt_task_sleep_until (%s), resume time: %lld, actual time: "
|
---|
94 | "%lld\n", ObjectName().c_str(), strerror_r(-status, errorMsg, sizeof(errorMsg)), time, GetTime());
|
---|
95 | }
|
---|
96 | // Printf("rt_task_sleep_until, resume time: %lld, actual time:
|
---|
97 | // %lld\n",time,GetTime());
|
---|
98 | #else
|
---|
99 | Time current = GetTime();
|
---|
100 | if (current < time) {
|
---|
101 | usleep((time - current) / 1000);
|
---|
102 | }
|
---|
103 | #endif
|
---|
104 | }
|
---|
105 |
|
---|
106 | void Thread::SleepMS(uint32_t time) const {
|
---|
107 | #ifdef __XENO__
|
---|
108 | int status = rt_task_sleep(time * 1000 * 1000);
|
---|
109 | if (status != 0) {
|
---|
110 | char errorMsg[256];
|
---|
111 | Err("erreur rt_task_sleep (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
112 | }
|
---|
113 | #else
|
---|
114 | usleep(time * 1000);
|
---|
115 | #endif
|
---|
116 | }
|
---|
117 |
|
---|
118 | void Thread::SleepUS(uint32_t time) const {
|
---|
119 | #ifdef __XENO__
|
---|
120 | int status = rt_task_sleep(time * 1000);
|
---|
121 | if (status != 0) {
|
---|
122 | char errorMsg[256];
|
---|
123 | Err("erreur rt_task_sleep (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
124 | }
|
---|
125 | #else
|
---|
126 | usleep(time);
|
---|
127 | #endif
|
---|
128 | }
|
---|
129 |
|
---|
130 | } // end namespace core
|
---|
131 | } // end namespace flair
|
---|