Pulled out into utility function.

I didn't see any other way to do it. If we only cared about shutdown hooks we registered, we could keep a list of the threads and check it, but this wouldn't work for anything registered by external libraries.