Added file based locking.
Empty post meta cache to avoid concurrency issues.

Since the post metadata can be potentially loaded outside the lock,
flush it to ensure that it's read again inside it.
Fix wait code and use usleep() instead of sleep().