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 Test1943SuspendRawMonitorExit {
32 
33 jrawMonitorID mon;
34 std::atomic<bool> locked(false);
35 
Java_art_Test1943_nativeRun(JNIEnv * env,jclass)36 extern "C" JNIEXPORT void JNICALL Java_art_Test1943_nativeRun(JNIEnv* env, jclass) {
37   if (JvmtiErrorToException(
38       env, jvmti_env, jvmti_env->CreateRawMonitor("Test1943 monitor", &mon))) {
39     return;
40   }
41   // Grab the monitor twice
42   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorEnter(mon))) {
43     return;
44   }
45   locked.store(true);
46   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorWait(mon, 0))) {
47     return;
48   }
49   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon))) {
50     return;
51   }
52   locked.store(false);
53 }
54 
55 
Java_art_Test1943_waitForPause(JNIEnv *,jclass)56 extern "C" JNIEXPORT void JNICALL Java_art_Test1943_waitForPause(JNIEnv*, jclass) {
57   while (!locked.load()) { }
58 }
59 
Java_art_Test1943_nativeNotify(JNIEnv * env,jclass)60 extern "C" JNIEXPORT void JNICALL Java_art_Test1943_nativeNotify(JNIEnv* env, jclass) {
61   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorEnter(mon))) {
62     return;
63   }
64   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorNotifyAll(mon))) {
65     return;
66   }
67   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon))) {
68     return;
69   }
70 }
71 
Java_art_Test1943_grabRawMonitor(JNIEnv * env,jclass)72 extern "C" JNIEXPORT void JNICALL Java_art_Test1943_grabRawMonitor(JNIEnv* env, jclass) {
73   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorEnter(mon))) {
74     return;
75   }
76   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon))) {
77     return;
78   }
79 }
80 
81 }  // namespace Test1943SuspendRawMonitorExit
82 }  // namespace art
83