rewrite mutex implementation to fix some subtle bugs, also add some more locking...