# File lib/rufus/cloche.rb, line 290
    def lock(ltype, type, key, &block)

      @mutex.synchronize do
        begin

          d, f = path_for(type, key)
          fn = File.join(d, f)

          FileUtils.mkdir_p(d) if ltype == :create && ( ! File.exist?(d))
          FileUtils.touch(fn) if ltype == :create && ( ! File.exist?(fn))

          file = File.new(fn, 'r+') rescue nil

          return false if file.nil?

          file.flock(File::LOCK_EX) unless @nolock

          if ltype == :write
            Thread.pass
            21.times { return false unless File.exist?(fn) }
          end
            #
            # We got the lock, but is the file still here?
            #
            # Asking more than one time, since, at least on OSX snoleo,
            # File.exist? might say yes for a file just deleted
            # (by another process)

          block.call(file)

        ensure
          begin
            file.flock(File::LOCK_UN) unless @nolock
          rescue Exception => e
            #p [ :lock, @fpath, e ]
            #e.backtrace.each { |l| puts l }
          end
          begin
            file.close if file
          rescue Exception => e
            #p [ :close_fail, e ]
          end
        end
      end
    end