File: kvu_timestamp.cpp

package info (click to toggle)
ecasound 2.9.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,292 kB
  • sloc: cpp: 39,475; sh: 4,335; lisp: 1,918; ansic: 1,883; makefile: 888; python: 617; ruby: 202
file content (153 lines) | stat: -rw-r--r-- 3,080 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// ------------------------------------------------------------------------
// kvu_timestamp.cpp: Monotonic timestamps
// Copyright (C) 2009 Kai Vehmanen
//
// Attributes:
//     eca-style-version: 3
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
// ------------------------------------------------------------------------

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* note: code assumes timespect struct to be defined in
 *       time.h (i.e. SUSv2 or later) */

#include <time.h>

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#include <kvu_timestamp.h>

#if HAVE_CLOCK_GETTIME

/* IMPLEMENTATION: clock_gettime() */
/* *********************************/

int kvu_clock_is_monotonic(void)
{
  int res = -1;

#if defined CLOCK_MONOTONIC
  struct timespec tp;
  res = clock_getres(CLOCK_MONOTONIC, &tp);
#endif

  if (res == 0)
    return 1;

  return 0;
}

int kvu_clock_getres(struct timespec *dst)
{
  struct timespec tp;
  int res = -1;

#if defined CLOCK_MONOTONIC
  res = clock_getres(CLOCK_MONOTONIC, &tp);
#endif

  if (res < 0) 
    res =
      clock_getres(CLOCK_REALTIME, &tp);
  if (res == 0) {
    dst->tv_sec = tp.tv_sec;
    dst->tv_sec = tp.tv_nsec;
  }

  return res;
}

int kvu_clock_gettime(struct timespec *dst)
{
  struct timespec tp;
  int res = -1;

#if defined CLOCK_MONOTONIC
  res = clock_gettime(CLOCK_MONOTONIC, &tp);
#endif

  if (res < 0) 
    res =
      clock_gettime(CLOCK_REALTIME, &tp);
  if (res == 0) {
    dst->tv_sec = tp.tv_sec;
    dst->tv_nsec = tp.tv_nsec;
  }

  return res;
}

#elif HAVE_GETTIMEOFDAY

/* IMPLEMENTATION: gettimeofday() */
/* ********************************/

int kvu_clock_is_monotonic(void)
{
  return 0;
}

int kvu_clock_getres(struct timespec *arg)
{
  /* note: an estimate, actual resolution may
   *       be worse */
  arg->tv_sec = 0;
  arg->tv_nsec = 1000;
  return 0;
}

int kvu_clock_gettime(struct timespec *arg)
{
  struct timeval tmp;
  int res =
    gettimeofday(&tmp, NULL);
  if (res == 0) {
    arg->tv_sec = tmp.tv_sec;
    arg->tv_nsec = tmp.tv_usec * 1000;
  }

  return res;
}

#else

/* IMPLEMENTATION: fallback / no-op */
/* **********************************/

int kvu_clock_is_monotonic(void)
{
  DBC_NEVER_REACHED();
  return 0;
}

int kvu_clock_getres(struct timespec *res)
{
  DBC_NEVER_REACHED();
  return -1;
}

int kvu_clock_gettime(struct timespec *tp)
{
  DBC_NEVER_REACHED();
  return -1;
}

#endif