ThreadEntry.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include <pthread.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <time.h>
  21. #include <ucontext.h>
  22. #include "BacktraceAsyncSafeLog.h"
  23. #include "ThreadEntry.h"
  24. // Initialize static member variables.
  25. ThreadEntry* ThreadEntry::list_ = nullptr;
  26. pthread_mutex_t ThreadEntry::list_mutex_ = PTHREAD_MUTEX_INITIALIZER;
  27. // Assumes that ThreadEntry::list_mutex_ has already been locked before
  28. // creating a ThreadEntry object.
  29. ThreadEntry::ThreadEntry(pid_t pid, pid_t tid)
  30. : pid_(pid), tid_(tid), ref_count_(1), mutex_(PTHREAD_MUTEX_INITIALIZER),
  31. wait_mutex_(PTHREAD_MUTEX_INITIALIZER), wait_value_(0),
  32. next_(ThreadEntry::list_), prev_(nullptr) {
  33. pthread_condattr_t attr;
  34. pthread_condattr_init(&attr);
  35. pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
  36. pthread_cond_init(&wait_cond_, &attr);
  37. // Add ourselves to the list.
  38. if (ThreadEntry::list_) {
  39. ThreadEntry::list_->prev_ = this;
  40. }
  41. ThreadEntry::list_ = this;
  42. }
  43. ThreadEntry* ThreadEntry::Get(pid_t pid, pid_t tid, bool create) {
  44. pthread_mutex_lock(&ThreadEntry::list_mutex_);
  45. ThreadEntry* entry = list_;
  46. while (entry != nullptr) {
  47. if (entry->Match(pid, tid)) {
  48. break;
  49. }
  50. entry = entry->next_;
  51. }
  52. if (!entry) {
  53. if (create) {
  54. entry = new ThreadEntry(pid, tid);
  55. }
  56. } else {
  57. entry->ref_count_++;
  58. }
  59. pthread_mutex_unlock(&ThreadEntry::list_mutex_);
  60. return entry;
  61. }
  62. void ThreadEntry::Remove(ThreadEntry* entry) {
  63. entry->Unlock();
  64. pthread_mutex_lock(&ThreadEntry::list_mutex_);
  65. if (--entry->ref_count_ == 0) {
  66. delete entry;
  67. }
  68. pthread_mutex_unlock(&ThreadEntry::list_mutex_);
  69. }
  70. // Assumes that ThreadEntry::list_mutex_ has already been locked before
  71. // deleting a ThreadEntry object.
  72. ThreadEntry::~ThreadEntry() {
  73. if (list_ == this) {
  74. list_ = next_;
  75. } else {
  76. if (next_) {
  77. next_->prev_ = prev_;
  78. }
  79. prev_->next_ = next_;
  80. }
  81. next_ = nullptr;
  82. prev_ = nullptr;
  83. pthread_cond_destroy(&wait_cond_);
  84. }
  85. bool ThreadEntry::Wait(int value) {
  86. timespec ts;
  87. clock_gettime(CLOCK_MONOTONIC, &ts);
  88. ts.tv_sec += 5;
  89. bool wait_completed = true;
  90. pthread_mutex_lock(&wait_mutex_);
  91. while (wait_value_ != value) {
  92. int ret = pthread_cond_timedwait(&wait_cond_, &wait_mutex_, &ts);
  93. if (ret != 0) {
  94. BACK_ASYNC_SAFE_LOGW("pthread_cond_timedwait for value %d failed: %s", value, strerror(ret));
  95. wait_completed = false;
  96. break;
  97. }
  98. }
  99. pthread_mutex_unlock(&wait_mutex_);
  100. return wait_completed;
  101. }
  102. void ThreadEntry::Wake() {
  103. pthread_mutex_lock(&wait_mutex_);
  104. wait_value_++;
  105. pthread_mutex_unlock(&wait_mutex_);
  106. pthread_cond_signal(&wait_cond_);
  107. }
  108. void ThreadEntry::CopyUcontextFromSigcontext(void* sigcontext) {
  109. ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(sigcontext);
  110. // The only thing the unwinder cares about is the mcontext data.
  111. memcpy(&ucontext_.uc_mcontext, &ucontext->uc_mcontext, sizeof(ucontext->uc_mcontext));
  112. }