Add shutdown hooks, and fix OS X kqueue threading issue see http://lists.apple.com/archives/darwin-dev/2011/Jun/msg00016.html