xref: /aosp_15_r20/art/test/1951-monitor-enter-no-suspend/raw_monitor.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2013 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include <atomic>
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker // Test infrastructure
22*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
23*795d594fSAndroid Build Coastguard Worker #include "scoped_local_ref.h"
24*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art {
27*795d594fSAndroid Build Coastguard Worker namespace Test1951MonitorEnterNoSuspend {
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker using RawMonitorEnterNoSuspend = jvmtiError(*)(jvmtiEnv* env, jrawMonitorID mon);
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker template <typename T>
Dealloc(T * t)32*795d594fSAndroid Build Coastguard Worker static void Dealloc(T* t) {
33*795d594fSAndroid Build Coastguard Worker   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(t));
34*795d594fSAndroid Build Coastguard Worker }
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker template <typename T, typename ...Rest>
Dealloc(T * t,Rest...rs)37*795d594fSAndroid Build Coastguard Worker static void Dealloc(T* t, Rest... rs) {
38*795d594fSAndroid Build Coastguard Worker   Dealloc(t);
39*795d594fSAndroid Build Coastguard Worker   Dealloc(rs...);
40*795d594fSAndroid Build Coastguard Worker }
DeallocParams(jvmtiParamInfo * params,jint n_params)41*795d594fSAndroid Build Coastguard Worker static void DeallocParams(jvmtiParamInfo* params, jint n_params) {
42*795d594fSAndroid Build Coastguard Worker   for (jint i = 0; i < n_params; i++) {
43*795d594fSAndroid Build Coastguard Worker     Dealloc(params[i].name);
44*795d594fSAndroid Build Coastguard Worker   }
45*795d594fSAndroid Build Coastguard Worker }
46*795d594fSAndroid Build Coastguard Worker 
GetNoSuspendFunction(JNIEnv * env)47*795d594fSAndroid Build Coastguard Worker RawMonitorEnterNoSuspend GetNoSuspendFunction(JNIEnv* env) {
48*795d594fSAndroid Build Coastguard Worker   // Get the extensions.
49*795d594fSAndroid Build Coastguard Worker   jint n_ext = 0;
50*795d594fSAndroid Build Coastguard Worker   jvmtiExtensionFunctionInfo* infos = nullptr;
51*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetExtensionFunctions(&n_ext, &infos))) {
52*795d594fSAndroid Build Coastguard Worker     return nullptr;
53*795d594fSAndroid Build Coastguard Worker   }
54*795d594fSAndroid Build Coastguard Worker   RawMonitorEnterNoSuspend result = nullptr;
55*795d594fSAndroid Build Coastguard Worker   for (jint i = 0; i < n_ext; i++) {
56*795d594fSAndroid Build Coastguard Worker     jvmtiExtensionFunctionInfo* cur_info = &infos[i];
57*795d594fSAndroid Build Coastguard Worker     if (strcmp("com.android.art.concurrent.raw_monitor_enter_no_suspend", cur_info->id) == 0) {
58*795d594fSAndroid Build Coastguard Worker       result = reinterpret_cast<RawMonitorEnterNoSuspend>(cur_info->func);
59*795d594fSAndroid Build Coastguard Worker     }
60*795d594fSAndroid Build Coastguard Worker     // Cleanup the cur_info
61*795d594fSAndroid Build Coastguard Worker     DeallocParams(cur_info->params, cur_info->param_count);
62*795d594fSAndroid Build Coastguard Worker     Dealloc(cur_info->id, cur_info->short_description, cur_info->params, cur_info->errors);
63*795d594fSAndroid Build Coastguard Worker   }
64*795d594fSAndroid Build Coastguard Worker   // Cleanup the array.
65*795d594fSAndroid Build Coastguard Worker   Dealloc(infos);
66*795d594fSAndroid Build Coastguard Worker   return result;
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker 
69*795d594fSAndroid Build Coastguard Worker static std::atomic<bool> started(false);
70*795d594fSAndroid Build Coastguard Worker static std::atomic<bool> resumed(false);
71*795d594fSAndroid Build Coastguard Worker static std::atomic<bool> progress(false);
72*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1951_otherThreadStart(JNIEnv * env,jclass)73*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test1951_otherThreadStart(JNIEnv* env, jclass) {
74*795d594fSAndroid Build Coastguard Worker   jrawMonitorID mon;
75*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->CreateRawMonitor("test 1951", &mon))) {
76*795d594fSAndroid Build Coastguard Worker     return;
77*795d594fSAndroid Build Coastguard Worker   }
78*795d594fSAndroid Build Coastguard Worker   RawMonitorEnterNoSuspend enter_func = GetNoSuspendFunction(env);
79*795d594fSAndroid Build Coastguard Worker   if (enter_func == nullptr) {
80*795d594fSAndroid Build Coastguard Worker     return;
81*795d594fSAndroid Build Coastguard Worker   }
82*795d594fSAndroid Build Coastguard Worker   started = true;
83*795d594fSAndroid Build Coastguard Worker   while (!resumed) {}
84*795d594fSAndroid Build Coastguard Worker   jvmtiError err = enter_func(jvmti_env, mon);
85*795d594fSAndroid Build Coastguard Worker   CHECK_EQ(err, JVMTI_ERROR_NONE);
86*795d594fSAndroid Build Coastguard Worker   progress = true;
87*795d594fSAndroid Build Coastguard Worker   err = jvmti_env->RawMonitorExit(mon);
88*795d594fSAndroid Build Coastguard Worker   CHECK_EQ(err, JVMTI_ERROR_NONE);
89*795d594fSAndroid Build Coastguard Worker }
90*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1951_waitForStart(JNIEnv *,jclass)91*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test1951_waitForStart(JNIEnv*, jclass) {
92*795d594fSAndroid Build Coastguard Worker   while (!started) {}
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1951_otherThreadResume(JNIEnv *,jclass)95*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test1951_otherThreadResume(JNIEnv*, jclass) {
96*795d594fSAndroid Build Coastguard Worker   resumed = true;
97*795d594fSAndroid Build Coastguard Worker }
98*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1951_otherThreadProgressed(JNIEnv *,jclass)99*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jboolean JNICALL Java_art_Test1951_otherThreadProgressed(JNIEnv*, jclass) {
100*795d594fSAndroid Build Coastguard Worker   return progress;
101*795d594fSAndroid Build Coastguard Worker }
102*795d594fSAndroid Build Coastguard Worker 
103*795d594fSAndroid Build Coastguard Worker }  // namespace Test1951MonitorEnterNoSuspend
104*795d594fSAndroid Build Coastguard Worker }  // namespace art
105