1 /*
2  * Copyright (C) 2017 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 #include <atomic>
18 
19 #include "android-base/logging.h"
20 #include "jni.h"
21 #include "scoped_local_ref.h"
22 #include "scoped_primitive_array.h"
23 
24 #include "jvmti.h"
25 
26 // Test infrastructure
27 #include "jvmti_helper.h"
28 #include "test_env.h"
29 
30 namespace art {
31 namespace Test1921NativeSuspendMonitorRecursive {
32 
33 std::atomic<bool> should_pause(false);
34 std::atomic<bool> paused(false);
35 std::atomic<bool> done(false);
36 std::atomic<bool> started(false);
37 
Java_art_Test1921_nativeSpin(JNIEnv * env,jclass)38 extern "C" JNIEXPORT void JNICALL Java_art_Test1921_nativeSpin(JNIEnv* env, jclass) {
39   jrawMonitorID mon;
40   if (JvmtiErrorToException(
41       env, jvmti_env, jvmti_env->CreateRawMonitor("Test1921 monitor", &mon))) {
42     return;
43   }
44   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorEnter(mon))) {
45     return;
46   }
47   while (!done.load()) {
48     while (should_pause.load()) {
49       paused.store(true);
50     }
51     paused.store(false);
52     if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorEnter(mon))) {
53       return;
54     }
55     started.store(true);
56     if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon))) {
57       return;
58     }
59   }
60   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon))) {
61     return;
62   }
63 }
64 
Java_art_Test1921_isNativeThreadSpinning(JNIEnv *,jclass)65 extern "C" JNIEXPORT jboolean JNICALL Java_art_Test1921_isNativeThreadSpinning(JNIEnv*, jclass) {
66   return started.load();
67 }
68 
Java_art_Test1921_pause(JNIEnv *,jclass)69 extern "C" JNIEXPORT void JNICALL Java_art_Test1921_pause(JNIEnv*, jclass) {
70   should_pause.store(true);
71   paused.store(false);
72   while (!paused.load()) { }
73 }
74 
Java_art_Test1921_resume(JNIEnv *,jclass)75 extern "C" JNIEXPORT void JNICALL Java_art_Test1921_resume(JNIEnv*, jclass) {
76   should_pause.store(false);
77   while (paused.load()) { }
78 }
79 
Java_art_Test1921_reset(JNIEnv *,jclass)80 extern "C" JNIEXPORT void JNICALL Java_art_Test1921_reset(JNIEnv*, jclass) {
81   started.store(false);
82 }
83 
Java_art_Test1921_nativeFinish(JNIEnv *,jclass)84 extern "C" JNIEXPORT void JNICALL Java_art_Test1921_nativeFinish(JNIEnv*, jclass) {
85   done.store(true);
86 }
87 
88 }  // namespace Test1921NativeSuspendMonitorRecursive
89 }  // namespace art
90