blob: 0ffc92384d933a5cf17507c9a22acf6345309caf [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
Dor Askayoa120bb32016-10-22 22:20:29 +030060#if defined(EXTRASYSCONFDIR)
61#define EXTRA_DRIVERS_SYSCONFDIR_INFO ":" \
62 EXTRASYSCONFDIR VULKAN_DIR VULKAN_ICDCONF_DIR
63#define EXTRA_ELAYERS_SYSCONFDIR_INFO ":" \
64 EXTRASYSCONFDIR VULKAN_DIR VULKAN_ELAYERCONF_DIR
65#define EXTRA_ILAYERS_SYSCONFDIR_INFO ":" \
66 EXTRASYSCONFDIR VULKAN_DIR VULKAN_ILAYERCONF_DIR
James Jonesc28b0a12015-07-23 17:39:37 -070067#else
Dor Askayoa120bb32016-10-22 22:20:29 +030068#define EXTRA_DRIVERS_SYSCONFDIR_INFO
69#define EXTRA_ELAYERS_SYSCONFDIR_INFO
70#define EXTRA_ILAYERS_SYSCONFDIR_INFO
71#endif
72
73#if defined(EXTRADATADIR)
74#define EXTRA_DRIVERS_DATADIR_INFO ":" \
75 EXTRADATADIR VULKAN_DIR VULKAN_ICDCONF_DIR
76#define EXTRA_ELAYERS_DATADIR_INFO ":" \
77 EXTRADATADIR VULKAN_DIR VULKAN_ELAYERCONF_DIR
78#define EXTRA_ILAYERS_DATADIR_INFO ":" \
79 EXTRADATADIR VULKAN_DIR VULKAN_ILAYERCONF_DIR
80#else
81#define EXTRA_DRIVERS_DATADIR_INFO
82#define EXTRA_ELAYERS_DATADIR_INFO
83#define EXTRA_ILAYERS_DATADIR_INFO
James Jonesc28b0a12015-07-23 17:39:37 -070084#endif
85
Jon Ashburn23d36b12016-02-02 17:47:28 -070086#define DEFAULT_VK_DRIVERS_INFO \
Dor Askayoa120bb32016-10-22 22:20:29 +030087 SYSCONFDIR VULKAN_DIR VULKAN_ICDCONF_DIR ":" \
88 DATADIR VULKAN_DIR VULKAN_ICDCONF_DIR \
89 EXTRA_DRIVERS_SYSCONFDIR_INFO \
90 EXTRA_DRIVERS_DATADIR_INFO
Jon Ashburn23d36b12016-02-02 17:47:28 -070091#define DEFAULT_VK_ELAYERS_INFO \
Dor Askayoa120bb32016-10-22 22:20:29 +030092 SYSCONFDIR VULKAN_DIR VULKAN_ELAYERCONF_DIR ":" \
93 DATADIR VULKAN_DIR VULKAN_ELAYERCONF_DIR \
94 EXTRA_ELAYERS_SYSCONFDIR_INFO \
95 EXTRA_ELAYERS_DATADIR_INFO
Jon Ashburn23d36b12016-02-02 17:47:28 -070096#define DEFAULT_VK_ILAYERS_INFO \
Dor Askayoa120bb32016-10-22 22:20:29 +030097 SYSCONFDIR VULKAN_DIR VULKAN_ILAYERCONF_DIR ":" \
98 DATADIR VULKAN_DIR VULKAN_ILAYERCONF_DIR \
99 EXTRA_ILAYERS_SYSCONFDIR_INFO \
100 EXTRA_ILAYERS_DATADIR_INFO
101
102#define DEFAULT_VK_DRIVERS_PATH ""
Jamie Madill2fcbd152016-04-27 16:33:23 -0400103#define DEFAULT_VK_LAYERS_PATH ""
Dor Askayoa120bb32016-10-22 22:20:29 +0300104
Jamie Madill00c3c912016-04-06 18:26:46 -0400105#if !defined(LAYERS_SOURCE_PATH)
106#define LAYERS_SOURCE_PATH NULL
107#endif
Jon Ashburn1b958222015-08-06 11:22:33 -0600108#define LAYERS_PATH_ENV "VK_LAYER_PATH"
Dor Askayoa120bb32016-10-22 22:20:29 +0300109
John Drinkwater9ac3f4f2016-08-01 17:00:00 +0100110#define HOME_VK_DRIVERS_INFO VULKAN_DIR VULKAN_ICDCONF_DIR
111#define HOME_VK_ELAYERS_INFO VULKAN_DIR VULKAN_ELAYERCONF_DIR
112#define HOME_VK_ILAYERS_INFO VULKAN_DIR VULKAN_ILAYERCONF_DIR
Ian Elliott457810e2015-02-04 11:22:39 -0700113
Ian Elliotte5369462015-02-12 16:44:56 -0700114// C99:
Jon Ashburn23d36b12016-02-02 17:47:28 -0700115#define PRINTF_SIZE_T_SPECIFIER "%zu"
Tobin Ehlisb0497852015-02-11 14:24:02 -0700116
Jon Ashburn2077e382015-06-29 11:25:34 -0600117// File IO
Jon Ashburn23d36b12016-02-02 17:47:28 -0700118static inline bool loader_platform_file_exists(const char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -0600119 if (access(path, F_OK))
120 return false;
121 else
122 return true;
123}
124
Jon Ashburn23d36b12016-02-02 17:47:28 -0700125static inline bool loader_platform_is_path_absolute(const char *path) {
Jon Ashburn15315172015-07-07 15:06:25 -0600126 if (path[0] == '/')
127 return true;
128 else
129 return false;
130}
131
Jon Ashburn23d36b12016-02-02 17:47:28 -0700132static inline char *loader_platform_dirname(char *path) {
Jon Ashburn15315172015-07-07 15:06:25 -0600133 return dirname(path);
134}
135
Jon Ashburn2077e382015-06-29 11:25:34 -0600136// Dynamic Loading of libraries:
Jon Ashburn23d36b12016-02-02 17:47:28 -0700137typedef void *loader_platform_dl_handle;
138static inline loader_platform_dl_handle
139loader_platform_open_library(const char *libPath) {
Chia-I Wu6a218342015-02-18 14:39:54 -0700140 return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700141}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700142static inline const char *
143loader_platform_open_library_error(const char *libPath) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700144 return dlerror();
145}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700146static inline void
147loader_platform_close_library(loader_platform_dl_handle library) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700148 dlclose(library);
149}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700150static inline void *
151loader_platform_get_proc_address(loader_platform_dl_handle library,
152 const char *name) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700153 assert(library);
154 assert(name);
155 return dlsym(library, name);
156}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700157static inline const char *
158loader_platform_get_proc_address_error(const char *name) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700159 return dlerror();
160}
161
162// Threads:
163typedef pthread_t loader_platform_thread;
Jon Ashburn87d6aa92015-08-28 15:19:27 -0600164#define THREAD_LOCAL_DECL __thread
Jon Ashburn23d36b12016-02-02 17:47:28 -0700165#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700166 pthread_once_t var = PTHREAD_ONCE_INIT;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700167#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) pthread_once_t var;
168static inline void loader_platform_thread_once(pthread_once_t *ctl,
169 void (*func)(void)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700170 assert(func != NULL);
171 assert(ctl != NULL);
Michael Lentine695f2c22015-09-09 12:39:13 -0700172 pthread_once(ctl, func);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700173}
174
175// Thread IDs:
176typedef pthread_t loader_platform_thread_id;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700177static inline loader_platform_thread_id loader_platform_get_thread_id() {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700178 return pthread_self();
179}
180
181// Thread mutex:
182typedef pthread_mutex_t loader_platform_thread_mutex;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700183static inline void
184loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700185 pthread_mutex_init(pMutex, NULL);
186}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700187static inline void
188loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700189 pthread_mutex_lock(pMutex);
190}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700191static inline void
192loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700193 pthread_mutex_unlock(pMutex);
194}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700195static inline void
196loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700197 pthread_mutex_destroy(pMutex);
198}
Mike Stroyaned238bb2015-05-15 08:50:57 -0600199typedef pthread_cond_t loader_platform_thread_cond;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700200static inline void
201loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) {
Mike Stroyaned238bb2015-05-15 08:50:57 -0600202 pthread_cond_init(pCond, NULL);
203}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700204static inline void
205loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond,
206 loader_platform_thread_mutex *pMutex) {
Mike Stroyaned238bb2015-05-15 08:50:57 -0600207 pthread_cond_wait(pCond, pMutex);
208}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700209static inline void
210loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) {
Mike Stroyaned238bb2015-05-15 08:50:57 -0600211 pthread_cond_broadcast(pCond);
212}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700213
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600214#define loader_stack_alloc(size) alloca(size)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700215
216#elif defined(_WIN32) // defined(__linux__)
217/* Windows-specific common code: */
Tobin Ehlis890adf12015-11-02 15:45:19 -0700218// WinBase.h defines CreateSemaphore and synchapi.h defines CreateEvent
219// undefine them to avoid conflicts with VkLayerDispatchTable struct members.
220#ifdef CreateSemaphore
221#undef CreateSemaphore
222#endif
223#ifdef CreateEvent
224#undef CreateEvent
225#endif
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700226#include <assert.h>
Tony Barbour1d825c72015-06-18 16:29:32 -0600227#include <stdio.h>
Jon Ashburnfc1031e2015-11-17 15:31:02 -0700228#include <string.h>
Jon Ashburnffad94d2015-06-30 14:46:22 -0700229#include <io.h>
230#include <stdbool.h>
Cody Northrop33fa0412015-07-08 16:48:37 -0600231#include <shlwapi.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700232#ifdef __cplusplus
233#include <iostream>
234#include <string>
235using namespace std;
236#endif // __cplusplus
237
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600238// VK Library Filenames, Paths, etc.:
Ian Elliott457810e2015-02-04 11:22:39 -0700239#define PATH_SEPERATOR ';'
Jon Ashburn2077e382015-06-29 11:25:34 -0600240#define DIRECTORY_SYMBOL '\\'
Jon Ashburnffad94d2015-06-30 14:46:22 -0700241#define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE
Lenny Komow367c4ea2016-11-07 11:40:00 -0700242#define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\" API_NAME "\\Drivers"
Jamie Madill2fcbd152016-04-27 16:33:23 -0400243#define DEFAULT_VK_DRIVERS_PATH ""
Lenny Komow367c4ea2016-11-07 11:40:00 -0700244#define DEFAULT_VK_ELAYERS_INFO "SOFTWARE\\Khronos\\" API_NAME "\\ExplicitLayers"
245#define DEFAULT_VK_ILAYERS_INFO "SOFTWARE\\Khronos\\" API_NAME "\\ImplicitLayers"
Jamie Madill00c3c912016-04-06 18:26:46 -0400246#if !defined(DEFAULT_VK_LAYERS_PATH)
Jamie Madill2fcbd152016-04-27 16:33:23 -0400247#define DEFAULT_VK_LAYERS_PATH ""
Jamie Madill00c3c912016-04-06 18:26:46 -0400248#endif
249#if !defined(LAYERS_SOURCE_PATH)
250#define LAYERS_SOURCE_PATH NULL
251#endif
Jon Ashburn1b958222015-08-06 11:22:33 -0600252#define LAYERS_PATH_ENV "VK_LAYER_PATH"
Jon Ashburn83657862016-02-17 13:18:08 -0700253#define HOME_VK_DRIVERS_INFO ""
254#define HOME_VK_ELAYERS_INFO ""
255#define HOME_VK_ILAYERS_INFO ""
Jon Ashburn23d36b12016-02-02 17:47:28 -0700256#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Courtney Goeltzenleuchter22d8d792015-10-07 17:03:42 -0600257
Jon Ashburn2077e382015-06-29 11:25:34 -0600258// File IO
Jon Ashburn23d36b12016-02-02 17:47:28 -0700259static bool loader_platform_file_exists(const char *path) {
Jon Ashburnffad94d2015-06-30 14:46:22 -0700260 if ((_access(path, 0)) == -1)
Jon Ashburn2077e382015-06-29 11:25:34 -0600261 return false;
262 else
263 return true;
264}
265
Jon Ashburn23d36b12016-02-02 17:47:28 -0700266static bool loader_platform_is_path_absolute(const char *path) {
Jon Ashburn15315172015-07-07 15:06:25 -0600267 return !PathIsRelative(path);
268}
269
270// WIN32 runtime doesn't have dirname().
Jon Ashburn23d36b12016-02-02 17:47:28 -0700271static inline char *loader_platform_dirname(char *path) {
Jon Ashburn15315172015-07-07 15:06:25 -0600272 char *current, *next;
273
274 // TODO/TBD: Do we need to deal with the Windows's ":" character?
275
276 for (current = path; *current != '\0'; current = next) {
277 next = strchr(current, DIRECTORY_SYMBOL);
278 if (next == NULL) {
279 if (current != path)
280 *(current - 1) = '\0';
281 return path;
282 } else {
283 // Point one character past the DIRECTORY_SYMBOL:
284 next++;
285 }
286 }
287 return path;
288}
289
290// WIN32 runtime doesn't have basename().
291// Microsoft also doesn't have basename(). Paths are different on Windows, and
292// so this is just a temporary solution in order to get us compiling, so that we
293// can test some scenarios, and develop the correct solution for Windows.
Jon Ashburn23d36b12016-02-02 17:47:28 -0700294// TODO: Develop a better, permanent solution for Windows, to replace this
295// temporary code:
296static char *loader_platform_basename(char *pathname) {
Jon Ashburn15315172015-07-07 15:06:25 -0600297 char *current, *next;
298
Jon Ashburn23d36b12016-02-02 17:47:28 -0700299 // TODO/TBD: Do we need to deal with the Windows's ":" character?
Jon Ashburn15315172015-07-07 15:06:25 -0600300
301 for (current = pathname; *current != '\0'; current = next) {
302 next = strchr(current, DIRECTORY_SYMBOL);
303 if (next == NULL) {
304 // No more DIRECTORY_SYMBOL's so return p:
305 return current;
306 } else {
307 // Point one character past the DIRECTORY_SYMBOL:
308 next++;
309 }
310 }
311 // We shouldn't get to here, but this makes the compiler happy:
312 return current;
313}
314
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700315// Dynamic Loading:
316typedef HMODULE loader_platform_dl_handle;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700317static loader_platform_dl_handle
318loader_platform_open_library(const char *libPath) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700319 return LoadLibrary(libPath);
320}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700321static char *loader_platform_open_library_error(const char *libPath) {
Tobin Ehlisb2dfb552016-06-07 06:07:13 -0600322 static char errorMsg[164];
323 snprintf(errorMsg, 163, "Failed to open dynamic library \"%s\"", libPath);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700324 return errorMsg;
325}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700326static void loader_platform_close_library(loader_platform_dl_handle library) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700327 FreeLibrary(library);
328}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700329static void *loader_platform_get_proc_address(loader_platform_dl_handle library,
330 const char *name) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700331 assert(library);
332 assert(name);
333 return GetProcAddress(library, name);
334}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700335static char *loader_platform_get_proc_address_error(const char *name) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700336 static char errorMsg[120];
Jon Ashburn23d36b12016-02-02 17:47:28 -0700337 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library",
338 name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700339 return errorMsg;
340}
341
342// Threads:
343typedef HANDLE loader_platform_thread;
Jon Ashburn87d6aa92015-08-28 15:19:27 -0600344#define THREAD_LOCAL_DECL __declspec(thread)
Jon Ashburn23d36b12016-02-02 17:47:28 -0700345#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700346 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700347#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) INIT_ONCE var;
348static BOOL CALLBACK
349InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) {
Piers Daniell4da523a2015-02-23 16:23:13 -0700350 void (*func)(void) = (void (*)(void))Parameter;
351 func();
352 return TRUE;
353}
354
Jon Ashburn23d36b12016-02-02 17:47:28 -0700355static void loader_platform_thread_once(void *ctl, void (*func)(void)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700356 assert(func != NULL);
357 assert(ctl != NULL);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700358 InitOnceExecuteOnce((PINIT_ONCE)ctl, InitFuncWrapper, func, NULL);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700359}
360
361// Thread IDs:
362typedef DWORD loader_platform_thread_id;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700363static loader_platform_thread_id loader_platform_get_thread_id() {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700364 return GetCurrentThreadId();
365}
366
367// Thread mutex:
368typedef CRITICAL_SECTION loader_platform_thread_mutex;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700369static void
370loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700371 InitializeCriticalSection(pMutex);
372}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700373static void
374loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700375 EnterCriticalSection(pMutex);
376}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700377static void
378loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700379 LeaveCriticalSection(pMutex);
380}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700381static void
382loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700383 DeleteCriticalSection(pMutex);
384}
Mike Stroyanc3d98332015-05-15 17:34:51 -0600385typedef CONDITION_VARIABLE loader_platform_thread_cond;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700386static void
387loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) {
Mike Stroyanc3d98332015-05-15 17:34:51 -0600388 InitializeConditionVariable(pCond);
389}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700390static void
391loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond,
392 loader_platform_thread_mutex *pMutex) {
Mike Stroyanc3d98332015-05-15 17:34:51 -0600393 SleepConditionVariableCS(pCond, pMutex, INFINITE);
394}
Jon Ashburn23d36b12016-02-02 17:47:28 -0700395static void
396loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) {
Mike Stroyanc3d98332015-05-15 17:34:51 -0600397 WakeAllConditionVariable(pCond);
398}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700399
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600400// Windows Registry:
Jon Ashburn23d36b12016-02-02 17:47:28 -0700401char *loader_get_registry_string(const HKEY hive, const LPCTSTR sub_key,
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600402 const char *value);
403
Jon Ashburn3f8c3002015-10-15 13:47:58 -0600404#define loader_stack_alloc(size) _alloca(size)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700405#else // defined(_WIN32)
406
407#error The "loader_platform.h" file must be modified for this OS.
408
409// NOTE: In order to support another OS, an #elif needs to be added (above the
410// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
411// contents of this file must be created.
412
413// NOTE: Other OS-specific changes are also needed for this OS. Search for
414// files with "WIN32" in it, as a quick way to find files that must be changed.
415
416#endif // defined(_WIN32)
417
jon547bbee2015-10-29 14:57:03 -0600418// returns true if the given string appears to be a relative or absolute
419// path, as opposed to a bare filename.
Jon Ashburn23d36b12016-02-02 17:47:28 -0700420static inline bool loader_platform_is_path(const char *path) {
jon547bbee2015-10-29 14:57:03 -0600421 return strchr(path, DIRECTORY_SYMBOL) != NULL;
422}