1/*
2* Copyright (C) 2011 The Android Open Source Project
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16#include "TimeUtils.h"
17
18#ifdef _WIN32
19#include <windows.h>
20#include <time.h>
21#include <stdio.h>
22#elif defined(__linux__)
23#include <stdlib.h>
24#include <sys/time.h>
25#include <time.h>
26#include <unistd.h>
27#else
28#include <sys/time.h>
29#include <unistd.h>
30#endif
31
32long long GetCurrentTimeMS()
33{
34#ifdef _WIN32
35    static LARGE_INTEGER freq;
36    static bool bNotInit = true;
37    if ( bNotInit ) {
38        bNotInit = (QueryPerformanceFrequency( &freq ) == FALSE);
39    }
40    LARGE_INTEGER currVal;
41    QueryPerformanceCounter( &currVal );
42
43    return currVal.QuadPart / (freq.QuadPart / 1000);
44
45#elif defined(__linux__)
46
47    struct timespec now;
48    clock_gettime(CLOCK_MONOTONIC, &now);
49    long long iDiff = (now.tv_sec * 1000LL) + now.tv_nsec/1000000LL;
50    return iDiff;
51
52#else /* Others, e.g. OS X */
53
54    struct timeval now;
55    gettimeofday(&now, NULL);
56    long long iDiff = (now.tv_sec * 1000LL) + now.tv_usec/1000LL;
57    return iDiff;
58
59#endif
60}
61
62void TimeSleepMS(int p_mili)
63{
64#ifdef _WIN32
65    Sleep(p_mili);
66#else
67    usleep(p_mili * 1000);
68#endif
69}
70