xref: /aosp_15_r20/art/test/1934-jvmti-signal-thread/signal_threads.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 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 <pthread.h>
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <cstdio>
20*795d594fSAndroid Build Coastguard Worker #include <iostream>
21*795d594fSAndroid Build Coastguard Worker #include <vector>
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
24*795d594fSAndroid Build Coastguard Worker #include "jni.h"
25*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker #include "scoped_local_ref.h"
28*795d594fSAndroid Build Coastguard Worker #include "scoped_primitive_array.h"
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker // Test infrastructure
31*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
32*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker namespace art {
35*795d594fSAndroid Build Coastguard Worker namespace Test1934SignalThreads {
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker struct NativeMonitor {
38*795d594fSAndroid Build Coastguard Worker   jrawMonitorID continue_monitor;
39*795d594fSAndroid Build Coastguard Worker   bool should_continue;
40*795d594fSAndroid Build Coastguard Worker   jrawMonitorID start_monitor;
41*795d594fSAndroid Build Coastguard Worker   bool should_start;
42*795d594fSAndroid Build Coastguard Worker };
43*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1934_allocNativeMonitor(JNIEnv * env,jclass)44*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jlong JNICALL Java_art_Test1934_allocNativeMonitor(JNIEnv* env, jclass) {
45*795d594fSAndroid Build Coastguard Worker   NativeMonitor* mon;
46*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env,
47*795d594fSAndroid Build Coastguard Worker                             jvmti_env,
48*795d594fSAndroid Build Coastguard Worker                             jvmti_env->Allocate(sizeof(NativeMonitor),
49*795d594fSAndroid Build Coastguard Worker                                                 reinterpret_cast<unsigned char**>(&mon)))) {
50*795d594fSAndroid Build Coastguard Worker     return -1L;
51*795d594fSAndroid Build Coastguard Worker   }
52*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env,
53*795d594fSAndroid Build Coastguard Worker                             jvmti_env,
54*795d594fSAndroid Build Coastguard Worker                             jvmti_env->CreateRawMonitor("test-1934 start",
55*795d594fSAndroid Build Coastguard Worker                                                         &mon->start_monitor))) {
56*795d594fSAndroid Build Coastguard Worker     return -1L;
57*795d594fSAndroid Build Coastguard Worker   }
58*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env,
59*795d594fSAndroid Build Coastguard Worker                             jvmti_env,
60*795d594fSAndroid Build Coastguard Worker                             jvmti_env->CreateRawMonitor("test-1934 continue",
61*795d594fSAndroid Build Coastguard Worker                                                         &mon->continue_monitor))) {
62*795d594fSAndroid Build Coastguard Worker     return -1L;
63*795d594fSAndroid Build Coastguard Worker   }
64*795d594fSAndroid Build Coastguard Worker   mon->should_continue = false;
65*795d594fSAndroid Build Coastguard Worker   mon->should_start = false;
66*795d594fSAndroid Build Coastguard Worker   return static_cast<jlong>(reinterpret_cast<intptr_t>(mon));
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1934_nativeWaitForOtherThread(JNIEnv * env,jclass,jlong id)69*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void Java_art_Test1934_nativeWaitForOtherThread(JNIEnv* env,
70*795d594fSAndroid Build Coastguard Worker                                                                      jclass,
71*795d594fSAndroid Build Coastguard Worker                                                                      jlong id) {
72*795d594fSAndroid Build Coastguard Worker   NativeMonitor* mon = reinterpret_cast<NativeMonitor*>(static_cast<intptr_t>(id));
73*795d594fSAndroid Build Coastguard Worker   // Start
74*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorEnter(mon->start_monitor))) {
75*795d594fSAndroid Build Coastguard Worker     return;
76*795d594fSAndroid Build Coastguard Worker   }
77*795d594fSAndroid Build Coastguard Worker   mon->should_start = true;
78*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env,
79*795d594fSAndroid Build Coastguard Worker                             jvmti_env,
80*795d594fSAndroid Build Coastguard Worker                             jvmti_env->RawMonitorNotifyAll(mon->start_monitor))) {
81*795d594fSAndroid Build Coastguard Worker     JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon->start_monitor));
82*795d594fSAndroid Build Coastguard Worker     return;
83*795d594fSAndroid Build Coastguard Worker   }
84*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon->start_monitor))) {
85*795d594fSAndroid Build Coastguard Worker     return;
86*795d594fSAndroid Build Coastguard Worker   }
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker   // Finish
89*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorEnter(mon->continue_monitor))) {
90*795d594fSAndroid Build Coastguard Worker     return;
91*795d594fSAndroid Build Coastguard Worker   }
92*795d594fSAndroid Build Coastguard Worker   while (!mon->should_continue) {
93*795d594fSAndroid Build Coastguard Worker     if (JvmtiErrorToException(env,
94*795d594fSAndroid Build Coastguard Worker                               jvmti_env,
95*795d594fSAndroid Build Coastguard Worker                               jvmti_env->RawMonitorWait(mon->continue_monitor, -1L))) {
96*795d594fSAndroid Build Coastguard Worker       JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon->continue_monitor));
97*795d594fSAndroid Build Coastguard Worker       return;
98*795d594fSAndroid Build Coastguard Worker     }
99*795d594fSAndroid Build Coastguard Worker   }
100*795d594fSAndroid Build Coastguard Worker   JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon->continue_monitor));
101*795d594fSAndroid Build Coastguard Worker }
102*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1934_nativeDoInterleaved(JNIEnv * env,jclass,jlong id,jobject closure)103*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void Java_art_Test1934_nativeDoInterleaved(JNIEnv* env,
104*795d594fSAndroid Build Coastguard Worker                                                                 jclass,
105*795d594fSAndroid Build Coastguard Worker                                                                 jlong id,
106*795d594fSAndroid Build Coastguard Worker                                                                 jobject closure) {
107*795d594fSAndroid Build Coastguard Worker   NativeMonitor* mon = reinterpret_cast<NativeMonitor*>(static_cast<intptr_t>(id));
108*795d594fSAndroid Build Coastguard Worker   // Wait for start.
109*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorEnter(mon->start_monitor))) {
110*795d594fSAndroid Build Coastguard Worker     return;
111*795d594fSAndroid Build Coastguard Worker   }
112*795d594fSAndroid Build Coastguard Worker   while (!mon->should_start) {
113*795d594fSAndroid Build Coastguard Worker     if (JvmtiErrorToException(env,
114*795d594fSAndroid Build Coastguard Worker                               jvmti_env,
115*795d594fSAndroid Build Coastguard Worker                               jvmti_env->RawMonitorWait(mon->start_monitor, -1L))) {
116*795d594fSAndroid Build Coastguard Worker       return;
117*795d594fSAndroid Build Coastguard Worker     }
118*795d594fSAndroid Build Coastguard Worker   }
119*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon->start_monitor))) {
120*795d594fSAndroid Build Coastguard Worker     return;
121*795d594fSAndroid Build Coastguard Worker   }
122*795d594fSAndroid Build Coastguard Worker 
123*795d594fSAndroid Build Coastguard Worker   // Call closure.
124*795d594fSAndroid Build Coastguard Worker   ScopedLocalRef<jclass> runnable_klass(env, env->FindClass("java/lang/Runnable"));
125*795d594fSAndroid Build Coastguard Worker   if (env->ExceptionCheck()) {
126*795d594fSAndroid Build Coastguard Worker     return;
127*795d594fSAndroid Build Coastguard Worker   }
128*795d594fSAndroid Build Coastguard Worker   jmethodID doRun = env->GetMethodID(runnable_klass.get(), "run", "()V");
129*795d594fSAndroid Build Coastguard Worker   if (env->ExceptionCheck()) {
130*795d594fSAndroid Build Coastguard Worker     return;
131*795d594fSAndroid Build Coastguard Worker   }
132*795d594fSAndroid Build Coastguard Worker   env->CallVoidMethod(closure, doRun);
133*795d594fSAndroid Build Coastguard Worker 
134*795d594fSAndroid Build Coastguard Worker   // Tell other thread to finish.
135*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorEnter(mon->continue_monitor))) {
136*795d594fSAndroid Build Coastguard Worker     return;
137*795d594fSAndroid Build Coastguard Worker   }
138*795d594fSAndroid Build Coastguard Worker   mon->should_continue = true;
139*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env,
140*795d594fSAndroid Build Coastguard Worker                             jvmti_env,
141*795d594fSAndroid Build Coastguard Worker                             jvmti_env->RawMonitorNotifyAll(mon->continue_monitor))) {
142*795d594fSAndroid Build Coastguard Worker     JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon->continue_monitor));
143*795d594fSAndroid Build Coastguard Worker     return;
144*795d594fSAndroid Build Coastguard Worker   }
145*795d594fSAndroid Build Coastguard Worker   JvmtiErrorToException(env, jvmti_env, jvmti_env->RawMonitorExit(mon->continue_monitor));
146*795d594fSAndroid Build Coastguard Worker }
147*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1934_destroyNativeMonitor(JNIEnv *,jclass,jlong id)148*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void Java_art_Test1934_destroyNativeMonitor(JNIEnv*, jclass, jlong id) {
149*795d594fSAndroid Build Coastguard Worker   NativeMonitor* mon = reinterpret_cast<NativeMonitor*>(static_cast<intptr_t>(id));
150*795d594fSAndroid Build Coastguard Worker   jvmti_env->DestroyRawMonitor(mon->start_monitor);
151*795d594fSAndroid Build Coastguard Worker   jvmti_env->DestroyRawMonitor(mon->continue_monitor);
152*795d594fSAndroid Build Coastguard Worker   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(mon));
153*795d594fSAndroid Build Coastguard Worker }
154*795d594fSAndroid Build Coastguard Worker 
155*795d594fSAndroid Build Coastguard Worker }  // namespace Test1934SignalThreads
156*795d594fSAndroid Build Coastguard Worker }  // namespace art
157*795d594fSAndroid Build Coastguard Worker 
158