'Acquire a cache lock for this tile.
Returns nothing, but blocks until the lock has been acquired.
Lock is implemented as an empty directory next to the tile file.'

def lock(self, layer, coord, format):

    lockpath = self._lockpath(layer, coord, format)
    due = (time.time() + layer.stale_lock_timeout)
    while True:
        try:
            umask_old = os.umask(self.umask)
            if (time.time() > due):
                try:
                    os.rmdir(lockpath)
                except OSError:
                    pass
            os.makedirs(lockpath, (511 & (~ self.umask)))
            break
        except OSError as e:
            if (e.errno != 17):
                raise
            time.sleep(0.2)
        finally:
            os.umask(umask_old)
