summaryrefslogtreecommitdiff
path: root/portable/src/ptimer.c
blob: 8cb72b934796f08d485164ca66d6408fa5d14cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*---------------------------------------------------------------------------*
 *  ptimer.c  *
 *                                                                           *
 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
 *                                                                           *
 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
 *  you may not use this file except in compliance with the License.         *
 *                                                                           *
 *  You may obtain a copy of the License at                                  *
 *      http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                           *
 *  Unless required by applicable law or agreed to in writing, software      *
 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 
 *  See the License for the specific language governing permissions and      *
 *  limitations under the License.                                           *
 *                                                                           *
 *---------------------------------------------------------------------------*/



#include "pmemory.h"
#include "ptimer.h"
#include "pmutex.h"

#ifdef _WIN32

/*
  Note that this implementation assumes that QueryPerformanceCounter is
  available (requires NT 3.1 and above) and that 64 bit arithmetic is
  available (requires VC)
*/

struct PTimer_t
{
  LARGE_INTEGER PerformanceFreq;
  LARGE_INTEGER RefTime;
  LARGE_INTEGER elapsed;
};



/**
 * Creates a new timer object.
 **/
ESR_ReturnCode PTimerCreate(PTimer **timer)
{
  PTimer *tmp = NULL;
  
  if (timer == NULL)
    return ESR_INVALID_ARGUMENT;
  tmp = NEW(PTimer, "PTimer");
  if (tmp == NULL)
    return ESR_OUT_OF_MEMORY;
    
  if (QueryPerformanceFrequency(&tmp->PerformanceFreq) == 0)
  {
    FREE(tmp);
    return ESR_NOT_SUPPORTED;
  }
  tmp->PerformanceFreq.QuadPart /= 1000;
  
  tmp->RefTime.QuadPart = 0;
  tmp->elapsed.QuadPart = 0;
  *timer = tmp;
  return ESR_SUCCESS;
}

ESR_ReturnCode PTimerDestroy(PTimer *timer)
{
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  FREE(timer);
  return ESR_SUCCESS;
}

/**
 * Starts the timer. This sets the reference time from which all new elapsed
 * time are computed.  This does not reset the elapsed time to 0.  This is
 * useful to pause the timer.
 **/
ESR_ReturnCode PTimerStart(PTimer *timer)
{
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  return (QueryPerformanceCounter(&timer->RefTime) ?
          ESR_SUCCESS :
          ESR_NOT_SUPPORTED);
}

/**
 * Stops the timer.
 **/
ESR_ReturnCode PTimerStop(PTimer *timer)
{
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  if (timer->RefTime.QuadPart != 0)
  {
    LARGE_INTEGER now;
    if (!QueryPerformanceCounter(&now)) return ESR_NOT_SUPPORTED;
    timer->elapsed.QuadPart += now.QuadPart - timer->RefTime.QuadPart;
    timer->RefTime.QuadPart = 0;
  }
  return ESR_SUCCESS;
}

/**
 * Returns the timer elapsed time.  If the Timer is in the stopped state,
 * successive calls to getElapsed() will always return the same value.  If
 * the Timer is in the started state, successive calls will return the
 * elapsed time since the last time PTimerStart() was called.
 */
ESR_ReturnCode PTimerGetElapsed(PTimer *timer, asr_uint32_t* elapsed)
{
  if (timer == NULL || elapsed == NULL)
    return ESR_INVALID_ARGUMENT;
    
  if (timer->RefTime.QuadPart != 0)
  {
    LARGE_INTEGER now;
    if (!QueryPerformanceCounter(&now)) return ESR_NOT_SUPPORTED;
    *elapsed = (asr_uint32_t) ((timer->elapsed.QuadPart + (now.QuadPart - timer->RefTime.QuadPart))
                           / timer->PerformanceFreq.QuadPart);
  }
  else
    *elapsed = (asr_uint32_t) (timer->elapsed.QuadPart / timer->PerformanceFreq.QuadPart);

  return ESR_SUCCESS;
}


/**
 * Resets the elapsed time to 0 and resets the reference time of the Timer.
 * This effectively reset the timer in the same state it was right after creation.
 **/
