xbmc/xbmc — pull request #690 by Memphiz (2012-02-13 18:27:40+00:00)
Title: [fix] - let the scripts react on the abortRequested flag before killing them

This fixes the shutdown of python scripts.

Until now we set abortRequested true and then raised the SystemExit exception on each running python thread in a running python script. There was no chance for the script to react on the abortRequested flag because we didn't even release the Global Interpreter Log.

This is an example script which didn't shutdown properly: http://pastebin.com/dyUcb4sA

This PR realises the following shutdown flow:

1. When script gets stopped - set the abortRequested flag to the scripts module
2. Release the GIL and wait for stoppedEvent to get set for up to 5 secs (thats the time where the script and its threads can react on the abortRequested flag)
3. Meanwhile - the script can shutdown and the PyThread should go out of his Process() handler
4. When Process() is nearly finished set the stoppedEvent and wait on the m_pExecuter->m_critSection which is entered by the ::stop method at that time
6. ::stop gets signaled and locks the GIL again
5. ::stop function now kills all remaining threads in that script by raising the SystemExit exception
6. ::stop leaves the m_pExecuter->m_critSection
7. ::Process handler is able to enter it now and shuts down the interpreter for that script. And done we are.

This allows a really clean shutdown. But would be nice if jimfcarroll or anyone else could have a look.

Thx to cptspiff for the open ear :)

PS: i would like this to be eden code ...