Add SDL_sem implementation using Atomics and WaitOnAddress API. Keep Semaphore Kernel Object impl for Windows 7 and older - choose at runtime v2: - Fix mixed int/LONG types - Reorder definitions - Add missing include v3: - Use `GetModuleHandle()` to load the API Set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 0302b8d..bac14a7 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -1211,6 +1211,22 @@ extern "C" {
#define SDL_HINT_WINDOWS_FORCE_MUTEX_CRITICAL_SECTIONS "SDL_WINDOWS_FORCE_MUTEX_CRITICAL_SECTIONS"
/**
+ * \brief Force SDL to use Kernel Semaphores on Windows.
+ * Kernel Semaphores are inter-process and require a context
+ * switch on every interaction. On Windows 8 and newer, the
+ * WaitOnAddress API is available. Using that and atomics to
+ * implement semaphores increases performance.
+ * SDL will fall back to Kernel Objects on older OS versions
+ * or if forced to by this hint.
+ *
+ * This variable can be set to the following values:
+ * "0" - Use Atomics and WaitOnAddress API when available. If not, fall back to Kernel Objects. (default)
+ * "1" - Force the use of Kernel Objects in all cases.
+ *
+ */
+#define SDL_HINT_WINDOWS_FORCE_SEMAPHORE_KERNEL "SDL_WINDOWS_FORCE_SEMAPHORE_KERNEL"
+
+/**
* \brief Tell SDL which Dispmanx layer to use on a Raspberry PI
*
* Also known as Z-order. The variable can take a negative or positive value.
diff --git a/src/thread/windows/SDL_syssem.c b/src/thread/windows/SDL_syssem.c
index 217ea32..ed0e78b 100644
--- a/src/thread/windows/SDL_syssem.c
+++ b/src/thread/windows/SDL_syssem.c
@@ -22,27 +22,239 @@
#if SDL_THREAD_WINDOWS
-/* Semaphore functions using the Win32 API */
+/**
+ * Semaphore functions using the Win32 API
+ * There are two implementations available based on:
+ * - Kernel Semaphores. Available on all OS versions. (kern)
+ * Heavy-weight inter-process kernel objects.
+ * - Atomics and WaitOnAddress API. (atom)
+ * Faster due to significantly less context switches.
+ * Requires Windows 8 or newer.
+ * which are chosen at runtime.
+*/
#include "../../core/windows/SDL_windows.h"
+#include "SDL_hints.h"
#include "SDL_thread.h"
+#include "SDL_timer.h"
+
+typedef SDL_sem * (*pfnSDL_CreateSemaphore)(Uint32);
+typedef void (*pfnSDL_DestroySemaphore)(SDL_sem *);
+typedef int (*pfnSDL_SemWaitTimeout)(SDL_sem *, Uint32);
+typedef int (*pfnSDL_SemTryWait)(SDL_sem *);
+typedef int (*pfnSDL_SemWait)(SDL_sem *);
+typedef Uint32 (*pfnSDL_SemValue)(SDL_sem *);
+typedef int (*pfnSDL_SemPost)(SDL_sem *);
-struct SDL_semaphore
+typedef struct SDL_semaphore_impl_t
{
- HANDLE id;
+ pfnSDL_CreateSemaphore Create;
+ pfnSDL_DestroySemaphore Destroy;
+ pfnSDL_SemWaitTimeout WaitTimeout;
+ pfnSDL_SemTryWait TryWait;
+ pfnSDL_SemWait Wait;
+ pfnSDL_SemValue Value;
+ pfnSDL_SemPost Post;
+} SDL_sem_impl_t;
+
+/* Implementation will be chosen at runtime based on available Kernel features */
+static SDL_sem_impl_t SDL_sem_impl_active = {0};
+
+
+/**
+ * Atomic + WaitOnAddress implementation
+ */
+
+typedef BOOL(WINAPI *pfnWaitOnAddress)(volatile VOID*, PVOID, SIZE_T, DWORD);
+typedef VOID(WINAPI *pfnWakeByAddressSingle)(PVOID);
+
+static pfnWaitOnAddress pWaitOnAddress = NULL;
+static pfnWakeByAddressSingle pWakeByAddressSingle = NULL;
+
+typedef struct SDL_semaphore_atom
+{
+ LONG count;
+} SDL_sem_atom;
+
+static SDL_sem *
+SDL_CreateSemaphore_atom(Uint32 initial_value)
+{
+ SDL_sem_atom *sem;
+
+ sem = (SDL_sem_atom *) SDL_malloc(sizeof(*sem));
+ if (sem) {
+ sem->count = initial_value;
+ } else {
+ SDL_OutOfMemory();
+ }
+ return (SDL_sem *)sem;
+}
+
+static void
+SDL_DestroySemaphore_atom(SDL_sem * sem)
+{
+ if (sem) {
+ SDL_free(sem);
+ }
+}
+
+static int
+SDL_SemTryWait_atom(SDL_sem * _sem)
+{
+ SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
LONG count;
+
+ if (!sem) {
+ return SDL_SetError("Passed a NULL sem");
+ }
+
+ count = sem->count;
+ if (count == 0) {
+ return SDL_MUTEX_TIMEDOUT;
+ }
+
+ if (InterlockedCompareExchange(&sem->count, count - 1, count) == count) {
+ return 0;
+ }
+
+ return SDL_MUTEX_TIMEDOUT;
+}
+
+static int
+SDL_SemWait_atom(SDL_sem * _sem)
+{
+ SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
+ LONG count;
+
+ if (!sem) {
+ return SDL_SetError("Passed a NULL sem");
+ }
+
+ for (;;) {
+ count = sem->count;
+ while (count == 0) {
+ if (pWaitOnAddress(&sem->count, &count, sizeof(sem->count), INFINITE) == FALSE) {
+ return SDL_SetError("WaitOnAddress() failed");
+ }
+ count = sem->count;
+ }
+
+ if (InterlockedCompareExchange(&sem->count, count - 1, count) == count) {
+ return 0;
+ }
+ }
+}
+
+static int
+SDL_SemWaitTimeout_atom(SDL_sem * _sem, Uint32 timeout)
+{
+ SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
+ LONG count;
+ Uint32 now;
+ Uint32 deadline;
+ DWORD timeout_eff;
+
+ if (timeout == SDL_MUTEX_MAXWAIT) {
+ return SDL_SemWait_atom(_sem);
+ }
+
+ if (!sem) {
+ return SDL_SetError("Passed a NULL sem");
+ }
+
+ /**
+ * WaitOnAddress is subject to spurious and stolen wakeups so we
+ * need to recalculate the effective timeout before every wait
+ */
+ now = SDL_GetTicks();
+ deadline = now + (DWORD) timeout;
+
+ for (;;) {
+ count = sem->count;
+ /* If no semaphore is available we need to wait */
+ while (count == 0) {
+ now = SDL_GetTicks();
+ if (deadline > now) {
+ timeout_eff = deadline - now;
+ } else {
+ return SDL_MUTEX_TIMEDOUT;
+ }
+ if (pWaitOnAddress(&sem->count, &count, sizeof(count), timeout_eff) == FALSE) {
+ if (GetLastError() == ERROR_TIMEOUT) {
+ return SDL_MUTEX_TIMEDOUT;
+ }
+ return SDL_SetError("WaitOnAddress() failed");
+ }
+ count = sem->count;
+ }
+
+ /* Actually the semaphore is only consumed if this succeeds */
+ /* If it doesn't we need to do everything again */
+ if (InterlockedCompareExchange(&sem->count, count - 1, count) == count) {
+ return 0;
+ }
+ }
+}
+
+static Uint32
+SDL_SemValue_atom(SDL_sem * _sem)
+{
+ SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
+
+ if (!sem) {
+ SDL_SetError("Passed a NULL sem");
+ return 0;
+ }
+
+ return (Uint32)sem->count;
+}
+
+static int
+SDL_SemPost_atom(SDL_sem * _sem)
+{
+ SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
+
+ if (!sem) {
+ return SDL_SetError("Passed a NULL sem");
+ }
+
+ InterlockedIncrement(&sem->count);
+ pWakeByAddressSingle(&sem->count);
+
+ return 0;
+}
+
+static const SDL_sem_impl_t SDL_sem_impl_atom =
+{
+ &SDL_CreateSemaphore_atom,
+ &SDL_DestroySemaphore_atom,
+ &SDL_SemWaitTimeout_atom,
+ &SDL_SemTryWait_atom,
+ &SDL_SemWait_atom,
+ &SDL_SemValue_atom,
+ &SDL_SemPost_atom,
};
+/**
+ * Fallback Semaphore implementation using Kernel Semaphores
+ */
+
+typedef struct SDL_semaphore_kern
+{
+ HANDLE id;
+ LONG count;
+} SDL_sem_kern;
+
/* Create a semaphore */
-SDL_sem *
-SDL_CreateSemaphore(Uint32 initial_value)
+static SDL_sem *
+SDL_CreateSemaphore_kern(Uint32 initial_value)
{
- SDL_sem *sem;
+ SDL_sem_kern *sem;
/* Allocate sem memory */
- sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
+ sem = (SDL_sem_kern *) SDL_malloc(sizeof(*sem));
if (sem) {
/* Create the semaphore, with max value 32K */
#if __WINRT__
@@ -59,13 +271,14 @@ SDL_CreateSemaphore(Uint32 initial_value)
} else {
SDL_OutOfMemory();
}
- return (sem);
+ return (SDL_sem *)sem;
}
/* Free the semaphore */
-void
-SDL_DestroySemaphore(SDL_sem * sem)
+static void
+SDL_DestroySemaphore_kern(SDL_sem * _sem)
{
+ SDL_sem_kern *sem = (SDL_sem_kern *)_sem;
if (sem) {
if (sem->id) {
CloseHandle(sem->id);
@@ -75,9 +288,10 @@ SDL_DestroySemaphore(SDL_sem * sem)
}
}
-int
-SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
+static int
+SDL_SemWaitTimeout_kern(SDL_sem * _sem, Uint32 timeout)
{
+ SDL_sem_kern *sem = (SDL_sem_kern *)_sem;
int retval;
DWORD dwMilliseconds;
@@ -105,22 +319,23 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
return retval;
}
-int
-SDL_SemTryWait(SDL_sem * sem)
+static int
+SDL_SemTryWait_kern(SDL_sem * sem)
{
- return SDL_SemWaitTimeout(sem, 0);
+ return SDL_SemWaitTimeout_kern(sem, 0);
}
-int
-SDL_SemWait(SDL_sem * sem)
+static int
+SDL_SemWait_kern(SDL_sem * sem)
{
- return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
+ return SDL_SemWaitTimeout_kern(sem, SDL_MUTEX_MAXWAIT);
}
/* Returns the current count of the semaphore */
-Uint32
-SDL_SemValue(SDL_sem * sem)
+static Uint32
+SDL_SemValue_kern(SDL_sem * _sem)
{
+ SDL_sem_kern *sem = (SDL_sem_kern *)_sem;
if (!sem) {
SDL_SetError("Passed a NULL sem");
return 0;
@@ -128,9 +343,10 @@ SDL_SemValue(SDL_sem * sem)
return (Uint32)sem->count;
}
-int
-SDL_SemPost(SDL_sem * sem)
+static int
+SDL_SemPost_kern(SDL_sem * _sem)
{
+ SDL_sem_kern *sem = (SDL_sem_kern *)_sem;
if (!sem) {
return SDL_SetError("Passed a NULL sem");
}
@@ -147,6 +363,90 @@ SDL_SemPost(SDL_sem * sem)
return 0;
}
+static const SDL_sem_impl_t SDL_sem_impl_kern =
+{
+ &SDL_CreateSemaphore_kern,
+ &SDL_DestroySemaphore_kern,
+ &SDL_SemWaitTimeout_kern,
+ &SDL_SemTryWait_kern,
+ &SDL_SemWait_kern,
+ &SDL_SemValue_kern,
+ &SDL_SemPost_kern,
+};
+
+
+/**
+ * Runtime selection and redirection
+ */
+
+SDL_sem *
+SDL_CreateSemaphore(Uint32 initial_value)
+{
+ if (SDL_sem_impl_active.Create == NULL) {
+ /* Default to fallback implementation */
+ const SDL_sem_impl_t * impl = &SDL_sem_impl_kern;
+
+ if (!SDL_GetHintBoolean(SDL_HINT_WINDOWS_FORCE_SEMAPHORE_KERNEL, SDL_FALSE)) {
+ /* We already statically link to features from this Api
+ * Set (e.g. WaitForSingleObject). Dynamically loading
+ * API Sets is not explicitly documented but according to
+ * Microsoft our specific use case is legal and correct:
+ * https://github.com/microsoft/STL/pull/593#issuecomment-655799859
+ */
+ HMODULE synch120 = GetModuleHandleW(L"api-ms-win-core-synch-l1-2-0.dll");
+ if (synch120) {
+ /* Try to load required functions provided by Win 8 or newer */
+ pWaitOnAddress = (pfnWaitOnAddress) GetProcAddress(synch120, "WaitOnAddress");
+ pWakeByAddressSingle = (pfnWakeByAddressSingle) GetProcAddress(synch120, "WakeByAddressSingle");
+
+ if(pWaitOnAddress && pWakeByAddressSingle) {
+ impl = &SDL_sem_impl_atom;
+ }
+ }
+ }
+
+ /* Copy instead of using pointer to save one level of indirection */
+ SDL_memcpy(&SDL_sem_impl_active, impl, sizeof(SDL_sem_impl_active));
+ }
+ return SDL_sem_impl_active.Create(initial_value);
+}
+
+void
+SDL_DestroySemaphore(SDL_sem * sem)
+{
+ SDL_sem_impl_active.Destroy(sem);
+}
+
+int
+SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
+{
+ return SDL_sem_impl_active.WaitTimeout(sem, timeout);
+}
+
+int
+SDL_SemTryWait(SDL_sem * sem)
+{
+ return SDL_sem_impl_active.TryWait(sem);
+}
+
+int
+SDL_SemWait(SDL_sem * sem)
+{
+ return SDL_sem_impl_active.Wait(sem);
+}
+
+Uint32
+SDL_SemValue(SDL_sem * sem)
+{
+ return SDL_sem_impl_active.Value(sem);
+}
+
+int
+SDL_SemPost(SDL_sem * sem)
+{
+ return SDL_sem_impl_active.Post(sem);
+}
+
#endif /* SDL_THREAD_WINDOWS */
/* vi: set ts=4 sw=4 expandtab: */