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.
Also, do some code cleanup.