• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <pthread.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <time.h>
22 #include <ucontext.h>
23 
24 #include <chrono>
25 
26 #include <unwindstack/Log.h>
27 
28 #include "ThreadEntry.h"
29 
30 namespace unwindstack {
31 
32 std::mutex ThreadEntry::entries_mutex_;
33 std::map<pid_t, ThreadEntry*> ThreadEntry::entries_;
34 
35 // Assumes that ThreadEntry::entries_mutex_ has already been locked before
36 // creating a ThreadEntry object.
ThreadEntry(pid_t tid)37 ThreadEntry::ThreadEntry(pid_t tid) : tid_(tid), ref_count_(1), wait_value_(0) {
38   // Add ourselves to the global list.
39   entries_[tid_] = this;
40 }
41 
Get(pid_t tid,bool create)42 ThreadEntry* ThreadEntry::Get(pid_t tid, bool create) {
43   ThreadEntry* entry = nullptr;
44 
45   std::lock_guard<std::mutex> guard(entries_mutex_);
46   auto iter = entries_.find(tid);
47   if (iter == entries_.end()) {
48     if (create) {
49       entry = new ThreadEntry(tid);
50     }
51   } else {
52     entry = iter->second;
53     entry->ref_count_++;
54   }
55 
56   return entry;
57 }
58 
Remove(ThreadEntry * entry)59 void ThreadEntry::Remove(ThreadEntry* entry) {
60   entry->Unlock();
61 
62   std::lock_guard<std::mutex> guard(entries_mutex_);
63   if (--entry->ref_count_ == 0) {
64     delete entry;
65   }
66 }
67 
68 // Assumes that ThreadEntry::entries_mutex_ has already been locked before
69 // deleting a ThreadEntry object.
~ThreadEntry()70 ThreadEntry::~ThreadEntry() {
71   auto iter = entries_.find(tid_);
72   if (iter != entries_.end()) {
73     entries_.erase(iter);
74   }
75 }
76 
Wait(WaitType type)77 bool ThreadEntry::Wait(WaitType type) {
78   static const std::chrono::duration wait_time(std::chrono::seconds(5));
79   std::unique_lock<std::mutex> lock(wait_mutex_);
80   if (wait_cond_.wait_for(lock, wait_time, [this, type] { return wait_value_ == type; })) {
81     return true;
82   } else {
83     log_async_safe("pthread_cond_timedwait for value %d failed", type);
84     return false;
85   }
86 }
87 
Wake()88 void ThreadEntry::Wake() {
89   wait_mutex_.lock();
90   wait_value_++;
91   wait_mutex_.unlock();
92 
93   wait_cond_.notify_one();
94 }
95 
CopyUcontextFromSigcontext(void * sigcontext)96 void ThreadEntry::CopyUcontextFromSigcontext(void* sigcontext) {
97   ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(sigcontext);
98   // The only thing the unwinder cares about is the mcontext data.
99   memcpy(&ucontext_.uc_mcontext, &ucontext->uc_mcontext, sizeof(ucontext->uc_mcontext));
100 }
101 
102 }  // namespace unwindstack
103