xref: /aosp_15_r20/art/test/203-multi-checkpoint/multi_checkpoint.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 "art_method-inl.h"
18*795d594fSAndroid Build Coastguard Worker #include "base/mutex-inl.h"
19*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "thread-inl.h"
21*795d594fSAndroid Build Coastguard Worker #include "thread_pool.h"
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker namespace art {
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker struct TestClosure : public Closure {
26*795d594fSAndroid Build Coastguard Worker   bool first_run_start;
27*795d594fSAndroid Build Coastguard Worker   bool first_run_end;
28*795d594fSAndroid Build Coastguard Worker   bool second_run;
29*795d594fSAndroid Build Coastguard Worker   bool second_run_interleaved;
30*795d594fSAndroid Build Coastguard Worker 
Runart::TestClosure31*795d594fSAndroid Build Coastguard Worker   void Run(Thread* self) override {
32*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(self, Thread::Current()) << "Not running on target thread!";
33*795d594fSAndroid Build Coastguard Worker     if (!first_run_start) {
34*795d594fSAndroid Build Coastguard Worker       CHECK(!second_run);
35*795d594fSAndroid Build Coastguard Worker       first_run_start = true;
36*795d594fSAndroid Build Coastguard Worker       // Suspend ourself so that we will perform the second run.
37*795d594fSAndroid Build Coastguard Worker       {
38*795d594fSAndroid Build Coastguard Worker         ScopedObjectAccess soa(self);
39*795d594fSAndroid Build Coastguard Worker         self->FullSuspendCheck();
40*795d594fSAndroid Build Coastguard Worker       }
41*795d594fSAndroid Build Coastguard Worker       first_run_end = true;
42*795d594fSAndroid Build Coastguard Worker     } else {
43*795d594fSAndroid Build Coastguard Worker       CHECK(!second_run);
44*795d594fSAndroid Build Coastguard Worker       CHECK(first_run_start);
45*795d594fSAndroid Build Coastguard Worker       second_run = true;
46*795d594fSAndroid Build Coastguard Worker       second_run_interleaved = !first_run_end;
47*795d594fSAndroid Build Coastguard Worker     }
48*795d594fSAndroid Build Coastguard Worker   }
49*795d594fSAndroid Build Coastguard Worker 
Checkart::TestClosure50*795d594fSAndroid Build Coastguard Worker   void Check() {
51*795d594fSAndroid Build Coastguard Worker     CHECK(first_run_start);
52*795d594fSAndroid Build Coastguard Worker     CHECK(first_run_end);
53*795d594fSAndroid Build Coastguard Worker     CHECK(second_run);
54*795d594fSAndroid Build Coastguard Worker     CHECK(second_run_interleaved);
55*795d594fSAndroid Build Coastguard Worker   }
56*795d594fSAndroid Build Coastguard Worker };
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker static TestClosure gTestClosure = {};
59*795d594fSAndroid Build Coastguard Worker 
Java_Main_checkCheckpointsRun(JNIEnv *,jclass)60*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_Main_checkCheckpointsRun(JNIEnv*, jclass) {
61*795d594fSAndroid Build Coastguard Worker   gTestClosure.Check();
62*795d594fSAndroid Build Coastguard Worker }
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker struct SetupClosure : public Closure {
Runart::SetupClosure65*795d594fSAndroid Build Coastguard Worker   void Run(Thread* self) override {
66*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(self, Thread::Current()) << "Not running on target thread!";
67*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(self);
68*795d594fSAndroid Build Coastguard Worker     MutexLock tscl_mu(self, *Locks::thread_suspend_count_lock_);
69*795d594fSAndroid Build Coastguard Worker     // Both should succeed since we are in runnable and have the lock.
70*795d594fSAndroid Build Coastguard Worker     CHECK(self->RequestCheckpoint(&gTestClosure)) << "Could not set first checkpoint.";
71*795d594fSAndroid Build Coastguard Worker     CHECK(self->RequestCheckpoint(&gTestClosure)) << "Could not set second checkpoint.";
72*795d594fSAndroid Build Coastguard Worker   }
73*795d594fSAndroid Build Coastguard Worker };
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker static SetupClosure gSetupClosure = {};
76*795d594fSAndroid Build Coastguard Worker 
Java_Main_pushCheckpoints(JNIEnv *,jclass,jobject thr)77*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_Main_pushCheckpoints(JNIEnv*, jclass, jobject thr) {
78*795d594fSAndroid Build Coastguard Worker   Thread* self = Thread::Current();
79*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(self);
80*795d594fSAndroid Build Coastguard Worker   MutexLock tll_mu(self, *Locks::thread_list_lock_);
81*795d594fSAndroid Build Coastguard Worker   Thread* target = Thread::FromManagedThread(soa, thr);
82*795d594fSAndroid Build Coastguard Worker   while (true) {
83*795d594fSAndroid Build Coastguard Worker     MutexLock tscl_mu(self, *Locks::thread_suspend_count_lock_);
84*795d594fSAndroid Build Coastguard Worker     if (target->RequestCheckpoint(&gSetupClosure)) {
85*795d594fSAndroid Build Coastguard Worker       break;
86*795d594fSAndroid Build Coastguard Worker     }
87*795d594fSAndroid Build Coastguard Worker   }
88*795d594fSAndroid Build Coastguard Worker }
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker }  // namespace art
91