xref: /aosp_15_r20/system/unwinding/libunwindstack/ThreadEntry.h (revision eb293b8f56ee8303637c5595cfcdeef8039e85c6)
1*eb293b8fSAndroid Build Coastguard Worker /*
2*eb293b8fSAndroid Build Coastguard Worker  * Copyright (C) 2013 The Android Open Source Project
3*eb293b8fSAndroid Build Coastguard Worker  *
4*eb293b8fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*eb293b8fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*eb293b8fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*eb293b8fSAndroid Build Coastguard Worker  *
8*eb293b8fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*eb293b8fSAndroid Build Coastguard Worker  *
10*eb293b8fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*eb293b8fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*eb293b8fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*eb293b8fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*eb293b8fSAndroid Build Coastguard Worker  * limitations under the License.
15*eb293b8fSAndroid Build Coastguard Worker  */
16*eb293b8fSAndroid Build Coastguard Worker 
17*eb293b8fSAndroid Build Coastguard Worker #pragma once
18*eb293b8fSAndroid Build Coastguard Worker 
19*eb293b8fSAndroid Build Coastguard Worker #include <pthread.h>
20*eb293b8fSAndroid Build Coastguard Worker #include <sys/types.h>
21*eb293b8fSAndroid Build Coastguard Worker #include <ucontext.h>
22*eb293b8fSAndroid Build Coastguard Worker 
23*eb293b8fSAndroid Build Coastguard Worker #include <condition_variable>
24*eb293b8fSAndroid Build Coastguard Worker #include <map>
25*eb293b8fSAndroid Build Coastguard Worker #include <mutex>
26*eb293b8fSAndroid Build Coastguard Worker 
27*eb293b8fSAndroid Build Coastguard Worker namespace unwindstack {
28*eb293b8fSAndroid Build Coastguard Worker 
29*eb293b8fSAndroid Build Coastguard Worker enum WaitType : int {
30*eb293b8fSAndroid Build Coastguard Worker   WAIT_FOR_UCONTEXT = 1,
31*eb293b8fSAndroid Build Coastguard Worker   WAIT_FOR_UNWIND_TO_COMPLETE,
32*eb293b8fSAndroid Build Coastguard Worker   WAIT_FOR_THREAD_TO_RESTART,
33*eb293b8fSAndroid Build Coastguard Worker };
34*eb293b8fSAndroid Build Coastguard Worker 
35*eb293b8fSAndroid Build Coastguard Worker class ThreadEntry {
36*eb293b8fSAndroid Build Coastguard Worker  public:
37*eb293b8fSAndroid Build Coastguard Worker   static ThreadEntry* Get(pid_t tid, bool create = true);
38*eb293b8fSAndroid Build Coastguard Worker 
39*eb293b8fSAndroid Build Coastguard Worker   static void Remove(ThreadEntry* entry);
40*eb293b8fSAndroid Build Coastguard Worker 
41*eb293b8fSAndroid Build Coastguard Worker   void Wake();
42*eb293b8fSAndroid Build Coastguard Worker 
43*eb293b8fSAndroid Build Coastguard Worker   bool Wait(WaitType type, pid_t tid);
44*eb293b8fSAndroid Build Coastguard Worker 
45*eb293b8fSAndroid Build Coastguard Worker   void CopyUcontextFromSigcontext(void* sigcontext);
46*eb293b8fSAndroid Build Coastguard Worker 
Lock()47*eb293b8fSAndroid Build Coastguard Worker   inline void Lock() {
48*eb293b8fSAndroid Build Coastguard Worker     mutex_.lock();
49*eb293b8fSAndroid Build Coastguard Worker 
50*eb293b8fSAndroid Build Coastguard Worker     // Always reset the wait value since this could be the first or nth
51*eb293b8fSAndroid Build Coastguard Worker     // time this entry is locked.
52*eb293b8fSAndroid Build Coastguard Worker     wait_value_ = 0;
53*eb293b8fSAndroid Build Coastguard Worker   }
54*eb293b8fSAndroid Build Coastguard Worker 
Unlock()55*eb293b8fSAndroid Build Coastguard Worker   inline void Unlock() { mutex_.unlock(); }
56*eb293b8fSAndroid Build Coastguard Worker 
GetUcontext()57*eb293b8fSAndroid Build Coastguard Worker   inline ucontext_t* GetUcontext() { return &ucontext_; }
58*eb293b8fSAndroid Build Coastguard Worker 
59*eb293b8fSAndroid Build Coastguard Worker  private:
60*eb293b8fSAndroid Build Coastguard Worker   ThreadEntry(pid_t tid);
61*eb293b8fSAndroid Build Coastguard Worker   ~ThreadEntry();
62*eb293b8fSAndroid Build Coastguard Worker 
63*eb293b8fSAndroid Build Coastguard Worker   pid_t tid_;
64*eb293b8fSAndroid Build Coastguard Worker   int ref_count_;
65*eb293b8fSAndroid Build Coastguard Worker   std::mutex mutex_;
66*eb293b8fSAndroid Build Coastguard Worker   std::mutex wait_mutex_;
67*eb293b8fSAndroid Build Coastguard Worker   std::condition_variable wait_cond_;
68*eb293b8fSAndroid Build Coastguard Worker   int wait_value_;
69*eb293b8fSAndroid Build Coastguard Worker   ucontext_t ucontext_;
70*eb293b8fSAndroid Build Coastguard Worker 
71*eb293b8fSAndroid Build Coastguard Worker   static std::mutex entries_mutex_;
72*eb293b8fSAndroid Build Coastguard Worker   static std::map<pid_t, ThreadEntry*> entries_;
73*eb293b8fSAndroid Build Coastguard Worker 
74*eb293b8fSAndroid Build Coastguard Worker   static const char* GetWaitTypeName(WaitType type);
75*eb293b8fSAndroid Build Coastguard Worker };
76*eb293b8fSAndroid Build Coastguard Worker 
77*eb293b8fSAndroid Build Coastguard Worker }  // namespace unwindstack
78