blob: fd3130900adc7fd05b058611d9777a51c76c5ecf [file] [log] [blame]
Ian Elliott81ac44c2015-01-13 17:52:38 -07001/*
Ian Elliott81ac44c2015-01-13 17:52:38 -07002 *
Jon Ashburn44aed662016-02-02 17:47:28 -07003 * Copyright (c) 2015-2016 The Khronos Group Inc.
4 * Copyright (c) 2015-2016 Valve Corporation
5 * Copyright (c) 2015-2016 LunarG, Inc.
Ian Elliott81ac44c2015-01-13 17:52:38 -07006 *
Jon Ashburn44aed662016-02-02 17:47:28 -07007 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and/or associated documentation files (the "Materials"), to
9 * deal in the Materials without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Materials, and to permit persons to whom the Materials are
12 * furnished to do so, subject to the following conditions:
Ian Elliott81ac44c2015-01-13 17:52:38 -070013 *
Jon Ashburn44aed662016-02-02 17:47:28 -070014 * The above copyright notice(s) and this permission notice shall be included in
15 * all copies or substantial portions of the Materials.
Ian Elliott81ac44c2015-01-13 17:52:38 -070016 *
Jon Ashburn44aed662016-02-02 17:47:28 -070017 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Ian Elliott81ac44c2015-01-13 17:52:38 -070018 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jon Ashburn44aed662016-02-02 17:47:28 -070019 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Ian Elliott81ac44c2015-01-13 17:52:38 -070020 *
Jon Ashburn44aed662016-02-02 17:47:28 -070021 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
24 * USE OR OTHER DEALINGS IN THE MATERIALS.
25 *
26 * Author: Ian Elliot <ian@lunarg.com>
27 * Author: Jon Ashburn <jon@lunarg.com>
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060028 *
Ian Elliott81ac44c2015-01-13 17:52:38 -070029 */
Jon Ashburnf6620c92015-10-15 13:47:58 -060030#pragma once
Ian Elliott81ac44c2015-01-13 17:52:38 -070031
Mark Lobodzinski051ae4b2015-11-24 12:04:15 -070032#if defined(_WIN32)
33// WinSock2.h must be included *BEFORE* windows.h
Mark Lobodzinski051ae4b2015-11-24 12:04:15 -070034#include <WinSock2.h>
35#endif // _WIN32
36
David Pinedo329ca9e2015-11-06 12:54:48 -070037#include "vulkan/vk_platform.h"
38#include "vulkan/vk_sdk_platform.h"
Cody Northrop62ac1c52015-07-08 16:48:37 -060039
Ian Elliott81ac44c2015-01-13 17:52:38 -070040#if defined(__linux__)
41/* Linux-specific common code: */
42
43// Headers:
44//#define _GNU_SOURCE 1
45// TBD: Are the contents of the following file used?
46#include <unistd.h>
47// Note: The following file is for dynamic loading:
48#include <dlfcn.h>
49#include <pthread.h>
50#include <assert.h>
Jon Ashburn429e19f2015-11-17 15:31:02 -070051#include <string.h>
Jon Ashburnffd5d672015-06-29 11:25:34 -060052#include <stdbool.h>
Michael Lentine92689442015-09-09 12:39:13 -070053#include <stdlib.h>
Jon Ashburn38144502015-07-07 15:06:25 -060054#include <libgen.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -070055
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060056// VK Library Filenames, Paths, etc.:
Ian Elliott665c5632015-02-04 11:22:39 -070057#define PATH_SEPERATOR ':'
Jon Ashburnffd5d672015-06-29 11:25:34 -060058#define DIRECTORY_SYMBOL '/'
59
Jon Ashburn44aed662016-02-02 17:47:28 -070060#define VULKAN_ICDCONF_DIR \
61 "/" \
62 "vulkan" \
63 "/" \
64 "icd.d"
65#define VULKAN_ICD_DIR \
66 "/" \
67 "vulkan" \
68 "/" \
69 "icd"
70#define VULKAN_ELAYERCONF_DIR \
71 "/" \
72 "vulkan" \
73 "/" \
74 "explicit_layer.d"
75#define VULKAN_ILAYERCONF_DIR \
76 "/" \
77 "vulkan" \
78 "/" \
79 "implicit_layer.d"
80#define VULKAN_LAYER_DIR \
81 "/" \
82 "vulkan" \
83 "/" \
84 "layer"
James Jonesac945bc2015-07-23 17:39:37 -070085
86#if defined(LOCALPREFIX)
Jon Ashburn44aed662016-02-02 17:47:28 -070087#define LOCAL_DRIVERS_INFO \
88 LOCALPREFIX "/" SYSCONFDIR VULKAN_ICDCONF_DIR ":" LOCALPREFIX \
89 "/" DATADIR VULKAN_ICDCONF_DIR ":"
90#define LOCAL_ELAYERS_INFO \
91 LOCALPREFIX "/" SYSCONFDIR VULKAN_ELAYERCONF_DIR ":" LOCALPREFIX \
92 "/" DATADIR VULKAN_ELAYERCONF_DIR ":"
93#define LOCAL_ILAYERS_INFO \
94 LOCALPREFIX "/" SYSCONFDIR VULKAN_ILAYERCONF_DIR ":" LOCALPREFIX \
95 "/" DATADIR VULKAN_ILAYERCONF_DIR ":"
James Jonesac945bc2015-07-23 17:39:37 -070096#else
97#define LOCAL_DRIVERS_INFO
Jon Ashburnd2c905c2015-12-17 17:38:24 -070098#define LOCAL_ELAYERS_INFO
99#define LOCAL_ILAYERS_INFO
James Jonesac945bc2015-07-23 17:39:37 -0700100#endif
101
Jon Ashburn44aed662016-02-02 17:47:28 -0700102#define DEFAULT_VK_DRIVERS_INFO \
103 LOCAL_DRIVERS_INFO \
104 "/" SYSCONFDIR VULKAN_ICDCONF_DIR ":" \
James Jonesac945bc2015-07-23 17:39:37 -0700105 "/usr/" DATADIR VULKAN_ICDCONF_DIR
Jon Ashburn44aed662016-02-02 17:47:28 -0700106#define DEFAULT_VK_ELAYERS_INFO \
107 LOCAL_ELAYERS_INFO \
108 "/" SYSCONFDIR VULKAN_ELAYERCONF_DIR ":" \
Jon Ashburna867ee42016-02-16 15:34:16 -0700109 "/usr/" DATADIR VULKAN_ELAYERCONF_DIR
Jon Ashburn44aed662016-02-02 17:47:28 -0700110#define DEFAULT_VK_ILAYERS_INFO \
111 LOCAL_ILAYERS_INFO \
112 "/" SYSCONFDIR VULKAN_ILAYERCONF_DIR ":" \
James Jonesac945bc2015-07-23 17:39:37 -0700113 "/usr/" DATADIR VULKAN_ILAYERCONF_DIR
Jon Ashburn68e2bef2015-08-06 11:22:33 -0600114#define LAYERS_PATH_ENV "VK_LAYER_PATH"
Jon Ashburna867ee42016-02-16 15:34:16 -0700115#define HOME_VK_DRIVERS_INFO "/.local/share" VULKAN_ICDCONF_DIR
116#define HOME_VK_ELAYERS_INFO "/.local/share" VULKAN_ELAYERCONF_DIR
117#define HOME_VK_ILAYERS_INFO "/.local/share" VULKAN_ILAYERCONF_DIR
Ian Elliott665c5632015-02-04 11:22:39 -0700118
Ian Elliottecb60ec2015-02-12 16:44:56 -0700119// C99:
Jon Ashburn44aed662016-02-02 17:47:28 -0700120#define PRINTF_SIZE_T_SPECIFIER "%zu"
Tobin Ehlisbf0146e2015-02-11 14:24:02 -0700121
Jon Ashburnffd5d672015-06-29 11:25:34 -0600122// File IO
Jon Ashburn44aed662016-02-02 17:47:28 -0700123static inline bool loader_platform_file_exists(const char *path) {
Jon Ashburnffd5d672015-06-29 11:25:34 -0600124 if (access(path, F_OK))
125 return false;
126 else
127 return true;
128}
129
Jon Ashburn44aed662016-02-02 17:47:28 -0700130static inline bool loader_platform_is_path_absolute(const char *path) {
Jon Ashburn38144502015-07-07 15:06:25 -0600131 if (path[0] == '/')
132 return true;
133 else
134 return false;
135}
136
Jon Ashburn44aed662016-02-02 17:47:28 -0700137static inline char *loader_platform_dirname(char *path) {
Jon Ashburn38144502015-07-07 15:06:25 -0600138 return dirname(path);
139}
140
Jon Ashburn5b25d9f2016-01-04 14:01:38 -0700141// Environment variables
142
Jon Ashburn44aed662016-02-02 17:47:28 -0700143static inline char *loader_getenv(const char *name) { return getenv(name); }
Jon Ashburn5b25d9f2016-01-04 14:01:38 -0700144
Jon Ashburn44aed662016-02-02 17:47:28 -0700145static inline void loader_free_getenv(const char *val) {}
Jon Ashburn5b25d9f2016-01-04 14:01:38 -0700146
Jon Ashburnffd5d672015-06-29 11:25:34 -0600147// Dynamic Loading of libraries:
Jon Ashburn44aed662016-02-02 17:47:28 -0700148typedef void *loader_platform_dl_handle;
149static inline loader_platform_dl_handle
150loader_platform_open_library(const char *libPath) {
Chia-I Wu7397c432015-02-18 14:39:54 -0700151 return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700152}
Jon Ashburn44aed662016-02-02 17:47:28 -0700153static inline const char *
154loader_platform_open_library_error(const char *libPath) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700155 return dlerror();
156}
Jon Ashburn44aed662016-02-02 17:47:28 -0700157static inline void
158loader_platform_close_library(loader_platform_dl_handle library) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700159 dlclose(library);
160}
Jon Ashburn44aed662016-02-02 17:47:28 -0700161static inline void *
162loader_platform_get_proc_address(loader_platform_dl_handle library,
163 const char *name) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700164 assert(library);
165 assert(name);
166 return dlsym(library, name);
167}
Jon Ashburn44aed662016-02-02 17:47:28 -0700168static inline const char *
169loader_platform_get_proc_address_error(const char *name) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700170 return dlerror();
171}
172
173// Threads:
174typedef pthread_t loader_platform_thread;
Jon Ashburn413d6582015-08-28 15:19:27 -0600175#define THREAD_LOCAL_DECL __thread
Jon Ashburn44aed662016-02-02 17:47:28 -0700176#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
Ian Elliott81ac44c2015-01-13 17:52:38 -0700177 pthread_once_t var = PTHREAD_ONCE_INIT;
Jon Ashburn44aed662016-02-02 17:47:28 -0700178#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) pthread_once_t var;
179static inline void loader_platform_thread_once(pthread_once_t *ctl,
180 void (*func)(void)) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700181 assert(func != NULL);
182 assert(ctl != NULL);
Michael Lentine92689442015-09-09 12:39:13 -0700183 pthread_once(ctl, func);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700184}
185
186// Thread IDs:
187typedef pthread_t loader_platform_thread_id;
Jon Ashburn44aed662016-02-02 17:47:28 -0700188static inline loader_platform_thread_id loader_platform_get_thread_id() {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700189 return pthread_self();
190}
191
192// Thread mutex:
193typedef pthread_mutex_t loader_platform_thread_mutex;
Jon Ashburn44aed662016-02-02 17:47:28 -0700194static inline void
195loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700196 pthread_mutex_init(pMutex, NULL);
197}
Jon Ashburn44aed662016-02-02 17:47:28 -0700198static inline void
199loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700200 pthread_mutex_lock(pMutex);
201}
Jon Ashburn44aed662016-02-02 17:47:28 -0700202static inline void
203loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700204 pthread_mutex_unlock(pMutex);
205}
Jon Ashburn44aed662016-02-02 17:47:28 -0700206static inline void
207loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700208 pthread_mutex_destroy(pMutex);
209}
Mike Stroyan354ed672015-05-15 08:50:57 -0600210typedef pthread_cond_t loader_platform_thread_cond;
Jon Ashburn44aed662016-02-02 17:47:28 -0700211static inline void
212loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) {
Mike Stroyan354ed672015-05-15 08:50:57 -0600213 pthread_cond_init(pCond, NULL);
214}
Jon Ashburn44aed662016-02-02 17:47:28 -0700215static inline void
216loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond,
217 loader_platform_thread_mutex *pMutex) {
Mike Stroyan354ed672015-05-15 08:50:57 -0600218 pthread_cond_wait(pCond, pMutex);
219}
Jon Ashburn44aed662016-02-02 17:47:28 -0700220static inline void
221loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) {
Mike Stroyan354ed672015-05-15 08:50:57 -0600222 pthread_cond_broadcast(pCond);
223}
Ian Elliott81ac44c2015-01-13 17:52:38 -0700224
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -0600225#define loader_stack_alloc(size) alloca(size)
Ian Elliott81ac44c2015-01-13 17:52:38 -0700226
227#elif defined(_WIN32) // defined(__linux__)
228/* Windows-specific common code: */
Tobin Ehlisc8d75932015-11-02 15:45:19 -0700229// WinBase.h defines CreateSemaphore and synchapi.h defines CreateEvent
230// undefine them to avoid conflicts with VkLayerDispatchTable struct members.
231#ifdef CreateSemaphore
232#undef CreateSemaphore
233#endif
234#ifdef CreateEvent
235#undef CreateEvent
236#endif
Ian Elliott81ac44c2015-01-13 17:52:38 -0700237#include <assert.h>
Tony Barbour69698512015-06-18 16:29:32 -0600238#include <stdio.h>
Jon Ashburn429e19f2015-11-17 15:31:02 -0700239#include <string.h>
Jon Ashburnee33ae72015-06-30 14:46:22 -0700240#include <io.h>
241#include <stdbool.h>
Cody Northrop62ac1c52015-07-08 16:48:37 -0600242#include <shlwapi.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -0700243#ifdef __cplusplus
244#include <iostream>
245#include <string>
246using namespace std;
247#endif // __cplusplus
248
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600249// VK Library Filenames, Paths, etc.:
Ian Elliott665c5632015-02-04 11:22:39 -0700250#define PATH_SEPERATOR ';'
Jon Ashburnffd5d672015-06-29 11:25:34 -0600251#define DIRECTORY_SYMBOL '\\'
Jon Ashburnee33ae72015-06-30 14:46:22 -0700252#define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE
253#define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\Vulkan\\Drivers"
Jon Ashburnd2c905c2015-12-17 17:38:24 -0700254#define DEFAULT_VK_ELAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ExplicitLayers"
255#define DEFAULT_VK_ILAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers"
Jon Ashburn68e2bef2015-08-06 11:22:33 -0600256#define LAYERS_PATH_ENV "VK_LAYER_PATH"
Jon Ashburn60299432016-02-17 13:18:08 -0700257#define HOME_VK_DRIVERS_INFO ""
258#define HOME_VK_ELAYERS_INFO ""
259#define HOME_VK_ILAYERS_INFO ""
Jon Ashburn44aed662016-02-02 17:47:28 -0700260#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Courtney Goeltzenleuchterb5738ca2015-10-07 17:03:42 -0600261
Jon Ashburnffd5d672015-06-29 11:25:34 -0600262// File IO
Jon Ashburn44aed662016-02-02 17:47:28 -0700263static bool loader_platform_file_exists(const char *path) {
Jon Ashburnee33ae72015-06-30 14:46:22 -0700264 if ((_access(path, 0)) == -1)
Jon Ashburnffd5d672015-06-29 11:25:34 -0600265 return false;
266 else
267 return true;
268}
269
Jon Ashburn44aed662016-02-02 17:47:28 -0700270static bool loader_platform_is_path_absolute(const char *path) {
Jon Ashburn38144502015-07-07 15:06:25 -0600271 return !PathIsRelative(path);
272}
273
274// WIN32 runtime doesn't have dirname().
Jon Ashburn44aed662016-02-02 17:47:28 -0700275static inline char *loader_platform_dirname(char *path) {
Jon Ashburn38144502015-07-07 15:06:25 -0600276 char *current, *next;
277
278 // TODO/TBD: Do we need to deal with the Windows's ":" character?
279
280 for (current = path; *current != '\0'; current = next) {
281 next = strchr(current, DIRECTORY_SYMBOL);
282 if (next == NULL) {
283 if (current != path)
284 *(current - 1) = '\0';
285 return path;
286 } else {
287 // Point one character past the DIRECTORY_SYMBOL:
288 next++;
289 }
290 }
291 return path;
292}
293
294// WIN32 runtime doesn't have basename().
295// Microsoft also doesn't have basename(). Paths are different on Windows, and
296// so this is just a temporary solution in order to get us compiling, so that we
297// can test some scenarios, and develop the correct solution for Windows.
Jon Ashburn44aed662016-02-02 17:47:28 -0700298// TODO: Develop a better, permanent solution for Windows, to replace this
299// temporary code:
300static char *loader_platform_basename(char *pathname) {
Jon Ashburn38144502015-07-07 15:06:25 -0600301 char *current, *next;
302
Jon Ashburn44aed662016-02-02 17:47:28 -0700303 // TODO/TBD: Do we need to deal with the Windows's ":" character?
Jon Ashburn38144502015-07-07 15:06:25 -0600304
305 for (current = pathname; *current != '\0'; current = next) {
306 next = strchr(current, DIRECTORY_SYMBOL);
307 if (next == NULL) {
308 // No more DIRECTORY_SYMBOL's so return p:
309 return current;
310 } else {
311 // Point one character past the DIRECTORY_SYMBOL:
312 next++;
313 }
314 }
315 // We shouldn't get to here, but this makes the compiler happy:
316 return current;
317}
318
Jon Ashburn5b25d9f2016-01-04 14:01:38 -0700319// Environment variables
320
Jon Ashburn44aed662016-02-02 17:47:28 -0700321static inline char *loader_getenv(const char *name) {
Jon Ashburn5b25d9f2016-01-04 14:01:38 -0700322 char *retVal;
323 DWORD valSize;
324
325 valSize = GetEnvironmentVariableA(name, NULL, 0);
326
327 // valSize DOES include the null terminator, so for any set variable
328 // will always be at least 1. If it's 0, the variable wasn't set.
329 if (valSize == 0)
330 return NULL;
331
Jon Ashburn44aed662016-02-02 17:47:28 -0700332 // TODO; FIXME This should be using any app defined memory allocation
Jon Ashburn5b25d9f2016-01-04 14:01:38 -0700333 retVal = (char *)malloc(valSize);
334
335 GetEnvironmentVariableA(name, retVal, valSize);
336
337 return retVal;
338}
339
Jon Ashburn44aed662016-02-02 17:47:28 -0700340static inline void loader_free_getenv(const char *val) { free((void *)val); }
Jon Ashburn5b25d9f2016-01-04 14:01:38 -0700341
Ian Elliott81ac44c2015-01-13 17:52:38 -0700342// Dynamic Loading:
343typedef HMODULE loader_platform_dl_handle;
Jon Ashburn44aed662016-02-02 17:47:28 -0700344static loader_platform_dl_handle
345loader_platform_open_library(const char *libPath) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700346 return LoadLibrary(libPath);
347}
Jon Ashburn44aed662016-02-02 17:47:28 -0700348static char *loader_platform_open_library_error(const char *libPath) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700349 static char errorMsg[120];
350 snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath);
351 return errorMsg;
352}
Jon Ashburn44aed662016-02-02 17:47:28 -0700353static void loader_platform_close_library(loader_platform_dl_handle library) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700354 FreeLibrary(library);
355}
Jon Ashburn44aed662016-02-02 17:47:28 -0700356static void *loader_platform_get_proc_address(loader_platform_dl_handle library,
357 const char *name) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700358 assert(library);
359 assert(name);
360 return GetProcAddress(library, name);
361}
Jon Ashburn44aed662016-02-02 17:47:28 -0700362static char *loader_platform_get_proc_address_error(const char *name) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700363 static char errorMsg[120];
Jon Ashburn44aed662016-02-02 17:47:28 -0700364 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library",
365 name);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700366 return errorMsg;
367}
368
369// Threads:
370typedef HANDLE loader_platform_thread;
Jon Ashburn413d6582015-08-28 15:19:27 -0600371#define THREAD_LOCAL_DECL __declspec(thread)
Jon Ashburn44aed662016-02-02 17:47:28 -0700372#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
Ian Elliott81ac44c2015-01-13 17:52:38 -0700373 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
Jon Ashburn44aed662016-02-02 17:47:28 -0700374#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) INIT_ONCE var;
375static BOOL CALLBACK
376InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) {
Piers Daniell886be472015-02-23 16:23:13 -0700377 void (*func)(void) = (void (*)(void))Parameter;
378 func();
379 return TRUE;
380}
381
Jon Ashburn44aed662016-02-02 17:47:28 -0700382static void loader_platform_thread_once(void *ctl, void (*func)(void)) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700383 assert(func != NULL);
384 assert(ctl != NULL);
Jon Ashburn44aed662016-02-02 17:47:28 -0700385 InitOnceExecuteOnce((PINIT_ONCE)ctl, InitFuncWrapper, func, NULL);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700386}
387
388// Thread IDs:
389typedef DWORD loader_platform_thread_id;
Jon Ashburn44aed662016-02-02 17:47:28 -0700390static loader_platform_thread_id loader_platform_get_thread_id() {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700391 return GetCurrentThreadId();
392}
393
394// Thread mutex:
395typedef CRITICAL_SECTION loader_platform_thread_mutex;
Jon Ashburn44aed662016-02-02 17:47:28 -0700396static void
397loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700398 InitializeCriticalSection(pMutex);
399}
Jon Ashburn44aed662016-02-02 17:47:28 -0700400static void
401loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700402 EnterCriticalSection(pMutex);
403}
Jon Ashburn44aed662016-02-02 17:47:28 -0700404static void
405loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700406 LeaveCriticalSection(pMutex);
407}
Jon Ashburn44aed662016-02-02 17:47:28 -0700408static void
409loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700410 DeleteCriticalSection(pMutex);
411}
Mike Stroyan33053d02015-05-15 17:34:51 -0600412typedef CONDITION_VARIABLE loader_platform_thread_cond;
Jon Ashburn44aed662016-02-02 17:47:28 -0700413static void
414loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) {
Mike Stroyan33053d02015-05-15 17:34:51 -0600415 InitializeConditionVariable(pCond);
416}
Jon Ashburn44aed662016-02-02 17:47:28 -0700417static void
418loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond,
419 loader_platform_thread_mutex *pMutex) {
Mike Stroyan33053d02015-05-15 17:34:51 -0600420 SleepConditionVariableCS(pCond, pMutex, INFINITE);
421}
Jon Ashburn44aed662016-02-02 17:47:28 -0700422static void
423loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) {
Mike Stroyan33053d02015-05-15 17:34:51 -0600424 WakeAllConditionVariable(pCond);
425}
Ian Elliott81ac44c2015-01-13 17:52:38 -0700426
Ian Elliott76005132015-03-31 15:32:41 -0600427// Windows Registry:
Jon Ashburn44aed662016-02-02 17:47:28 -0700428char *loader_get_registry_string(const HKEY hive, const LPCTSTR sub_key,
Ian Elliott76005132015-03-31 15:32:41 -0600429 const char *value);
430
Jon Ashburnf6620c92015-10-15 13:47:58 -0600431#define loader_stack_alloc(size) _alloca(size)
Ian Elliott81ac44c2015-01-13 17:52:38 -0700432#else // defined(_WIN32)
433
434#error The "loader_platform.h" file must be modified for this OS.
435
436// NOTE: In order to support another OS, an #elif needs to be added (above the
437// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
438// contents of this file must be created.
439
440// NOTE: Other OS-specific changes are also needed for this OS. Search for
441// files with "WIN32" in it, as a quick way to find files that must be changed.
442
443#endif // defined(_WIN32)
444
jon5fb79fa2015-10-29 14:57:03 -0600445// returns true if the given string appears to be a relative or absolute
446// path, as opposed to a bare filename.
Jon Ashburn44aed662016-02-02 17:47:28 -0700447static inline bool loader_platform_is_path(const char *path) {
jon5fb79fa2015-10-29 14:57:03 -0600448 return strchr(path, DIRECTORY_SYMBOL) != NULL;
449}