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.
Merge remote-tracking branch 'refs/remotes/ivan/bug/5866_bitmask-lock-not-removed' into develop