2001-04-26 16:45:43 +00:00
|
|
|
/*
|
2011-04-08 13:03:26 -07:00
|
|
|
Simple DirectMedia Layer
|
2015-05-26 06:27:46 -07:00
|
|
|
Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
2001-04-26 16:45:43 +00:00
|
|
|
*/
|
2013-11-24 23:56:17 -05:00
|
|
|
#include "../../SDL_internal.h"
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2011-10-31 05:56:58 -04:00
|
|
|
#if SDL_THREAD_WINDOWS
|
|
|
|
|
2001-04-26 16:45:43 +00:00
|
|
|
/* Semaphore functions using the Win32 API */
|
|
|
|
|
2011-01-24 21:20:30 -08:00
|
|
|
#include "../../core/windows/SDL_windows.h"
|
2001-04-26 16:45:43 +00:00
|
|
|
|
|
|
|
#include "SDL_thread.h"
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
struct SDL_semaphore
|
|
|
|
{
|
|
|
|
HANDLE id;
|
2011-01-24 15:46:11 -08:00
|
|
|
LONG count;
|
2001-04-26 16:45:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Create a semaphore */
|
2006-07-10 21:04:37 +00:00
|
|
|
SDL_sem *
|
|
|
|
SDL_CreateSemaphore(Uint32 initial_value)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
SDL_sem *sem;
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* Allocate sem memory */
|
|
|
|
sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
|
|
|
|
if (sem) {
|
|
|
|
/* Create the semaphore, with max value 32K */
|
|
|
|
sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
|
|
|
|
sem->count = initial_value;
|
|
|
|
if (!sem->id) {
|
|
|
|
SDL_SetError("Couldn't create semaphore");
|
|
|
|
SDL_free(sem);
|
|
|
|
sem = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
}
|
|
|
|
return (sem);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the semaphore */
|
2006-07-10 21:04:37 +00:00
|
|
|
void
|
|
|
|
SDL_DestroySemaphore(SDL_sem * sem)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
if (sem) {
|
|
|
|
if (sem->id) {
|
|
|
|
CloseHandle(sem->id);
|
|
|
|
sem->id = 0;
|
|
|
|
}
|
|
|
|
SDL_free(sem);
|
|
|
|
}
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
int retval;
|
|
|
|
DWORD dwMilliseconds;
|
|
|
|
|
|
|
|
if (!sem) {
|
2013-03-31 12:48:50 -04:00
|
|
|
return SDL_SetError("Passed a NULL sem");
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout == SDL_MUTEX_MAXWAIT) {
|
|
|
|
dwMilliseconds = INFINITE;
|
|
|
|
} else {
|
|
|
|
dwMilliseconds = (DWORD) timeout;
|
|
|
|
}
|
|
|
|
switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
|
|
|
|
case WAIT_OBJECT_0:
|
Date: Thu, 27 Dec 2007 07:38:25 +0000
From: John Bartholomew
Subject: [SDL] SDL Semaphore implementation broken on Windows?
Hi,
Over the past couple of days, I've been battling with SDL, SDL_Mixer and SMPEG to try to find an audio hang bug. I believe I've found the problem, which I think is a race condition inside SDL's semaphore implementation (at least the Windows implementation). The semaphore code uses Windows' built in semaphore functions, but it also maintains a separate count value. This count value is updated with bare increment and decrement operations in SemPost and SemWaitTimeout - no locking primitives to protect them.
In tracking down the apparent audio bug, I found that at some point a semaphore's count value was being decremented to -1, which is clearly not a valid value for it to take.
I'm still not certain exactly what sequence of operations is occuring for this to happen, but I believe that overall it's a race condition between a thread calling SemPost (which increments the count) and the thread on the other end calling SemWait (which decrements it).
I will try to make a test case to verify this, but I'm not sure if I'll be able to (threading errors being difficult to reproduce even in the best circumstances).
However, assuming this is the cause of my problems, there is a very
simple fix:
Windows provides InterlockedIncrement() and InterlockedDecrement()
functions to perform increments and decrements which are guaranteed to be atomic. So the fix is in thread/win32/SDL_syssem.c: replace occurrences of --sem->count with InterlockedDecrement(&sem->count); and replace occurrences of ++sem->count with InterlockedIncrement(&sem->count);
This is using SDL v1.2.12, built with VC++ 2008 Express, running on a
Core 2 duo processor.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403535
2009-02-17 05:39:18 +00:00
|
|
|
InterlockedDecrement(&sem->count);
|
2006-07-10 21:04:37 +00:00
|
|
|
retval = 0;
|
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
retval = SDL_MUTEX_TIMEDOUT;
|
|
|
|
break;
|
|
|
|
default:
|
2013-03-31 12:48:50 -04:00
|
|
|
retval = SDL_SetError("WaitForSingleObject() failed");
|
2006-07-10 21:04:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return retval;
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_SemTryWait(SDL_sem * sem)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
return SDL_SemWaitTimeout(sem, 0);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_SemWait(SDL_sem * sem)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the current count of the semaphore */
|
2006-07-10 21:04:37 +00:00
|
|
|
Uint32
|
|
|
|
SDL_SemValue(SDL_sem * sem)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
if (!sem) {
|
|
|
|
SDL_SetError("Passed a NULL sem");
|
|
|
|
return 0;
|
|
|
|
}
|
2010-07-05 12:32:44 -07:00
|
|
|
return (Uint32)sem->count;
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_SemPost(SDL_sem * sem)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
if (!sem) {
|
2013-03-31 12:48:50 -04:00
|
|
|
return SDL_SetError("Passed a NULL sem");
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
/* Increase the counter in the first place, because
|
|
|
|
* after a successful release the semaphore may
|
|
|
|
* immediately get destroyed by another thread which
|
|
|
|
* is waiting for this semaphore.
|
|
|
|
*/
|
Date: Thu, 27 Dec 2007 07:38:25 +0000
From: John Bartholomew
Subject: [SDL] SDL Semaphore implementation broken on Windows?
Hi,
Over the past couple of days, I've been battling with SDL, SDL_Mixer and SMPEG to try to find an audio hang bug. I believe I've found the problem, which I think is a race condition inside SDL's semaphore implementation (at least the Windows implementation). The semaphore code uses Windows' built in semaphore functions, but it also maintains a separate count value. This count value is updated with bare increment and decrement operations in SemPost and SemWaitTimeout - no locking primitives to protect them.
In tracking down the apparent audio bug, I found that at some point a semaphore's count value was being decremented to -1, which is clearly not a valid value for it to take.
I'm still not certain exactly what sequence of operations is occuring for this to happen, but I believe that overall it's a race condition between a thread calling SemPost (which increments the count) and the thread on the other end calling SemWait (which decrements it).
I will try to make a test case to verify this, but I'm not sure if I'll be able to (threading errors being difficult to reproduce even in the best circumstances).
However, assuming this is the cause of my problems, there is a very
simple fix:
Windows provides InterlockedIncrement() and InterlockedDecrement()
functions to perform increments and decrements which are guaranteed to be atomic. So the fix is in thread/win32/SDL_syssem.c: replace occurrences of --sem->count with InterlockedDecrement(&sem->count); and replace occurrences of ++sem->count with InterlockedIncrement(&sem->count);
This is using SDL v1.2.12, built with VC++ 2008 Express, running on a
Core 2 duo processor.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403535
2009-02-17 05:39:18 +00:00
|
|
|
InterlockedIncrement(&sem->count);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
|
2009-02-17 05:57:54 +00:00
|
|
|
InterlockedDecrement(&sem->count); /* restore */
|
2013-03-31 12:48:50 -04:00
|
|
|
return SDL_SetError("ReleaseSemaphore() failed");
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
|
2011-10-31 05:56:58 -04:00
|
|
|
#endif /* SDL_THREAD_WINDOWS */
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|