Timer.cpp
Go to the documentation of this file.
1 /*
2  * Katana Native Interface - A C++ interface to the robot arm Katana.
3  * Copyright (C) 2005 Neuronics AG
4  * Check out the AUTHORS file for detailed contact information.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #include "common/Timer.h"
22 
23 #ifdef WIN32
24 #include <Windows.h>
25 #endif
26 
27 namespace KNI {
28 
29  void
30  Timer::Set(long timeout) {
31  _timeout = timeout;
32  }
33 
34 #ifdef WIN32
35  void Timer::Start() {
36  _ct = clock();
37  }
38 
39  long Timer::_ElapsedTime() const {
40  return static_cast<long>(1000.0*(static_cast<double>((clock()-_ct))/static_cast<double>(CLOCKS_PER_SEC)));
41  }
42 
43  Timer::Timer() : _timeout(0), _ct(0) {}
44  Timer::Timer(long timeout) : _timeout(timeout), _ct(0) {}
45 
46 #else
47 
48  Timer::Timer() : _timeout(0), _ct() {}
49  Timer::Timer(long timeout) : _timeout(timeout), _ct() {}
50 
51  void Timer::Start() {
52  gettimeofday(&_ct, 0);
53  }
54 
55  long Timer::_ElapsedTime() const {
56  struct timeval end;
57  gettimeofday(&end, 0);
58  return (end.tv_sec*1000+end.tv_usec/1000) - (_ct.tv_sec*1000+_ct.tv_usec/1000);
59  }
60 
61 #endif
62 
63  void
64  Timer::Set_And_Start(long timeout) {
65  Set(timeout);
66  Start();
67  }
68 
69  bool Timer::Elapsed() const {
70  if( _timeout <= 0 ) return false; // (timeout <= 0) => INFINITE
71  if( _ElapsedTime() < _timeout) return false;
72  return true;
73  }
74  long Timer::ElapsedTime() const {
75  return _ElapsedTime();
76  }
77 
78  void Timer::WaitUntilElapsed() const {
79  if(Elapsed()) return;
84  }
85 
86  void sleep(long time) {
87  if(time <= 0) return;
88  #ifdef WIN32
89  Sleep(time);
90  #else // POSIX 1b
91  timespec time2sleep;
92  time2sleep.tv_sec = 0;
93  time2sleep.tv_nsec = time * 1000 * 1000; // SLEEP in milliseconds
94  nanosleep(&time2sleep, NULL);
95  #endif
96  }
97 
98 }
long ElapsedTime() const
Definition: Timer.cpp:74
bool Elapsed() const
Definition: Timer.cpp:69
struct timeval _ct
Definition: Timer.h:48
long _ElapsedTime() const
Platform specific implementation of ElapsedTime().
Definition: Timer.cpp:55
long tv_sec
Definition: pthread.h:308
void Set(long timeout)
Definition: Timer.cpp:30
void WaitUntilElapsed() const
Definition: Timer.cpp:78
long tv_nsec
Definition: pthread.h:309
void Set_And_Start(long timeout)
Definition: Timer.cpp:64
void sleep(long time)
Definition: Timer.cpp:86
void Start()
Definition: Timer.cpp:51
long _timeout
Definition: Timer.h:43
Definition: Timer.h:30


kni
Author(s): Martin Günther
autogenerated on Fri Jan 3 2020 04:01:17