ThreadEntry.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifndef _LIBBACKTRACE_THREAD_ENTRY_H
  17. #define _LIBBACKTRACE_THREAD_ENTRY_H
  18. #include <pthread.h>
  19. #include <sys/types.h>
  20. #include <ucontext.h>
  21. class ThreadEntry {
  22. public:
  23. static ThreadEntry* Get(pid_t pid, pid_t tid, bool create = true);
  24. static void Remove(ThreadEntry* entry);
  25. void Wake();
  26. bool Wait(int);
  27. void CopyUcontextFromSigcontext(void*);
  28. inline void Lock() {
  29. pthread_mutex_lock(&mutex_);
  30. // Always reset the wait value since this could be the first or nth
  31. // time this entry is locked.
  32. wait_value_ = 0;
  33. }
  34. inline void Unlock() {
  35. pthread_mutex_unlock(&mutex_);
  36. }
  37. inline ucontext_t* GetUcontext() { return &ucontext_; }
  38. private:
  39. ThreadEntry(pid_t pid, pid_t tid);
  40. ~ThreadEntry();
  41. bool Match(pid_t chk_pid, pid_t chk_tid) { return (chk_pid == pid_ && chk_tid == tid_); }
  42. pid_t pid_;
  43. pid_t tid_;
  44. int ref_count_;
  45. pthread_mutex_t mutex_;
  46. pthread_mutex_t wait_mutex_;
  47. pthread_cond_t wait_cond_;
  48. int wait_value_;
  49. ThreadEntry* next_;
  50. ThreadEntry* prev_;
  51. ucontext_t ucontext_;
  52. static ThreadEntry* list_;
  53. static pthread_mutex_t list_mutex_;
  54. };
  55. #endif // _LIBBACKTRACE_THREAD_ENTRY_H