Move chThdShouldTerminate to top of write loop.