blob: 3a02640461ae8b8caa84eb8659f2fde9b36860f7 [file] [log] [blame]
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001/*
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07002 *
Jon Ashburn23d36b12016-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 Elliott2d4ab1e2015-01-13 17:52:38 -07006 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06007 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070010 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060011 * http://www.apache.org/licenses/LICENSE-2.0
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070012 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060013 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Jon Ashburn23d36b12016-02-02 17:47:28 -070018 *
19 * Author: Ian Elliot <ian@lunarg.com>
20 * Author: Jon Ashburn <jon@lunarg.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060021 *
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070022 */
Jon Ashburn3f8c3002015-10-15 13:47:58 -060023#pragma once
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070024
Mark Lobodzinski450e4632015-11-24 12:04:15 -070025#if defined(_WIN32)
26// WinSock2.h must be included *BEFORE* windows.h
Mark Lobodzinski450e4632015-11-24 12:04:15 -070027#include <WinSock2.h>
28#endif // _WIN32
29
David Pinedo9316d3b2015-11-06 12:54:48 -070030#include "vulkan/vk_platform.h"
31#include "vulkan/vk_sdk_platform.h"
Cody Northrop33fa0412015-07-08 16:48:37 -060032
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070033#if defined(__linux__)
34/* Linux-specific common code: */
35
36// Headers:
37//#define _GNU_SOURCE 1
38// TBD: Are the contents of the following file used?
39#include <unistd.h>
40// Note: The following file is for dynamic loading:
41#include <dlfcn.h>
42#include <pthread.h>
43#include <assert.h>
Jon Ashburnfc1031e2015-11-17 15:31:02 -070044#include <string.h>
Jon Ashburn2077e382015-06-29 11:25:34 -060045#include <stdbool.h>
Michael Lentine695f2c22015-09-09 12:39:13 -070046#include <stdlib.h>
Jon Ashburn15315172015-07-07 15:06:25 -060047#include <libgen.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070048
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060049// VK Library Filenames, Paths, etc.:
Ian Elliott457810e2015-02-04 11:22:39 -070050#define PATH_SEPERATOR ':'
Jon Ashburn2077e382015-06-29 11:25:34 -060051#define DIRECTORY_SYMBOL '/'
52
John Drinkwater9ac3f4f2016-08-01 17:00:00 +010053#define VULKAN_DIR "/vulkan/"
54#define VULKAN_ICDCONF_DIR "icd.d"
55#define VULKAN_ICD_DIR "icd"
56#define VULKAN_ELAYERCONF_DIR "explicit_layer.d"
57#define VULKAN_ILAYERCONF_DIR "implicit_layer.d"
58#define VULKAN_LAYER_DIR "layer"
James Jonesc28b0a12015-07-23 17:39:37 -070059
60#if defined(LOCALPREFIX)
Jon Ashburn23d36b12016-02-02 17:47:28 -070061#define LOCAL_DRIVERS_INFO \
John Drinkwater9ac3f4f2016-08-01 17:00:00 +010062 LOCALPREFIX "/" SYSCONFDIR VULKAN_DIR VULKAN_ICDCONF_DIR ":" \
63 LOCALPREFIX "/" DATADIR VULKAN_DIR VULKAN_ICDCONF_DIR ":"
Jon Ashburn23d36b12016-02-02 17:47:28 -070064#define LOCAL_ELAYERS_INFO \
John Drinkwater9ac3f4f2016-08-01 17:00:00 +010065 LOCALPREFIX "/" SYSCONFDIR VULKAN_DIR VULKAN_ELAYERCONF_DIR ":" \
66 LOCALPREFIX "/" DATADIR VULKAN_DIR VULKAN_ELAYERCONF_DIR ":"
Jon Ashburn23d36b12016-02-02 17:47:28 -070067#define LOCAL_ILAYERS_INFO \
John Drinkwater9ac3f4f2016-08-01 17:00:00 +010068 LOCALPREFIX "/" SYSCONFDIR VULKAN_DIR VULKAN_ILAYERCONF_DIR ":" \
69 LOCALPREFIX "/" DATADIR VULKAN_DIR VULKAN_ILAYERCONF_DIR ":"
James Jonesc28b0a12015-07-23 17:39:37 -070070#else
71#define LOCAL_DRIVERS_INFO
Jon Ashburn075ce432015-12-17 17:38:24 -070072#define LOCAL_ELAYERS_INFO
73#define LOCAL_ILAYERS_INFO
James Jonesc28b0a12015-07-23 17:39:37 -070074#endif
75
Jon Ashburn23d36b12016-02-02 17:47:28 -070076#define DEFAULT_VK_DRIVERS_INFO \
77 LOCAL_DRIVERS_INFO \
John Drinkwater9ac3f4f2016-08-01 17:00:00 +010078 "/" SYSCONFDIR VULKAN_DIR VULKAN_ICDCONF_DIR ":" \
79 "/usr/" DATADIR VULKAN_DIR VULKAN_ICDCONF_DIR
Jamie Madill2fcbd152016-04-27 16:33:23 -040080#define DEFAULT_VK_DRIVERS_PATH ""
Jon Ashburn23d36b12016-02-02 17:47:28 -070081#define DEFAULT_VK_ELAYERS_INFO \
82 LOCAL_ELAYERS_INFO \
John Drinkwater9ac3f4f2016-08-01 17:00:00 +010083 "/" SYSCONFDIR VULKAN_DIR VULKAN_ELAYERCONF_DIR ":" \
84 "/usr/" DATADIR VULKAN_DIR VULKAN_ELAYERCONF_DIR
Jon Ashburn23d36b12016-02-02 17:47:28 -070085#define DEFAULT_VK_ILAYERS_INFO \
86 LOCAL_ILAYERS_INFO \
John Drinkwater9ac3f4f2016-08-01 17:00:00 +010087 "/" SYSCONFDIR VULKAN_DIR VULKAN_ILAYERCONF_DIR ":" \
88 "/usr/" DATADIR VULKAN_DIR VULKAN_ILAYERCONF_DIR
Jamie Madill2fcbd152016-04-27 16:33:23 -040089#define DEFAULT_VK_LAYERS_PATH ""
Jamie Madill00c3c912016-04-06 18:26:46 -040090#if !defined(LAYERS_SOURCE_PATH)
91#define LAYERS_SOURCE_PATH NULL
92#endif
Jon Ashburn1b958222015-08-06 11:22:33 -060093#define LAYERS_PATH_ENV "VK_LAYER_PATH"
John Drinkwater9ac3f4f2016-08-01 17:00:00 +010094#define HOME_VK_DRIVERS_INFO VULKAN_DIR VULKAN_ICDCONF_DIR
95#define HOME_VK_ELAYERS_INFO VULKAN_DIR VULKAN_ELAYERCONF_DIR
96#define HOME_VK_ILAYERS_INFO VULKAN_DIR VULKAN_ILAYERCONF_DIR
Ian Elliott457810e2015-02-04 11:22:39 -070097
Ian Elliotte5369462015-02-12 16:44:56 -070098// C99:
Jon Ashburn23d36b12016-02-02 17:47:28 -070099#define PRINTF_SIZE_T_SPECIFIER "%zu"
Tobin Ehlisb0497852015-02-11 14:24:02 -0700100
Jon Ashburn2077e382015-06-29 11:25:34 -0600101// File IO
Jon Ashburn23d36b12016-02-02 17:47:28 -0700102static inline bool loader_platform_file_exists(const char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -0600103 if (access(path, F_OK))
104 return false;
105 else
106 return true;
107}
108
Jon Ashburn23d36b12016-02-02 17:47:28 -0700109static inline bool loader_platform_is_path_absolute(const char *path) {
Jon Ashburn15315172015-07-07 15:06:25 -0600110 if (path[0] == '/')
111 return true;
112 else
113 return false;
114}
115
Jon Ashburn23d36b12016-02-02 17:47:28 -0700116static inline char *loader_platform_dirname(char *path) {
Jon Ashburn15315172015-07-07 15:06:25 -0600117 return dirname(path);
118}
119
Jon Ashburn2077e382015-06-29 11:25:34 -0600120// Dynamic Loading of libraries:
Jon Ashburn23d36b12016-02-02 17:47:28 -0700121typedef void *loader_platform_dl_handle;
122static inline loader_platform_dl_handle
123loader_platform_open_library(const char *libPath) {
Chia-I Wu6a218342015-02-18 14:39:54 -0700124 return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700125}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700126static inline const char *
127loader_platform_open_library_error(const char *libPath) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700128 return dlerror();
129}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700130static inline void
131loader_platform_close_library(loader_platform_dl_handle library) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700132 dlclose(library);
133}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700134static inline void *
135loader_platform_get_proc_address(loader_platform_dl_handle library,
136 const char *name) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700137 assert(library);
138 assert(name);
139 return dlsym(library, name);
140}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700141static inline const char *
142loader_platform_get_proc_address_error(const char *name) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700143 return dlerror();
144}
145
146// Threads:
147typedef pthread_t loader_platform_thread;
Jon Ashburn87d6aa92015-08-28 15:19:27 -0600148#define THREAD_LOCAL_DECL __thread
Jon Ashburn23d36b12016-02-02 17:47:28 -0700149#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700150 pthread_once_t var = PTHREAD_ONCE_INIT;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700151#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) pthread_once_t var;
152static inline void loader_platform_thread_once(pthread_once_t *ctl,
153 void (*func)(void)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700154 assert(func != NULL);
155 assert(ctl != NULL);
Michael Lentine695f2c22015-09-09 12:39:13 -0700156 pthread_once(ctl, func);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700157}
158
159// Thread IDs:
160typedef pthread_t loader_platform_thread_id;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700161static inline loader_platform_thread_id loader_platform_get_thread_id() {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700162 return pthread_self();
163}
164
165// Thread mutex:
166typedef pthread_mutex_t loader_platform_thread_mutex;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700167static inline void
168loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700169 pthread_mutex_init(pMutex, NULL);
170}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700171static inline void
172loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700173 pthread_mutex_lock(pMutex);
174}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700175static inline void
176loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700177 pthread_mutex_unlock(pMutex);
178}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700179static inline void
180loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700181 pthread_mutex_destroy(pMutex);
182}
Mike Stroyaned238bb2015-05-15 08:50:57 -0600183typedef pthread_cond_t loader_platform_thread_cond;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700184static inline void
185loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) {
Mike Stroyaned238bb2015-05-15 08:50:57 -0600186 pthread_cond_init(pCond, NULL);
187}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700188static inline void
189loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond,
190 loader_platform_thread_mutex *pMutex) {
Mike Stroyaned238bb2015-05-15 08:50:57 -0600191 pthread_cond_wait(pCond, pMutex);
192}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700193static inline void
194loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) {
Mike Stroyaned238bb2015-05-15 08:50:57 -0600195 pthread_cond_broadcast(pCond);
196}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700197
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600198#define loader_stack_alloc(size) alloca(size)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700199
200#elif defined(_WIN32) // defined(__linux__)
201/* Windows-specific common code: */
Tobin Ehlis890adf12015-11-02 15:45:19 -0700202// WinBase.h defines CreateSemaphore and synchapi.h defines CreateEvent
203// undefine them to avoid conflicts with VkLayerDispatchTable struct members.
204#ifdef CreateSemaphore
205#undef CreateSemaphore
206#endif
207#ifdef CreateEvent
208#undef CreateEvent
209#endif
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700210#include <assert.h>
Tony Barbour1d825c72015-06-18 16:29:32 -0600211#include <stdio.h>
Jon Ashburnfc1031e2015-11-17 15:31:02 -0700212#include <string.h>
Jon Ashburnffad94d2015-06-30 14:46:22 -0700213#include <io.h>
214#include <stdbool.h>
Cody Northrop33fa0412015-07-08 16:48:37 -0600215#include <shlwapi.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700216#ifdef __cplusplus
217#include <iostream>
218#include <string>
219using namespace std;
220#endif // __cplusplus
221
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600222// VK Library Filenames, Paths, etc.:
Ian Elliott457810e2015-02-04 11:22:39 -0700223#define PATH_SEPERATOR ';'
Jon Ashburn2077e382015-06-29 11:25:34 -0600224#define DIRECTORY_SYMBOL '\\'
Jon Ashburnffad94d2015-06-30 14:46:22 -0700225#define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE
226#define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\Vulkan\\Drivers"
Jamie Madill2fcbd152016-04-27 16:33:23 -0400227// TODO: Are these the correct paths
228#define DEFAULT_VK_DRIVERS_PATH ""
Jon Ashburn075ce432015-12-17 17:38:24 -0700229#define DEFAULT_VK_ELAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ExplicitLayers"
230#define DEFAULT_VK_ILAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers"
Jamie Madill00c3c912016-04-06 18:26:46 -0400231#if !defined(DEFAULT_VK_LAYERS_PATH)
Jamie Madill2fcbd152016-04-27 16:33:23 -0400232#define DEFAULT_VK_LAYERS_PATH ""
Jamie Madill00c3c912016-04-06 18:26:46 -0400233#endif
234#if !defined(LAYERS_SOURCE_PATH)
235#define LAYERS_SOURCE_PATH NULL
236#endif
Jon Ashburn1b958222015-08-06 11:22:33 -0600237#define LAYERS_PATH_ENV "VK_LAYER_PATH"
Jon Ashburn83657862016-02-17 13:18:08 -0700238#define HOME_VK_DRIVERS_INFO ""
239#define HOME_VK_ELAYERS_INFO ""
240#define HOME_VK_ILAYERS_INFO ""
Jon Ashburn23d36b12016-02-02 17:47:28 -0700241#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Courtney Goeltzenleuchter22d8d792015-10-07 17:03:42 -0600242
Jon Ashburn2077e382015-06-29 11:25:34 -0600243// File IO
Jon Ashburn23d36b12016-02-02 17:47:28 -0700244static bool loader_platform_file_exists(const char *path) {
Jon Ashburnffad94d2015-06-30 14:46:22 -0700245 if ((_access(path, 0)) == -1)
Jon Ashburn2077e382015-06-29 11:25:34 -0600246 return false;
247 else
248 return true;
249}
250
Jon Ashburn23d36b12016-02-02 17:47:28 -0700251static bool loader_platform_is_path_absolute(const char *path) {
Jon Ashburn15315172015-07-07 15:06:25 -0600252 return !PathIsRelative(path);
253}
254
255// WIN32 runtime doesn't have dirname().
Jon Ashburn23d36b12016-02-02 17:47:28 -0700256static inline char *loader_platform_dirname(char *path) {
Jon Ashburn15315172015-07-07 15:06:25 -0600257 char *current, *next;
258
259 // TODO/TBD: Do we need to deal with the Windows's ":" character?
260
261 for (current = path; *current != '\0'; current = next) {
262 next = strchr(current, DIRECTORY_SYMBOL);
263 if (next == NULL) {
264 if (current != path)
265 *(current - 1) = '\0';
266 return path;
267 } else {
268 // Point one character past the DIRECTORY_SYMBOL:
269 next++;
270 }
271 }
272 return path;
273}
274
275// WIN32 runtime doesn't have basename().
276// Microsoft also doesn't have basename(). Paths are different on Windows, and
277// so this is just a temporary solution in order to get us compiling, so that we
278// can test some scenarios, and develop the correct solution for Windows.
Jon Ashburn23d36b12016-02-02 17:47:28 -0700279// TODO: Develop a better, permanent solution for Windows, to replace this
280// temporary code:
281static char *loader_platform_basename(char *pathname) {
Jon Ashburn15315172015-07-07 15:06:25 -0600282 char *current, *next;
283
Jon Ashburn23d36b12016-02-02 17:47:28 -0700284 // TODO/TBD: Do we need to deal with the Windows's ":" character?
Jon Ashburn15315172015-07-07 15:06:25 -0600285
286 for (current = pathname; *current != '\0'; current = next) {
287 next = strchr(current, DIRECTORY_SYMBOL);
288 if (next == NULL) {
289 // No more DIRECTORY_SYMBOL's so return p:
290 return current;
291 } else {
292 // Point one character past the DIRECTORY_SYMBOL:
293 next++;
294 }
295 }
296 // We shouldn't get to here, but this makes the compiler happy:
297 return current;
298}
299
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700300// Dynamic Loading:
301typedef HMODULE loader_platform_dl_handle;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700302static loader_platform_dl_handle
303loader_platform_open_library(const char *libPath) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700304 return LoadLibrary(libPath);
305}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700306static char *loader_platform_open_library_error(const char *libPath) {
Tobin Ehlisb2dfb552016-06-07 06:07:13 -0600307 static char errorMsg[164];
308 snprintf(errorMsg, 163, "Failed to open dynamic library \"%s\"", libPath);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700309 return errorMsg;
310}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700311static void loader_platform_close_library(loader_platform_dl_handle library) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700312 FreeLibrary(library);
313}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700314static void *loader_platform_get_proc_address(loader_platform_dl_handle library,
315 const char *name) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700316 assert(library);
317 assert(name);
318 return GetProcAddress(library, name);
319}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700320static char *loader_platform_get_proc_address_error(const char *name) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700321 static char errorMsg[120];
Jon Ashburn23d36b12016-02-02 17:47:28 -0700322 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library",
323 name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700324 return errorMsg;
325}
326
327// Threads:
328typedef HANDLE loader_platform_thread;
Jon Ashburn87d6aa92015-08-28 15:19:27 -0600329#define THREAD_LOCAL_DECL __declspec(thread)
Jon Ashburn23d36b12016-02-02 17:47:28 -0700330#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700331 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700332#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) INIT_ONCE var;
333static BOOL CALLBACK
334InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) {
Piers Daniell4da523a2015-02-23 16:23:13 -0700335 void (*func)(void) = (void (*)(void))Parameter;
336 func();
337 return TRUE;
338}
339
Jon Ashburn23d36b12016-02-02 17:47:28 -0700340static void loader_platform_thread_once(void *ctl, void (*func)(void)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700341 assert(func != NULL);
342 assert(ctl != NULL);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700343 InitOnceExecuteOnce((PINIT_ONCE)ctl, InitFuncWrapper, func, NULL);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700344}
345
346// Thread IDs:
347typedef DWORD loader_platform_thread_id;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700348static loader_platform_thread_id loader_platform_get_thread_id() {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700349 return GetCurrentThreadId();
350}
351
352// Thread mutex:
353typedef CRITICAL_SECTION loader_platform_thread_mutex;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700354static void
355loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700356 InitializeCriticalSection(pMutex);
357}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700358static void
359loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700360 EnterCriticalSection(pMutex);
361}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700362static void
363loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700364 LeaveCriticalSection(pMutex);
365}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700366static void
367loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700368 DeleteCriticalSection(pMutex);
369}
Mike Stroyanc3d98332015-05-15 17:34:51 -0600370typedef CONDITION_VARIABLE loader_platform_thread_cond;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700371static void
372loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) {
Mike Stroyanc3d98332015-05-15 17:34:51 -0600373 InitializeConditionVariable(pCond);
374}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700375static void
376loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond,
377 loader_platform_thread_mutex *pMutex) {
Mike Stroyanc3d98332015-05-15 17:34:51 -0600378 SleepConditionVariableCS(pCond, pMutex, INFINITE);
379}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700380static void
381loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) {
Mike Stroyanc3d98332015-05-15 17:34:51 -0600382 WakeAllConditionVariable(pCond);
383}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700384
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600385// Windows Registry:
Jon Ashburn23d36b12016-02-02 17:47:28 -0700386char *loader_get_registry_string(const HKEY hive, const LPCTSTR sub_key,
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600387 const char *value);
388
Jon Ashburn3f8c3002015-10-15 13:47:58 -0600389#define loader_stack_alloc(size) _alloca(size)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700390#else // defined(_WIN32)
391
392#error The "loader_platform.h" file must be modified for this OS.
393
394// NOTE: In order to support another OS, an #elif needs to be added (above the
395// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
396// contents of this file must be created.
397
398// NOTE: Other OS-specific changes are also needed for this OS. Search for
399// files with "WIN32" in it, as a quick way to find files that must be changed.
400
401#endif // defined(_WIN32)
402
jon547bbee2015-10-29 14:57:03 -0600403// returns true if the given string appears to be a relative or absolute
404// path, as opposed to a bare filename.
Jon Ashburn23d36b12016-02-02 17:47:28 -0700405static inline bool loader_platform_is_path(const char *path) {
jon547bbee2015-10-29 14:57:03 -0600406 return strchr(path, DIRECTORY_SYMBOL) != NULL;
407}