Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2015 LunarG, Inc. |
| 5 | * Copyright 2014 Valve Software |
| 6 | * All Rights Reserved. |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included |
| 16 | * in all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | * Authors: |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 27 | * Jon Ashburn <jon@luanrg.com> |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 28 | * Ian Elliott <ian@lunarg.com> |
| 29 | */ |
| 30 | |
| 31 | #ifndef LOADER_PLATFORM_H |
| 32 | #define LOADER_PLATFORM_H |
| 33 | |
| 34 | #if defined(__linux__) |
| 35 | /* Linux-specific common code: */ |
| 36 | |
| 37 | // Headers: |
| 38 | //#define _GNU_SOURCE 1 |
| 39 | // TBD: Are the contents of the following file used? |
| 40 | #include <unistd.h> |
| 41 | // Note: The following file is for dynamic loading: |
| 42 | #include <dlfcn.h> |
| 43 | #include <pthread.h> |
| 44 | #include <assert.h> |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 45 | #include <stdbool.h> |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 46 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 47 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 48 | #define PATH_SEPERATOR ':' |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 49 | #define DIRECTORY_SYMBOL '/' |
| 50 | |
| 51 | // TODO: Need to handle different Linux distros |
| 52 | #define DEFAULT_VK_DRIVERS_INFO "/usr/share/vulkan/icd.d:/etc/vulkan/icd.d" |
| 53 | #define DEFAULT_VK_DRIVERS_PATH "/usr/lib/i386-linux-gnu/vulkan/icd:/usr/lib/x86_64-linux-gnu/vulkan/icd" |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 54 | #define LAYERS_PATH_ENV "LIBVK_LAYERS_PATH" |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 55 | #define VK_LAYER_LIBRARY_PREFIX "libVKLayer" |
| 56 | #define VK_LAYER_LIBRARY_PREFIX_LEN 10 |
| 57 | #define VK_LIBRARY_SUFFIX ".so" |
| 58 | #define VK_LIBRARY_SUFFIX_LEN 3 |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 59 | #ifndef DEFAULT_VK_LAYERS_PATH |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 60 | // TODO: Are these good default locations? |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 61 | #define DEFAULT_VK_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/vk:/usr/lib/x86_64-linux-gnu/vk" |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
Ian Elliott | ecb60ec | 2015-02-12 16:44:56 -0700 | [diff] [blame] | 64 | // C99: |
Tobin Ehlis | bf0146e | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 65 | #define PRINTF_SIZE_T_SPECIFIER "%zu" |
| 66 | |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 67 | // File IO |
| 68 | static inline bool loader_platform_file_exists(const char *path) |
| 69 | { |
| 70 | if (access(path, F_OK)) |
| 71 | return false; |
| 72 | else |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | // Dynamic Loading of libraries: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 77 | typedef void * loader_platform_dl_handle; |
| 78 | static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath) |
| 79 | { |
Chia-I Wu | 7397c43 | 2015-02-18 14:39:54 -0700 | [diff] [blame] | 80 | return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 81 | } |
| 82 | static inline char * loader_platform_open_library_error(const char* libPath) |
| 83 | { |
| 84 | return dlerror(); |
| 85 | } |
| 86 | static inline void loader_platform_close_library(loader_platform_dl_handle library) |
| 87 | { |
| 88 | dlclose(library); |
| 89 | } |
| 90 | static inline void * loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 91 | const char *name) |
| 92 | { |
| 93 | assert(library); |
| 94 | assert(name); |
| 95 | return dlsym(library, name); |
| 96 | } |
| 97 | static inline char * loader_platform_get_proc_address_error(const char *name) |
| 98 | { |
| 99 | return dlerror(); |
| 100 | } |
| 101 | |
| 102 | // Threads: |
| 103 | typedef pthread_t loader_platform_thread; |
| 104 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
| 105 | pthread_once_t var = PTHREAD_ONCE_INIT; |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 106 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \ |
| 107 | pthread_once_t var; |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 108 | static inline void loader_platform_thread_once(void *ctl, void (* func) (void)) |
| 109 | { |
| 110 | assert(func != NULL); |
| 111 | assert(ctl != NULL); |
| 112 | pthread_once((pthread_once_t *) ctl, func); |
| 113 | } |
| 114 | |
| 115 | // Thread IDs: |
| 116 | typedef pthread_t loader_platform_thread_id; |
| 117 | static inline loader_platform_thread_id loader_platform_get_thread_id() |
| 118 | { |
| 119 | return pthread_self(); |
| 120 | } |
| 121 | |
| 122 | // Thread mutex: |
| 123 | typedef pthread_mutex_t loader_platform_thread_mutex; |
| 124 | static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex) |
| 125 | { |
| 126 | pthread_mutex_init(pMutex, NULL); |
| 127 | } |
| 128 | static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex) |
| 129 | { |
| 130 | pthread_mutex_lock(pMutex); |
| 131 | } |
| 132 | static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex) |
| 133 | { |
| 134 | pthread_mutex_unlock(pMutex); |
| 135 | } |
| 136 | static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex) |
| 137 | { |
| 138 | pthread_mutex_destroy(pMutex); |
| 139 | } |
Mike Stroyan | 354ed67 | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 140 | typedef pthread_cond_t loader_platform_thread_cond; |
| 141 | static inline void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond) |
| 142 | { |
| 143 | pthread_cond_init(pCond, NULL); |
| 144 | } |
| 145 | static inline void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex) |
| 146 | { |
| 147 | pthread_cond_wait(pCond, pMutex); |
| 148 | } |
| 149 | static inline void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond) |
| 150 | { |
| 151 | pthread_cond_broadcast(pCond); |
| 152 | } |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 153 | |
Courtney Goeltzenleuchter | b620ace | 2015-07-05 11:28:29 -0600 | [diff] [blame] | 154 | #define loader_stack_alloc(size) alloca(size) |
| 155 | static inline void *loader_aligned_alloc(size_t alignment, size_t size) { return aligned_alloc(alignment, size); } |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 156 | |
| 157 | #elif defined(_WIN32) // defined(__linux__) |
| 158 | /* Windows-specific common code: */ |
| 159 | |
| 160 | // Headers: |
Piers Daniell | e2bca48 | 2015-02-24 13:58:47 -0700 | [diff] [blame] | 161 | #include <WinSock2.h> |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 162 | #include <windows.h> |
| 163 | #include <assert.h> |
Tony Barbour | 6969851 | 2015-06-18 16:29:32 -0600 | [diff] [blame] | 164 | #include <stdio.h> |
Jon Ashburn | ee33ae7 | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 165 | #include <io.h> |
| 166 | #include <stdbool.h> |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 167 | #ifdef __cplusplus |
| 168 | #include <iostream> |
| 169 | #include <string> |
| 170 | using namespace std; |
| 171 | #endif // __cplusplus |
| 172 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 173 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 174 | #define PATH_SEPERATOR ';' |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 175 | #define DIRECTORY_SYMBOL '\\' |
Jon Ashburn | ee33ae7 | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 176 | #define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE |
| 177 | #define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\Vulkan\\Drivers" |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 178 | // TODO: Are these the correct paths |
scygan | 8420b4c | 2015-06-01 19:47:08 +0200 | [diff] [blame] | 179 | #define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64" |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 180 | // TODO/TBD: Is this an appropriate prefix for Windows? |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 181 | #define LAYERS_PATH_REGISTRY_VALUE "VK_LAYERS_PATH" |
| 182 | #define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES" |
| 183 | #define LAYERS_PATH_ENV "VK_LAYERS_PATH" |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 184 | // TODO/TBD: Is this an appropriate suffix for Windows? |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 185 | #define VK_LAYER_LIBRARY_PREFIX "VKLayer" |
Jon Ashburn | b67859d | 2015-04-24 14:10:50 -0700 | [diff] [blame] | 186 | #define VK_LAYER_LIBRARY_PREFIX_LEN 7 |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 187 | #define VK_LIBRARY_SUFFIX ".dll" |
| 188 | #define VK_LIBRARY_SUFFIX_LEN 4 |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 189 | #ifndef DEFAULT_VK_LAYERS_PATH |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 190 | // TODO: Is this a good default location? |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 191 | #define DEFAULT_VK_LAYERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64" |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 192 | #endif // DEFAULT_VK_LAYERS_PATH |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 193 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 194 | // C99: |
| 195 | // Microsoft didn't implement C99 in Visual Studio; but started adding it with |
| 196 | // VS2013. However, VS2013 still didn't have snprintf(). The following is a |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 197 | // work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the |
| 198 | // "CMakeLists.txt" file). |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 199 | #define snprintf _snprintf |
Tobin Ehlis | bf0146e | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 200 | #define PRINTF_SIZE_T_SPECIFIER "%Iu" |
David Pinedo | 3053c67 | 2015-07-02 09:04:37 -0600 | [diff] [blame] | 201 | |
| 202 | // Microsoft doesn't implement alloca, instead we have _alloca |
| 203 | #define alloca _alloca |
| 204 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 205 | // Microsoft also doesn't have basename(). Paths are different on Windows, and |
| 206 | // so this is just a temporary solution in order to get us compiling, so that we |
| 207 | // can test some scenarios, and develop the correct solution for Windows. |
| 208 | // TODO: Develop a better, permanent solution for Windows, to replace this |
| 209 | // temporary code: |
| 210 | static char *basename(char *pathname) |
| 211 | { |
| 212 | char *current, *next; |
| 213 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 214 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
| 215 | |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 216 | #define DIRECTORY_SYMBOL_CHAR '\\' |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 217 | for (current = pathname; *current != '\0'; current = next) { |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 218 | next = strchr(current, DIRECTORY_SYMBOL_CHAR); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 219 | if (next == NULL) { |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 220 | // No more DIRECTORY_SYMBOL_CHAR's so return p: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 221 | return current; |
| 222 | } else { |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 223 | // Point one character past the DIRECTORY_SYMBOL_CHAR: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 224 | next++; |
| 225 | } |
| 226 | } |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 227 | // We shouldn't get to here, but this makes the compiler happy: |
| 228 | return current; |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 231 | // File IO |
Jon Ashburn | ee33ae7 | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 232 | static bool loader_platform_file_exists(const char *path) |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 233 | { |
Jon Ashburn | ee33ae7 | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 234 | if ((_access(path, 0)) == -1) |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 235 | return false; |
| 236 | else |
| 237 | return true; |
| 238 | } |
| 239 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 240 | // Dynamic Loading: |
| 241 | typedef HMODULE loader_platform_dl_handle; |
| 242 | static loader_platform_dl_handle loader_platform_open_library(const char* libPath) |
| 243 | { |
| 244 | return LoadLibrary(libPath); |
| 245 | } |
| 246 | static char * loader_platform_open_library_error(const char* libPath) |
| 247 | { |
| 248 | static char errorMsg[120]; |
| 249 | snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath); |
| 250 | return errorMsg; |
| 251 | } |
| 252 | static void loader_platform_close_library(loader_platform_dl_handle library) |
| 253 | { |
| 254 | FreeLibrary(library); |
| 255 | } |
| 256 | static void * loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 257 | const char *name) |
| 258 | { |
| 259 | assert(library); |
| 260 | assert(name); |
| 261 | return GetProcAddress(library, name); |
| 262 | } |
| 263 | static char * loader_platform_get_proc_address_error(const char *name) |
| 264 | { |
| 265 | static char errorMsg[120]; |
| 266 | snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name); |
| 267 | return errorMsg; |
| 268 | } |
| 269 | |
| 270 | // Threads: |
| 271 | typedef HANDLE loader_platform_thread; |
| 272 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
| 273 | INIT_ONCE var = INIT_ONCE_STATIC_INIT; |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 274 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \ |
| 275 | INIT_ONCE var; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 276 | static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) |
| 277 | { |
| 278 | void (*func)(void) = (void (*)(void))Parameter; |
| 279 | func(); |
| 280 | return TRUE; |
| 281 | } |
| 282 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 283 | static void loader_platform_thread_once(void *ctl, void (* func) (void)) |
| 284 | { |
| 285 | assert(func != NULL); |
| 286 | assert(ctl != NULL); |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 287 | InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Thread IDs: |
| 291 | typedef DWORD loader_platform_thread_id; |
| 292 | static loader_platform_thread_id loader_platform_get_thread_id() |
| 293 | { |
| 294 | return GetCurrentThreadId(); |
| 295 | } |
| 296 | |
| 297 | // Thread mutex: |
| 298 | typedef CRITICAL_SECTION loader_platform_thread_mutex; |
| 299 | static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex) |
| 300 | { |
| 301 | InitializeCriticalSection(pMutex); |
| 302 | } |
| 303 | static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex) |
| 304 | { |
| 305 | EnterCriticalSection(pMutex); |
| 306 | } |
| 307 | static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex) |
| 308 | { |
| 309 | LeaveCriticalSection(pMutex); |
| 310 | } |
| 311 | static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex) |
| 312 | { |
| 313 | DeleteCriticalSection(pMutex); |
| 314 | } |
Mike Stroyan | 33053d0 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 315 | typedef CONDITION_VARIABLE loader_platform_thread_cond; |
| 316 | static void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond) |
| 317 | { |
| 318 | InitializeConditionVariable(pCond); |
| 319 | } |
| 320 | static void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex) |
| 321 | { |
| 322 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 323 | } |
| 324 | static void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond) |
| 325 | { |
| 326 | WakeAllConditionVariable(pCond); |
| 327 | } |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 328 | |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 329 | // Windows Registry: |
| 330 | char *loader_get_registry_string(const HKEY hive, |
| 331 | const LPCTSTR sub_key, |
| 332 | const char *value); |
| 333 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 334 | #else // defined(_WIN32) |
| 335 | |
| 336 | #error The "loader_platform.h" file must be modified for this OS. |
| 337 | |
| 338 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 339 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 340 | // contents of this file must be created. |
| 341 | |
| 342 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 343 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 344 | |
| 345 | #endif // defined(_WIN32) |
| 346 | |
Ian Elliott | 20f0687 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 347 | #else /* LOADER_PLATFORM_H */ |
| 348 | #ifndef LOADER_PLATFORM_H_TEMP |
| 349 | #define LOADER_PLATFORM_H_TEMP |
| 350 | |
| 351 | // NOTE: The following are hopefully-temporary macros to ensure that people |
| 352 | // don't forget to use the loader_platform_*() functions above: |
| 353 | |
| 354 | #if defined(__linux__) |
| 355 | /* Linux-specific common code: */ |
| 356 | |
| 357 | // Dynamic Loading: |
| 358 | #define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION |
| 359 | #define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY |
| 360 | #define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION |
| 361 | #define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION |
| 362 | |
| 363 | // Threads: |
| 364 | #define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION |
| 365 | #define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION |
| 366 | |
| 367 | // Thread mutex: |
| 368 | #define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION |
| 369 | #define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION |
| 370 | #define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION |
| 371 | #define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION |
| 372 | |
| 373 | |
| 374 | #elif defined(_WIN32) // defined(__linux__) |
| 375 | /* Windows-specific common code: */ |
| 376 | |
| 377 | // Dynamic Loading: |
| 378 | //#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION |
| 379 | #define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION |
| 380 | #define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION |
| 381 | |
| 382 | // Threads: |
| 383 | #define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION |
| 384 | #define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION |
| 385 | |
| 386 | // Thread mutex: |
| 387 | #define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION |
| 388 | #define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION |
| 389 | #define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION |
| 390 | #define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION |
| 391 | |
Courtney Goeltzenleuchter | b620ace | 2015-07-05 11:28:29 -0600 | [diff] [blame] | 392 | #define loader_stack_alloc(size) _alloca(size) |
| 393 | static inline void *loader_aligned_alloc(size_t alignment, size_t size) { return _aligned_alloc(alignment, size); } |
Ian Elliott | 20f0687 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 394 | |
| 395 | #endif // defined(_WIN32) |
| 396 | #endif /* LOADER_PLATFORM_H_TEMP */ |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 397 | #endif /* LOADER_PLATFORM_H */ |