ESR_ReturnCode PTimerReset(PTimer *timer)
{
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  timer->RefTime.QuadPart = 0;
  timer->elapsed.QuadPart = 0;
  return ESR_SUCCESS;
}

#elif defined(POSIX)
#include "ptrd.h"
/*
POSIX has a timer
*/
/* Clocks and timers: clock_settime, clock_gettime, clock_getres, timer_xxx and nanosleep */
#ifndef _POSIX_TIMERS
#ifndef __vxworks /* __vxworks does not define it! */
#error "Timer is not defined!"
#endif /* __vxworks */
#endif /* _POSIX_TIMERS */

#define TIMER_MAX_VAL 10000

struct PTimer_t
{
  timer_t  timer;
  asr_uint32_t elapsed;
};

/**
* Creates a new timer object.
**/
ESR_ReturnCode PTimerCreate(PTimer **timer)
{
  PTimer *tmp = NULL;

  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  tmp = NEW(PTimer, "PTimer");
  if (tmp == NULL) return ESR_OUT_OF_MEMORY;

  *timer = tmp;
  if (timer_create(CLOCK_REALTIME, NULL, &(tmp->timer)) < 0)
    return ESR_NOT_SUPPORTED;

  return ESR_SUCCESS;
}

ESR_ReturnCode PTimerDestroy(PTimer *timer)
{
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  timer_delete(timer->timer);
  FREE(timer);

  return ESR_SUCCESS;
}

/**
* Starts the timer. This sets the reference time from which all new elapsed
* time are computed.  This does not reset the elapsed time to 0.  This is
* useful to pause the timer.
**/
ESR_ReturnCode PTimerStart(PTimer *timer)
{
  struct itimerspec expire_time;

  if (timer == NULL) return ESR_INVALID_ARGUMENT;

  expire_time.it_value.tv_sec = TIMER_MAX_VAL; /* set a large time for the timer */
  expire_time.it_value.tv_nsec = 0;
  return (timer_settime(timer->timer, 0, &expire_time, NULL) == 0 ?
          ESR_SUCCESS :
          ESR_NOT_SUPPORTED);
}

/**
* Stops the timer.
**/
ESR_ReturnCode PTimerStop(PTimer *timer)
{
  struct itimerspec remaining;

  if (timer == NULL) return ESR_INVALID_ARGUMENT;

  if (timer_gettime(timer->timer, &remaining) != 0) return ESR_NOT_SUPPORTED;
#if defined(__vxworks)
  timer_cancel(timer->timer);
#endif
  timer->elapsed = (asr_uint32_t) ((TIMER_MAX_VAL - remaining.it_value.tv_sec) * SECOND2MSECOND
						- remaining.it_value.tv_nsec / MSECOND2NSECOND);

  return ESR_SUCCESS;
}

/**
* Returns the timer elapsed time.  If the Timer is in the stopped state,
* successive calls to getElapsed() will always return the same value.  If
* the Timer is in the started state, successive calls will return the
* elapsed time since the last time PTimerStart() was called.
*/
ESR_ReturnCode PTimerGetElapsed(PTimer *timer, asr_uint32_t* elapsed)
{
  if (timer == NULL || elapsed == NULL)
    return ESR_INVALID_ARGUMENT;

  if (timer->elapsed == 0) /* stop is not called */
  {
    struct itimerspec remaining;
    if (timer_gettime(timer->timer, &remaining) != 0) return ESR_NOT_SUPPORTED;
    *elapsed = (asr_uint32_t) ((TIMER_MAX_VAL - remaining.it_value.tv_sec) * SECOND2MSECOND
						- remaining.it_value.tv_nsec / MSECOND2NSECOND);
  }
  else
    *elapsed = timer->elapsed;

  return ESR_SUCCESS;
}


/**
* Resets the elapsed time to 0 and resets the reference time of the Timer.
* This effectively reset the timer in the same state it was right after creation.
**/
ESR_ReturnCode PTimerReset(PTimer *timer)
{
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  timer->elapsed = 0;
  return ESR_SUCCESS;
}

#else
#error "Ptimer not implemented for this platform."
#endif