fix concurrency issue with LockFile

there is a subtle concurrent issue with LockFile.  After discussing with David, we have fixed it, simplified the codes as well as added unit tests.  

I also write a simple console app doing a full test continuously for 1 hour to ensure everything works as expected and no other issues (the old codes would fail in the first few seconds).