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