amino  1.0-beta2
Lightweight Robot Utility Library
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 
62 #define AA_CLOCK 0
63 
64 #endif
65 
66 #include <errno.h>
68 static inline struct timespec
69 aa_tm_make( time_t sec, long nsec ) {
70  struct timespec t;
71  t.tv_sec = sec;
72  t.tv_nsec = nsec;
73  return t;
74 }
75 
78 static inline struct timespec
79 aa_tm_make_norm( time_t sec, long nsec ) {
80  long nsp = aa_lmodulo( nsec, AA_IBILLION );
81  return aa_tm_make( sec + (nsec - nsp)/AA_IBILLION, nsp );
82 }
83 
85 static inline struct timespec
86 aa_tm_add( struct timespec t1, struct timespec t0 ) {
87  return aa_tm_make_norm( t1.tv_sec + t0.tv_sec,
88  t1.tv_nsec + t0.tv_nsec );
89 }
90 
92 static inline struct timespec
93 aa_tm_sub( const struct timespec a, const struct timespec b ) {
94  return aa_tm_make_norm( a.tv_sec - b.tv_sec,
95  a.tv_nsec - b.tv_nsec );
96 }
97 
99 AA_API struct timespec
100 aa_tm_now();
101 
103 static inline struct timespec
104 aa_tm_future( const struct timespec reltime ) {
105  return aa_tm_add( reltime, aa_tm_now() );
106 }
107 
109 static inline long
110 aa_tm_cmp( const struct timespec t1, const struct timespec t2 ) {
111  return ( t1.tv_sec != t2.tv_sec ) ?
112  (t1.tv_sec - t2.tv_sec) :
113  (t1.tv_nsec - t2.tv_nsec);
114 }
115 
117 static inline struct timespec
118 aa_tm_min( const struct timespec t1, const struct timespec t2 ) {
119  return (aa_tm_cmp(t1,t2)<0) ? t1 : t2;
120 }
121 
123 static inline struct timespec
124 aa_tm_max( const struct timespec t1, const struct timespec t2 ) {
125  return (aa_tm_cmp(t1,t2)>0) ? t1 : t2;
126 }
127 
129 static inline int
130 aa_tm_isafter( const struct timespec abstime ) {
131  return aa_tm_cmp(aa_tm_now(), abstime) > 0;
132 }
133 
135 static inline int64_t
136 aa_tm_timespec2usec( const struct timespec t ) {
137  return t.tv_sec*1000000 + t.tv_nsec/1000;
138 }
139 
141 static inline int64_t
142 aa_tm_timespec2msec( const struct timespec t ) {
143  return t.tv_sec*1000 + t.tv_nsec/1000000;
144 }
145 
147 static inline double
148 aa_tm_timespec2sec( const struct timespec t ) {
149  return (double)t.tv_sec + (double)t.tv_nsec/1e9;
150 }
151 
153 static inline struct timespec
154 aa_tm_sec2timespec( double t ) {
155  time_t sec = (time_t) t;
156  long nsec = (long) ((t-(double)sec)*AA_IBILLION);
157  return aa_tm_make_norm( sec, nsec );
158 }
159 
161 static inline int
162 aa_tm_relsleep( struct timespec t ) {
163  struct timespec rem;
164  int r;
165  do {
166  r = nanosleep( &t, &rem );
167  assert( 0 == r || EINTR == errno );
168  t.tv_sec = rem.tv_sec;
169  t.tv_nsec = rem.tv_nsec;
170  } while( 0 != r ) ;
171  return 0;
172 }
173 
174 
175 #if (defined(__GNUC__) || defined(__ICC)) && (defined(__i386__) || defined(__x86_64__))
176 #define AA_FEATURE_RDTSC
177 AA_API uint64_t aa_rdtsc(void);
178 
179 
180 #endif
181 
182 #endif //AMINO_TIME_H
#define AA_IBILLION
(int) 1e9
Definition: amino.h:116
#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:86
static struct timespec aa_tm_future(const struct timespec reltime)
returns reltime + now
Definition: time.h:104
AA_API struct timespec aa_tm_now()
gets current time via AA_CLOCK
static struct timespec aa_tm_sub(const struct timespec a, const struct timespec b)
subtract two times: a - b
Definition: time.h:93
static int64_t aa_tm_timespec2usec(const struct timespec t)
convert timespec t to microseconds
Definition: time.h:136
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:79
static struct timespec aa_tm_sec2timespec(double t)
convert seconds t to timespec
Definition: time.h:154
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:110
static struct timespec aa_tm_min(const struct timespec t1, const struct timespec t2)
Return the earlier of t1, t2.
Definition: time.h:118
static int64_t aa_tm_timespec2msec(const struct timespec t)
convert timespec t to milliseconds
Definition: time.h:142
static int aa_tm_isafter(const struct timespec abstime)
is the current time later than abstime?
Definition: time.h:130
static struct timespec aa_tm_max(const struct timespec t1, const struct timespec t2)
Return the latter of t1, t2.
Definition: time.h:124
static double aa_tm_timespec2sec(const struct timespec t)
convert timespec t to seconds
Definition: time.h:148
static struct timespec aa_tm_make(time_t sec, long nsec)
create a struct timespec with given elements
Definition: time.h:69
static int aa_tm_relsleep(struct timespec t)
sleep for specified time
Definition: time.h:162