Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/brpc/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#include "bthread/task_group.h"

namespace bthread {
extern BAIDU_THREAD_LOCAL TaskGroup* tls_task_group;
EXTERN_BAIDU_VOLATILE_THREAD_LOCAL(TaskGroup*, tls_task_group);
}

// This is the only place that both client/server must link, so we put
Expand Down Expand Up @@ -714,7 +714,7 @@ void Controller::OnVersionedRPCReturned(const CompletionInfo& info,
response_attachment().clear();

// Retry backoff.
bthread::TaskGroup* g = bthread::tls_task_group;
bthread::TaskGroup* g = bthread::BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
int64_t backoff_time_us = retry_policy->GetBackoffTimeMs(this) * 1000L;
if (backoff_time_us > 0 &&
backoff_time_us < _deadline_us - butil::gettimeofday_us()) {
Expand Down
4 changes: 0 additions & 4 deletions src/brpc/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
#include "brpc/options.pb.h" // ProtocolType
#include "brpc/span.pb.h"

namespace bthread {
extern __thread bthread::LocalStorage tls_bls;
}

namespace brpc {

class Span;
Expand Down
17 changes: 9 additions & 8 deletions src/bthread/bthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ pthread_mutex_t g_task_control_mutex = PTHREAD_MUTEX_INITIALIZER;
// are not constructed before main().
TaskControl* g_task_control = NULL;

extern BAIDU_THREAD_LOCAL TaskGroup* tls_task_group;
EXTERN_BAIDU_VOLATILE_THREAD_LOCAL(TaskGroup*, tls_task_group);
extern void (*g_worker_startfn)();
extern void (*g_tagged_worker_startfn)(bthread_tag_t);
Expand Down Expand Up @@ -263,7 +262,7 @@ static bool validate_bthread_concurrency_by_tag(const char*, int32_t val) {
return bthread_setconcurrency_by_tag(val, FLAGS_bthread_current_tag) == 0;
}

__thread TaskGroup* tls_task_group_nosignal = NULL;
BAIDU_VOLATILE_THREAD_LOCAL(TaskGroup*, tls_task_group_nosignal, NULL);

BUTIL_FORCE_INLINE int
start_from_non_worker(bthread_t* __restrict tid,
Expand All @@ -283,13 +282,14 @@ start_from_non_worker(bthread_t* __restrict tid,
// 1. NOSIGNAL is often for creating many bthreads in batch,
// inserting into the same TaskGroup maximizes the batch.
// 2. bthread_flush() needs to know which TaskGroup to flush.
TaskGroup* g = tls_task_group_nosignal;
if (NULL == g) {
auto g = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group_nosignal);
if (NULL == g || g->tag() != tag) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use CHECK_EQ(g->tag(), tag) is more appropriate.

g = c->choose_one_group(tag);
tls_task_group_nosignal = g;
BAIDU_SET_VOLATILE_THREAD_LOCAL(tls_task_group_nosignal, g);
}
return g->start_background<true>(tid, attr, fn, arg);
}
BAIDU_SET_VOLATILE_THREAD_LOCAL(tls_task_group_nosignal, NULL);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this?

return c->choose_one_group(tag)->start_background<true>(tid, attr, fn, arg);
Comment on lines +292 to 293
}

Expand All @@ -298,7 +298,8 @@ start_from_non_worker(bthread_t* __restrict tid,
// tag equal to thread local
// tag equal to BTHREAD_TAG_INVALID
BUTIL_FORCE_INLINE bool can_run_thread_local(const bthread_attr_t* __restrict attr) {
return attr == nullptr || attr->tag == tls_task_group->tag() ||
return attr == nullptr ||
attr->tag == BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group)->tag() ||
attr->tag == BTHREAD_TAG_INVALID;
}

Expand Down Expand Up @@ -360,10 +361,10 @@ void bthread_flush() {
if (g) {
return g->flush_nosignal_tasks();
}
g = bthread::tls_task_group_nosignal;
g = bthread::BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group_nosignal);
if (g) {
// NOSIGNAL tasks were created in this non-worker.
bthread::tls_task_group_nosignal = NULL;
bthread::BAIDU_SET_VOLATILE_THREAD_LOCAL(tls_task_group_nosignal, NULL);
return g->flush_nosignal_tasks_remote();
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/bthread/butex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ int wait_pthread(ButexPthreadWaiter& pw, const timespec* abstime) {
}
}

extern BAIDU_THREAD_LOCAL TaskGroup* tls_task_group;
EXTERN_BAIDU_VOLATILE_THREAD_LOCAL(TaskGroup*, tls_task_group);

// Returns 0 when no need to unschedule or successfully unscheduled,
// -1 otherwise.
Expand Down Expand Up @@ -280,7 +280,8 @@ void butex_destroy(void* butex) {

// if TaskGroup tls_task_group is belong to tag
inline bool is_same_tag(bthread_tag_t tag) {
return tls_task_group && tls_task_group->tag() == tag;
auto g = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
return g && g->tag() == tag;
}

// nosignal is true & tag is same can return true
Expand All @@ -290,7 +291,8 @@ inline bool check_nosignal(bool nosignal, bthread_tag_t tag) {

// if tag is same return tls_task_group else choose one group with tag
inline TaskGroup* get_task_group(TaskControl* c, bthread_tag_t tag) {
return is_same_tag(tag) ? tls_task_group : c->choose_one_group(tag);
return is_same_tag(tag) ? BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group)
: c->choose_one_group(tag);
}

inline void run_in_local_task_group(TaskGroup* g, TaskMeta* next_meta, bool nosignal) {
Expand Down Expand Up @@ -320,7 +322,7 @@ int butex_wake(void* arg, bool nosignal) {
ButexBthreadWaiter* bbw = static_cast<ButexBthreadWaiter*>(front);
unsleep_if_necessary(bbw, get_global_timer_thread());
TaskGroup* g = get_task_group(bbw->control, bbw->task_meta->attr.tag);
if (g == tls_task_group) {
if (g == BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group)) {
run_in_local_task_group(g, bbw->task_meta, nosignal);
} else {
g->ready_to_run_remote(bbw->task_meta, check_nosignal(nosignal, g->tag()));
Expand Down Expand Up @@ -384,7 +386,7 @@ int butex_wake_n(void* arg, size_t n, bool nosignal) {
}
}
auto g = get_task_group(next->control, next->task_meta->attr.tag);
if (g == tls_task_group) {
if (g == BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group)) {
run_in_local_task_group(g, next->task_meta, nosignal);
} else {
g->ready_to_run_remote(next->task_meta, check_nosignal(nosignal, g->tag()));
Expand Down Expand Up @@ -488,7 +490,9 @@ int butex_requeue(void* arg, void* arg2) {
}
ButexBthreadWaiter* bbw = static_cast<ButexBthreadWaiter*>(front);
unsleep_if_necessary(bbw, get_global_timer_thread());
auto g = is_same_tag(bbw->task_meta->attr.tag) ? tls_task_group : NULL;
auto g = is_same_tag(bbw->task_meta->attr.tag)
? BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group)
: NULL;
if (g) {
TaskGroup::exchange(&g, bbw->task_meta);
} else {
Expand Down Expand Up @@ -597,7 +601,7 @@ void wait_for_butex(void* arg) {
// the two functions. The on-stack ButexBthreadWaiter is safe to use and
// bw->waiter_state will not change again.
// unsleep_if_necessary(bw, get_global_timer_thread());
tls_task_group->ready_to_run(bw->task_meta);
BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group)->ready_to_run(bw->task_meta);
// FIXME: jump back to original thread is buggy.

// // Value unmatched or waiter is already woken up by TimerThread, jump
Expand Down Expand Up @@ -677,7 +681,7 @@ int butex_wait(void* arg, int expected_value, const timespec* abstime, bool prep
butil::atomic_thread_fence(butil::memory_order_acquire);
return -1;
}
TaskGroup* g = tls_task_group;
TaskGroup* g = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
if (NULL == g || g->is_current_pthread_task()) {
return butex_wait_from_pthread(g, b, expected_value, abstime, prepend);
}
Expand Down
8 changes: 4 additions & 4 deletions src/bthread/fd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern int pthread_fd_wait(int fd, unsigned events, const timespec* abstime);

namespace bthread {

extern BAIDU_THREAD_LOCAL TaskGroup* tls_task_group;
EXTERN_BAIDU_VOLATILE_THREAD_LOCAL(TaskGroup*, tls_task_group);

template <typename T, size_t NBLOCK, size_t BLOCK_SIZE>
class LazyArray {
Expand Down Expand Up @@ -446,7 +446,7 @@ int bthread_fd_wait(int fd, unsigned events) {
errno = EINVAL;
return -1;
}
bthread::TaskGroup* g = bthread::tls_task_group;
bthread::TaskGroup* g = bthread::BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
if (NULL != g && !g->is_current_pthread_task()) {
return bthread::get_epoll_thread(fd).fd_wait(
fd, events, NULL);
Expand All @@ -463,7 +463,7 @@ int bthread_fd_timedwait(int fd, unsigned events,
errno = EINVAL;
return -1;
}
bthread::TaskGroup* g = bthread::tls_task_group;
bthread::TaskGroup* g = bthread::BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
if (NULL != g && !g->is_current_pthread_task()) {
return bthread::get_epoll_thread(fd).fd_wait(
fd, events, abstime);
Expand All @@ -473,7 +473,7 @@ int bthread_fd_timedwait(int fd, unsigned events,

int bthread_connect(int sockfd, const sockaddr* serv_addr,
socklen_t addrlen) {
bthread::TaskGroup* g = bthread::tls_task_group;
bthread::TaskGroup* g = bthread::BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
if (NULL == g || g->is_current_pthread_task()) {
return ::connect(sockfd, serv_addr, addrlen);
}
Expand Down
Loading
Loading