~ubuntu-branches/ubuntu/raring/ecasound2.2/raring

« back to all changes in this revision

Viewing changes to kvutils/kvu_timestamp.h

  • Committer: Bazaar Package Importer
  • Author(s): Junichi Uekawa
  • Date: 2009-11-02 18:22:35 UTC
  • mfrom: (5.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20091102182235-4ngh7699dmkgonyu
Tags: 2.7.0-1
* New upstream release.
* Depend on libreadline-dev instead of libreadline5-dev by request of
  Mattias Klose. It's now libreadline6-dev. (closes: #553748)
* Update menu file to use section Applications/ instead of Apps/.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ------------------------------------------------------------------------
 
2
// kvu_timestamp.h: Monotonic timestamps
 
3
// Copyright (C) 2009 Kai Vehmanen
 
4
//
 
5
// Attributes:
 
6
//     eca-style-version: 3
 
7
//
 
8
// This program is free software; you can redistribute it and/or modify
 
9
// it under the terms of the GNU General Public License as published by
 
10
// the Free Software Foundation; either version 2 of the License, or
 
11
// (at your option) any later version.
 
12
// 
 
13
// This program is distributed in the hope that it will be useful,
 
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
// GNU General Public License for more details.
 
17
// 
 
18
// You should have received a copy of the GNU General Public License
 
19
// along with this program; if not, write to the Free Software
 
20
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
21
// ------------------------------------------------------------------------
 
22
 
 
23
#ifndef INCLUDED_KVU_TIMESTAMP_H
 
24
#define INCLUDED_KVU_TIMESTAMP_H
 
25
 
 
26
#include <time.h>
 
27
 
 
28
/**
 
29
 * Returns non-zero if clock is monotonic.
 
30
 *
 
31
 * See POSIX 'CLOCK_MONOTONIC'
 
32
 */
 
33
int kvu_clock_is_monotonic(void);
 
34
 
 
35
/**
 
36
 * Finds the resolution (precision) of the clock
 
37
 * source.
 
38
 *
 
39
 * See POSIX clock_getres().
 
40
 */
 
41
int kvu_clock_getres(struct kvu_timespec *res);
 
42
 
 
43
/**
 
44
 * Retrieves a timestamp
 
45
 *
 
46
 * See POSIX clock_gettime()
 
47
 */
 
48
int kvu_clock_gettime(struct timespec *tp);
 
49
 
 
50
/**
 
51
 * Returns the timestamp as seconds
 
52
  */
 
53
static inline double kvu_timespec_seconds(struct timespec *tp)
 
54
{
 
55
  return tp->tv_sec + 
 
56
    (static_cast<double>(tp->tv_nsec) / 1000000000.0);
 
57
}
 
58
 
 
59
/** 
 
60
 * Adapted from macro definitions in glibc's sys/time.h (LGPL 2.1)
 
61
 */
 
62
static inline void kvu_timespec_clear(struct timespec *tvp)
 
63
{
 
64
  tvp->tv_sec = tvp->tv_nsec = 0;
 
65
}
 
66
 
 
67
static inline void kvu_timespec_assign(struct timespec *x, const struct timespec *y)
 
68
{
 
69
  x->tv_sec = y->tv_sec;
 
70
  x->tv_nsec = y->tv_nsec;
 
71
}
 
72
 
 
73
/** 
 
74
 * result = a + b
 
75
 *
 
76
 * Adapted from macro definitions in glibc's sys/time.h (LGPL 2.1)
 
77
 */
 
78
static inline void kvu_timespec_add(const struct timespec *a, 
 
79
                                    const struct timespec *b, 
 
80
                                    struct timespec *result)
 
81
{
 
82
  (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;                       
 
83
  (result)->tv_nsec = (a)->tv_nsec + (b)->tv_nsec;
 
84
  if ((result)->tv_nsec >= 1000000000) {
 
85
    ++(result)->tv_sec;
 
86
    (result)->tv_nsec -= 1000000000;
 
87
  }
 
88
}
 
89
                        
 
90
/** 
 
91
 * Returns 'a < b'
 
92
 *
 
93
 * Adapted from macro definitions in glibc's sys/time.h (LGPL 2.1)
 
94
 */                                                     
 
95
static inline int kvu_timespec_cmp_lt(const struct timespec *a, 
 
96
                                      const struct timespec *b)
 
97
 
98
  return (((a)->tv_sec == (b)->tv_sec) ?
 
99
          ((a)->tv_nsec < (b)->tv_nsec) :
 
100
          ((a)->tv_sec < (b)->tv_sec));
 
101
}
 
102
 
 
103
/**
 
104
 * Returns 'a > b'
 
105
 *
 
106
 * Adapted from macro definitions in glibc's sys/time.h (LGPL 2.1)
 
107
 */
 
108
static inline int kvu_timespec_cmp_gt(const struct timespec *a, 
 
109
                                      const struct timespec *b)
 
110
 
111
  return (((a)->tv_sec == (b)->tv_sec) ?
 
112
          ((a)->tv_nsec > (b)->tv_nsec) :
 
113
          ((a)->tv_sec > (b)->tv_sec));
 
114
}
 
115
 
 
116
/**
 
117
 * result = a - b 
 
118
 *
 
119
 * Adapted from macro definitions in glibc's sys/time.h (LGPL 2.1)
 
120
 */
 
121
static inline void kvu_timespec_sub(const struct timespec *a, 
 
122
                                    const struct timespec *b, 
 
123
                                    struct timespec *result)
 
124
{
 
125
  (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;
 
126
  (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec;
 
127
  if ((result)->tv_nsec < 0) {
 
128
    --(result)->tv_sec;
 
129
    (result)->tv_nsec += 1000000000;
 
130
  }
 
131
}                                                                         \
 
132
 
 
133
#endif /* INCLUDED_KVU_TIMESTAMP_H */