Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / auxiliary / os / os_time.c
1 /**************************************************************************
2  *
3  * Copyright 2008-2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27
28 /**
29  * @file
30  * OS independent time-manipulation functions.
31  * 
32  * @author Jose Fonseca <jfonseca@vmware.com>
33  */
34
35
36 #include "pipe/p_config.h"
37
38 #if defined(PIPE_OS_UNIX)
39 #  include <sys/time.h> /* timeval */
40 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
41 #  include <windows.h>
42 #  include <winddi.h>
43 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
44 #  include <windows.h>
45 extern VOID KeQuerySystemTime(PLARGE_INTEGER);
46 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
47 #  include <windows.h>
48 #else
49 #  error Unsupported OS
50 #endif
51
52 #include "os_time.h"
53
54
55 int64_t
56 os_time_get(void)
57 {
58 #if defined(PIPE_OS_UNIX)
59
60    struct timeval tv;
61    gettimeofday(&tv, NULL);
62    return tv.tv_usec + tv.tv_sec*1000000LL;
63
64 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
65
66    static LONGLONG frequency;
67    LONGLONG counter;
68    if(!frequency)
69       EngQueryPerformanceFrequency(&frequency);
70    EngQueryPerformanceCounter(&counter);
71    return counter*INT64_C(1000000)/frequency;
72
73 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
74
75    static LARGE_INTEGER frequency;
76    LARGE_INTEGER counter;
77    if(!frequency.QuadPart)
78       QueryPerformanceFrequency(&frequency);
79    QueryPerformanceCounter(&counter);
80    return counter.QuadPart*INT64_C(1000000)/frequency.QuadPart;
81
82 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
83
84    /* Updated every 10 miliseconds, measured in units of 100 nanoseconds.
85     * http://msdn.microsoft.com/en-us/library/ms801642.aspx */
86    LARGE_INTEGER counter;
87    KeQuerySystemTime(&counter);
88    return counter.QuadPart/10;
89
90 #endif
91 }
92
93
94 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
95
96 void
97 os_time_sleep(int64_t usecs)
98 {
99    static LONGLONG frequency;
100    LONGLONG start, curr, end;
101    
102    EngQueryPerformanceCounter(&start);
103    
104    if(!frequency)
105       EngQueryPerformanceFrequency(&frequency);
106    
107    end = start + (usecs * frequency + 999999LL)/1000000LL;
108    
109    do {
110       EngQueryPerformanceCounter(&curr);
111    } while(start <= curr && curr < end || 
112            end < start && (curr < end || start <= curr));
113 }
114
115 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
116
117 void
118 os_time_sleep(int64_t usecs)
119 {
120    Sleep((usecs + 999) / 1000);
121 }
122
123 #endif