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 /* Semaphore functions using the Win32 API */
26 
27 #include "../../core/windows/SDL_windows.h"
28 
29 #include "SDL_thread.h"
30 
31 struct SDL_semaphore
32 {
33     HANDLE id;
34     LONG count;
35 };
36 
37 
38 /* Create a semaphore */
39 SDL_sem *
SDL_CreateSemaphore(Uint32 initial_value)40 SDL_CreateSemaphore(Uint32 initial_value)
41 {
42     SDL_sem *sem;
43 
44     /* Allocate sem memory */
45     sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
46     if (sem) {
47         /* Create the semaphore, with max value 32K */
48 #if __WINRT__
49         sem->id = CreateSemaphoreEx(NULL, initial_value, 32 * 1024, NULL, 0, SEMAPHORE_ALL_ACCESS);
50 #else
51         sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
52 #endif
53         sem->count = initial_value;
54         if (!sem->id) {
55             SDL_SetError("Couldn't create semaphore");
56             SDL_free(sem);
57             sem = NULL;
58         }
59     } else {
60         SDL_OutOfMemory();
61     }
62     return (sem);
63 }
64 
65 /* Free the semaphore */
66 void
SDL_DestroySemaphore(SDL_sem * sem)67 SDL_DestroySemaphore(SDL_sem * sem)
68 {
69     if (sem) {
70         if (sem->id) {
71             CloseHandle(sem->id);
72             sem->id = 0;
73         }
74         SDL_free(sem);
75     }
76 }
77 
78 int
SDL_SemWaitTimeout(SDL_sem * sem,Uint32 timeout)79 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
80 {
81     int retval;
82     DWORD dwMilliseconds;
83 
84     if (!sem) {
85         return SDL_SetError("Passed a NULL sem");
86     }
87 
88     if (timeout == SDL_MUTEX_MAXWAIT) {
89         dwMilliseconds = INFINITE;
90     } else {
91         dwMilliseconds = (DWORD) timeout;
92     }
93     switch (WaitForSingleObjectEx(sem->id, dwMilliseconds, FALSE)) {
94     case WAIT_OBJECT_0:
95         InterlockedDecrement(&sem->count);
96         retval = 0;
97         break;
98     case WAIT_TIMEOUT:
99         retval = SDL_MUTEX_TIMEDOUT;
100         break;
101     default:
102         retval = SDL_SetError("WaitForSingleObject() failed");
103         break;
104     }
105     return retval;
106 }
107 
108 int
SDL_SemTryWait(SDL_sem * sem)109 SDL_SemTryWait(SDL_sem * sem)
110 {
111     return SDL_SemWaitTimeout(sem, 0);
112 }
113 
114 int
SDL_SemWait(SDL_sem * sem)115 SDL_SemWait(SDL_sem * sem)
116 {
117     return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
118 }
119 
120 /* Returns the current count of the semaphore */
121 Uint32
SDL_SemValue(SDL_sem * sem)122 SDL_SemValue(SDL_sem * sem)
123 {
124     if (!sem) {
125         SDL_SetError("Passed a NULL sem");
126         return 0;
127     }
128     return (Uint32)sem->count;
129 }
130 
131 int
SDL_SemPost(SDL_sem * sem)132 SDL_SemPost(SDL_sem * sem)
133 {
134     if (!sem) {
135         return SDL_SetError("Passed a NULL sem");
136     }
137     /* Increase the counter in the first place, because
138      * after a successful release the semaphore may
139      * immediately get destroyed by another thread which
140      * is waiting for this semaphore.
141      */
142     InterlockedIncrement(&sem->count);
143     if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
144         InterlockedDecrement(&sem->count);      /* restore */
145         return SDL_SetError("ReleaseSemaphore() failed");
146     }
147     return 0;
148 }
149 
150 #endif /* SDL_THREAD_WINDOWS */
151 
152 /* vi: set ts=4 sw=4 expandtab: */
153