sanitizer_thread_registry.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //===-- sanitizer_thread_registry.h -----------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is shared between sanitizer tools.
  10. //
  11. // General thread bookkeeping functionality.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef SANITIZER_THREAD_REGISTRY_H
  14. #define SANITIZER_THREAD_REGISTRY_H
  15. #include "sanitizer_common.h"
  16. #include "sanitizer_list.h"
  17. #include "sanitizer_mutex.h"
  18. namespace __sanitizer {
  19. enum ThreadStatus {
  20. ThreadStatusInvalid, // Non-existent thread, data is invalid.
  21. ThreadStatusCreated, // Created but not yet running.
  22. ThreadStatusRunning, // The thread is currently running.
  23. ThreadStatusFinished, // Joinable thread is finished but not yet joined.
  24. ThreadStatusDead // Joined, but some info is still available.
  25. };
  26. enum class ThreadType {
  27. Regular, // Normal thread
  28. Worker, // macOS Grand Central Dispatch (GCD) worker thread
  29. Fiber, // Fiber
  30. };
  31. // Generic thread context. Specific sanitizer tools may inherit from it.
  32. // If thread is dead, context may optionally be reused for a new thread.
  33. class ThreadContextBase {
  34. public:
  35. explicit ThreadContextBase(u32 tid);
  36. const u32 tid; // Thread ID. Main thread should have tid = 0.
  37. u64 unique_id; // Unique thread ID.
  38. u32 reuse_count; // Number of times this tid was reused.
  39. tid_t os_id; // PID (used for reporting).
  40. uptr user_id; // Some opaque user thread id (e.g. pthread_t).
  41. char name[64]; // As annotated by user.
  42. ThreadStatus status;
  43. bool detached;
  44. ThreadType thread_type;
  45. u32 parent_tid;
  46. ThreadContextBase *next; // For storing thread contexts in a list.
  47. atomic_uint32_t thread_destroyed; // To address race of Joined vs Finished
  48. void SetName(const char *new_name);
  49. void SetDead();
  50. void SetJoined(void *arg);
  51. void SetFinished();
  52. void SetStarted(tid_t _os_id, ThreadType _thread_type, void *arg);
  53. void SetCreated(uptr _user_id, u64 _unique_id, bool _detached,
  54. u32 _parent_tid, void *arg);
  55. void Reset();
  56. void SetDestroyed();
  57. bool GetDestroyed();
  58. // The following methods may be overriden by subclasses.
  59. // Some of them take opaque arg that may be optionally be used
  60. // by subclasses.
  61. virtual void OnDead() {}
  62. virtual void OnJoined(void *arg) {}
  63. virtual void OnFinished() {}
  64. virtual void OnStarted(void *arg) {}
  65. virtual void OnCreated(void *arg) {}
  66. virtual void OnReset() {}
  67. virtual void OnDetached(void *arg) {}
  68. protected:
  69. ~ThreadContextBase();
  70. };
  71. typedef ThreadContextBase* (*ThreadContextFactory)(u32 tid);
  72. class MUTEX ThreadRegistry {
  73. public:
  74. ThreadRegistry(ThreadContextFactory factory);
  75. ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
  76. u32 thread_quarantine_size, u32 max_reuse);
  77. void GetNumberOfThreads(uptr *total = nullptr, uptr *running = nullptr,
  78. uptr *alive = nullptr);
  79. uptr GetMaxAliveThreads();
  80. void Lock() ACQUIRE() { mtx_.Lock(); }
  81. void CheckLocked() const CHECK_LOCKED() { mtx_.CheckLocked(); }
  82. void Unlock() RELEASE() { mtx_.Unlock(); }
  83. // Should be guarded by ThreadRegistryLock.
  84. ThreadContextBase *GetThreadLocked(u32 tid) {
  85. return threads_.empty() ? nullptr : threads_[tid];
  86. }
  87. u32 CreateThread(uptr user_id, bool detached, u32 parent_tid, void *arg);
  88. typedef void (*ThreadCallback)(ThreadContextBase *tctx, void *arg);
  89. // Invokes callback with a specified arg for each thread context.
  90. // Should be guarded by ThreadRegistryLock.
  91. void RunCallbackForEachThreadLocked(ThreadCallback cb, void *arg);
  92. typedef bool (*FindThreadCallback)(ThreadContextBase *tctx, void *arg);
  93. // Finds a thread using the provided callback. Returns kInvalidTid if no
  94. // thread is found.
  95. u32 FindThread(FindThreadCallback cb, void *arg);
  96. // Should be guarded by ThreadRegistryLock. Return 0 if no thread
  97. // is found.
  98. ThreadContextBase *FindThreadContextLocked(FindThreadCallback cb,
  99. void *arg);
  100. ThreadContextBase *FindThreadContextByOsIDLocked(tid_t os_id);
  101. void SetThreadName(u32 tid, const char *name);
  102. void SetThreadNameByUserId(uptr user_id, const char *name);
  103. void DetachThread(u32 tid, void *arg);
  104. void JoinThread(u32 tid, void *arg);
  105. // Finishes thread and returns previous status.
  106. ThreadStatus FinishThread(u32 tid);
  107. void StartThread(u32 tid, tid_t os_id, ThreadType thread_type, void *arg);
  108. void SetThreadUserId(u32 tid, uptr user_id);
  109. private:
  110. const ThreadContextFactory context_factory_;
  111. const u32 max_threads_;
  112. const u32 thread_quarantine_size_;
  113. const u32 max_reuse_;
  114. Mutex mtx_;
  115. u64 total_threads_; // Total number of created threads. May be greater than
  116. // max_threads_ if contexts were reused.
  117. uptr alive_threads_; // Created or running.
  118. uptr max_alive_threads_;
  119. uptr running_threads_;
  120. InternalMmapVector<ThreadContextBase *> threads_;
  121. IntrusiveList<ThreadContextBase> dead_threads_;
  122. IntrusiveList<ThreadContextBase> invalid_threads_;
  123. void QuarantinePush(ThreadContextBase *tctx);
  124. ThreadContextBase *QuarantinePop();
  125. };
  126. typedef GenericScopedLock<ThreadRegistry> ThreadRegistryLock;
  127. } // namespace __sanitizer
  128. #endif // SANITIZER_THREAD_REGISTRY_H