private void tryWithMutex(MutableAtomicValue<byte[]> result, MakeValue makeValue)
      throws Exception {
    long startMs = System.currentTimeMillis();
    int retryCount = 0;

    if (mutex.acquire(promotedToLock.getMaxLockTime(), promotedToLock.getMaxLockTimeUnit())) {
      try {
        boolean done = false;
        while (!done) {
          result.stats.incrementPromotedTries();
          if (tryOnce(result, makeValue)) {
            result.succeeded = true;
            done = true;
          } else {
            if (!promotedToLock
                .getRetryPolicy()
                .allowRetry(
                    retryCount++,
                    System.currentTimeMillis() - startMs,
                    RetryLoop.getDefaultRetrySleeper())) {
              done = true;
            }
          }
        }
      } finally {
        mutex.release();
      }
    }

    result.stats.setPromotedTimeMs(System.currentTimeMillis() - startMs);
  }
 /**
  * Creates in mutex promotion mode. The optimistic lock will be tried first using the given retry
  * policy. If the increment does not succeed, a {@link InterProcessMutex} will be tried with its
  * own retry policy
  *
  * @param client the client
  * @param path path to hold the value
  * @param retryPolicy the retry policy to use
  * @param promotedToLock the arguments for the mutex promotion
  */
 public DistributedAtomicValue(
     CuratorFramework client,
     String path,
     RetryPolicy retryPolicy,
     PromotedToLock promotedToLock) {
   this.client = client;
   this.path = path;
   this.retryPolicy = retryPolicy;
   this.promotedToLock = promotedToLock;
   mutex =
       (promotedToLock != null) ? new InterProcessMutex(client, promotedToLock.getPath()) : null;
 }