Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package android.cts.util;
     17 
     18 import java.util.concurrent.Semaphore;
     19 import java.util.concurrent.TimeUnit;
     20 
     21 import android.util.Log;
     22 
     23 import junit.framework.Assert;
     24 
     25 /**
     26  * class for checking if rendering function is alive or not.
     27  * panic if watch-dog is not reset over certain amount of time
     28  */
     29 public class WatchDog implements Runnable {
     30     private static final String TAG = "WatchDog";
     31     private Thread mThread;
     32     private Semaphore mSemaphore;
     33     private volatile boolean mStopRequested;
     34     private final long mTimeoutInMilliSecs;
     35     private TimeoutCallback mCallback = null;
     36 
     37     public WatchDog(long timeoutInMilliSecs) {
     38         mTimeoutInMilliSecs = timeoutInMilliSecs;
     39     }
     40 
     41     public WatchDog(long timeoutInMilliSecs, TimeoutCallback callback) {
     42         this(timeoutInMilliSecs);
     43         mCallback = callback;
     44     }
     45 
     46     /** start watch-dog */
     47     public void start() {
     48         Log.i(TAG, "start");
     49         mStopRequested = false;
     50         mSemaphore = new Semaphore(0);
     51         mThread = new Thread(this);
     52         mThread.start();
     53     }
     54 
     55     /** stop watch-dog */
     56     public void stop() {
     57         Log.i(TAG, "stop");
     58         if (mThread == null) {
     59             return; // already finished
     60         }
     61         mStopRequested = true;
     62         mSemaphore.release();
     63         try {
     64             mThread.join();
     65         } catch (InterruptedException e) {
     66             // ignore
     67         }
     68         mThread = null;
     69         mSemaphore = null;
     70     }
     71 
     72     /** resets watch-dog, thus prevent it from panic */
     73     public void reset() {
     74         if (!mStopRequested) { // stop requested, but rendering still on-going
     75             mSemaphore.release();
     76         }
     77     }
     78 
     79     @Override
     80     public void run() {
     81         while (!mStopRequested) {
     82             try {
     83                 boolean success = mSemaphore.tryAcquire(mTimeoutInMilliSecs, TimeUnit.MILLISECONDS);
     84                 if (mCallback == null) {
     85                     Assert.assertTrue("Watchdog timed-out", success);
     86                 } else if (!success) {
     87                     mCallback.onTimeout();
     88                 }
     89             } catch (InterruptedException e) {
     90                 // this thread will not be interrupted,
     91                 // but if it happens, just check the exit condition.
     92             }
     93         }
     94     }
     95 
     96     /**
     97      * Called by the Watchdog when it has timed out.
     98      */
     99     public interface TimeoutCallback {
    100 
    101         public void onTimeout();
    102     }
    103 }
    104