~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/system/Clock.cxx

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 
3
 * http://www.musicpd.org
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
 */
 
19
 
 
20
#include "Clock.hxx"
 
21
 
 
22
#ifdef WIN32
 
23
#include <windows.h>
 
24
#elif defined(__APPLE__)
 
25
#include <mach/mach_time.h>
 
26
#else
 
27
#include <time.h>
 
28
#ifndef CLOCK_MONOTONIC
 
29
#include <sys/time.h>
 
30
#endif
 
31
#endif
 
32
 
 
33
unsigned
 
34
MonotonicClockMS(void)
 
35
{
 
36
#ifdef WIN32
 
37
        return GetTickCount();
 
38
#elif defined(__APPLE__) /* OS X does not define CLOCK_MONOTONIC */
 
39
        static mach_timebase_info_data_t base;
 
40
        if (base.denom == 0)
 
41
                (void)mach_timebase_info(&base);
 
42
 
 
43
        return (unsigned)((mach_absolute_time() * base.numer)
 
44
                          / (1000000 * base.denom));
 
45
#elif defined(CLOCK_MONOTONIC)
 
46
        struct timespec ts;
 
47
        clock_gettime(CLOCK_MONOTONIC, &ts);
 
48
        return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
 
49
#else
 
50
        /* we have no monotonic clock, fall back to gettimeofday() */
 
51
        struct timeval tv;
 
52
        gettimeofday(&tv, 0);
 
53
        return tv.tv_sec * 1000 + tv.tv_usec / 1000;
 
54
#endif
 
55
}
 
56
 
 
57
uint64_t
 
58
MonotonicClockUS(void)
 
59
{
 
60
#ifdef WIN32
 
61
        LARGE_INTEGER l_value, l_frequency;
 
62
 
 
63
        if (!QueryPerformanceCounter(&l_value) ||
 
64
            !QueryPerformanceFrequency(&l_frequency))
 
65
                return 0;
 
66
 
 
67
        uint64_t value = l_value.QuadPart;
 
68
        uint64_t frequency = l_frequency.QuadPart;
 
69
 
 
70
        if (frequency > 1000000) {
 
71
                value *= 10000;
 
72
                value /= frequency / 100;
 
73
        } else if (frequency < 1000000) {
 
74
                value *= 10000;
 
75
                value /= frequency;
 
76
                value *= 100;
 
77
        }
 
78
 
 
79
        return value;
 
80
#elif defined(__APPLE__) /* OS X does not define CLOCK_MONOTONIC */
 
81
        static mach_timebase_info_data_t base;
 
82
        if (base.denom == 0)
 
83
                (void)mach_timebase_info(&base);
 
84
 
 
85
        return ((uint64_t)mach_absolute_time() * (uint64_t)base.numer)
 
86
                / (1000 * (uint64_t)base.denom);
 
87
#elif defined(CLOCK_MONOTONIC)
 
88
        struct timespec ts;
 
89
        clock_gettime(CLOCK_MONOTONIC, &ts);
 
90
        return (uint64_t)ts.tv_sec * 1000000 + (uint64_t)(ts.tv_nsec / 1000);
 
91
#else
 
92
        /* we have no monotonic clock, fall back to gettimeofday() */
 
93
        struct timeval tv;
 
94
        gettimeofday(&tv, 0);
 
95
        return (uint64_t)tv.tv_sec * 1000 + (uint64_t)tv.tv_usec;
 
96
#endif
 
97
}
 
98