Mercurial > sdl-ios-xcode
view Xcode/TemplatesForProjectBuilder/SDL OpenGL Application/main.c @ 3069:caefe2344f65
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.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 17 Feb 2009 05:39:18 +0000 |
parents | 4d2d0548f5b2 |
children |
line wrap: on
line source
/* Simple program: Create a blank window, wait for keypress, quit. Please see the SDL documentation for details on using the SDL API: /Developer/Documentation/SDL/docs.html */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include "SDL.h" extern void Atlantis_Init (); extern void Atlantis_Reshape (int w, int h); extern void Atlantis_Animate (); extern void Atlantis_Display (); static SDL_Surface *gScreen; static void initAttributes () { // Setup attributes we want for the OpenGL context int value; // Don't set color bit sizes (SDL_GL_RED_SIZE, etc) // Mac OS X will always use 8-8-8-8 ARGB for 32-bit screens and // 5-5-5 RGB for 16-bit screens // Request a 16-bit depth buffer (without this, there is no depth buffer) value = 16; SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, value); // Request double-buffered OpenGL // The fact that windows are double-buffered on Mac OS X has no effect // on OpenGL double buffering. value = 1; SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, value); } static void printAttributes () { // Print out attributes of the context we created int nAttr; int i; int attr[] = { SDL_GL_RED_SIZE, SDL_GL_BLUE_SIZE, SDL_GL_GREEN_SIZE, SDL_GL_ALPHA_SIZE, SDL_GL_BUFFER_SIZE, SDL_GL_DEPTH_SIZE }; char *desc[] = { "Red size: %d bits\n", "Blue size: %d bits\n", "Green size: %d bits\n", "Alpha size: %d bits\n", "Color buffer size: %d bits\n", "Depth bufer size: %d bits\n" }; nAttr = sizeof(attr) / sizeof(int); for (i = 0; i < nAttr; i++) { int value; SDL_GL_GetAttribute (attr[i], &value); printf (desc[i], value); } } static void createSurface (int fullscreen) { Uint32 flags = 0; flags = SDL_OPENGL; if (fullscreen) flags |= SDL_FULLSCREEN; // Create window gScreen = SDL_SetVideoMode (640, 480, 0, flags); if (gScreen == NULL) { fprintf (stderr, "Couldn't set 640x480 OpenGL video mode: %s\n", SDL_GetError()); SDL_Quit(); exit(2); } } static void initGL () { Atlantis_Init (); Atlantis_Reshape (gScreen->w, gScreen->h); } static void drawGL () { Atlantis_Animate (); Atlantis_Display (); } static void mainLoop () { SDL_Event event; int done = 0; int fps = 24; int delay = 1000/fps; int thenTicks = -1; int nowTicks; while ( !done ) { /* Check for events */ while ( SDL_PollEvent (&event) ) { switch (event.type) { case SDL_MOUSEMOTION: break; case SDL_MOUSEBUTTONDOWN: break; case SDL_KEYDOWN: /* Any keypress quits the app... */ case SDL_QUIT: done = 1; break; default: break; } } // Draw at 24 hz // This approach is not normally recommended - it is better to // use time-based animation and run as fast as possible drawGL (); SDL_GL_SwapBuffers (); // Time how long each draw-swap-delay cycle takes // and adjust delay to get closer to target framerate if (thenTicks > 0) { nowTicks = SDL_GetTicks (); delay += (1000/fps - (nowTicks-thenTicks)); thenTicks = nowTicks; if (delay < 0) delay = 1000/fps; } else { thenTicks = SDL_GetTicks (); } SDL_Delay (delay); } } int main(int argc, char *argv[]) { // Init SDL video subsystem if ( SDL_Init (SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } // Set GL context attributes initAttributes (); // Create GL context createSurface (0); // Get GL context attributes printAttributes (); // Init GL state initGL (); // Draw, get events... mainLoop (); // Cleanup SDL_Quit(); return 0; }