1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package lockedregioncodeinjection; 15 16 public class TestTarget { 17 public static int boostCount = 0; 18 public static int unboostCount = 0; 19 public static int invokeCount = 0; 20 public static boolean nextUnboostThrows = false; 21 22 // If this is not null, then this is the lock under test. The lock must not be held when boost() 23 // or unboost() are called. 24 public static Object mLock = null; 25 public static int boostCountLocked = 0; 26 public static int unboostCountLocked = 0; 27 boost()28 public static void boost() { 29 boostCount++; 30 if (mLock != null && Thread.currentThread().holdsLock(mLock)) { 31 boostCountLocked++; 32 } 33 } 34 unboost()35 public static void unboost() { 36 if (nextUnboostThrows) { 37 nextUnboostThrows = false; 38 throw new RuntimeException(); 39 } 40 unboostCount++; 41 if (mLock != null && Thread.currentThread().holdsLock(mLock)) { 42 unboostCountLocked++; 43 } 44 } 45 invoke()46 public static void invoke() { 47 invokeCount++; 48 } 49 resetCount()50 public static void resetCount() { 51 boostCount = 0; 52 unboostCount = 0; 53 invokeCount = 0; 54 } 55 synchronizedCall()56 public synchronized void synchronizedCall() { 57 invoke(); 58 } 59 synchronizedCallReturnInt()60 public synchronized int synchronizedCallReturnInt() { 61 invoke(); 62 return 0; 63 } 64 synchronizedCallReturnObject()65 public synchronized Object synchronizedCallReturnObject() { 66 invoke(); 67 return this; 68 } 69 synchronizedThrowsOnUnboost()70 public void synchronizedThrowsOnUnboost() { 71 nextUnboostThrows = true; 72 synchronized(this) { 73 invoke(); 74 } 75 } 76 } 77