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

Last change on this file since 69 was 15, checked in by Bayard Gildas, 8 years ago

sources reformatted with flair-format-dir script

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