We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 570

Summary: SDL_SemWaitTimeout generic implementation is wrong
Product: SDL Reporter: Henrik Kouri <henrik.kouri>
Component: threadAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: critical    
Priority: P2 CC: sezeroz
Version: 1.2.13Keywords: target-1.2.14
Hardware: All   
OS: All   

Description Henrik Kouri 2008-03-21 13:29:22 UTC
SDL_SemWaitTimeout in src/thread/generic/SDL_syssem.c line 179 (SVN trunk):

--sem->count;

should be

if (retval == 0) {
    --sem->count;
}

Without this, sem->count will underflow on timeout effectively breaking the semaphore. It appears that the implementation has been wrong since the initial revision. Below is a sample C++ app to demonstrate the problem (should output plain zeros when correct):

#include <SDL.h>
#include <iostream>

int main(int argc, char* argv[])
{
	SDL_Init(0);
	SDL_sem* sem = SDL_CreateSemaphore(0);
	std::cout << SDL_SemValue(sem) << std::endl;
	
	SDL_SemTryWait(sem);
	std::cout << SDL_SemValue(sem) << std::endl;
	
	SDL_SemTryWait(sem);
	std::cout << SDL_SemValue(sem) << std::endl;
	
	// Breaks here! Integer underflows!
	SDL_SemWaitTimeout(sem, 10);
	std::cout << SDL_SemValue(sem) << std::endl;
	
	SDL_SemTryWait(sem);
	std::cout << SDL_SemValue(sem) << std::endl;
	
	SDL_SemTryWait(sem);
	std::cout << SDL_SemValue(sem) << std::endl;

	SDL_DestroySemaphore(sem);
	sem = NULL;
	SDL_Quit();
	return 0;
}
Comment 1 Ryan C. Gordon 2009-09-13 16:33:20 UTC
Tagging this bug with "target-1.2.14" so we can try to resolve it for SDL 1.2.14.

Please note that we may choose to resolve it as WONTFIX. This tag is largely so we have a comprehensive wishlist of bugs to examine for 1.2.14 (and so we can close bugs that we'll never fix, rather than have them live forever in Bugzilla).

--ryan.
Comment 2 Sam Lantinga 2009-09-21 00:35:21 UTC
Fixed in subversion, thanks!