Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Ian Elliott | 2d4ab1e | 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 | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 27 | * Jon Ashburn <jon@luanrg.com> |
Ian Elliott | 2d4ab1e | 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 | |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 34 | #include "vk_platform.h" |
| 35 | |
Ian Elliott | 2d4ab1e | 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 | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 47 | #include <stdbool.h> |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 48 | #include <libgen.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 49 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 50 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 51 | #define PATH_SEPERATOR ':' |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 52 | #define DIRECTORY_SYMBOL '/' |
| 53 | |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 54 | #define VULKAN_ICDCONF_DIR "/" "vulkan" "/" "icd.d" |
| 55 | #define VULKAN_ICD_DIR "/" "vulkan" "/" "icd" |
James Jones | 34c500b | 2015-07-24 11:35:16 -0700 | [diff] [blame] | 56 | #define VULKAN_ELAYERCONF_DIR "/" "vulkan" "/" "explicit_layer.d" |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 57 | #define VULKAN_ILAYERCONF_DIR "/" "vulkan" "/" "implicit_layer.d" |
| 58 | #define VULKAN_LAYER_DIR "/" "vulkan" "/" "layer" |
| 59 | |
| 60 | #if defined(LOCALPREFIX) |
| 61 | #define LOCAL_DRIVERS_INFO \ |
| 62 | LOCALPREFIX "/" SYSCONFDIR VULKAN_ICDCONF_DIR ":" \ |
| 63 | LOCALPREFIX "/" DATADIR VULKAN_ICDCONF_DIR ":" |
| 64 | #define LOCAL_DRIVERS_PATH \ |
| 65 | LOCALPREFIX "/" LIBDIR VULKAN_ICD_DIR ":" |
| 66 | #define LOCAL_LAYERS_INFO \ |
| 67 | LOCALPREFIX "/" SYSCONFDIR VULKAN_ELAYERCONF_DIR ":" \ |
| 68 | LOCALPREFIX "/" SYSCONFDIR VULKAN_ILAYERCONF_DIR ":" \ |
| 69 | LOCALPREFIX "/" DATADIR VULKAN_ELAYERCONF_DIR ":" \ |
| 70 | LOCALPREFIX "/" DATADIR VULKAN_ILAYERCONF_DIR ":" |
| 71 | #define LOCAL_LAYERS_PATH \ |
| 72 | LOCALPREFIX "/" LIBDIR VULKAN_LAYER_DIR ":" |
| 73 | #else |
| 74 | #define LOCAL_DRIVERS_INFO |
| 75 | #define LOCAL_DRIVERS_PATH |
| 76 | #define LOCAL_LAYERS_INFO |
| 77 | #define LOCAL_LAYERS_PATH |
| 78 | #endif |
| 79 | |
Adam Jackson | 3869f41 | 2015-07-23 16:29:26 -0400 | [diff] [blame] | 80 | #define DEFAULT_VK_DRIVERS_INFO \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 81 | LOCAL_DRIVERS_INFO \ |
James Jones | 09a436f | 2015-07-24 09:47:37 -0700 | [diff] [blame] | 82 | "/" SYSCONFDIR VULKAN_ICDCONF_DIR ":" \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 83 | "/usr/" DATADIR VULKAN_ICDCONF_DIR |
Adam Jackson | 3869f41 | 2015-07-23 16:29:26 -0400 | [diff] [blame] | 84 | #define DEFAULT_VK_DRIVERS_PATH \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 85 | LOCAL_DRIVERS_PATH \ |
| 86 | "/usr/" LIBDIR VULKAN_ICD_DIR |
Adam Jackson | 3869f41 | 2015-07-23 16:29:26 -0400 | [diff] [blame] | 87 | #define DEFAULT_VK_LAYERS_INFO \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 88 | LOCAL_LAYERS_INFO \ |
James Jones | 09a436f | 2015-07-24 09:47:37 -0700 | [diff] [blame] | 89 | "/" SYSCONFDIR VULKAN_ELAYERCONF_DIR ":" \ |
| 90 | "/" SYSCONFDIR VULKAN_ILAYERCONF_DIR ":" \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 91 | "/usr/" DATADIR VULKAN_ELAYERCONF_DIR ":" \ |
| 92 | "/usr/" DATADIR VULKAN_ILAYERCONF_DIR |
Adam Jackson | 3869f41 | 2015-07-23 16:29:26 -0400 | [diff] [blame] | 93 | #define DEFAULT_VK_LAYERS_PATH \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 94 | LOCAL_LAYERS_PATH \ |
| 95 | "/usr/" LIBDIR VULKAN_LAYER_DIR |
Jon Ashburn | 2d0c4bb | 2015-07-06 15:40:35 -0600 | [diff] [blame] | 96 | #define LAYERS_PATH_ENV "VK_LAYER_DIRS" |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 97 | |
Ian Elliott | e536946 | 2015-02-12 16:44:56 -0700 | [diff] [blame] | 98 | // C99: |
Tobin Ehlis | b049785 | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 99 | #define PRINTF_SIZE_T_SPECIFIER "%zu" |
| 100 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 101 | // File IO |
| 102 | static inline bool loader_platform_file_exists(const char *path) |
| 103 | { |
| 104 | if (access(path, F_OK)) |
| 105 | return false; |
| 106 | else |
| 107 | return true; |
| 108 | } |
| 109 | |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 110 | static inline bool loader_platform_is_path_absolute(const char *path) |
| 111 | { |
| 112 | if (path[0] == '/') |
| 113 | return true; |
| 114 | else |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | static inline char *loader_platform_dirname(char *path) |
| 119 | { |
| 120 | return dirname(path); |
| 121 | } |
| 122 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 123 | // Dynamic Loading of libraries: |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 124 | typedef void * loader_platform_dl_handle; |
| 125 | static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath) |
| 126 | { |
Chia-I Wu | 6a21834 | 2015-02-18 14:39:54 -0700 | [diff] [blame] | 127 | return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 128 | } |
| 129 | static inline char * loader_platform_open_library_error(const char* libPath) |
| 130 | { |
| 131 | return dlerror(); |
| 132 | } |
| 133 | static inline void loader_platform_close_library(loader_platform_dl_handle library) |
| 134 | { |
| 135 | dlclose(library); |
| 136 | } |
| 137 | static inline void * loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 138 | const char *name) |
| 139 | { |
| 140 | assert(library); |
| 141 | assert(name); |
| 142 | return dlsym(library, name); |
| 143 | } |
| 144 | static inline char * loader_platform_get_proc_address_error(const char *name) |
| 145 | { |
| 146 | return dlerror(); |
| 147 | } |
| 148 | |
| 149 | // Threads: |
| 150 | typedef pthread_t loader_platform_thread; |
| 151 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
| 152 | pthread_once_t var = PTHREAD_ONCE_INIT; |
Jon Ashburn | 27cd584 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 153 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \ |
| 154 | pthread_once_t var; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 155 | static inline void loader_platform_thread_once(void *ctl, void (* func) (void)) |
| 156 | { |
| 157 | assert(func != NULL); |
| 158 | assert(ctl != NULL); |
| 159 | pthread_once((pthread_once_t *) ctl, func); |
| 160 | } |
| 161 | |
| 162 | // Thread IDs: |
| 163 | typedef pthread_t loader_platform_thread_id; |
| 164 | static inline loader_platform_thread_id loader_platform_get_thread_id() |
| 165 | { |
| 166 | return pthread_self(); |
| 167 | } |
| 168 | |
| 169 | // Thread mutex: |
| 170 | typedef pthread_mutex_t loader_platform_thread_mutex; |
| 171 | static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex) |
| 172 | { |
| 173 | pthread_mutex_init(pMutex, NULL); |
| 174 | } |
| 175 | static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex) |
| 176 | { |
| 177 | pthread_mutex_lock(pMutex); |
| 178 | } |
| 179 | static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex) |
| 180 | { |
| 181 | pthread_mutex_unlock(pMutex); |
| 182 | } |
| 183 | static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex) |
| 184 | { |
| 185 | pthread_mutex_destroy(pMutex); |
| 186 | } |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 187 | typedef pthread_cond_t loader_platform_thread_cond; |
| 188 | static inline void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond) |
| 189 | { |
| 190 | pthread_cond_init(pCond, NULL); |
| 191 | } |
| 192 | static inline void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex) |
| 193 | { |
| 194 | pthread_cond_wait(pCond, pMutex); |
| 195 | } |
| 196 | static inline void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond) |
| 197 | { |
| 198 | pthread_cond_broadcast(pCond); |
| 199 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 200 | |
Courtney Goeltzenleuchter | 7f5aafc | 2015-07-05 11:28:29 -0600 | [diff] [blame] | 201 | #define loader_stack_alloc(size) alloca(size) |
| 202 | static inline void *loader_aligned_alloc(size_t alignment, size_t size) { return aligned_alloc(alignment, size); } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 203 | |
| 204 | #elif defined(_WIN32) // defined(__linux__) |
| 205 | /* Windows-specific common code: */ |
| 206 | |
| 207 | // Headers: |
Piers Daniell | 188f820 | 2015-02-24 13:58:47 -0700 | [diff] [blame] | 208 | #include <WinSock2.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 209 | #include <windows.h> |
| 210 | #include <assert.h> |
Tony Barbour | 1d825c7 | 2015-06-18 16:29:32 -0600 | [diff] [blame] | 211 | #include <stdio.h> |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 212 | #include <io.h> |
| 213 | #include <stdbool.h> |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 214 | #include <shlwapi.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 215 | #ifdef __cplusplus |
| 216 | #include <iostream> |
| 217 | #include <string> |
| 218 | using namespace std; |
| 219 | #endif // __cplusplus |
| 220 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 221 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 222 | #define PATH_SEPERATOR ';' |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 223 | #define DIRECTORY_SYMBOL '\\' |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 224 | #define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE |
| 225 | #define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\Vulkan\\Drivers" |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 226 | // TODO: Are these the correct paths |
scygan | 713b1b0 | 2015-06-01 19:47:08 +0200 | [diff] [blame] | 227 | #define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64" |
Jon Ashburn | 2d0c4bb | 2015-07-06 15:40:35 -0600 | [diff] [blame] | 228 | #define DEFAULT_VK_LAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ExplicitLayers;SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers" |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 229 | #define DEFAULT_VK_LAYERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64" |
Jon Ashburn | bb23456 | 2015-07-23 18:58:21 -0600 | [diff] [blame] | 230 | #define LAYERS_PATH_ENV "VK_LAYER_FOLDERS" |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 231 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 232 | // C99: |
| 233 | // Microsoft didn't implement C99 in Visual Studio; but started adding it with |
| 234 | // VS2013. However, VS2013 still didn't have snprintf(). The following is a |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 235 | // work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the |
| 236 | // "CMakeLists.txt" file). |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 237 | #define snprintf _snprintf |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 238 | #define strdup _strdup |
Tobin Ehlis | b049785 | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 239 | #define PRINTF_SIZE_T_SPECIFIER "%Iu" |
David Pinedo | a01c13a | 2015-07-02 09:04:37 -0600 | [diff] [blame] | 240 | |
David Pinedo | a01c13a | 2015-07-02 09:04:37 -0600 | [diff] [blame] | 241 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 242 | // File IO |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 243 | static bool loader_platform_file_exists(const char *path) |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 244 | { |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 245 | if ((_access(path, 0)) == -1) |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 246 | return false; |
| 247 | else |
| 248 | return true; |
| 249 | } |
| 250 | |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 251 | static bool loader_platform_is_path_absolute(const char *path) |
Jon Ashburn | 1531517 | 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 | 2d4ab1e | 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; |
| 335 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
| 336 | INIT_ONCE var = INIT_ONCE_STATIC_INIT; |
Jon Ashburn | 27cd584 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 337 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \ |
| 338 | INIT_ONCE var; |
Piers Daniell | 4da523a | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 339 | static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) |
| 340 | { |
| 341 | void (*func)(void) = (void (*)(void))Parameter; |
| 342 | func(); |
| 343 | return TRUE; |
| 344 | } |
| 345 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 346 | static void loader_platform_thread_once(void *ctl, void (* func) (void)) |
| 347 | { |
| 348 | assert(func != NULL); |
| 349 | assert(ctl != NULL); |
Piers Daniell | 4da523a | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 350 | InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // Thread IDs: |
| 354 | typedef DWORD loader_platform_thread_id; |
| 355 | static loader_platform_thread_id loader_platform_get_thread_id() |
| 356 | { |
| 357 | return GetCurrentThreadId(); |
| 358 | } |
| 359 | |
| 360 | // Thread mutex: |
| 361 | typedef CRITICAL_SECTION loader_platform_thread_mutex; |
| 362 | static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex) |
| 363 | { |
| 364 | InitializeCriticalSection(pMutex); |
| 365 | } |
| 366 | static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex) |
| 367 | { |
| 368 | EnterCriticalSection(pMutex); |
| 369 | } |
| 370 | static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex) |
| 371 | { |
| 372 | LeaveCriticalSection(pMutex); |
| 373 | } |
| 374 | static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex) |
| 375 | { |
| 376 | DeleteCriticalSection(pMutex); |
| 377 | } |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 378 | typedef CONDITION_VARIABLE loader_platform_thread_cond; |
| 379 | static void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond) |
| 380 | { |
| 381 | InitializeConditionVariable(pCond); |
| 382 | } |
| 383 | static void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex) |
| 384 | { |
| 385 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 386 | } |
| 387 | static void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond) |
| 388 | { |
| 389 | WakeAllConditionVariable(pCond); |
| 390 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 391 | |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 392 | // Windows Registry: |
| 393 | char *loader_get_registry_string(const HKEY hive, |
| 394 | const LPCTSTR sub_key, |
| 395 | const char *value); |
| 396 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 397 | #else // defined(_WIN32) |
| 398 | |
| 399 | #error The "loader_platform.h" file must be modified for this OS. |
| 400 | |
| 401 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 402 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 403 | // contents of this file must be created. |
| 404 | |
| 405 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 406 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 407 | |
| 408 | #endif // defined(_WIN32) |
| 409 | |
Ian Elliott | 655cad7 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 410 | #else /* LOADER_PLATFORM_H */ |
| 411 | #ifndef LOADER_PLATFORM_H_TEMP |
| 412 | #define LOADER_PLATFORM_H_TEMP |
| 413 | |
| 414 | // NOTE: The following are hopefully-temporary macros to ensure that people |
| 415 | // don't forget to use the loader_platform_*() functions above: |
| 416 | |
| 417 | #if defined(__linux__) |
| 418 | /* Linux-specific common code: */ |
| 419 | |
| 420 | // Dynamic Loading: |
| 421 | #define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION |
| 422 | #define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY |
| 423 | #define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION |
| 424 | #define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION |
| 425 | |
| 426 | // Threads: |
| 427 | #define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION |
| 428 | #define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION |
| 429 | |
| 430 | // Thread mutex: |
| 431 | #define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION |
| 432 | #define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION |
| 433 | #define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION |
| 434 | #define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION |
| 435 | |
| 436 | |
| 437 | #elif defined(_WIN32) // defined(__linux__) |
| 438 | /* Windows-specific common code: */ |
| 439 | |
| 440 | // Dynamic Loading: |
| 441 | //#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION |
| 442 | #define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION |
| 443 | #define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION |
| 444 | |
| 445 | // Threads: |
| 446 | #define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION |
| 447 | #define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION |
| 448 | |
| 449 | // Thread mutex: |
| 450 | #define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION |
| 451 | #define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION |
| 452 | #define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION |
| 453 | #define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION |
| 454 | |
Courtney Goeltzenleuchter | 7f5aafc | 2015-07-05 11:28:29 -0600 | [diff] [blame] | 455 | #define loader_stack_alloc(size) _alloca(size) |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 456 | static inline void *loader_aligned_alloc(size_t alignment, size_t size) { return _aligned_malloc(alignment, size); } |
Ian Elliott | 655cad7 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 457 | |
| 458 | #endif // defined(_WIN32) |
| 459 | #endif /* LOADER_PLATFORM_H_TEMP */ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 460 | #endif /* LOADER_PLATFORM_H */ |