Skip to content

Commit

Permalink
Merge pull request #2730 from MJY-HUST/fix_gflags_validate
Browse files Browse the repository at this point in the history
fix gflags bthread_concurrency_by_tag validate error
  • Loading branch information
yanglimingcn authored Aug 27, 2024
2 parents 48996cf + a47fdd5 commit d3c6854
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
19 changes: 9 additions & 10 deletions src/bthread/bthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ DEFINE_int32(bthread_min_concurrency, 0,
" The laziness is disabled when this value is non-positive,"
" and workers will be created eagerly according to -bthread_concurrency and bthread_setconcurrency(). ");

DEFINE_int32(bthread_current_tag, BTHREAD_TAG_DEFAULT, "Set bthread concurrency for this tag");
DEFINE_int32(bthread_current_tag, BTHREAD_TAG_INVALID, "Set bthread concurrency for this tag");

DEFINE_int32(bthread_concurrency_by_tag, 0,
DEFINE_int32(bthread_concurrency_by_tag, 8 + BTHREAD_EPOLL_THREAD_NUM,
"Number of pthread workers of FLAGS_bthread_current_tag");

static bool never_set_bthread_concurrency = true;
static bool never_set_bthread_concurrency_by_tag = true;

static bool validate_bthread_concurrency(const char*, int32_t val) {
// bthread_setconcurrency sets the flag on success path which should
Expand Down Expand Up @@ -147,13 +146,15 @@ static bool validate_bthread_min_concurrency(const char*, int32_t val) {
}

static bool validate_bthread_current_tag(const char*, int32_t val) {
if (val < BTHREAD_TAG_DEFAULT || val >= FLAGS_task_group_ntags) {
if (val == BTHREAD_TAG_INVALID) {
return true;
} else if (val < BTHREAD_TAG_DEFAULT || val >= FLAGS_task_group_ntags) {
return false;
}
BAIDU_SCOPED_LOCK(bthread::g_task_control_mutex);
auto c = bthread::get_task_control();
if (c == NULL) {
FLAGS_bthread_concurrency_by_tag = 0;
FLAGS_bthread_concurrency_by_tag = 8 + BTHREAD_EPOLL_THREAD_NUM;
return true;
}
FLAGS_bthread_concurrency_by_tag = c->concurrency(val);
Expand Down Expand Up @@ -385,12 +386,10 @@ int bthread_getconcurrency_by_tag(bthread_tag_t tag) {
}

int bthread_setconcurrency_by_tag(int num, bthread_tag_t tag) {
if (bthread::never_set_bthread_concurrency_by_tag) {
bthread::never_set_bthread_concurrency_by_tag = false;
if (tag == BTHREAD_TAG_INVALID) {
return 0;
}
if (tag < BTHREAD_TAG_DEFAULT || tag >= FLAGS_task_group_ntags) {
return EPERM;
} else if (tag < BTHREAD_TAG_DEFAULT || tag >= FLAGS_task_group_ntags) {
return EINVAL;
}
auto c = bthread::get_or_new_task_control();
BAIDU_SCOPED_LOCK(bthread::g_task_control_mutex);
Expand Down
3 changes: 1 addition & 2 deletions test/bthread_setconcurrency_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ int current_tag(int tag) {
}

TEST(BthreadTest, current_tag) {
ASSERT_EQ(false, current_tag(-1));
ASSERT_EQ(false, current_tag(-2));
ASSERT_EQ(true, current_tag(0));
ASSERT_EQ(false, current_tag(1));
}
Expand All @@ -213,7 +213,6 @@ int concurrency_by_tag(int num) {
}

TEST(BthreadTest, concurrency_by_tag) {
ASSERT_EQ(concurrency_by_tag(1), true);
ASSERT_EQ(concurrency_by_tag(1), false);
auto con = bthread_getconcurrency_by_tag(0);
ASSERT_EQ(concurrency_by_tag(con), true);
Expand Down

0 comments on commit d3c6854

Please sign in to comment.