MCPcopy Create free account
hub / github.com/apache/brpc / init_for_pthread_stack_trace

Function init_for_pthread_stack_trace

src/bthread/bthread.cpp:128–178  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

126BAIDU_THREAD_LOCAL TaskMeta* pthread_fake_meta = NULL;
127
128bthread_t init_for_pthread_stack_trace() {
129 if (NULL != pthread_fake_meta) {
130 return pthread_fake_meta->tid;
131 }
132
133 TaskControl* c = get_task_control();
134 if (NULL == c) {
135 LOG(ERROR) << "TaskControl has not been created, "
136 "please use bthread_start_xxx before call this function";
137 return INVALID_BTHREAD;
138 }
139
140 butil::ResourceId<TaskMeta> slot;
141 pthread_fake_meta = butil::get_resource(&slot);
142 if (BAIDU_UNLIKELY(NULL == pthread_fake_meta)) {
143 LOG(ERROR) << "Fail to get TaskMeta";
144 return INVALID_BTHREAD;
145 }
146
147 pthread_fake_meta->attr = BTHREAD_ATTR_PTHREAD;
148 pthread_fake_meta->tid = make_tid(*pthread_fake_meta->version_butex, slot);
149 // Make TaskTracer use signal trace mode for pthread.
150 c->_task_tracer.set_running_status(syscall(SYS_gettid), pthread_fake_meta);
151
152 // Release the TaskMeta at exit of pthread.
153 butil::thread_atexit([]() {
154 // Similar to TaskGroup::task_runner.
155 bool tracing;
156 {
157 BAIDU_SCOPED_LOCK(pthread_fake_meta->version_lock);
158 tracing = TaskTracer::set_end_status_unsafe(pthread_fake_meta);
159 // If resulting version is 0,
160 // change it to 1 to make bthread_t never be 0.
161 if (0 == ++*pthread_fake_meta->version_butex) {
162 ++*pthread_fake_meta->version_butex;
163 }
164 }
165
166 if (tracing) {
167 // Wait for tracing completion.
168 get_task_control()->_task_tracer.WaitForTracing(pthread_fake_meta);
169 }
170 get_task_control()->_task_tracer.set_status(
171 TASK_STATUS_UNKNOWN, pthread_fake_meta);
172
173 butil::return_resource(get_slot(pthread_fake_meta->tid));
174 pthread_fake_meta = NULL;
175 });
176
177 return pthread_fake_meta->tid;
178}
179
180void stack_trace(std::ostream& os, bthread_t tid) {
181 TaskControl* c = get_task_control();

Callers

nothing calls this directly

Calls 9

make_tidFunction · 0.85
thread_atexitFunction · 0.85
set_running_statusMethod · 0.80
WaitForTracingMethod · 0.80
set_statusMethod · 0.80
get_task_controlFunction · 0.70
get_slotFunction · 0.70
get_resourceFunction · 0.50
return_resourceFunction · 0.50

Tested by

no test coverage detected