amino
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 
51 #define AA_CLOCK CLOCK_MONOTONIC
52 
53 #else
54 
55 /* You're probably on a horrible MacOSX machine. Get some real POSIX
56  * support already! */
57 
58 #define AA_CLOCK 0
59 
60 #endif
61 
62 #include <errno.h>
64 static inline struct timespec
65 aa_tm_make( time_t sec, long nsec ) {
66  struct timespec t;
67  t.tv_sec = sec;
68  t.tv_nsec = nsec;
69  return t;
70 }
71 
74 static inline struct timespec
75 aa_tm_make_norm( time_t sec, long nsec ) {
76  long nsp = aa_lmodulo( nsec, AA_IBILLION );
77  return aa_tm_make( sec + (nsec - nsp)/AA_IBILLION, nsp );
78 }
79 
81 static inline struct timespec
82 aa_tm_add( struct timespec t1, struct timespec t0 ) {
83  return aa_tm_make_norm( t1.tv_sec + t0.tv_sec,
84  t1.tv_nsec + t0.tv_nsec );
85 }
86 
88 static inline struct timespec
89 aa_tm_sub( const struct timespec a, const struct timespec b ) {
90  return aa_tm_make_norm( a.tv_sec - b.tv_sec,
91  a.tv_nsec - b.tv_nsec );
92 }
93 
95 AA_API struct timespec
96 aa_tm_now();
97 
99 static inline struct timespec
100 aa_tm_future( const struct timespec reltime ) {
101  return aa_tm_add( reltime, aa_tm_now() );
102 }
103 
105 static inline long
106 aa_tm_cmp( const struct timespec t1, const struct timespec t2 ) {
107  return ( t1.tv_sec != t2.tv_sec ) ?
108  (t1.tv_sec - t2.tv_sec) :
109  (t1.tv_nsec - t2.tv_nsec);
110 }
111 
113 static inline struct timespec
114 aa_tm_min( const struct timespec t1, const struct timespec t2 ) {
115  return (aa_tm_cmp(t1,t2)<0) ? t1 : t2;
116 }
117 
119 static inline struct timespec
120 aa_tm_max( const struct timespec t1, const struct timespec t2 ) {
121  return (aa_tm_cmp(t1,t2)>0) ? t1 : t2;
122 }
123 
125 static inline int
126 aa_tm_isafter( const struct timespec abstime ) {
127  return aa_tm_cmp(aa_tm_now(), abstime) > 0;
128 }
129 
131 static inline int64_t
132 aa_tm_timespec2usec( const struct timespec t ) {
133  return t.tv_sec*1000000 + t.tv_nsec/1000;
134 }
135 
137 static inline int64_t
138 aa_tm_timespec2msec( const struct timespec t ) {
139  return t.tv_sec*1000 + t.tv_nsec/1000000;
140 }
141 
143 static inline double
144 aa_tm_timespec2sec( const struct timespec t ) {
145  return (double)t.tv_sec + (double)t.tv_nsec/1e9;
146 }
147 
149 static inline struct timespec
150 aa_tm_sec2timespec( double t ) {
151  time_t sec = (time_t) t;
152  long nsec = (long) ((t-(double)sec)*AA_IBILLION);
153  return aa_tm_make_norm( sec, nsec );
154 }
155 
157 static inline int
158 aa_tm_relsleep( struct timespec t ) {
159  struct timespec rem;
160  int r;
161  do {
162  r = nanosleep( &t, &rem );
163  assert( 0 == r || EINTR == errno );
164  t.tv_sec = rem.tv_sec;
165  t.tv_nsec = rem.tv_nsec;
166  } while( 0 != r ) ;
167  return 0;
168 }
169 
170 
171 #if (defined(__GNUC__) || defined(__ICC)) && (defined(__i386__) || defined(__x86_64__))
172 #define AA_FEATURE_RDTSC
173 AA_API uint64_t aa_rdtsc(void);
174 
175 
176 #endif
177 
178 #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:126
static struct timespec aa_tm_sec2timespec(double t)
convert seconds t to timespec
Definition: time.h:150
static struct timespec aa_tm_make(time_t sec, long nsec)
create a struct timespec with given elements
Definition: time.h:65
static double aa_tm_timespec2sec(const struct timespec t)
convert timespec t to seconds
Definition: time.h:144
static struct timespec aa_tm_future(const struct timespec reltime)
returns reltime + now
Definition: time.h:100
static int64_t aa_tm_timespec2usec(const struct timespec t)
convert timespec t to microseconds
Definition: time.h:132
#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:106
static struct timespec aa_tm_max(const struct timespec t1, const struct timespec t2)
Return the latter of t1, t2.
Definition: time.h:120
static struct timespec aa_tm_sub(const struct timespec a, const struct timespec b)
subtract two times: a - b
Definition: time.h:89
#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:82
static int64_t aa_tm_timespec2msec(const struct timespec t)
convert timespec t to milliseconds
Definition: time.h:138
static struct timespec aa_tm_min(const struct timespec t1, const struct timespec t2)
Return the earlier of t1, t2.
Definition: time.h:114
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:75
static int aa_tm_relsleep(struct timespec t)
sleep for specified time
Definition: time.h:158