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

Last change on this file since 213 was 213, checked in by Sanahuja Guillaume, 4 years ago

thread stack size rework
add Matrix class

File size: 4.0 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
[213]32Thread::Thread(const Object *parent, string name, uint8_t priority,uint32_t stackSize)
[15]33    : Object(parent, name, "Thread") {
[213]34  pimpl_ = new Thread_impl(this, priority,stackSize);
[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
[139]80bool Thread::IsRunning(void) const { return pimpl_->isRunning; }
81
[15]82void Thread::Resume(void) { pimpl_->Resume(); }
[2]83
[15]84int Thread::WaitUpdate(const IODevice *device) {
85  return pimpl_->WaitUpdate(device);
[2]86}
87
[15]88void Thread::SleepUntil(Time time) const {
[2]89#ifdef __XENO__
[15]90  int status = rt_task_sleep_until(time);
[133]91  if (status != 0) {
92                char errorMsg[256];
[202]93    Err("%error rt_task_sleep_until (%s), resume time: %lld, actual time: "
94        "%lld\n",strerror_r(-status, errorMsg, sizeof(errorMsg)), time, GetTime());
[133]95        }
[15]96// Printf("rt_task_sleep_until, resume time: %lld, actual time:
97// %lld\n",time,GetTime());
[2]98#else
[15]99  Time current = GetTime();
100  if (current < time) {
[202]101    //usleep((time - current) / 1000);
102    struct timespec req;
103    req.tv_nsec = time - current;
104    req.tv_sec = req.tv_nsec / 1000000000;
105    req.tv_nsec %= 1000000000;
106    struct timespec rem;
107    if(nanosleep(&req,&rem)!=0) Err("error in nanosleep\n");//todo, handle EINTR
108  } else {
109    Err("error resume time is passed by %lldns\n", ( current-time));
[15]110  }
[2]111#endif
112}
113
[15]114void Thread::SleepMS(uint32_t time) const {
[2]115#ifdef __XENO__
[15]116  int status = rt_task_sleep(time * 1000 * 1000);
[133]117  if (status != 0) {
118                char errorMsg[256];
119    Err("erreur rt_task_sleep (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
120        }
[2]121#else
[202]122  //usleep(time * 1000);
123  struct timespec req;
124  req.tv_nsec = time * 1000000;
125  req.tv_sec = req.tv_nsec / 1000000000;
126  req.tv_nsec %= 1000000000;
127  struct timespec rem;
128  if(nanosleep(&req,&rem)!=0) Err("error in nanosleep\n");//todo, handle EINTR
[2]129#endif
130}
131
[15]132void Thread::SleepUS(uint32_t time) const {
[2]133#ifdef __XENO__
[15]134  int status = rt_task_sleep(time * 1000);
[133]135  if (status != 0) {
136                char errorMsg[256];
137    Err("erreur rt_task_sleep (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
138        }
[2]139#else
[202]140  //usleep(time);
141  struct timespec req;
142  req.tv_nsec = time * 1000;
143  req.tv_sec = req.tv_nsec / 1000000000;
144  req.tv_nsec %= 1000000000;
145  struct timespec rem;
146  if(nanosleep(&req,&rem)!=0) Err("error in nanosleep\n");//todo, handle EINTR
[2]147#endif
148}
149
150} // end namespace core
151} // end namespace flair
Note: See TracBrowser for help on using the repository browser.