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
Don't defer action to thread here.

We already are running the methods in a thread from the backend.
Move reactor import to the top.
Work in progress...