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

Last change on this file since 133 was 133, checked in by Sanahuja Guillaume, 6 years ago

modif sterror

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 {
30namespace core {
31
32Thread::Thread(const Object *parent, string name, uint8_t priority)
33 : Object(parent, name, "Thread") {
34 pimpl_ = new Thread_impl(this, priority);
35}
36
37Thread::~Thread() { delete pimpl_; }
38
39void Thread::Start(void) { pimpl_->Start(); }
40
41void Thread::SafeStop(void) { pimpl_->SafeStop(); }
42
43bool Thread::ToBeStopped(void) const { return pimpl_->ToBeStopped(); }
44
45#ifdef __XENO__
46void 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
55void Thread::WarnUponSwitches(bool value) {
56 // Warn("Not applicable in non real time\n");
57}
58#endif
59
60void Thread::Join(void) { pimpl_->Join(); }
61
62void Thread::SetPeriodUS(uint32_t period) { pimpl_->SetPeriodUS(period); }
63
64uint32_t Thread::GetPeriodUS(void) const { return pimpl_->GetPeriodUS(); }
65
66void Thread::SetPeriodMS(uint32_t period) { pimpl_->SetPeriodMS(period); }
67
68uint32_t Thread::GetPeriodMS(void) const { return pimpl_->GetPeriodMS(); }
69
70bool Thread::IsPeriodSet(void) { return pimpl_->period_set; }
71
72void Thread::WaitPeriod(void) const { pimpl_->WaitPeriod(); }
73
74void Thread::Suspend(void) { pimpl_->Suspend(); }
75
76bool Thread::SuspendUntil(Time date) { return pimpl_->SuspendUntil(date); }
77
78bool Thread::IsSuspended(void) const { return pimpl_->IsSuspended(); }
79
80void Thread::Resume(void) { pimpl_->Resume(); }
81
82int Thread::WaitUpdate(const IODevice *device) {
83 return pimpl_->WaitUpdate(device);
84}
85
86void Thread::SleepUntil(Time time) const {
87#ifdef __XENO__
88 int status = rt_task_sleep_until(time);
89 if (status != 0) {
90 char errorMsg[256];
91 Err("%s, error rt_task_sleep_until (%s), resume time: %lld, actual time: "
92 "%lld\n", ObjectName().c_str(), strerror_r(-status, errorMsg, sizeof(errorMsg)), time, GetTime());
93 }
94// Printf("rt_task_sleep_until, resume time: %lld, actual time:
95// %lld\n",time,GetTime());
96#else
97 Time current = GetTime();
98 if (current < time) {
99 usleep((time - current) / 1000);
100 }
101#endif
102}
103
104void Thread::SleepMS(uint32_t time) const {
105#ifdef __XENO__
106 int status = rt_task_sleep(time * 1000 * 1000);
107 if (status != 0) {
108 char errorMsg[256];
109 Err("erreur rt_task_sleep (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
110 }
111#else
112 usleep(time * 1000);
113#endif
114}
115
116void Thread::SleepUS(uint32_t time) const {
117#ifdef __XENO__
118 int status = rt_task_sleep(time * 1000);
119 if (status != 0) {
120 char errorMsg[256];
121 Err("erreur rt_task_sleep (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
122 }
123#else
124 usleep(time);
125#endif
126}
127
128} // end namespace core
129} // end namespace flair
Note: See TracBrowser for help on using the repository browser.