blob: efd02b6ae406e3a1aaed5d259c09502d8c1ad2ff [file] [log] [blame]
Alex Lightc98f83e2018-07-26 08:28:36 -07001/*
2 * Copyright (C) 2013 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 "jvmti.h"
20
21// Test infrastructure
22#include "jvmti_helper.h"
23#include "scoped_local_ref.h"
24#include "test_env.h"
25
26namespace art {
27namespace Test1951MonitorEnterNoSuspend {
28
Andreas Gampec55bb392018-09-21 00:02:02 +000029using RawMonitorEnterNoSuspend = jvmtiError(*)(jvmtiEnv* env, jrawMonitorID mon);
Alex Lightc98f83e2018-07-26 08:28:36 -070030
31template <typename T>
32static void Dealloc(T* t) {
33 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(t));
34}
35
36template <typename T, typename ...Rest>
37static void Dealloc(T* t, Rest... rs) {
38 Dealloc(t);
39 Dealloc(rs...);
40}
41static void DeallocParams(jvmtiParamInfo* params, jint n_params) {
42 for (jint i = 0; i < n_params; i++) {
43 Dealloc(params[i].name);
44 }
45}
46
47RawMonitorEnterNoSuspend GetNoSuspendFunction(JNIEnv* env) {
48 // Get the extensions.
49 jint n_ext = 0;
50 jvmtiExtensionFunctionInfo* infos = nullptr;
51 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetExtensionFunctions(&n_ext, &infos))) {
52 return nullptr;
53 }
54 RawMonitorEnterNoSuspend result = nullptr;
55 for (jint i = 0; i < n_ext; i++) {
56 jvmtiExtensionFunctionInfo* cur_info = &infos[i];
57 if (strcmp("com.android.art.concurrent.raw_monitor_enter_no_suspend", cur_info->id) == 0) {
58 result = reinterpret_cast<RawMonitorEnterNoSuspend>(cur_info->func);
59 }
60 // Cleanup the cur_info
61 DeallocParams(cur_info->params, cur_info->param_count);
62 Dealloc(cur_info->id, cur_info->short_description, cur_info->params, cur_info->errors);
63 }
64 // Cleanup the array.
65 Dealloc(infos);
66 return result;
67}
68
69static std::atomic<bool> started(false);
70static std::atomic<bool> resumed(false);
71static std::atomic<bool> progress(false);
72
73extern "C" JNIEXPORT void JNICALL Java_art_Test1951_otherThreadStart(JNIEnv* env, jclass) {
74 jrawMonitorID mon;
75 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->CreateRawMonitor("test 1951", &mon))) {
76 return;
77 }
78 RawMonitorEnterNoSuspend enter_func = GetNoSuspendFunction(env);
79 if (enter_func == nullptr) {
80 return;
81 }
82 started = true;
83 while (!resumed) {}
84 jvmtiError err = enter_func(jvmti_env, mon);
85 CHECK_EQ(err, JVMTI_ERROR_NONE);
86 progress = true;
87 err = jvmti_env->RawMonitorExit(mon);
88 CHECK_EQ(err, JVMTI_ERROR_NONE);
89}
90
91extern "C" JNIEXPORT void JNICALL Java_art_Test1951_waitForStart(JNIEnv*, jclass) {
92 while (!started) {}
93}
94
95extern "C" JNIEXPORT void JNICALL Java_art_Test1951_otherThreadResume(JNIEnv*, jclass) {
96 resumed = true;
97}
98
99extern "C" JNIEXPORT jboolean JNICALL Java_art_Test1951_otherThreadProgressed(JNIEnv*, jclass) {
100 return progress;
101}
102
103} // namespace Test1951MonitorEnterNoSuspend
104} // namespace art