'Acquire a cache lock for this tile.
Returns nothing, but (TODO) blocks until the lock has been acquired.
Lock is implemented as a row in the "locks" table.'

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

    sys.stderr.write(('lock    %d/%d/%d,    %s' % (coord.zoom, coord.column, coord.row, format)))
    due = (time.time() + layer.stale_lock_timeout)
    while True:
        if (time.time() > due):
            sys.stderr.write(('...force    %d/%d/%d,    %s' % (coord.zoom, coord.column, coord.row, format)))
            self.unlock(layer, coord, format)
        db = connect(self.dbpath, isolation_level='EXCLUSIVE').cursor()
        try:
            db.execute('INSERT    INTO    locks\n                                                                                                                        (row,    column,    zoom,    format)\n                                                                                                                        VALUES    (?,    ?,    ?,    ?)', (coord.row, coord.column, coord.zoom, format))
        except IntegrityError:
            db.connection.close()
            time.sleep(0.2)
            continue
        else:
            db.connection.commit()
            db.connection.close()
            break
