1 /*
2  * Copyright (C) 2018 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 
17 package android.media.audio.cts;
18 
19 import android.util.Log;
20 
21 import java.util.function.BooleanSupplier;
22 
23 /**
24  * Helper class to simplify the handling of spurious wakeups in Object.wait()
25  */
26 final class SafeWaitObject {
27     /**
28      * Causes the current thread to wait until this object is notified and stopWaiting returns true,
29      * or a specified amount of time has elapsed.
30      * Unlike Object.wait(), it will not throw an InterruptedException, and will instead retry
31      * waiting until the deadline expires.
32      *
33      * @see Object#wait()
34      * @param timeoutMs The maximum time to wait in milliseconds.
35      * @param stopWaiting Predicate which returns false if the waiting should be continued
36      * @return false if the predicate stopWaiting still evaluates to false after the timeout expired
37      *         , otherwise true.
38      */
waitFor(long timeoutMs, BooleanSupplier stopWaiting)39     public boolean waitFor(long timeoutMs, BooleanSupplier stopWaiting) {
40         final long deadline = System.currentTimeMillis() + timeoutMs;
41         synchronized (this) {
42             while (!stopWaiting.getAsBoolean()) {
43                 final long timeToWait = deadline - System.currentTimeMillis();
44                 if (timeToWait <= 0) {
45                     return false;
46                 }
47                 try {
48                     this.wait(timeToWait);
49                 } catch (InterruptedException e) {
50                     Log.v("SafeWaitObject", "waiting interrupted, resuming");
51                 }
52             }
53         }
54         return true;
55     }
56 
57     /**
58      * Blocks the current thread until stopWaiting returns true
59      * @param timeoutMs the maximum amount of time to block for
60      * @param periodMs the period between two checking attempts
61      * @param stopWaiting Predicate which returns false if the waiting should be continued
62      * @return false if the predicate stopWaiting still evaluates to false after the timeout
63      *          expired, otherwise true.
64      */
checkConditionFor(long timeoutMs, long periodMs, BooleanSupplier stopWaiting)65     public static boolean checkConditionFor(long timeoutMs, long periodMs,
66             BooleanSupplier stopWaiting) {
67         final long deadline = System.currentTimeMillis() + timeoutMs;
68         while (!stopWaiting.getAsBoolean() && (System.currentTimeMillis() < deadline)) {
69             try {
70                 Thread.sleep(periodMs);
71             } catch (InterruptedException e) {
72                 Log.v("SafeWaitObject", "sleeping interrupted, resuming");
73             }
74         }
75         return true;
76     }
77 }
78