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 180

Summary: Killing threads doesn't work on Mac OS X
Product: SDL Reporter: Christian Walther <cwalther>
Component: threadAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED WONTFIX QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: HG 1.2   
Hardware: PowerPC   
OS: Mac OS X 10.4 (PPC)   
Attachments: Test program

Description Christian Walther 2006-03-25 03:20:24 UTC
In test/testhread, thread #2 continues to run after it's supposed to be killed. Tested on Mac OS X 10.4.5 on both PPC and i386, both with the Xcode-built framework and the autoconf-built dylib.

I haven't been able to figure out why so far (but I'm no pthreads expert). pthread_cancel() in SDL_SYS_KillThread() returns no error, but it doesn't seem to have any effect.
Comment 1 Sam Lantinga 2006-04-13 10:07:05 UTC
SDL_KillThread() will be deprecated and possibly removed n SDL 1.3, since there are too many cases where killing a thread is extremely dangerous.  I'm not sure if it's worth fixing this at this point.

If anybody knows some magic incantation to make this work, please let me know.
Comment 2 Ryan C. Gordon 2006-04-27 05:53:39 UTC
Did some experimentation...SDL_KillThread() is actually blocking in pthread_cancel() on an Intel iMac running 10.4.6, unless I explicitly call pthread_detach() first...detaching doesn't kill the thread, either, it just prevents SDL_KillThread() from blocking indefinitely.

Tried adding this to SDL_SYS_SetupThread() in src/threads/pthread/SDL_systhread.c, no dice, either.

    pthread_setcanceltype(PTHREAD_CANCEL_ENABLE, &oldstate);


The pthread_kill() path takes down the whole process--I guess it delivers the signal to every thread on Mac OS X, or just the process generally--so I guess that's a non-starter.

If my test program added a pthread_testcancel() call to its loop, it would in fact die.

So what we could do, I guess, is this:
- Add a call to pthread_detach() right before pthread_cancel()
- Sprinkle some calls to pthread_testcancel() into a few places, like SDL_Delay(). There are several cancelation points in the C runtime (file i/o, etc), which will catch some non-SDL things, but lots of Mac OS X, unlike lots of Unix, doesn't necessarily hit these cancellation points to do common things...I suspect file i/o done at the Carbon layer won't trigger it. It also doesn't help if the thread is doing "while (1) {}".

Alternately:
- Flag this bug as WONTFIX and tell people "don't use SDL_KillThread()."

Sam, thoughts?

--ryan.

Comment 3 Ryan C. Gordon 2006-04-27 05:55:38 UTC
Created attachment 107 [details]
Test program


Here's my test case. Build like this:

  gcc -o testkill testkill.c `sdl-config --cflags --libs`


If you want to have the thread call pthread_testcancel(), build like this:

  gcc -DUSE_TESTCANCEL -o testkill testkill.c `sdl-config --cflags --libs`

--ryan.
Comment 4 Sam Lantinga 2006-04-27 05:57:32 UTC
"don't use SDL_KillThread()"