rewrite to support new switch_thread_init and cleanup function.