/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.heartbeat;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * This class can be used for controlling heartbeat execution of threads.
 *
 * In particular, the {@link ScheduledTimer} blocks on the {@link #tick()} method, waiting for
 * someone to invoke the {@link #schedule()} method.
 *
 * The contract of this class is that the {@link #schedule()} method should only be called after the
 * {@link #tick()} was called and that there is exactly one {@link #schedule()} call per
 * {@link #tick()} call.
 *
 * The {@link #schedule()} method is not meant to be invoked directly. Instead, the
 * {@link HeartbeatScheduler} class should be used.
 */
public final class ScheduledTimer implements HeartbeatTimer {
  private final String mThreadName;
  private final Lock mLock;
  private final Condition mCondition;

  /**
   * Creates a new instance of {@link ScheduledTimer}.
   *
   * @param threadName the thread name
   * @param intervalMs the heartbeat interval (unused)
   */
  public ScheduledTimer(String threadName, long intervalMs) {
    mThreadName = threadName;
    mLock = new ReentrantLock();
    mCondition = mLock.newCondition();
  }

  /**
   * @return the thread name
   */
  public String getThreadName() {
    return mThreadName;
  }

  /**
   * Schedules execution of the heartbeat.
   */
  protected void schedule() {
    mLock.lock();
    try {
      mCondition.signal();
    } finally {
      mLock.unlock();
    }
  }

  /**
   * Waits until the heartbeat is scheduled for execution.
   *
   * @throws InterruptedException if the thread is interrupted while waiting
   */
  public synchronized void tick() throws InterruptedException {
    mLock.lock();
    try {
      HeartbeatScheduler.addTimer(this);
      mCondition.await();
    } finally {
      mLock.unlock();
    }
  }
}

@@ -1702,16 +1702,131 @@
 ndition;
+%0A  /** True when schedule() has been called, but tick() hasn't finished. **/%0A  private volatile boolean mScheduled;
 %0A%0A  /**%0A
@@ -2149,16 +2149,40 @@
 tion();%0A
+    mScheduled = false;%0A
   %7D%0A%0A  /
@@ -2391,24 +2391,177 @@
 ;%0A    try %7B%0A
+      if (mScheduled) %7B%0A        throw new IllegalStateException(%22Called schedule twice without waiting for any ticks%22);%0A      %7D%0A      mScheduled = true;%0A
       mCondi
@@ -2920,26 +2920,141 @@
     
-mCondition.await()
+// Wait in a loop to handle spurious wakeups%0A      while (!mScheduled) %7B%0A        mCondition.await();%0A      %7D%0A      mScheduled = false
 ;%0A  

Handle spurious wakeups in ScheduledTimer.tick()