Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix gflags bthread_concurrency_by_tag validate error #2730

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading