Reimplemented fcntl-style locking, still need to fix lock race condition.
Fixed file locking race condition.