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 bthread_usleep return unexpected error #2511

Merged
merged 1 commit into from
Jan 31, 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
2 changes: 1 addition & 1 deletion config_brpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ if [ "$SYSTEM" = "Darwin" ]; then
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_MallocExtension_ReleaseFreeMemory"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStart"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStop"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,__Z13GetStackTracePPvii"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,__Z13GetStackTracePPvii"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_RegisterThriftProtocol"
fi
append_linking() {
Expand Down
11 changes: 8 additions & 3 deletions src/bthread/task_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ int TaskGroup::init(size_t runqueue_capacity) {
LOG(FATAL) << "Fail to get TaskMeta";
return -1;
}
m->sleep_failed = false;
m->stop = false;
m->interrupted = false;
m->about_to_quit = false;
Expand Down Expand Up @@ -372,6 +373,7 @@ int TaskGroup::start_foreground(TaskGroup** pg,
return ENOMEM;
}
CHECK(m->current_waiter.load(butil::memory_order_relaxed) == NULL);
m->sleep_failed = false;
m->stop = false;
m->interrupted = false;
m->about_to_quit = false;
Expand Down Expand Up @@ -431,6 +433,7 @@ int TaskGroup::start_background(bthread_t* __restrict th,
return ENOMEM;
}
CHECK(m->current_waiter.load(butil::memory_order_relaxed) == NULL);
m->sleep_failed = false;
m->stop = false;
m->interrupted = false;
m->about_to_quit = false;
Expand Down Expand Up @@ -759,7 +762,8 @@ void TaskGroup::_add_sleep_event(void* void_args) {
butil::microseconds_from_now(e.timeout_us));

if (!sleep_id) {
// fail to schedule timer, go back to previous thread.
e.meta->sleep_failed = true;
// Fail to schedule timer, go back to previous thread.
g->ready_to_run(e.tid);
return;
}
Expand Down Expand Up @@ -801,8 +805,9 @@ int TaskGroup::usleep(TaskGroup** pg, uint64_t timeout_us) {
g->set_remained(_add_sleep_event, &e);
sched(pg);
g = *pg;
if (e.meta->current_sleep == 0 && !e.meta->interrupted) {
// Fail to `_add_sleep_event'.
if (e.meta->sleep_failed) {
// Fail to schedule timer, return error.
e.meta->sleep_failed = false;
errno = ESTOP;
return -1;
}
Expand Down
3 changes: 3 additions & 0 deletions src/bthread/task_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ struct TaskMeta {
butil::atomic<ButexWaiter*> current_waiter;
uint64_t current_sleep;

// A flag to mark if the Timer scheduling failed.
bool sleep_failed;

// A builtin flag to mark if the thread is stopping.
bool stop;

Expand Down
Loading