Execute thread-local-storage callbacks (GitHub #11).

Pull request by @glysbaysb, cleaned up and adjusted to match code style.