def lock_file(file_path, &block) # :nodoc:
      @@file_lock.synchronize do

        # Windows does not support file locking.
        if !windows? && file_path.exist?
          file_path.open('r+') do |f|
            begin
              f.flock File::LOCK_EX
              yield
            ensure
              f.flock File::LOCK_UN
            end
          end
        else
          yield
        end

      end
    end
def lock_file(file_path, &block) # :nodoc:
      @@file_lock.synchronize do

        # Windows does not support file locking.
        if !windows? && file_path.exist?
          file_path.open('r+') do |f|
            begin
              f.flock File::LOCK_EX
              yield
            ensure
              f.flock File::LOCK_UN
            end
          end
        else
          yield
        end

      end
    end
Borrowed from Rails' ActiveSupport FileStore. Also under MIT licence.
 Lock a file for a block so only one process or thread can modify it at a time.