summaryrefslogtreecommitdiff
path: root/src/base/TimeSource.cpp
blob: e90575dcdcb3386f79635998454b5ee7f0a240a0 (plain)
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
//
//  libavg - Media Playback Engine.
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "TimeSource.h"

#include "Logger.h"
#include "Exception.h"

#ifdef _WIN32
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
#include <Mmsystem.h>
#else
#include <sys/time.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>

#include <errno.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <unistd.h>

using namespace std;

namespace avg {

TimeSource* TimeSource::m_pTimeSource = 0;

TimeSource * TimeSource::get()
{
    if (!m_pTimeSource) {
#ifdef _WIN32
        TIMECAPS tc;
        UINT wTimerRes;
        MMRESULT err = timeGetDevCaps(&tc, sizeof(TIMECAPS));
        AVG_ASSERT(err == TIMERR_NOERROR);
        wTimerRes = max(tc.wPeriodMin, 1);
        timeBeginPeriod(wTimerRes);
#endif
        m_pTimeSource = new TimeSource;
    }
    return m_pTimeSource;
}

TimeSource::TimeSource()
{
#ifdef __APPLE__
    mach_timebase_info(&m_TimebaseInfo);
#endif
}

TimeSource::~TimeSource()
{
}

long long TimeSource::getCurrentMillisecs()
{
    return getCurrentMicrosecs()/1000;
}

long long TimeSource::getCurrentMicrosecs()
{
#ifdef _WIN32
    return (long long)(timeGetTime())*1000;
#else
#ifdef __APPLE__
    long long systemTime = mach_absolute_time();
    return (systemTime * m_TimebaseInfo.numer/m_TimebaseInfo.denom)/1000;
#else
    struct timespec now;
    int rc = clock_gettime(CLOCK_MONOTONIC, &now);
    assert(rc == 0);
    return ((long long)now.tv_sec)*1000000+now.tv_nsec/1000;
#endif
#endif
}

void TimeSource::sleepUntil(long long targetTime)
{
    long long now = getCurrentMillisecs();
#ifdef __APPLE__
    if (targetTime > now) {
        msleep(targetTime-now);
    }
#else
    while (now<targetTime) {
        if (targetTime-now<=2) {
            msleep(0);
         } else {
            msleep(int(targetTime-now-2));
        }
        now = getCurrentMillisecs();
    }
#endif
}

void msleep(int millisecs)
{
#if _WIN32
            Sleep(millisecs);
#else
            usleep((long long)(millisecs)*1000);
#endif
}

}