]> git.cworth.org Git - apitrace/blob - os_win32.cpp
Tweaks.
[apitrace] / 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 #include <string.h>
28 #include <stdio.h>
29
30 #include "os.hpp"
31 #include "log.hpp"
32
33
34 namespace OS {
35
36
37 /* 
38  * Trick from http://locklessinc.com/articles/pthreads_on_windows/
39  */
40 static CRITICAL_SECTION
41 CriticalSection = {
42     (PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
43 };
44
45
46 void
47 AcquireMutex(void)
48 {
49     EnterCriticalSection(&CriticalSection); 
50 }
51
52
53 void
54 ReleaseMutex(void)
55 {
56     LeaveCriticalSection(&CriticalSection); 
57 }
58
59
60 bool
61 GetProcessName(char *str, size_t size)
62 {
63     char szProcessPath[PATH_MAX];
64     char *lpProcessName;
65     char *lpProcessExt;
66
67     GetModuleFileNameA(NULL, szProcessPath, sizeof(szProcessPath)/sizeof(szProcessPath[0]));
68
69     lpProcessName = strrchr(szProcessPath, '\\');
70     lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
71
72     lpProcessExt = strrchr(lpProcessName, '.');
73     if (lpProcessExt) {
74         *lpProcessExt = '\0';
75     }
76
77     strncpy(str, lpProcessName, size);
78
79     return true;
80 }
81
82 bool
83 GetCurrentDir(char *str, size_t size)
84 {
85     DWORD ret;
86     ret = GetCurrentDirectoryA(size, str);
87     str[size - 1] = 0;
88     return ret == 0 ? false : true;
89 }
90
91 void
92 DebugMessage(const char *message)
93 {
94    OutputDebugStringA(message);
95    if (!IsDebuggerPresent()) {
96       fflush(stdout);
97       fputs(message, stderr);
98       fflush(stderr);
99    }
100 }
101
102 void
103 Abort(void)
104 {
105 #ifndef NDEBUG
106     DebugBreak();
107 #else
108     ExitProcess(0);
109 #endif
110 }
111
112
113
114 } /* namespace OS */
115
116
117 #if 0
118 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
119     switch(fdwReason) {
120     case DLL_PROCESS_ATTACH:
121     case DLL_THREAD_ATTACH:
122         return TRUE;
123     case DLL_THREAD_DETACH:
124         return TRUE;
125     case DLL_PROCESS_DETACH:
126         Log::Close();
127         return TRUE;
128     }
129     (void)hinstDLL;
130     (void)lpvReserved;
131     return TRUE;
132 }
133 #endif