1 //
2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 //
9 // Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 //
17 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 // POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #include "../osinclude.h"
36
37 #define STRICT
38 #define VC_EXTRALEAN 1
39 #include <windows.h>
40 #include <cassert>
41 #include <process.h>
42 #include <psapi.h>
43 #include <cstdio>
44 #include <cstdint>
45
46 //
47 // This file contains the Window-OS-specific functions
48 //
49
50 #if !(defined(_WIN32) || defined(_WIN64))
51 #error Trying to build a windows specific file in a non windows build.
52 #endif
53
54 namespace glslang {
55
ToGenericTLSIndex(DWORD handle)56 inline OS_TLSIndex ToGenericTLSIndex (DWORD handle)
57 {
58 return (OS_TLSIndex)((uintptr_t)handle + 1);
59 }
60
ToNativeTLSIndex(OS_TLSIndex nIndex)61 inline DWORD ToNativeTLSIndex (OS_TLSIndex nIndex)
62 {
63 return (DWORD)((uintptr_t)nIndex - 1);
64 }
65
66 //
67 // Thread Local Storage Operations
68 //
OS_AllocTLSIndex()69 OS_TLSIndex OS_AllocTLSIndex()
70 {
71 DWORD dwIndex = TlsAlloc();
72 if (dwIndex == TLS_OUT_OF_INDEXES) {
73 assert(0 && "OS_AllocTLSIndex(): Unable to allocate Thread Local Storage");
74 return OS_INVALID_TLS_INDEX;
75 }
76
77 return ToGenericTLSIndex(dwIndex);
78 }
79
OS_SetTLSValue(OS_TLSIndex nIndex,void * lpvValue)80 bool OS_SetTLSValue(OS_TLSIndex nIndex, void *lpvValue)
81 {
82 if (nIndex == OS_INVALID_TLS_INDEX) {
83 assert(0 && "OS_SetTLSValue(): Invalid TLS Index");
84 return false;
85 }
86
87 if (TlsSetValue(ToNativeTLSIndex(nIndex), lpvValue))
88 return true;
89 else
90 return false;
91 }
92
OS_GetTLSValue(OS_TLSIndex nIndex)93 void* OS_GetTLSValue(OS_TLSIndex nIndex)
94 {
95 assert(nIndex != OS_INVALID_TLS_INDEX);
96 return TlsGetValue(ToNativeTLSIndex(nIndex));
97 }
98
OS_FreeTLSIndex(OS_TLSIndex nIndex)99 bool OS_FreeTLSIndex(OS_TLSIndex nIndex)
100 {
101 if (nIndex == OS_INVALID_TLS_INDEX) {
102 assert(0 && "OS_SetTLSValue(): Invalid TLS Index");
103 return false;
104 }
105
106 if (TlsFree(ToNativeTLSIndex(nIndex)))
107 return true;
108 else
109 return false;
110 }
111
112 HANDLE GlobalLock;
113
InitGlobalLock()114 void InitGlobalLock()
115 {
116 GlobalLock = CreateMutex(0, false, 0);
117 }
118
GetGlobalLock()119 void GetGlobalLock()
120 {
121 WaitForSingleObject(GlobalLock, INFINITE);
122 }
123
ReleaseGlobalLock()124 void ReleaseGlobalLock()
125 {
126 ReleaseMutex(GlobalLock);
127 }
128
EnterGenericThread(void * entry)129 unsigned int __stdcall EnterGenericThread (void* entry)
130 {
131 return ((TThreadEntrypoint)entry)(0);
132 }
133
134 //#define DUMP_COUNTERS
135
OS_DumpMemoryCounters()136 void OS_DumpMemoryCounters()
137 {
138 #ifdef DUMP_COUNTERS
139 PROCESS_MEMORY_COUNTERS counters;
140 GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters));
141 printf("Working set size: %d\n", counters.WorkingSetSize);
142 #else
143 printf("Recompile with DUMP_COUNTERS defined to see counters.\n");
144 #endif
145 }
146
147 } // namespace glslang
148