amino  1.0-beta2
Lightweight Robot Utility Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
time.h
Go to the documentation of this file.
1 /* -*- mode: C; c-basic-offset: 4 -*- */
2 /* ex: set shiftwidth=4 tabstop=4 expandtab: */
3 /*
4  * Copyright (c) 2010-2011, Georgia Tech Research Corporation
5  * All rights reserved.
6  *
7  * Author(s): Neil T. Dantam <ntd@gatech.edu>
8  * Georgia Tech Humanoid Robotics Lab
9  * Under Direction of Prof. Mike Stilman <mstilman@cc.gatech.edu>
10  *
11  *
12  * This file is provided under the following "BSD-style" License:
13  *
14  *
15  * Redistribution and use in source and binary forms, with or
16  * without modification, are permitted provided that the following
17  * conditions are met:
18  *
19  * * Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  *
22  * * Redistributions in binary form must reproduce the above
23  * copyright notice, this list of conditions and the following
24  * disclaimer in the documentation and/or other materials provided
25  * with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
28  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
29  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  *
41  */
42 #ifndef AMINO_TIME_H
43 #define AMINO_TIME_H
44 
49 #ifdef CLOCK_MONOTONIC
50 
54 #define AA_CLOCK CLOCK_MONOTONIC
55 
56 #else
57 
58 /* You're probably on a horrible MacOSX machine. Get some real POSIX
59  * support already! */
60 
61 #define AA_CLOCK 0
62 
63 #endif
64 
65 #include <errno.h>
67 static inline struct timespec
68 aa_tm_make( time_t sec, long nsec ) {
69  struct timespec t;
70  t.tv_sec = sec;
71  t.tv_nsec = nsec;
72  return t;
73 }
74 
77 static inline struct timespec
78 aa_tm_make_norm( time_t sec, long nsec ) {
79  long nsp = aa_lmodulo( nsec, AA_IBILLION );
80  return aa_tm_make( sec + (nsec - nsp)/AA_IBILLION, nsp );
81 }
82 
84 static inline struct timespec
85 aa_tm_add( struct timespec t1, struct timespec t0 ) {
86  return aa_tm_make_norm( t1.tv_sec + t0.tv_sec,
87  t1.tv_nsec + t0.tv_nsec );
88 }
89 
91 static inline struct timespec
92 aa_tm_sub( const struct timespec a, const struct timespec b ) {
93  return aa_tm_make_norm( a.tv_sec - b.tv_sec,
94  a.tv_nsec - b.tv_nsec );
95 }
96 
98 AA_API struct timespec
99 aa_tm_now();
100 
102 static inline struct timespec
103 aa_tm_future( const struct timespec reltime ) {
104  return aa_tm_add( reltime, aa_tm_now() );
105 }
106 
108 static inline long
109 aa_tm_cmp( const struct timespec t1, const struct timespec t2 ) {
110  return ( t1.tv_sec != t2.tv_sec ) ?
111  (t1.tv_sec - t2.tv_sec) :
112  (t1.tv_nsec - t2.tv_nsec);
113 }
114 
116 static inline struct timespec
117 aa_tm_min( const struct timespec t1, const struct timespec t2 ) {
118  return (aa_tm_cmp(t1,t2)<0) ? t1 : t2;
119 }
120 
122 static inline struct timespec
123 aa_tm_max( const struct timespec t1, const struct timespec t2 ) {
124  return (aa_tm_cmp(t1,t2)>0) ? t1 : t2;
125 }
126 
128 static inline int
129 aa_tm_isafter( const struct timespec abstime ) {
130  return aa_tm_cmp(aa_tm_now(), abstime) > 0;
131 }
132 
134 static inline int64_t
135 aa_tm_timespec2usec( const struct timespec t ) {
136  return t.tv_sec*1000000 + t.tv_nsec/1000;
137 }
138 
140 static inline int64_t
141 aa_tm_timespec2msec( const struct timespec t ) {
142  return t.tv_sec*1000 + t.tv_nsec/1000000;
143 }
144 
146 static inline double
147 aa_tm_timespec2sec( const struct timespec t ) {
148  return (double)t.tv_sec + (double)t.tv_nsec/1e9;
149 }
150 
152 static inline struct timespec
153 aa_tm_sec2timespec( double t ) {
154  time_t sec = (time_t) t;
155  long nsec = (long) ((t-(double)sec)*AA_IBILLION);
156  return aa_tm_make_norm( sec, nsec );
157 }
158 
160 static inline int
161 aa_tm_relsleep( struct timespec t ) {
162  struct timespec rem;
163  int r;
164  do {
165  r = nanosleep( &t, &rem );
166  assert( 0 == r || EINTR == errno );
167  t.tv_sec = rem.tv_sec;
168  t.tv_nsec = rem.tv_nsec;
169  } while( 0 != r ) ;
170  return 0;
171 }
172 
173 
174 #if (defined(__GNUC__) || defined(__ICC)) && (defined(__i386__) || defined(__x86_64__))
175 #define AA_FEATURE_RDTSC
176 AA_API uint64_t aa_rdtsc(void);
177 
178 
179 #endif
180 
181 #endif //AMINO_TIME_H
AA_API struct timespec aa_tm_now()
gets current time via AA_CLOCK
static int aa_tm_isafter(const struct timespec abstime)
is the current time later than abstime?
Definition: time.h:129
static struct timespec aa_tm_sec2timespec(double t)
convert seconds t to timespec
Definition: time.h:153
static struct timespec aa_tm_make(time_t sec, long nsec)
create a struct timespec with given elements
Definition: time.h:68
static double aa_tm_timespec2sec(const struct timespec t)
convert timespec t to seconds
Definition: time.h:147
static struct timespec aa_tm_future(const struct timespec reltime)
returns reltime + now
Definition: time.h:103
static int64_t aa_tm_timespec2usec(const struct timespec t)
convert timespec t to microseconds
Definition: time.h:135
#define AA_IBILLION
(int) 1e9
Definition: amino.h:106
static long aa_tm_cmp(const struct timespec t1, const struct timespec t2)
t1 < t2: negative; t1 == t2: 0; t1 > t2: positive
Definition: time.h:109
static struct timespec aa_tm_max(const struct timespec t1, const struct timespec t2)
Return the latter of t1, t2.
Definition: time.h:123
static struct timespec aa_tm_sub(const struct timespec a, const struct timespec b)
subtract two times: a - b
Definition: time.h:92
#define AA_API
calling and name mangling convention for functions
Definition: amino.h:95
static long aa_lmodulo(long a, long b)
Fortran modulo, Ada mod.
Definition: math.h:137
static struct timespec aa_tm_add(struct timespec t1, struct timespec t0)
add two times: a + b
Definition: time.h:85
static int64_t aa_tm_timespec2msec(const struct timespec t)
convert timespec t to milliseconds
Definition: time.h:141
static struct timespec aa_tm_min(const struct timespec t1, const struct timespec t2)
Return the earlier of t1, t2.
Definition: time.h:117
static struct timespec aa_tm_make_norm(time_t sec, long nsec)
create a struct timespec with given elements, fixing things up if nsec is negative or more than a bil...
Definition: time.h:78
static int aa_tm_relsleep(struct timespec t)
sleep for specified time
Definition: time.h:161