diff --git a/deps/oblib/src/lib/thread/threads.cpp b/deps/oblib/src/lib/thread/threads.cpp index 489a5d2df..504ccb72d 100644 --- a/deps/oblib/src/lib/thread/threads.cpp +++ b/deps/oblib/src/lib/thread/threads.cpp @@ -209,6 +209,7 @@ void Threads::run(int64_t idx) ObTLTaGuard ta_guard(GET_TENANT_ID() ?:OB_SERVER_TENANT_ID); thread_idx_ = static_cast(idx); Worker worker; + Worker::set_worker_to_thread_local(&worker); run1(); } diff --git a/deps/oblib/src/lib/worker.cpp b/deps/oblib/src/lib/worker.cpp index 0c2399671..71aa0a470 100644 --- a/deps/oblib/src/lib/worker.cpp +++ b/deps/oblib/src/lib/worker.cpp @@ -58,15 +58,6 @@ Worker::Worker() disable_wait_(false) { worker_node_.get_data() = this; - if (OB_ISNULL(self_)) { - self_ = this; - } else { - // Ideally, there won't be worker creating when a routine, or - // thread, has a worker, i.e. self_ isn't null. Whereas ObThWorker - // which derived from Worker doesn't create instance on the same - // routine as it is. So we can't assert self_ is null when new - // Worker is initializing right now. - } } Worker::~Worker() diff --git a/deps/oblib/src/lib/worker.h b/deps/oblib/src/lib/worker.h index 0c94ee008..a1ab881cb 100644 --- a/deps/oblib/src/lib/worker.h +++ b/deps/oblib/src/lib/worker.h @@ -116,6 +116,7 @@ public: static void set_compatibility_mode(CompatMode mode); static CompatMode get_compatibility_mode(); static Worker& self(); + static void set_worker_to_thread_local(Worker *worker); public: static __thread Worker *self_; @@ -154,6 +155,12 @@ private: extern void *alloc_worker(); extern void common_yield(); + +inline void Worker::set_worker_to_thread_local(Worker *worker) +{ + self_ = worker; +} + inline Worker &Worker::self() { // wbuf won't been NULL. diff --git a/src/libtable/src/libobtable.cpp b/src/libtable/src/libobtable.cpp index f585e78b0..af48c78fb 100644 --- a/src/libtable/src/libobtable.cpp +++ b/src/libtable/src/libobtable.cpp @@ -33,6 +33,7 @@ int ObTableServiceLibrary::init() ::oceanbase::common::USE_CO_LATCH = false; // Create worker for current thread. ::oceanbase::lib::Worker worker; + ::oceanbase::lib::Worker::set_worker_to_thread_local(&worker); // setup easy easy_log_set_format(easy_log_format_adaptor); easy_log_level = EASY_LOG_INFO; diff --git a/src/observer/main.cpp b/src/observer/main.cpp index f32e58266..640907d5f 100644 --- a/src/observer/main.cpp +++ b/src/observer/main.cpp @@ -526,7 +526,7 @@ int main(int argc, char *argv[]) // thread has already had a worker, which can prevent binding // new worker with it. lib::Worker worker; - + lib::Worker::set_worker_to_thread_local(&worker); ObServer &observer = ObServer::get_instance(); LOG_INFO("observer starts", "observer_version", PACKAGE_STRING); // to speed up bootstrap phase, need set election INIT TS diff --git a/src/observer/omt/ob_tenant.cpp b/src/observer/omt/ob_tenant.cpp index c711806b5..6ee7de637 100644 --- a/src/observer/omt/ob_tenant.cpp +++ b/src/observer/omt/ob_tenant.cpp @@ -237,6 +237,7 @@ void ObPxPool::run(int64_t idx) set_thread_idx(idx); // Create worker for current thread. ObPxWorker worker; + Worker::set_worker_to_thread_local(&worker); run1(); } diff --git a/src/observer/omt/ob_th_worker.cpp b/src/observer/omt/ob_th_worker.cpp index c6b56899c..1a1038fbd 100644 --- a/src/observer/omt/ob_th_worker.cpp +++ b/src/observer/omt/ob_th_worker.cpp @@ -315,8 +315,7 @@ void ObThWorker::set_th_worker_thread_name(uint64_t tenant_id) void ObThWorker::worker(int64_t &tenant_id, int64_t &req_recv_timestamp, int32_t &worker_level) { int ret = OB_SUCCESS; - lib::Worker::self_ = this; - lib::Worker::self_ = this; + Worker::set_worker_to_thread_local(static_cast(this)); int64_t wait_start_time = 0; int64_t wait_end_time = 0; int64_t req_start_time = 0;