1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #if SDL_THREAD_WINDOWS
24 
25 /* Mutex functions using the Win32 API */
26 
27 #include "../../core/windows/SDL_windows.h"
28 
29 #include "SDL_mutex.h"
30 
31 
32 struct SDL_mutex
33 {
34     CRITICAL_SECTION cs;
35 };
36 
37 /* Create a mutex */
38 SDL_mutex *
SDL_CreateMutex(void)39 SDL_CreateMutex(void)
40 {
41     SDL_mutex *mutex;
42 
43     /* Allocate mutex memory */
44     mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
45     if (mutex) {
46         /* Initialize */
47         /* On SMP systems, a non-zero spin count generally helps performance */
48 #if __WINRT__
49         InitializeCriticalSectionEx(&mutex->cs, 2000, 0);
50 #else
51         InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
52 #endif
53     } else {
54         SDL_OutOfMemory();
55     }
56     return (mutex);
57 }
58 
59 /* Free the mutex */
60 void
SDL_DestroyMutex(SDL_mutex * mutex)61 SDL_DestroyMutex(SDL_mutex * mutex)
62 {
63     if (mutex) {
64         DeleteCriticalSection(&mutex->cs);
65         SDL_free(mutex);
66     }
67 }
68 
69 /* Lock the mutex */
70 int
SDL_LockMutex(SDL_mutex * mutex)71 SDL_LockMutex(SDL_mutex * mutex)
72 {
73     if (mutex == NULL) {
74         return SDL_SetError("Passed a NULL mutex");
75     }
76 
77     EnterCriticalSection(&mutex->cs);
78     return (0);
79 }
80 
81 /* TryLock the mutex */
82 int
SDL_TryLockMutex(SDL_mutex * mutex)83 SDL_TryLockMutex(SDL_mutex * mutex)
84 {
85     int retval = 0;
86     if (mutex == NULL) {
87         return SDL_SetError("Passed a NULL mutex");
88     }
89 
90     if (TryEnterCriticalSection(&mutex->cs) == 0) {
91         retval = SDL_MUTEX_TIMEDOUT;
92     }
93     return retval;
94 }
95 
96 /* Unlock the mutex */
97 int
SDL_UnlockMutex(SDL_mutex * mutex)98 SDL_UnlockMutex(SDL_mutex * mutex)
99 {
100     if (mutex == NULL) {
101         return SDL_SetError("Passed a NULL mutex");
102     }
103 
104     LeaveCriticalSection(&mutex->cs);
105     return (0);
106 }
107 
108 #endif /* SDL_THREAD_WINDOWS */
109 
110 /* vi: set ts=4 sw=4 expandtab: */
111