]> git.cworth.org Git - apitrace/blob - common/os_win32.cpp
Move os::Path to a separate header.
[apitrace] / common / os_win32.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #include <windows.h>
27
28 #include <assert.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "os.hpp"
33 #include "os_path.hpp"
34
35
36 namespace os {
37
38
39 /* 
40  * Trick from http://locklessinc.com/articles/pthreads_on_windows/
41  */
42 static CRITICAL_SECTION
43 criticalSection = {
44     (PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
45 };
46
47
48 void
49 acquireMutex(void)
50 {
51     EnterCriticalSection(&criticalSection);
52 }
53
54
55 void
56 releaseMutex(void)
57 {
58     LeaveCriticalSection(&criticalSection);
59 }
60
61
62 Path
63 getProcessName(void)
64 {
65     Path path;
66     size_t size = MAX_PATH;
67     char *buf = path.buf(size);
68
69     DWORD nWritten = GetModuleFileNameA(NULL, buf, size);
70     (void)nWritten;
71
72     path.truncate();
73
74     return path;
75 }
76
77 Path
78 getCurrentDir(void)
79 {
80     Path path;
81     size_t size = MAX_PATH;
82     char *buf = path.buf(size);
83     
84     DWORD ret = GetCurrentDirectoryA(size, buf);
85     (void)ret;
86     
87     buf[size - 1] = 0;
88     path.truncate();
89
90     return path;
91 }
92
93 void
94 log(const char *format, ...)
95 {
96     char buf[4096];
97
98     va_list ap;
99     va_start(ap, format);
100     fflush(stdout);
101     vsnprintf(buf, sizeof buf, format, ap);
102     va_end(ap);
103
104     OutputDebugStringA(buf);
105
106     /*
107      * Also write the message to stderr, when a debugger is not present (to
108      * avoid duplicate messages in command line debuggers).
109      */
110 #if _WIN32_WINNT > 0x0400
111     if (!IsDebuggerPresent()) {
112         fflush(stdout);
113         fputs(buf, stderr);
114         fflush(stderr);
115     }
116 #endif
117 }
118
119 long long
120 getTime(void)
121 {
122     static LARGE_INTEGER frequency;
123     LARGE_INTEGER counter;
124     if (!frequency.QuadPart)
125         QueryPerformanceFrequency(&frequency);
126     QueryPerformanceCounter(&counter);
127     return counter.QuadPart*1000000LL/frequency.QuadPart;
128 }
129
130 void
131 abort(void)
132 {
133 #ifndef NDEBUG
134     DebugBreak();
135 #else
136     ExitProcess(0);
137 #endif
138 }
139
140
141 static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
142 static void (*gCallback)(void) = NULL;
143
144 static LONG WINAPI
145 unhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
146 {
147     if (gCallback) {
148         gCallback();
149     }
150
151         if (prevExceptionFilter) {
152                 return prevExceptionFilter(pExceptionInfo);
153     } else {
154                 return EXCEPTION_CONTINUE_SEARCH;
155     }
156 }
157
158 void
159 setExceptionCallback(void (*callback)(void))
160 {
161     assert(!gCallback);
162
163     if (!gCallback) {
164         gCallback = callback;
165
166         assert(!prevExceptionFilter);
167
168         /*
169          * TODO: Unfortunately it seems that the CRT will reset the exception
170          * handler in certain circumnstances.  See
171          * http://www.codeproject.com/KB/winsdk/crash_hook.aspx
172          */
173         prevExceptionFilter = SetUnhandledExceptionFilter(unhandledExceptionFilter);
174     }
175 }
176
177 void
178 resetExceptionCallback(void)
179 {
180     gCallback = NULL;
181 }
182
183
184 } /* namespace os */