1319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe/*
2319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe * Copyright (C) 2017 The Android Open Source Project
3319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe *
4319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe * Licensed under the Apache License, Version 2.0 (the "License");
5319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe * you may not use this file except in compliance with the License.
6319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe * You may obtain a copy of the License at
7319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe *
8319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe *      http://www.apache.org/licenses/LICENSE-2.0
9319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe *
10319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe * Unless required by applicable law or agreed to in writing, software
11319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe * distributed under the License is distributed on an "AS IS" BASIS,
12319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe * See the License for the specific language governing permissions and
14319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe * limitations under the License.
15319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe */
16319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
17319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe#include <stdio.h>
18319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
19027444b64dd52e1d2beea7aa525fbb8146a516bcAndreas Gampe#include "android-base/macros.h"
20319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe#include "jni.h"
215e03a305edafb49a34b436d9a858281e53d390f2Andreas Gampe#include "jvmti.h"
22027444b64dd52e1d2beea7aa525fbb8146a516bcAndreas Gampe#include "scoped_utf_chars.h"
23319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
243f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe// Test infrastructure
253f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe#include "jvmti_helper.h"
263f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe#include "test_env.h"
27319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
28319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampenamespace art {
29319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampenamespace Test923Monitors {
30319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
31319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
32319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampestatic jlong MonitorToLong(jrawMonitorID id) {
33319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  return static_cast<jlong>(reinterpret_cast<uintptr_t>(id));
34319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}
35319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
36319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampestatic jrawMonitorID LongToMonitor(jlong l) {
37319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  return reinterpret_cast<jrawMonitorID>(static_cast<uintptr_t>(l));
38319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}
39319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
404665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampeextern "C" JNIEXPORT jlong JNICALL Java_art_Test923_createRawMonitor(
41319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
42319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  jrawMonitorID id;
43319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  jvmtiError result = jvmti_env->CreateRawMonitor("dummy", &id);
443f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe  if (JvmtiErrorToException(env, jvmti_env, result)) {
45319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe    return 0;
46319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  }
47319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  return MonitorToLong(id);
48319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}
49319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
504665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampeextern "C" JNIEXPORT void JNICALL Java_art_Test923_destroyRawMonitor(
51319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jlong l) {
52319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  jvmtiError result = jvmti_env->DestroyRawMonitor(LongToMonitor(l));
533f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe  JvmtiErrorToException(env, jvmti_env, result);
54319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}
55319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
564665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampeextern "C" JNIEXPORT void JNICALL Java_art_Test923_rawMonitorEnter(
57319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jlong l) {
58319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  jvmtiError result = jvmti_env->RawMonitorEnter(LongToMonitor(l));
593f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe  JvmtiErrorToException(env, jvmti_env, result);
60319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}
61319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
624665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampeextern "C" JNIEXPORT void JNICALL Java_art_Test923_rawMonitorExit(
63319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jlong l) {
64319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  jvmtiError result = jvmti_env->RawMonitorExit(LongToMonitor(l));
653f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe  JvmtiErrorToException(env, jvmti_env, result);
66319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}
67319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
684665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampeextern "C" JNIEXPORT void JNICALL Java_art_Test923_rawMonitorWait(
69319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jlong l, jlong millis) {
70319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  jvmtiError result = jvmti_env->RawMonitorWait(LongToMonitor(l), millis);
713f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe  JvmtiErrorToException(env, jvmti_env, result);
72319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}
73319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
744665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampeextern "C" JNIEXPORT void JNICALL Java_art_Test923_rawMonitorNotify(
75319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jlong l) {
76319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  jvmtiError result = jvmti_env->RawMonitorNotify(LongToMonitor(l));
773f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe  JvmtiErrorToException(env, jvmti_env, result);
78319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}
79319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
804665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampeextern "C" JNIEXPORT void JNICALL Java_art_Test923_rawMonitorNotifyAll(
81319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jlong l) {
82319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe  jvmtiError result = jvmti_env->RawMonitorNotifyAll(LongToMonitor(l));
833f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe  JvmtiErrorToException(env, jvmti_env, result);
84319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}
85319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe
86319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}  // namespace Test923Monitors
87319dbe87f2e1a3381696603384dc7f245800591dAndreas Gampe}  // namespace art
88