1 /*
2  * Copyright (C) 2015 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 #ifndef _MEMORY_REPLAY_THREAD_H
18 #define _MEMORY_REPLAY_THREAD_H
19 
20 #include <pthread.h>
21 #include <stdint.h>
22 #include <sys/types.h>
23 
24 class Action;
25 class Pointers;
26 
27 constexpr size_t ACTION_MEMORY_SIZE = 128;
28 
29 class Thread {
30  public:
31   Thread();
32   virtual ~Thread();
33 
34   void WaitForReady();
35   void WaitForPending();
36   void SetPending();
37   void ClearPending();
38 
39   Action* CreateAction(uintptr_t key_pointer, const char* type, const char* line);
AddTimeNsecs(uint64_t nsecs)40   void AddTimeNsecs(uint64_t nsecs) { total_time_nsecs_ += nsecs; }
41 
set_pointers(Pointers * pointers)42   void set_pointers(Pointers* pointers) { pointers_ = pointers; }
pointers()43   Pointers* pointers() { return pointers_; }
44 
GetAction()45   Action* GetAction() { return reinterpret_cast<Action*>(action_memory_); }
46 
47  private:
48   pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER;
49   pthread_cond_t cond_;
50   bool pending_ = false;
51 
52   pthread_t thread_id_;
53   pid_t tid_ = 0;
54   uint64_t total_time_nsecs_ = 0;
55 
56   Pointers* pointers_ = nullptr;
57 
58   // Per thread memory for an Action. Only one action can be processed.
59   // at a time.
60   static constexpr size_t ACTION_SIZE = 128;
61   uint8_t action_memory_[ACTION_SIZE];
62 
63   friend class Threads;
64 };
65 
66 #endif // _MEMORY_REPLAY_THREAD_H
67