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