deClock.c revision 10c475fcf13dbf996e85328e4fd6b14259fa1ebb
1/*-------------------------------------------------------------------------
2 * drawElements Utility Library
3 * ----------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief System clock.
22 *//*--------------------------------------------------------------------*/
23
24#include "deClock.h"
25
26#include <time.h>
27
28#if (DE_OS == DE_OS_WIN32)
29#   define WIN32_LEAN_AND_MEAN
30#   include <windows.h>
31#elif (DE_OS == DE_OS_UNIX) || (DE_OS == DE_OS_ANDROID) || (DE_OS == DE_OS_SYMBIAN)
32#   include <time.h>
33#elif (DE_OS == DE_OS_OSX) || (DE_OS == DE_OS_IOS)
34#	include <sys/time.h>
35#endif
36
37deUint64 deGetMicroseconds (void)
38{
39#if (DE_OS == DE_OS_WIN32)
40	LARGE_INTEGER freq;
41	LARGE_INTEGER count;
42	QueryPerformanceCounter(&count);
43	QueryPerformanceFrequency(&freq);
44	DE_ASSERT(freq.LowPart != 0 || freq.HighPart != 0);
45	/* \todo [2010-03-26 kalle] consider adding a 32bit-friendly implementation */
46
47	if (count.QuadPart < MAXLONGLONG / 1000000)
48	{
49		DE_ASSERT(freq.QuadPart != 0);
50		return count.QuadPart * 1000000 / freq.QuadPart;
51	}
52	else
53	{
54		DE_ASSERT(freq.QuadPart >= 1000000);
55		return count.QuadPart / (freq.QuadPart / 1000000);
56	}
57
58#elif (DE_OS == DE_OS_UNIX) || (DE_OS == DE_OS_ANDROID)
59	struct timespec currTime;
60	clock_gettime(CLOCK_MONOTONIC, &currTime);
61	return (deUint64)currTime.tv_sec*1000000 + ((deUint64)currTime.tv_nsec/1000);
62
63#elif  (DE_OS == DE_OS_SYMBIAN)
64	struct timespec currTime;
65	/* Symbian supports only realtime clock for clock_gettime. */
66	/* \todo [2011-08-22 kalle] Proper Symbian-based implementation that is guaranteed to be monotonic. */
67	clock_gettime(CLOCK_REALTIME, &currTime);
68	return (deUint64)currTime.tv_sec*1000000 + ((deUint64)currTime.tv_nsec/1000);
69
70#elif (DE_OS == DE_OS_OSX) || (DE_OS == DE_OS_IOS)
71	struct timeval currTime;
72	gettimeofday(&currTime, DE_NULL);
73	return (deUint64)currTime.tv_sec*1000000 + (deUint64)currTime.tv_usec;
74
75#else
76#   error "Not implemented for target OS"
77#endif
78}
79
80deUint64 deGetTime (void)
81{
82	return (deUint64)time(DE_NULL);
83}
84