po_hi_time.c 6.92 KB
Newer Older
1
2
3
4
5
/*
 * This is a part of PolyORB-HI-C distribution, a minimal
 * middleware written for generated code from AADL models.
 * You should use it with the Ocarina toolsuite.
 *
jhugues's avatar
jhugues committed
6
 * For more informations, please visit http://assert-project.net/taste
7
 *
jhugues's avatar
jhugues committed
8
 * Copyright (C) 2007-2009 Telecom ParisTech, 2010-2012 ESA & ISAE.
9
10
11
12
13
14
15
16
 */

#include <time.h>
#include <errno.h>

#include <po_hi_config.h>
#include <po_hi_time.h>
#include <po_hi_returns.h>
17
18
#include <po_hi_debug.h>

julien.delange's avatar
julien.delange committed
19
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
20
21
22
#include <pthread.h>
#elif defined (RTEMS_PURE)
#include <bsp.h>
23
#endif
24

jdelange's avatar
jdelange committed
25
26
27
28
29
30

#if defined (_WIN32)
#include <tchar.h>
#include <windows.h>
#endif

31
32
33
34
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
jdelange's avatar
jdelange committed
35

36
#if defined (POSIX) && defined (NEED_CLOCK_GETTIME)
37
38
39
40
41
42
#include <sys/time.h>
int clock_gettime(int clk_id, struct timespec *tp)
{
   struct timeval now;
   int rv = gettimeofday(&now, NULL);

43
   if (rv != 0)
44
45
46
47
48
49
50
51
52
53
54
   {
      return rv;
   }

   tp->tv_sec = now.tv_sec;
   tp->tv_nsec = now.tv_usec * 1000;

   return 0;
}
#endif

jdelange's avatar
jdelange committed
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
#if defined(_WIN32)
#define WINDOWS_TICK 10000000
#define SEC_TO_UNIX_EPOCH 11644473600LL

unsigned __po_hi_windows_tick_to_unix_seconds(long long win_ticks)
{
     return (unsigned)(win_ticks / WINDOWS_TICK - SEC_TO_UNIX_EPOCH);
}


LARGE_INTEGER __po_hi_unix_seconds_to_windows_tick(unsigned sec, unsigned nsec)
{
   LARGE_INTEGER ret;
   LARGE_INTEGER nsectoadd;
   ret.QuadPart = sec + SEC_TO_UNIX_EPOCH;
   ret.QuadPart = ret.QuadPart * WINDOWS_TICK;

   nsectoadd.QuadPart = nsec / 100;

   ret.QuadPart =  ret.QuadPart + nsectoadd.QuadPart;
   return ret;
}

#endif


81
82
83

int __po_hi_get_time (__po_hi_time_t* mytime)
{
julien.delange's avatar
julien.delange committed
84
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
85
86
   struct timespec ts;

87
88
89
90
91
92
93
94
95
96
97

#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
   clock_serv_t cclock;
   mach_timespec_t mts;
   host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
   clock_get_time(cclock, &mts);
   mach_port_deallocate(mach_task_self(), cclock);
   ts.tv_sec = mts.tv_sec;
   ts.tv_nsec = mts.tv_nsec;

#else
98
99
100
101
   if (clock_gettime (CLOCK_REALTIME, &ts)!=0)
   {
      return (__PO_HI_ERROR_CLOCK);
   }
102
103
#endif

104
105
   mytime->sec    = ts.tv_sec;
   mytime->nsec   = ts.tv_nsec;
106

jdelange's avatar
jdelange committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
   return (__PO_HI_SUCCESS);
#elif defined (_WIN32)
   SYSTEMTIME st;
   FILETIME ft;
   LARGE_INTEGER ularge;

   GetSystemTime(&st);
   SystemTimeToFileTime(&st,&ft);
   ularge.LowPart=ft.dwLowDateTime;
   ularge.HighPart=ft.dwHighDateTime;

   mytime->sec = __po_hi_windows_tick_to_unix_seconds (ularge.QuadPart);
   mytime->nsec = ularge.QuadPart % 10000000;
   mytime->nsec *= 100;

122
   return (__PO_HI_SUCCESS);
123
124
125
126
127
128
129
130
#elif defined (RTEMS_PURE)
   rtems_time_of_day    current_time;

   if (rtems_clock_get (RTEMS_CLOCK_GET_TOD, &current_time) != RTEMS_SUCCESSFUL)
   {
      __DEBUGMSG ("Error when trying to get the clock on RTEMS\n");
   }

131
   mytime->sec  = _TOD_To_seconds (&current_time);
132
   mytime->nsec =  current_time.ticks * rtems_configuration_get_microseconds_per_tick() * 1000;
133
134
135

   return (__PO_HI_SUCCESS);
#elif defined (XENO_NATIVE)
136
137
   mytime->sec  = rt_timer_tsc2ns (rt_timer_read ()) / 1000000000;
   mytime->nsec =  rt_timer_tsc2ns (rt_timer_read ()) - (mytime->sec * 1000000000);
138
139
140
141
   return (__PO_HI_SUCCESS);
#else
   return (__PO_HI_UNAVAILABLE);
#endif
142
143
}

