docs: def get_lock(lock_dir, **kw):
      """Obtain lock on compilation directory.
  
      Parameters
      ----------
      lock_dir : str
          Lock directory.
      kw : dict
          Additional arguments to be forwarded to the `lock` function when
          acquiring the lock.
  
      Notes
      -----
      We can lock only on 1 directory at a time.
  
      """
      if not hasattr(get_lock, 'n_lock'):
          # Initialization.
          get_lock.n_lock = 0
          if not hasattr(get_lock, 'lock_is_enabled'):
              # Enable lock by default.
              get_lock.lock_is_enabled = True
          get_lock.lock_dir = lock_dir
          get_lock.unlocker = Unlocker(get_lock.lock_dir)
      else:
          if lock_dir != get_lock.lock_dir:
              # Compilation directory has changed.
              # First ensure all old locks were released.
              assert get_lock.n_lock == 0
              # Update members for new compilation directory.
              get_lock.lock_dir = lock_dir
              get_lock.unlocker = Unlocker(get_lock.lock_dir)
  
      if get_lock.lock_is_enabled:
          # Only really try to acquire the lock if we do not have it already.
          if get_lock.n_lock == 0:
              lock(get_lock.lock_dir, **kw)
              atexit.register(Unlocker.unlock, get_lock.unlocker)
              # Store time at which the lock was set.
              get_lock.start_time = time.time()
          else:
              # Check whether we need to 'refresh' the lock. We do this
              # every 'config.compile.timeout / 2' seconds to ensure
              # no one else tries to override our lock after their
              # 'config.compile.timeout' timeout period.
              if get_lock.start_time is None:
                  # This should not happen. So if this happen, clean up
                  # the lock state and raise an error.
                  while get_lock.n_lock > 0:
                      release_lock()
                  raise Exception(
                      "For some unknow reason, the lock was already taken,"
                      " but no start time was registered.")
              now = time.time()
              if now - get_lock.start_time > TIMEOUT:
                  lockpath = os.path.join(get_lock.lock_dir, 'lock')
                  logger.info('Refreshing lock %s', str(lockpath))
                  refresh_lock(lockpath)
                  get_lock.start_time = now
      get_lock.n_lock += 1