ViSP  2.9.0
vpTime.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTime.cpp 4622 2014-01-28 17:40:36Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Time management and measurement.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
44 
45 #include <visp/vpTime.h>
46 #include <visp/vpDebug.h>
47 #include <iostream>
48 
49 
57 // Unix depend version
58 
59 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
60 # include <sys/time.h>
61 # include <unistd.h>
62 #elif defined(_WIN32)
63 # include <windows.h>
64 # include <winbase.h>
65 #endif
66 
85 double
87 {
88 #if defined(_WIN32)
89  LARGE_INTEGER time, frequency;
90  QueryPerformanceFrequency(&frequency);
91  if(frequency.QuadPart == 0){
92  return(timeGetTime());
93  }
94  else{
95  QueryPerformanceCounter(&time);
96  return (double)(1000.0*time.QuadPart/frequency.QuadPart);
97  }
98 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
99  struct timeval tp;
100  gettimeofday(&tp,0);
101  return(1000.0*tp.tv_sec + tp.tv_usec/1000.0);
102 #endif
103 }
104 
105 
111 double
113 {
114 #if defined(_WIN32)
115  LARGE_INTEGER time, frequency;
116  QueryPerformanceFrequency(&frequency);
117  if(frequency.QuadPart == 0){
118  return(timeGetTime());
119  }
120  else{
121  QueryPerformanceCounter(&time);
122  return (double)(1000000.0*time.QuadPart/frequency.QuadPart);
123  }
124 #else
125 
126  struct timeval tp;
127  gettimeofday(&tp,0);
128  return(1000000.0*tp.tv_sec + tp.tv_usec);
129 #endif
130 }
131 
132 
133 
148 int
149 vpTime::wait(double t0, double t)
150 {
151  double timeCurrent, timeToWait;
152  timeCurrent = measureTimeMs();
153 
154  timeToWait = t0 + t - timeCurrent;
155 
156  if ( timeToWait <= 0. ) // no need to wait
157  return(1);
158  else {
159 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
160  if (timeToWait > vpTime::minTimeForUsleepCall) {
161  usleep((unsigned long )((timeToWait-vpTime::minTimeForUsleepCall)*1000));
162  }
163 #elif defined(_WIN32)
164  if (timeToWait > vpTime::minTimeForUsleepCall) {
165  Sleep((DWORD)(timeToWait-vpTime::minTimeForUsleepCall));
166  }
167 #endif
168  // Blocking loop to have an accurate waiting
169  do {
170  timeCurrent = measureTimeMs();
171  timeToWait = t0 + t - timeCurrent;
172 
173  } while (timeToWait > 0.);
174 
175  return 0;
176  }
177 }
178 
179 
189 void vpTime::wait(double t)
190 {
191  double t0, timeCurrent, timeToWait;
192  t0 = timeCurrent = measureTimeMs();
193 
194  timeToWait = t;
195 
196  if ( timeToWait <= 0. ) // no need to wait
197  return;
198  else {
199 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
200  if (timeToWait > vpTime::minTimeForUsleepCall) {
201  usleep((unsigned long )((timeToWait-vpTime::minTimeForUsleepCall)*1000));
202  }
203 #elif defined(_WIN32)
204  if (timeToWait > vpTime::minTimeForUsleepCall) {
205  Sleep((DWORD)(timeToWait-vpTime::minTimeForUsleepCall));
206  }
207 #endif
208  // Blocking loop to have an accurate waiting
209  do {
210  timeCurrent = measureTimeMs();
211  timeToWait = t0 + t - timeCurrent;
212 
213  } while (timeToWait > 0.);
214 
215  return;
216  }
217 }
218 
226 {
227  return vpTime::measureTimeMs()/1000.0 ;
228 }
229 
236 void vpTime::sleepMs(double t)
237 {
238 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
239  usleep((unsigned long )(t*1000));
240 #elif defined(_WIN32)
241  Sleep((DWORD)(t));
242 #endif
243 }
244 
245 
static double measureTimeMicros()
Definition: vpTime.cpp:112
static void sleepMs(double t)
Definition: vpTime.cpp:236
static double measureTimeMs()
Definition: vpTime.cpp:86
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static double measureTimeSecond()
Definition: vpTime.cpp:225
static double minTimeForUsleepCall
Definition: vpTime.h:82