po_hi_time.c 4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * 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.
 *
 * For more informations, please visit http://ocarina.enst.fr
 *
 * Copyright (C) 2007-2008, GET-Telecom Paris.
 */

#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
23
#include <pthread.h>
#elif defined (RTEMS_PURE)
#include <bsp.h>
#endif 
24

25
#if defined (POSIX) && defined (NEED_CLOCK_GETTIME)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <sys/time.h>
int clock_gettime(int clk_id, struct timespec *tp)
{
   struct timeval now;
   int rv = gettimeofday(&now, NULL);

   if (rv != 0) 
   {
      return rv;
   }

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

   return 0;
}
#endif


int __po_hi_get_time (__po_hi_time_t* mytime)
{
julien.delange's avatar
julien.delange committed
47
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
48
49
50
51
52
53
54
55
56
57
58
59
60
   struct timespec ts;
   __po_hi_time_t tmp;

   if (clock_gettime (CLOCK_REALTIME, &ts)!=0)
   {
      return (__PO_HI_ERROR_CLOCK);
   }
   tmp = ts.tv_sec;
   tmp = tmp * 1000000;
   tmp += ts.tv_nsec / 1000;
   *mytime = tmp;

   return (__PO_HI_SUCCESS);
61
62
63
64
65
66
67
68
69
70
71
72
#elif defined (RTEMS_PURE)
   rtems_time_of_day    current_time;
   __po_hi_time_t       tmp;

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

   tmp = _TOD_To_seconds (&current_time) * 1000000;
   tmp += current_time.ticks * _TOD_Microseconds_per_tick;
73

74
75
76
77
   /*
    * FIXME: should check this assignment
    */
   *mytime = (__po_hi_time_t) tmp;
78
   
79
80
81
82
83
84
85
86
   return (__PO_HI_SUCCESS);
#elif defined (XENO_NATIVE)
   __po_hi_time_t tmp;
   tmp = (__po_hi_time_t) (rt_timer_tsc2ns (rt_timer_read ()));
   __DEBUGMSG ("TIMER value %llu\n", tmp);
   tmp = tmp / 1000;
   __DEBUGMSG ("TIMER value2 %llu\n", tmp);
   *mytime = (__po_hi_time_t)tmp;
87
88
89
90
   return (__PO_HI_SUCCESS);
#else
   return (__PO_HI_UNAVAILABLE);
#endif
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
}

__po_hi_time_t __po_hi_add_times (__po_hi_time_t left, __po_hi_time_t right)
{
   __po_hi_time_t rtime;
   rtime = left + right;
   return (rtime);
}

__po_hi_time_t __po_hi_seconds (__po_hi_uint32_t seconds)
{
   __po_hi_time_t mytime;
   mytime = seconds * 1000000;
   return (mytime);
}

__po_hi_time_t __po_hi_milliseconds (__po_hi_uint32_t milliseconds)
{
   __po_hi_time_t mytime;
   mytime = milliseconds * 1000;
   return (mytime);
}

__po_hi_time_t __po_hi_microseconds (__po_hi_uint32_t microseconds)
{
   __po_hi_time_t mytime;
   mytime = microseconds;
   return (mytime);
}

int __po_hi_delay_until (__po_hi_time_t time)
{
julien.delange's avatar
julien.delange committed
123
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
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
   pthread_mutex_t mutex;
   pthread_cond_t cond;
   struct timespec timer;
   int ret;

   timer.tv_sec = time / 1000000;

   timer.tv_nsec = (time - (timer.tv_sec*1000000)) * 1000;

   if (pthread_mutex_init (&mutex, NULL) != 0)
   {
      return (__PO_HI_ERROR_PTHREAD_MUTEX);
   }

   if (pthread_cond_init (&cond, NULL) != 0)
   {
      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))
   {
      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);
169
170
#elif defined (RTEMS_PURE)
   return (__PO_HI_UNAVAILABLE);
171
172
173
174
175
176
177
178
179
#elif defined (XENO_NATIVE)
  int ret;
  ret =  rt_task_sleep_until (rt_timer_ns2tsc (time * 1000));
  if (ret)
  {
      __DEBUGMSG ("[TASK] Error in rt_task_sleep_until, ret=%d, time=%llu\n", ret, time);
      return (__PO_HI_ERROR_PTHREAD_COND);
  }
  return (__PO_HI_SUCCESS);
180
181
182
#else
   return (__PO_HI_UNAVAILABLE);
#endif
183
}