Remove /tmp/bitmask.lock on quit. Closes #5866.

- Add a platform independent release_lock helper, so all the SO dependent
code goes inside the locks file.
- Do some code cleanup.