xref: /aosp_15_r20/system/extras/memory_replay/tests/ThreadTest.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include <pthread.h>
18*288bf522SAndroid Build Coastguard Worker #include <unistd.h>
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <utility>
21*288bf522SAndroid Build Coastguard Worker 
22*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
23*288bf522SAndroid Build Coastguard Worker 
24*288bf522SAndroid Build Coastguard Worker #include "Pointers.h"
25*288bf522SAndroid Build Coastguard Worker #include "Thread.h"
26*288bf522SAndroid Build Coastguard Worker 
27*288bf522SAndroid Build Coastguard Worker typedef std::pair<Thread*, volatile bool*> thread_data_t;
28*288bf522SAndroid Build Coastguard Worker 
TEST(ThreadTest,ready)29*288bf522SAndroid Build Coastguard Worker TEST(ThreadTest, ready) {
30*288bf522SAndroid Build Coastguard Worker   Thread thread;
31*288bf522SAndroid Build Coastguard Worker 
32*288bf522SAndroid Build Coastguard Worker   // A thread should be ready immediately. If not, this will hang forever.
33*288bf522SAndroid Build Coastguard Worker   thread.WaitForReady();
34*288bf522SAndroid Build Coastguard Worker }
35*288bf522SAndroid Build Coastguard Worker 
ThreadWaitForReady(void * data)36*288bf522SAndroid Build Coastguard Worker void* ThreadWaitForReady(void* data) {
37*288bf522SAndroid Build Coastguard Worker   thread_data_t* thread_data = reinterpret_cast<thread_data_t*>(data);
38*288bf522SAndroid Build Coastguard Worker   Thread* thread = thread_data->first;
39*288bf522SAndroid Build Coastguard Worker   volatile bool* finish = thread_data->second;
40*288bf522SAndroid Build Coastguard Worker 
41*288bf522SAndroid Build Coastguard Worker   thread->WaitForReady();
42*288bf522SAndroid Build Coastguard Worker   *finish = true;
43*288bf522SAndroid Build Coastguard Worker 
44*288bf522SAndroid Build Coastguard Worker   return nullptr;
45*288bf522SAndroid Build Coastguard Worker }
46*288bf522SAndroid Build Coastguard Worker 
TEST(ThreadTest,ready_thread)47*288bf522SAndroid Build Coastguard Worker TEST(ThreadTest, ready_thread) {
48*288bf522SAndroid Build Coastguard Worker   Thread thread;
49*288bf522SAndroid Build Coastguard Worker   volatile bool finish = false;
50*288bf522SAndroid Build Coastguard Worker   thread_data_t thread_data = std::make_pair(&thread, &finish);
51*288bf522SAndroid Build Coastguard Worker 
52*288bf522SAndroid Build Coastguard Worker   thread.SetPending();
53*288bf522SAndroid Build Coastguard Worker 
54*288bf522SAndroid Build Coastguard Worker   pthread_t thread_id;
55*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(pthread_create(&thread_id, nullptr, ThreadWaitForReady, &thread_data) == 0);
56*288bf522SAndroid Build Coastguard Worker 
57*288bf522SAndroid Build Coastguard Worker   ASSERT_FALSE(finish);
58*288bf522SAndroid Build Coastguard Worker   sleep(1);
59*288bf522SAndroid Build Coastguard Worker   ASSERT_FALSE(finish);
60*288bf522SAndroid Build Coastguard Worker 
61*288bf522SAndroid Build Coastguard Worker   thread.ClearPending();
62*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(pthread_join(thread_id, nullptr) == 0);
63*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(finish);
64*288bf522SAndroid Build Coastguard Worker }
65*288bf522SAndroid Build Coastguard Worker 
ThreadWaitForPending(void * data)66*288bf522SAndroid Build Coastguard Worker void* ThreadWaitForPending(void* data) {
67*288bf522SAndroid Build Coastguard Worker   thread_data_t* thread_data = reinterpret_cast<thread_data_t*>(data);
68*288bf522SAndroid Build Coastguard Worker   Thread* thread = thread_data->first;
69*288bf522SAndroid Build Coastguard Worker   volatile bool* finish = thread_data->second;
70*288bf522SAndroid Build Coastguard Worker 
71*288bf522SAndroid Build Coastguard Worker   thread->WaitForPending();
72*288bf522SAndroid Build Coastguard Worker   *finish = true;
73*288bf522SAndroid Build Coastguard Worker 
74*288bf522SAndroid Build Coastguard Worker   return nullptr;
75*288bf522SAndroid Build Coastguard Worker }
76*288bf522SAndroid Build Coastguard Worker 
TEST(ThreadTest,pending)77*288bf522SAndroid Build Coastguard Worker TEST(ThreadTest, pending) {
78*288bf522SAndroid Build Coastguard Worker   Thread thread;
79*288bf522SAndroid Build Coastguard Worker   volatile bool finish = false;
80*288bf522SAndroid Build Coastguard Worker   thread_data_t thread_data = std::make_pair(&thread, &finish);
81*288bf522SAndroid Build Coastguard Worker 
82*288bf522SAndroid Build Coastguard Worker   pthread_t thread_id;
83*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(pthread_create(&thread_id, nullptr, ThreadWaitForPending, &thread_data) == 0);
84*288bf522SAndroid Build Coastguard Worker 
85*288bf522SAndroid Build Coastguard Worker   ASSERT_FALSE(finish);
86*288bf522SAndroid Build Coastguard Worker   sleep(1);
87*288bf522SAndroid Build Coastguard Worker   ASSERT_FALSE(finish);
88*288bf522SAndroid Build Coastguard Worker 
89*288bf522SAndroid Build Coastguard Worker   thread.SetPending();
90*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(pthread_join(thread_id, nullptr) == 0);
91*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(finish);
92*288bf522SAndroid Build Coastguard Worker }
93*288bf522SAndroid Build Coastguard Worker 
TEST(ThreadTest,pointers)94*288bf522SAndroid Build Coastguard Worker TEST(ThreadTest, pointers) {
95*288bf522SAndroid Build Coastguard Worker   Pointers pointers(2);
96*288bf522SAndroid Build Coastguard Worker   Thread thread;
97*288bf522SAndroid Build Coastguard Worker 
98*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(thread.pointers() == nullptr);
99*288bf522SAndroid Build Coastguard Worker   thread.set_pointers(&pointers);
100*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(thread.pointers() == &pointers);
101*288bf522SAndroid Build Coastguard Worker }
102