source: flair-src/trunk/lib/FlairCore/src/Thread.cpp@ 13

Last change on this file since 13 was 2, checked in by Sanahuja Guillaume, 9 years ago

flaircore

File size: 3.2 KB
Line 
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
27using std::string;
28
29namespace flair
30{
31namespace core
32{
33
34Thread::Thread(const Object* parent,string name,uint8_t priority): Object(parent,name,"Thread")
35{
36 pimpl_=new Thread_impl(this,priority);
37}
38
39Thread::~Thread()
40{
41 delete pimpl_;
42}
43
44void Thread::Start(void)
45{
46 pimpl_->Start();
47}
48
49void Thread::SafeStop(void)
50{
51 pimpl_->SafeStop();
52}
53
54bool Thread::ToBeStopped(void) const
55{
56 return pimpl_->ToBeStopped();
57}
58
59#ifdef __XENO__
60void Thread::WarnUponSwitches(bool value)
61{
62 // Ask Xenomai to warn us upon switches to secondary mode.
63 if(value==true)
64 {
65 rt_task_set_mode(0, T_WARNSW, NULL);
66 }
67 else
68 {
69 rt_task_set_mode(T_WARNSW, 0, NULL);
70 }
71}
72#else
73void Thread::WarnUponSwitches(bool value)
74{
75 //Warn("Not applicable in non real time\n");
76}
77#endif
78
79void Thread::Join(void)
80{
81 pimpl_->Join();
82}
83
84void Thread::SetPeriodUS(uint32_t period) {
85 pimpl_->SetPeriodUS(period);
86}
87
88uint32_t Thread::GetPeriodUS(void) const {
89 return pimpl_->GetPeriodUS();
90}
91
92void Thread::SetPeriodMS(uint32_t period) {
93 pimpl_->SetPeriodMS(period);
94}
95
96uint32_t Thread::GetPeriodMS(void) const {
97 return pimpl_->GetPeriodMS();
98}
99
100
101bool Thread::IsPeriodSet(void)
102{
103 return pimpl_->period_set;
104}
105
106void Thread::WaitPeriod(void) const
107{
108 pimpl_->WaitPeriod();
109}
110
111void Thread::Suspend(void)
112{
113 pimpl_->Suspend();
114}
115
116bool Thread::SuspendUntil(Time date){
117 return pimpl_->SuspendUntil(date);
118}
119
120bool Thread::IsSuspended(void) const
121{
122 return pimpl_->IsSuspended();
123}
124
125void Thread::Resume(void)
126{
127 pimpl_->Resume();
128}
129
130int Thread::WaitUpdate(const IODevice* device)
131{
132 return pimpl_->WaitUpdate(device);
133}
134
135void Thread::SleepUntil(Time time) const
136{
137#ifdef __XENO__
138 int status=rt_task_sleep_until(time);
139 if(status!=0) Err("%s, error rt_task_sleep_until (%s), resume time: %lld, actual time: %lld\n",
140 ObjectName().c_str(),
141 strerror(-status),
142 time,
143 GetTime());
144 //Printf("rt_task_sleep_until, resume time: %lld, actual time: %lld\n",time,GetTime());
145#else
146 Time current=GetTime();
147 if(current<time)
148 {
149 usleep((time-current)/1000);
150 }
151#endif
152}
153
154void Thread::SleepMS(uint32_t time) const
155{
156#ifdef __XENO__
157 int status=rt_task_sleep(time*1000*1000);
158 if(status!=0) Err("erreur rt_task_sleep (%s)\n",strerror(-status));
159#else
160 usleep(time*1000);
161#endif
162}
163
164void Thread::SleepUS(uint32_t time) const
165{
166#ifdef __XENO__
167 int status=rt_task_sleep(time*1000);
168 if(status!=0) Err("erreur rt_task_sleep (%s)\n",strerror(-status));
169#else
170 usleep(time);
171#endif
172}
173
174} // end namespace core
175} // end namespace flair
Note: See TracBrowser for help on using the repository browser.