Skip to content

依赖 brpc 作为第三方库时使用 Tsan 报错 #2864

@MJY-HUST

Description

@MJY-HUST
  • 项目中依赖brpc作为第三方库时出现的报错问题: brpc中的mutex.cpp覆盖了pthread_mutex_lock的实现,疑似导致tsan识别不了bthread内部实现的pthread_mutex_lock,导致出现了告警,请问有办法解决这个warning吗?一个简单的复现方式
#include <bthread/bthread.h>
#include <gflags/gflags.h>
#include <gtest/gtest.h>

namespace my::test {

class TsanCheckTest : public ::testing::Test {};

int counter;
std::mutex mutex_std;
pthread_mutex_t mu;
const int kIncrementsPerThread = 100;
void* Locker(void*) {
 for (int i = 0; i < kIncrementsPerThread; ++i) {
   pthread_mutex_lock(&mu);
   counter++;
   pthread_mutex_unlock(&mu);
 }
 return nullptr;
}

TEST_F(TsanCheckTest, MutexUsedInPthread) {
 pthread_mutex_init(&mu, nullptr);
 const int num_threads = 8;
 pthread_t th[num_threads];

 counter = 0;
 for (int i = 0; i < num_threads; ++i) {
   ASSERT_EQ(0, pthread_create(&th[i], nullptr, Locker, nullptr));
 }
 for (int i = 0; i < num_threads; ++i) {
   ASSERT_EQ(0, pthread_join(th[i], nullptr));
 }
 ASSERT_EQ(counter, num_threads * kIncrementsPerThread);
}

TEST_F(TsanCheckTest, MutexUsedInBthread) {
 const int num_threads = 8;
 bthread_t th[num_threads];

 counter = 0;
 for (int i = 0; i < num_threads; ++i) {
   ASSERT_EQ(0, bthread_start_urgent(&th[i], nullptr, Locker, nullptr));
 }
 for (int i = 0; i < num_threads; ++i) {
   ASSERT_EQ(0, bthread_join(th[i], nullptr));
 }
 ASSERT_EQ(counter, num_threads * kIncrementsPerThread);
}

}  // namespace my::test

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions