• Elly Fong-Jones's avatar
    core: spruce up TakeNamedLock and friends · b401fd2a
    Elly Fong-Jones authored
    TakeNamedLock has a "waiting" mode that works like this:
    
      for (int i = 0; i < 10; i++)
        if (TryLock())
          break;
        usleep(i * 100);
    
    which means that in "waiting" mode, if the lock is unavailable
    for the duration, this function will block the calling thread
    (with the message loop not spinning) for up to 4.5 seconds. However,
    there are two problems with this design:
    
    * The caller has no way to know whether "waiting" is appropriate or not
    * The caller has to handle failure to acquire the lock either way
    
    If retries are needed to deal with external conditions changing that might
    have freed the lock up (another process exiting, e.g.) those retries should
    be done at higher levels with more context.
    
    More concretely, the only actual production use of this function is in
    CloudPrintServiceProcessMain:
    
        std::unique_ptr<ServiceProcessState> state(new ServiceProcessState);
        if (!state->Initialize())
          return 0;
    
    This change has TakeNamedLock make a single non-blocking attempt to
    take the lock, and moves the retry logic up into
    CloudPrintServiceProcessMain.
    
    Since I was here anyway, I converted the API to use unique_ptr, since it
    returns ownership of the lock if it succeeds.
    
    Bug: 1052430
    Change-Id: Ib87b23cc821fe59e69b76ffb3875917027bfdc4e
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2057530Reviewed-by: default avatarNico Weber <thakis@chromium.org>
    Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#744283}
    b401fd2a
mock_launchd.mm 5.52 KB