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 - SDL_SemWaitTimeout generic implementation is wrong
Summary: SDL_SemWaitTimeout generic implementation is wrong
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: thread (show other bugs)
Version: 1.2.13
Hardware: All All
: P2 critical
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords: target-1.2.14
Depends on:
Blocks:
 
Reported: 2008-03-21 13:29 UTC by Henrik Kouri
Modified: 2009-09-21 00:35 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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!