std: Destroy the TASK_LOCK mutex when we're done

This requires a little bit of a dance with the tasks which are exiting, but it's
easy enough to know when we're the last ones looking at the mutex.
Various test fixes and rebase conflicts