20 #include "gtest/gtest.h" 28 namespace base_internal {
34 static int num_identities_reused;
36 static const void*
const kCheckNoIdentity =
reinterpret_cast<void*
>(1);
38 static void TestThreadIdentityCurrent(
const void* assert_no_identity) {
39 ThreadIdentity* identity;
44 if (assert_no_identity == kCheckNoIdentity) {
46 EXPECT_TRUE(identity ==
nullptr);
50 EXPECT_TRUE(identity !=
nullptr);
51 ThreadIdentity* identity_no_init;
53 EXPECT_TRUE(identity == identity_no_init);
56 EXPECT_EQ(0, reinterpret_cast<intptr_t>(&identity->per_thread_synch) %
58 EXPECT_EQ(identity, identity->per_thread_synch.thread_identity());
61 num_identities_reused++;
64 TEST(ThreadIdentityTest, BasicIdentityWorks) {
66 TestThreadIdentityCurrent(
nullptr);
69 TEST(ThreadIdentityTest, BasicIdentityWorksThreaded) {
76 static const int kNumLoops = 3;
78 for (
int iter = 0; iter < kNumLoops; iter++) {
79 std::vector<std::thread> threads;
82 std::thread(TestThreadIdentityCurrent, kCheckNoIdentity));
84 for (
auto& thread : threads) {
92 EXPECT_LT(kNumThreads, num_identities_reused);
95 TEST(ThreadIdentityTest, ReusedThreadIdentityMutexTest) {
101 static const int kNumLoops = 10;
102 static const int kNumThreads = 12;
103 static const int kNumMutexes = 3;
104 static const int kNumLockLoops = 5;
106 Mutex mutexes[kNumMutexes];
107 for (
int iter = 0; iter < kNumLoops; ++iter) {
108 std::vector<std::thread> threads;
109 for (
int thread = 0; thread <
kNumThreads; ++thread) {
110 threads.push_back(std::thread([&]() {
111 for (
int l = 0; l < kNumLockLoops; ++l) {
112 for (
int m = 0; m < kNumMutexes; ++m) {
113 MutexLock lock(&mutexes[m]);
118 for (
auto& thread : threads) {
TEST(NotificationTest, SanityTest)
base_internal::ThreadIdentity * GetOrCreateCurrentThreadIdentity()
ThreadIdentity * CurrentThreadIdentityIfPresent()
static constexpr int kAlignment
constexpr int32_t kNumThreads