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

Method start_background

src/bthread/task_group.cpp:564–613  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

562
563template <bool REMOTE>
564int TaskGroup::start_background(bthread_t* __restrict th,
565 const bthread_attr_t* __restrict attr,
566 void * (*fn)(void*),
567 void* __restrict arg) {
568 if (__builtin_expect(!fn, 0)) {
569 return EINVAL;
570 }
571 const int64_t start_ns = butil::cpuwide_time_ns();
572 const bthread_attr_t using_attr = (attr ? *attr : BTHREAD_ATTR_NORMAL);
573 butil::ResourceId<TaskMeta> slot;
574 TaskMeta* m = butil::get_resource(&slot);
575 if (BAIDU_UNLIKELY(NULL == m)) {
576 return ENOMEM;
577 }
578 CHECK(m->current_waiter.load(butil::memory_order_relaxed) == NULL);
579 m->sleep_failed = false;
580 m->stop = false;
581 m->interrupted = false;
582 m->about_to_quit = false;
583 m->fn = fn;
584 m->arg = arg;
585 CHECK(m->stack == NULL);
586 m->attr = using_attr;
587 m->local_storage = LOCAL_STORAGE_INIT;
588 if (using_attr.flags & BTHREAD_INHERIT_SPAN) {
589 if (g_create_bthread_span) {
590 m->local_storage.rpcz_parent_span = g_create_bthread_span();
591 } else {
592 m->local_storage.rpcz_parent_span = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_bls).rpcz_parent_span;
593 }
594 }
595 m->cpuwide_start_ns = start_ns;
596 m->stat = EMPTY_STAT;
597 m->tid = make_tid(*m->version_butex, slot);
598 *th = m->tid;
599 if (using_attr.flags & BTHREAD_LOG_START_AND_FINISH) {
600 LOG(INFO) << "Started bthread " << m->tid;
601 }
602 _control->_nbthreads << 1;
603 _control->tag_nbthreads(tag()) << 1;
604#ifdef BRPC_BTHREAD_TRACER
605 _control->_task_tracer.set_status(TASK_STATUS_CREATED, m);
606#endif // BRPC_BTHREAD_TRACER
607 if (REMOTE) {
608 ready_to_run_remote(m, (using_attr.flags & BTHREAD_NOSIGNAL));
609 } else {
610 ready_to_run(m, (using_attr.flags & BTHREAD_NOSIGNAL));
611 }
612 return 0;
613}
614
615// Explicit instantiations.
616template int

Callers

nothing calls this directly

Calls 5

cpuwide_time_nsFunction · 0.85
make_tidFunction · 0.85
set_statusMethod · 0.80
get_resourceFunction · 0.50
loadMethod · 0.45

Tested by

no test coverage detected