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

Function bthread_setspecific

src/bthread/key.cpp:626–648  ·  view source on GitHub ↗

NOTE: Can't borrow_keytable in bthread_setspecific, otherwise following memory leak may occur: -> bthread_getspecific fails to borrow_keytable and returns NULL. -> bthread_setspecific succeeds to borrow_keytable and overwrites old data at the position with newly created data, the old data is leaked.

Source from the content-addressed store, hash-verified

624// -> bthread_setspecific succeeds to borrow_keytable and overwrites old data
625// at the position with newly created data, the old data is leaked.
626int bthread_setspecific(bthread_key_t key, void* data) {
627 bthread::KeyTable* kt = bthread::tls_bls.keytable;
628 if (NULL == kt) {
629 kt = new (std::nothrow) bthread::KeyTable;
630 if (NULL == kt) {
631 return ENOMEM;
632 }
633 bthread::tls_bls.keytable = kt;
634 bthread::TaskGroup* const g = bthread::BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
635 if (g) {
636 g->current_task()->local_storage.keytable = kt;
637 } else {
638 // Only cleanup keytable created by pthread.
639 // keytable created by bthread will be deleted
640 // in `return_keytable' or `bthread_keytable_pool_destroy'.
641 if (!bthread::tls_ever_created_keytable) {
642 bthread::tls_ever_created_keytable = true;
643 CHECK_EQ(0, butil::thread_atexit(bthread::cleanup_pthread, kt));
644 }
645 }
646 }
647 return kt->set_data(key, data);
648}
649
650void* bthread_getspecific(bthread_key_t key) {
651 bthread::KeyTable* kt = bthread::tls_bls.keytable;

Callers 14

EchoMethod · 0.85
worker1_implFunction · 0.85
worker2_implFunction · 0.85
use_invalid_keys_implFunction · 0.85
TESTFunction · 0.85
sid_dtorFunction · 0.85
sid_thread_implFunction · 0.85
set_before_anybth_implFunction · 0.85
pool_thread_implFunction · 0.85
pool_dtorFunction · 0.85
usleep_thread_implFunction · 0.85
lid_worker_implFunction · 0.85

Calls 2

thread_atexitFunction · 0.85
current_taskMethod · 0.80

Tested by 11

worker1_implFunction · 0.68
worker2_implFunction · 0.68
use_invalid_keys_implFunction · 0.68
TESTFunction · 0.68
sid_dtorFunction · 0.68
sid_thread_implFunction · 0.68
set_before_anybth_implFunction · 0.68
pool_thread_implFunction · 0.68
pool_dtorFunction · 0.68
usleep_thread_implFunction · 0.68
lid_worker_implFunction · 0.68