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