• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 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 #if __WINRT__
94     switch (WaitForSingleObjectEx(sem->id, dwMilliseconds, FALSE)) {
95 #else
96     switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
97 #endif
98     case WAIT_OBJECT_0:
99         InterlockedDecrement(&sem->count);
100         retval = 0;
101         break;
102     case WAIT_TIMEOUT:
103         retval = SDL_MUTEX_TIMEDOUT;
104         break;
105     default:
106         retval = SDL_SetError("WaitForSingleObject() failed");
107         break;
108     }
109     return retval;
110 }
111 
112 int
113 SDL_SemTryWait(SDL_sem * sem)
114 {
115     return SDL_SemWaitTimeout(sem, 0);
116 }
117 
118 int
119 SDL_SemWait(SDL_sem * sem)
120 {
121     return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
122 }
123 
124 /* Returns the current count of the semaphore */
125 Uint32
126 SDL_SemValue(SDL_sem * sem)
127 {
128     if (!sem) {
129         SDL_SetError("Passed a NULL sem");
130         return 0;
131     }
132     return (Uint32)sem->count;
133 }
134 
135 int
136 SDL_SemPost(SDL_sem * sem)
137 {
138     if (!sem) {
139         return SDL_SetError("Passed a NULL sem");
140     }
141     /* Increase the counter in the first place, because
142      * after a successful release the semaphore may
143      * immediately get destroyed by another thread which
144      * is waiting for this semaphore.
145      */
146     InterlockedIncrement(&sem->count);
147     if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
148         InterlockedDecrement(&sem->count);      /* restore */
149         return SDL_SetError("ReleaseSemaphore() failed");
150     }
151     return 0;
152 }
153 
154 #endif /* SDL_THREAD_WINDOWS */
155 
156 /* vi: set ts=4 sw=4 expandtab: */
157