Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use butil::ThreadLocal to store keytable #2645

Merged
merged 8 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/bthread/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,12 @@ class KeyTableList {
};

static KeyTable* borrow_keytable(bthread_keytable_pool_t* pool) {
if (pool != NULL && (pool->list->get()->keytable || pool->free_keytables)) {
butil::ThreadLocal<bthread::KeyTableList>* list = (butil::ThreadLocal<bthread::KeyTableList>*)pool->list;
if (pool != NULL && (list->get()->keytable || pool->free_keytables)) {
pthread_rwlock_rdlock(&pool->rwlock);
KeyTable* p = pool->list->get()->keytable;
KeyTable* p = list->get()->keytable;
if (p) {
pool->list->get()->keytable = p->next;
list->get()->keytable = p->next;
pthread_rwlock_unlock(&pool->rwlock);
return p;
}
Expand Down Expand Up @@ -273,8 +274,9 @@ void return_keytable(bthread_keytable_pool_t* pool, KeyTable* kt) {
delete kt;
return;
}
kt->next = pool->list->get()->keytable;
pool->list->get()->keytable = kt;
butil::ThreadLocal<bthread::KeyTableList>* list = (butil::ThreadLocal<bthread::KeyTableList>*)pool->list;
kt->next = list->get()->keytable;
list->get()->keytable = kt;
pthread_rwlock_unlock(&pool->rwlock);
}

Expand Down Expand Up @@ -336,7 +338,7 @@ int bthread_keytable_pool_destroy(bthread_keytable_pool_t* pool) {
bthread::KeyTable* saved_free_keytables = NULL;
pthread_rwlock_wrlock(&pool->rwlock);
pool->destroyed = 1;
delete pool->list;
delete (butil::ThreadLocal<bthread::KeyTableList>*)pool->list;
saved_free_keytables = (bthread::KeyTable*)pool->free_keytables;
pool->free_keytables = NULL;
pthread_rwlock_unlock(&pool->rwlock);
Expand All @@ -358,7 +360,7 @@ int bthread_keytable_pool_getstat(bthread_keytable_pool_t* pool,
LOG(ERROR) << "Param[pool] or Param[stat] is NULL";
return EINVAL;
}
pthread_rwlock_wrlock(&pool->rwlock);
pthread_rwlock_rdlock(&pool->rwlock);
size_t count = 0;
bthread::KeyTable* p = (bthread::KeyTable*)pool->free_keytables;
for (; p; p = p->next, ++count) {}
Expand Down
10 changes: 1 addition & 9 deletions src/bthread/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,9 @@ inline std::ostream& operator<<(std::ostream& os, bthread_key_t key) {
}
#endif // __cplusplus

namespace bthread{
class KeyTableList;
}

namespace butil {
template <typename T> class ThreadLocal;
}

typedef struct {
pthread_rwlock_t rwlock;
butil::ThreadLocal<bthread::KeyTableList>* list;
void* list;
void* free_keytables;
int destroyed;
} bthread_keytable_pool_t;
Expand Down
Loading