|
1 | 1 | #include "source/common/common/thread.h" |
2 | 2 |
|
| 3 | +#include <thread> |
| 4 | + |
| 5 | +#include "source/common/common/assert.h" |
| 6 | +#include "source/common/common/macros.h" |
| 7 | + |
3 | 8 | namespace Envoy { |
4 | 9 | namespace Thread { |
5 | 10 |
|
6 | | -bool MainThread::isMainThread() { |
7 | | - // If threading is off, only main thread is running. |
8 | | - auto main_thread_singleton = MainThreadSingleton::getExisting(); |
9 | | - if (main_thread_singleton == nullptr) { |
10 | | - return true; |
| 11 | +namespace { |
| 12 | + |
| 13 | +// Singleton structure capturing which thread is the main dispatcher thread, and |
| 14 | +// which is the test thread. This info is used for assertions around catching |
| 15 | +// exceptions and accessing data structures which are not mutex-protected, and |
| 16 | +// are expected only from the main thread. |
| 17 | +// |
| 18 | +// TODO(jmarantz): avoid the singleton and instead have this object owned |
| 19 | +// by the ThreadFactory. That will require plumbing the API::API into all |
| 20 | +// call-sites for isMainThread(), which might be a bit of work, but will make |
| 21 | +// tests more hermetic. |
| 22 | +struct ThreadIds { |
| 23 | + // Determines whether we are currently running on the main-thread or |
| 24 | + // test-thread. We need to allow for either one because we don't establish |
| 25 | + // the full threading model in all unit tests. |
| 26 | + bool inMainOrTestThread() const { |
| 27 | + // We don't take the lock when testing the thread IDs, as they are atomic, |
| 28 | + // and are cleared when being released. All possible thread orderings |
| 29 | + // result in the correct result even without a lock. |
| 30 | + std::thread::id id = std::this_thread::get_id(); |
| 31 | + return main_thread_id_ == id || test_thread_id_ == id; |
11 | 32 | } |
12 | | - // When threading is on, compare thread id with main thread id. |
13 | | - return main_thread_singleton->inMainThread() || main_thread_singleton->inTestThread(); |
14 | | -} |
15 | | - |
16 | | -void MainThread::clear() { |
17 | | - delete MainThreadSingleton::getExisting(); |
18 | | - MainThreadSingleton::clear(); |
19 | | -} |
20 | | - |
21 | | -void MainThread::initTestThread() { |
22 | | - if (!initialized()) { |
23 | | - MainThreadSingleton::initialize(new MainThread()); |
| 33 | + |
| 34 | + // Returns a singleton instance of this. The instance is never freed. |
| 35 | + static ThreadIds& get() { MUTABLE_CONSTRUCT_ON_FIRST_USE(ThreadIds); } |
| 36 | + |
| 37 | + // Call this when the MainThread exits. Nested semantics are supported, so |
| 38 | + // that if multiple MainThread instances are declared, we unwind them |
| 39 | + // properly. |
| 40 | + void releaseMainThread() { |
| 41 | + absl::MutexLock lock(&mutex_); |
| 42 | + ASSERT(main_thread_use_count_ > 0); |
| 43 | + ASSERT(std::this_thread::get_id() == main_thread_id_); |
| 44 | + if (--main_thread_use_count_ == 0) { |
| 45 | + // Clearing the thread ID when its use-count goes to zero allows us |
| 46 | + // to read the atomic without taking a lock. |
| 47 | + main_thread_id_ = std::thread::id{}; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Call this when the TestThread exits. Nested semantics are supported, so |
| 52 | + // that if multiple TestThread instances are declared, we unwind them |
| 53 | + // properly. |
| 54 | + void releaseTestThread() { |
| 55 | + absl::MutexLock lock(&mutex_); |
| 56 | + ASSERT(test_thread_use_count_ > 0); |
| 57 | + ASSERT(std::this_thread::get_id() == test_thread_id_); |
| 58 | + if (--test_thread_use_count_ == 0) { |
| 59 | + // Clearing the thread ID when its use-count goes to zero allows us |
| 60 | + // to read the atomic without taking a lock. |
| 61 | + test_thread_id_ = std::thread::id{}; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Declares current thread as the main one, or verifies that the current |
| 66 | + // thread matches any previous declarations. |
| 67 | + void registerMainThread() { |
| 68 | + absl::MutexLock lock(&mutex_); |
| 69 | + if (++main_thread_use_count_ > 1) { |
| 70 | + ASSERT(std::this_thread::get_id() == main_thread_id_); |
| 71 | + } else { |
| 72 | + main_thread_id_ = std::this_thread::get_id(); |
| 73 | + } |
24 | 74 | } |
25 | | - MainThreadSingleton::get().registerTestThread(); |
26 | | -} |
27 | 75 |
|
28 | | -void MainThread::initMainThread() { |
29 | | - if (!initialized()) { |
30 | | - MainThreadSingleton::initialize(new MainThread()); |
| 76 | + // Declares current thread as the test thread, or verifies that the current |
| 77 | + // thread matches any previous declarations. |
| 78 | + void registerTestThread() { |
| 79 | + absl::MutexLock lock(&mutex_); |
| 80 | + if (++test_thread_use_count_ > 1) { |
| 81 | + ASSERT(std::this_thread::get_id() == test_thread_id_); |
| 82 | + } else { |
| 83 | + test_thread_id_ = std::this_thread::get_id(); |
| 84 | + } |
31 | 85 | } |
32 | | - MainThreadSingleton::get().registerMainThread(); |
33 | | -} |
| 86 | + |
| 87 | +private: |
| 88 | + // The atomic thread IDs can be read without a mutex, but they are written |
| 89 | + // under a mutex so that they are consistent with their use_counts. this |
| 90 | + // avoids the possibility of two threads racing to claim being the main/test |
| 91 | + // thread. |
| 92 | + std::atomic<std::thread::id> main_thread_id_; |
| 93 | + std::atomic<std::thread::id> test_thread_id_; |
| 94 | + |
| 95 | + int32_t main_thread_use_count_ GUARDED_BY(mutex_) = 0; |
| 96 | + int32_t test_thread_use_count_ GUARDED_BY(mutex_) = 0; |
| 97 | + mutable absl::Mutex mutex_; |
| 98 | +}; |
| 99 | + |
| 100 | +} // namespace |
| 101 | + |
| 102 | +bool MainThread::isMainThread() { return ThreadIds::get().inMainOrTestThread(); } |
| 103 | + |
| 104 | +TestThread::TestThread() { ThreadIds::get().registerTestThread(); } |
| 105 | + |
| 106 | +TestThread::~TestThread() { ThreadIds::get().releaseTestThread(); } |
| 107 | + |
| 108 | +MainThread::MainThread() { ThreadIds::get().registerMainThread(); } |
| 109 | + |
| 110 | +MainThread::~MainThread() { ThreadIds::get().releaseMainThread(); } |
34 | 111 |
|
35 | 112 | } // namespace Thread |
36 | 113 | } // namespace Envoy |
0 commit comments