picox  0.1
xtime.h
[詳解]
1 
14 /*
15  * License: MIT license
16  * Copyright (c) <2016> <MaskedW [maskedw00@gmail.com]>
17  *
18  * Permission is hereby granted, free of charge, to any person
19  * obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without
21  * restriction, including without limitation the rights to use, copy,
22  * modify, merge, publish, distribute, sublicense, and/or sell copies
23  * of the Software, and to permit persons to whom the Software is
24  * furnished to do so, subject to the following conditions:
25  *
26  * The above copyright notice and this permission notice shall be
27  * included in all copies or substantial portions of the Software.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36  * SOFTWARE.
37  */
38 
39 
40 #ifndef picox_core_detail_xtime_h_
41 #define picox_core_detail_xtime_h_
42 
43 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif /* __cplusplus */
55 
56 
58 #define X_TICKS_PER_SEC X_CONF_TICKS_PER_SEC
59 
60 
63 typedef int32_t XTicks;
64 
65 
70 #define X_TICKS_FOREVER ((XTicks)-1)
71 
72 
80 typedef uint32_t XTime;
81 
82 
85 typedef struct
86 {
87  XTime tv_sec;
88  int32_t tv_usec;
89 } XTimeVal;
90 
91 
97 typedef int32_t XSeconds;
98 
99 
102 typedef int32_t XMSeconds;
103 
104 
107 typedef int32_t XUSeconds;
108 
109 
116 XTicks x_port_ticks_now(void);
117 
118 
129 int x_port_gettimeofday(XTimeVal* tv, void* tz_dammy);
130 
131 
140 void x_port_msleep(XMSeconds msec);
141 
142 
148 void x_port_usleep(XUSeconds usec);
149 
150 
159 void x_port_mdelay(XMSeconds msec);
160 
161 
170 void x_port_udelay(XUSeconds usec);
171 
172 
173 #define x_ticks_now x_port_ticks_now
174 #define x_gettimeofday x_port_gettimeofday
175 #define x_msleep x_port_msleep
176 #define x_usleep x_port_usleep
177 #define x_mdelay x_port_mdelay
178 #define x_udelay x_port_mdelay
179 
180 
197 static inline XTicks x_msec_to_ticks(XMSeconds msec)
198 {
199  const XTicks ret = X_DIV_ROUNDUP(msec * X_TICKS_PER_SEC, 1000);
200  return ret;
201 }
202 
203 
206 static inline XTicks x_usec_to_ticks(XUSeconds usec)
207 {
208  const XTicks ret = X_DIV_ROUNDUP(usec * X_TICKS_PER_SEC, 1000) / 1000;
209  return ret;
210 }
211 
212 
215 static inline XMSeconds x_ticks_to_msec(XTicks ticks)
216 {
217  const XMSeconds ret = (ticks * 1000) / X_TICKS_PER_SEC;
218  return ret;
219 }
220 
221 
224 static inline XUSeconds x_ticks_to_usec(XTicks ticks)
225 {
226  const XUSeconds ret = ((ticks * 1000) / X_TICKS_PER_SEC) * 1000;
227  return ret;
228 }
229 
230 
240 #define X_MSEC_TO_TICKS(msec) (X_DIV_ROUNDUP((msec) * X_TICKS_PER_SEC, 1000))
241 #define X_USEC_TO_TICKS(usec) (X_DIV_ROUNDUP((usec) * X_TICKS_PER_SEC, 1000) / 1000)
242 #define X_TICKS_TO_MSEC(ticks) (((ticks) * 1000) / X_TICKS_PER_SEC)
243 #define X_TICKS_TO_USEC(ticks) ((((ticks) * 1000) / X_TICKS_PER_SEC) * 1000)
244 
257 #define x_timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
258 
259 #define x_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
260 
261 #define x_timercmp(tvp, uvp, cmp) \
262  (((tvp)->tv_sec == (uvp)->tv_sec) ? \
263  ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
264  ((tvp)->tv_sec cmp (uvp)->tv_sec))
265 
266 #define x_timeradd(tvp, uvp, vvp) \
267  do { \
268  (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
269  (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
270  if ((vvp)->tv_usec >= X_INT32_C(1000000)) \
271  { \
272  (vvp)->tv_sec++; \
273  (vvp)->tv_usec -= X_INT32_C(1000000); \
274  } \
275  } while (0)
276 
277 #define x_timersub(tvp, uvp, vvp) \
278  do { \
279  (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
280  (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
281  if ((vvp)->tv_usec < 0) \
282  { \
283  (vvp)->tv_sec--; \
284  (vvp)->tv_usec += X_INT32_C(1000000); \
285  } \
286  } while (0)
287 
288 
295 static inline XTimeVal x_gettimeofday2(void)
296 {
297  XTimeVal tv;
298  x_gettimeofday(&tv, NULL);
299  return tv;
300 }
301 
302 
303 #ifdef __cplusplus
304 }
305 #endif /* __cplusplus */
306 
307 
308 #endif /* picox_core_detail_xtime_h_ */
int32_t XSeconds
秒単位の時間を格納する型です
Definition: xtime.h:97
XTicks x_port_ticks_now(void)
現在のチック時間を返します
Definition: xtime.c:86
uint32_t XTime
time_tの代替をするシステム時刻を格納するための型です
Definition: xtime.h:80
int32_t XTicks
システムチックを格納する型です
Definition: xtime.h:63
void x_port_mdelay(XMSeconds msec)
ミリ秒単位のディレイを行います
Definition: xtime.c:161
int32_t XMSeconds
ミリ秒単位の時間を格納する型です
Definition: xtime.h:102
int x_port_gettimeofday(XTimeVal *tv, void *tz_dammy)
現在時刻を返します
Definition: xtime.c:57
#define X_DIV_ROUNDUP(dividend, divisor)
あまりの切り上げを行う整数除算です
Definition: xutils.h:171
int32_t tv_usec
Definition: xtime.h:88
void x_port_usleep(XUSeconds usec)
マイクロ秒単位のスリープを行います
Definition: xtime.c:135
static XTimeVal x_gettimeofday2(void)
現在時刻を返します
Definition: xtime.h:291
static XMSeconds x_ticks_to_msec(XTicks ticks)
チックをミリ秒に変換した値を返します
Definition: xtime.h:214
#define X_TICKS_PER_SEC
Definition: xtime.h:58
高精度のシステム時刻を格納するための型です
Definition: xtime.h:85
int32_t XUSeconds
マイクロ秒単位の時間を格納する型です
Definition: xtime.h:107
static XTicks x_msec_to_ticks(XMSeconds msec)
ミリ秒をチックに変換した値を返します
Definition: xtime.h:196
static XUSeconds x_ticks_to_usec(XTicks ticks)
チックをマイクロ秒に変換した値を返します
Definition: xtime.h:223
static XTicks x_usec_to_ticks(XUSeconds usec)
マイクロ秒をチックに変換した値を返します
Definition: xtime.h:205
void x_port_udelay(XUSeconds usec)
マイクロ秒単位のディレイを行います
Definition: xtime.c:194
void x_port_msleep(XMSeconds msec)
ミリ秒単位のスリープを行います
Definition: xtime.c:109