• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 #pragma once
18 
19 #include <pthread.h>
20 #include <sys/types.h>
21 #include <ucontext.h>
22 
23 #include <condition_variable>
24 #include <map>
25 #include <mutex>
26 
27 namespace unwindstack {
28 
29 enum WaitType : int {
30   WAIT_FOR_UCONTEXT = 1,
31   WAIT_FOR_UNWIND_TO_COMPLETE,
32   WAIT_FOR_THREAD_TO_RESTART,
33 };
34 
35 class ThreadEntry {
36  public:
37   static ThreadEntry* Get(pid_t tid, bool create = true);
38 
39   static void Remove(ThreadEntry* entry);
40 
41   void Wake();
42 
43   bool Wait(WaitType type);
44 
45   void CopyUcontextFromSigcontext(void* sigcontext);
46 
Lock()47   inline void Lock() {
48     mutex_.lock();
49 
50     // Always reset the wait value since this could be the first or nth
51     // time this entry is locked.
52     wait_value_ = 0;
53   }
54 
Unlock()55   inline void Unlock() { mutex_.unlock(); }
56 
GetUcontext()57   inline ucontext_t* GetUcontext() { return &ucontext_; }
58 
59  private:
60   ThreadEntry(pid_t tid);
61   ~ThreadEntry();
62 
63   pid_t tid_;
64   int ref_count_;
65   std::mutex mutex_;
66   std::mutex wait_mutex_;
67   std::condition_variable wait_cond_;
68   int wait_value_;
69   ucontext_t ucontext_;
70 
71   static std::mutex entries_mutex_;
72   static std::map<pid_t, ThreadEntry*> entries_;
73 
74   static const char* GetWaitTypeName(WaitType type);
75 };
76 
77 }  // namespace unwindstack
78