144
int __po_hi_add_times (__po_hi_time_t* result, const __po_hi_time_t* left, const __po_hi_time_t* right)
145
{
146
147
148
149
150
151
152
153
   result->sec    = left->sec + right->sec;
   result->nsec   = left->nsec + right->nsec;
   if (result->nsec > 1000000000)
   {
      result->sec = result->sec + 1;
      result->nsec = result->nsec - 1000000000;
   }
   return 1;
154
155
}

156
int __po_hi_seconds (__po_hi_time_t* time, const __po_hi_uint32_t seconds)
157
{
158
159
160
   time->sec    = seconds;
   time->nsec   = 0;
   return 1;
161
162
}

163
int __po_hi_milliseconds (__po_hi_time_t* time, const __po_hi_uint32_t milliseconds)
164
{
165
166
167
   time->sec    = milliseconds / 1000;
   time->nsec   = (milliseconds - (time->sec * 1000)) * 1000000;
   return 1;
168
169
}

170
int __po_hi_microseconds (__po_hi_time_t* time, const __po_hi_uint32_t microseconds)
171
{
172
173
174
   time->sec    = microseconds / 1000000;
   time->nsec   = (microseconds - (time->sec * 1000000)) * 1000;
   return 1;
175
176
}

177
int __po_hi_delay_until (const __po_hi_time_t* time)
178
{
julien.delange's avatar
julien.delange committed
179
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
180
181
182
183
184
   pthread_mutex_t mutex;
   pthread_cond_t cond;
   struct timespec timer;
   int ret;

185
   timer.tv_sec = time->sec;
186

187
   timer.tv_nsec = time->nsec;
188
189
190

   if (pthread_mutex_init (&mutex, NULL) != 0)
   {
jdelange's avatar
jdelange committed
191
      __PO_HI_DEBUG_INFO ("[TIME] __po_hi_delay_until: cannot initialize mutex\n");
192
193
194
195
196
      return (__PO_HI_ERROR_PTHREAD_MUTEX);
   }

   if (pthread_cond_init (&cond, NULL) != 0)
   {
jdelange's avatar
jdelange committed
197
      __PO_HI_DEBUG_INFO ("[TIME] __po_hi_delay_until: cannot initialize cond\n");
198
199
200
201
202
203
204
205
206
207
      pthread_mutex_destroy (&mutex);
      return (__PO_HI_ERROR_PTHREAD_COND);
   }

   pthread_mutex_lock (&mutex);

   ret = pthread_cond_timedwait (&cond, &mutex, &timer);

   if ( (ret != 0) && (ret != ETIMEDOUT))
   {
jdelange's avatar
jdelange committed
208
      __PO_HI_DEBUG_INFO ("[TIME] __po_hi_delay_until: delay until error\n");
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
      ret = __PO_HI_ERROR_PTHREAD_COND;
   }
   else
   {
      ret = __PO_HI_SUCCESS;
   }

   pthread_mutex_unlock (&mutex);

   if (pthread_cond_destroy (&cond) != 0)
   {
      ret = __PO_HI_ERROR_PTHREAD_COND;
   }

   if (pthread_mutex_destroy (&mutex) != 0)
   {
      ret = __PO_HI_ERROR_PTHREAD_MUTEX;
   }
   return (ret);
228
229
#elif defined (RTEMS_PURE)
   return (__PO_HI_UNAVAILABLE);
230
231
#elif defined (XENO_NATIVE)
  int ret;
232
  ret =  rt_task_sleep_until (rt_timer_ns2tsc ( (time->sec * 1000000000) +  time->nsec));
233
234
  if (ret)
  {
235
      __DEBUGMSG ("[TASK] Error in rt_task_sleep_until, ret=%d\n", ret);
236
237
238
      return (__PO_HI_ERROR_PTHREAD_COND);
  }
  return (__PO_HI_SUCCESS);
jdelange's avatar
jdelange committed
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
265
266
#elif defined (_WIN32)
   HANDLE hTimer = NULL;
   LARGE_INTEGER ularge;

   hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
   ularge = __po_hi_unix_seconds_to_windows_tick (time->sec, time->nsec);

    if (!SetWaitableTimer(hTimer, &ularge, 0, NULL, NULL, 0))
    {
        __PO_HI_DEBUG_DEBUG("[DELAY UNTIL] SetWaitableTimer failed (%d)\n", GetLastError());
        return 2;
    }

    if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
    {
        __PO_HI_DEBUG_DEBUG("[DELAY UNTIL] WaitForSingleObject failed (%d)\n", GetLastError());
    }

    if (CloseHandle(hTimer) != TRUE)
    {
        __PO_HI_DEBUG_DEBUG("[DELAY UNTIL] CloseHandle failed (%d)\n", GetLastError());
    }

    printf ("wait completed\n");


  return __PO_HI_SUCCESS;

267
268
269
#else
   return (__PO_HI_UNAVAILABLE);
#endif
270
}
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

int __po_hi_time_is_greater (const __po_hi_time_t* value, const __po_hi_time_t* limit)
{
   if (value->sec > limit->sec)
   {
      return 1;
   }
   if (value->sec == limit->sec)
   {
      if (value->nsec > limit->nsec)
      {
         return 1;
      }
   }
   return 0;
}

int __po_hi_time_copy (__po_hi_time_t* dst, const __po_hi_time_t* src)
{
   dst->sec = src->sec;
   dst->nsec = src->nsec;
   return (__PO_HI_SUCCESS);
}