[AD] exiting

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Problem:

exit() in BeOS only seems to work from the main thread.  If I call it from
another thread, it just ends the thread.  I'm no even sure if it works
from the main thread.  This bug manefested itself when I put exit(0) in
the keyboard driver to kill the program on command-option-end (ctl-alt-end
on PC keyboards)  The keyboard driver would stop, but the program kept
running.

I then used BeOS's kill_team() function which kills all the threads in a
process.  This didn't work because either it would shutdown parts of
allegro improperly which resulted in deadlocks or the occasional crash.
The problem here was that it does not call allegro_exit()

So, I put allegro_exit() before kill_team().  This was closer to the
solution except that I would, most of the time, crash the main thread
because it was still running and calling allegro at the same time I was
shutting it down.

So, then I would automatically register the thread that called
allegro_init so that I can kill it before calling allegro_exit()

I decided to spawn a thread to kill off all the others so that I didn't
have to worry about who calls the termination function.  If I don't spawn
a killer thread, I could accidentally kill the killing thread before its
done butchering the other threads.

So, I think what I need is a sys_drv->terminate() which is called by
_al_assert and any other allegro code that may be called from a thread
that isn't the main one and which needs to terminate the program.

I think the reason is that each thread has its own atexit stack, so if I
call exit() from the keyboard driver it doesn't call allegro_exit and it
gets hung when it tries to kill the Allegro threads.  I'll have to
experiment with that however.

The other possiblity for the cause of the deadlocks is that if the thread
calling exit is a part of allegro, then it can't release its resources if
its in the middle of killing everybody else.

Whatever the reason, I don't know if its a good idea to be relying on
exit() to ever work properly in a multithreaded environment.

--
         The Phoenix - President of The Artistic Intuition Company
    Caelius * Zen-X * Mirror Reflex * Runica * X-Domain * Infinite Realms
                          http://www.io.com/~fenix



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/