Skip to content

Commit

Permalink
use object_pool to store keytable by gflag
Browse files Browse the repository at this point in the history
  • Loading branch information
MJY-HUST committed May 19, 2024
1 parent b601c89 commit d147ced
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions src/bthread/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <pthread.h>
#include "butil/macros.h"
#include "butil/atomicops.h"
#include "butil/object_pool.h"
#include "bvar/passive_status.h"
#include "bthread/errno.h" // EAGAIN
#include "bthread/task_group.h" // TaskGroup
Expand All @@ -30,6 +31,10 @@

namespace bthread {


DEFINE_bool(use_object_pool_store_keytable, false, "When this flags is on, using "
"object_pool to store keytable ");

class KeyTable;

// defined in task_group.cpp
Expand Down Expand Up @@ -219,21 +224,25 @@ static KeyTable* borrow_keytable(bthread_keytable_pool_t* pool) {
// Referenced in task_group.cpp, must be extern.
// Caller of this function must hold the KeyTable
void return_keytable(bthread_keytable_pool_t* pool, KeyTable* kt) {
if (NULL == kt) {
return;
}
if (pool == NULL) {
delete kt;
return;
}
std::unique_lock<pthread_mutex_t> mu(pool->mutex);
if (pool->destroyed) {
mu.unlock();
delete kt;
return;
if(FLAGS_use_object_pool_store_keytable) {
butil::return_object<bthread::KeyTable>(kt);
} else {
if (NULL == kt) {
return;
}
if (pool == NULL) {
delete kt;
return;
}
std::unique_lock<pthread_mutex_t> mu(pool->mutex);
if (pool->destroyed) {
mu.unlock();
delete kt;
return;
}
kt->next = (KeyTable*)pool->free_keytables;
pool->free_keytables = kt;
}
kt->next = (KeyTable*)pool->free_keytables;
pool->free_keytables = kt;
}

static void cleanup_pthread(void* arg) {
Expand Down Expand Up @@ -438,7 +447,11 @@ int bthread_key_delete(bthread_key_t key) {
int bthread_setspecific(bthread_key_t key, void* data) {
bthread::KeyTable* kt = bthread::tls_bls.keytable;
if (NULL == kt) {
kt = new (std::nothrow) bthread::KeyTable;
if (bthread::FLAGS_use_object_pool_store_keytable) {
kt = butil::get_object<bthread::KeyTable>();
} else {
kt = new (std::nothrow) bthread::KeyTable;
}
if (NULL == kt) {
return ENOMEM;
}
Expand Down Expand Up @@ -466,8 +479,12 @@ void* bthread_getspecific(bthread_key_t key) {
}
bthread::TaskGroup* const g = bthread::tls_task_group;
if (g) {
bthread::TaskMeta* const task = g->current_task();
kt = bthread::borrow_keytable(task->attr.keytable_pool);
if (bthread::FLAGS_use_object_pool_store_keytable) {
kt = butil::get_object<bthread::KeyTable>();
} else {
bthread::TaskMeta* const task = g->current_task();
kt = bthread::borrow_keytable(task->attr.keytable_pool);
}
if (kt) {
g->current_task()->local_storage.keytable = kt;
bthread::tls_bls.keytable = kt;
Expand Down

0 comments on commit d147ced

Please sign in to comment.