Skip to content
New issue

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

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

Already on GitHub? Sign in 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 2 commits
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
102 changes: 67 additions & 35 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/thread_key.h"
#include "bvar/passive_status.h"
#include "bthread/errno.h" // EAGAIN
#include "bthread/task_group.h" // TaskGroup
Expand Down Expand Up @@ -204,14 +205,55 @@ class BAIDU_CACHELINE_ALIGNMENT KeyTable {
SubKeyTable* _subs[KEY_1STLEVEL_SIZE];
};

class KeyTableList {
public:
KeyTableList() {
keytable = NULL;
}
~KeyTableList() {
bthread::TaskGroup* const g = bthread::tls_task_group;
bthread::KeyTable* old_kt = bthread::tls_bls.keytable;
while (keytable) {
bthread::KeyTable* kt = keytable;
keytable = kt->next;
bthread::tls_bls.keytable = kt;
if (g) {
g->current_task()->local_storage.keytable = kt;
}
chenBright marked this conversation as resolved.
Show resolved Hide resolved
delete kt;
if (old_kt == kt) {
old_kt = NULL;
}
}
bthread::tls_bls.keytable = old_kt;
if(g) {
g->current_task()->local_storage.keytable = old_kt;
}
}
KeyTable* keytable;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class 类型不建议直接把成员暴露成public,要么改成struct,要么封装成getter/setter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成了struct

};

static KeyTable* borrow_keytable(bthread_keytable_pool_t* pool) {
if (pool != NULL && pool->free_keytables) {
BAIDU_SCOPED_LOCK(pool->mutex);
KeyTable* p = (KeyTable*)pool->free_keytables;
butil::ThreadLocal<bthread::KeyTableList>* list = (butil::ThreadLocal<bthread::KeyTableList>*)pool->list;
chenBright marked this conversation as resolved.
Show resolved Hide resolved
if (pool != NULL && (list->get()->keytable || pool->free_keytables)) {
pthread_rwlock_rdlock(&pool->rwlock);
KeyTable* p = list->get()->keytable;
if (p) {
pool->free_keytables = p->next;
list->get()->keytable = p->next;
pthread_rwlock_unlock(&pool->rwlock);
return p;
}
pthread_rwlock_unlock(&pool->rwlock);
if (pool->free_keytables) {
pthread_rwlock_wrlock(&pool->rwlock);
p = (KeyTable*)pool->free_keytables;
if (p) {
pool->free_keytables = p->next;
pthread_rwlock_unlock(&pool->rwlock);
return p;
}
pthread_rwlock_unlock(&pool->rwlock);
}
}
return NULL;
}
Expand All @@ -226,14 +268,16 @@ void return_keytable(bthread_keytable_pool_t* pool, KeyTable* kt) {
delete kt;
return;
}
std::unique_lock<pthread_mutex_t> mu(pool->mutex);
pthread_rwlock_rdlock(&pool->rwlock);
if (pool->destroyed) {
mu.unlock();
pthread_rwlock_unlock(&pool->rwlock);
delete kt;
return;
}
kt->next = (KeyTable*)pool->free_keytables;
pool->free_keytables = kt;
butil::ThreadLocal<bthread::KeyTableList>* list = (butil::ThreadLocal<bthread::KeyTableList>*)pool->list;
chenBright marked this conversation as resolved.
Show resolved Hide resolved
kt->next = list->get()->keytable;
list->get()->keytable = kt;
pthread_rwlock_unlock(&pool->rwlock);
}

static void cleanup_pthread(void* arg) {
chenBright marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -279,7 +323,8 @@ int bthread_keytable_pool_init(bthread_keytable_pool_t* pool) {
LOG(ERROR) << "Param[pool] is NULL";
return EINVAL;
}
pthread_mutex_init(&pool->mutex, NULL);
pthread_rwlock_init(&pool->rwlock, NULL);
pool->list = new butil::ThreadLocal<bthread::KeyTableList>();
pool->free_keytables = NULL;
pool->destroyed = 0;
return 0;
Expand All @@ -291,33 +336,18 @@ int bthread_keytable_pool_destroy(bthread_keytable_pool_t* pool) {
return EINVAL;
}
bthread::KeyTable* saved_free_keytables = NULL;
{
BAIDU_SCOPED_LOCK(pool->mutex);
if (pool->free_keytables) {
saved_free_keytables = (bthread::KeyTable*)pool->free_keytables;
pool->free_keytables = NULL;
}
pool->destroyed = 1;
}
// Cheat get/setspecific and destroy the keytables.
bthread::TaskGroup* const g = bthread::tls_task_group;
bthread::KeyTable* old_kt = bthread::tls_bls.keytable;
while (saved_free_keytables) {
pthread_rwlock_wrlock(&pool->rwlock);
pool->destroyed = 1;
delete (butil::ThreadLocal<bthread::KeyTableList>*)pool->list;
saved_free_keytables = (bthread::KeyTable*)pool->free_keytables;
pool->free_keytables = NULL;
pthread_rwlock_unlock(&pool->rwlock);
while(saved_free_keytables) {
bthread::KeyTable* kt = saved_free_keytables;
saved_free_keytables = kt->next;
bthread::tls_bls.keytable = kt;
if (g) {
g->current_task()->local_storage.keytable = kt;
}
delete kt;
if (old_kt == kt) {
old_kt = NULL;
}
}
bthread::tls_bls.keytable = old_kt;
if (g) {
g->current_task()->local_storage.keytable = old_kt;
}
return 0;
// TODO: return_keytable may race with this function, we don't destroy
// the mutex right now.
// pthread_mutex_destroy(&pool->mutex);
Expand All @@ -330,11 +360,12 @@ int bthread_keytable_pool_getstat(bthread_keytable_pool_t* pool,
LOG(ERROR) << "Param[pool] or Param[stat] is NULL";
return EINVAL;
}
std::unique_lock<pthread_mutex_t> mu(pool->mutex);
pthread_rwlock_rdlock(&pool->rwlock);
size_t count = 0;
bthread::KeyTable* p = (bthread::KeyTable*)pool->free_keytables;
for (; p; p = p->next, ++count) {}
stat->nfree = count;
pthread_rwlock_unlock(&pool->rwlock);
return 0;
}

Expand Down Expand Up @@ -365,14 +396,15 @@ void bthread_keytable_pool_reserve(bthread_keytable_pool_t* pool,
kt->set_data(key, data);
} // else append kt w/o data.

std::unique_lock<pthread_mutex_t> mu(pool->mutex);
pthread_rwlock_wrlock(&pool->rwlock);
if (pool->destroyed) {
mu.unlock();
pthread_rwlock_unlock(&pool->rwlock);
delete kt;
break;
}
kt->next = (bthread::KeyTable*)pool->free_keytables;
pool->free_keytables = kt;
pthread_rwlock_unlock(&pool->rwlock);
if (data == NULL) {
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/bthread/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ inline std::ostream& operator<<(std::ostream& os, bthread_key_t key) {
#endif // __cplusplus

typedef struct {
pthread_mutex_t mutex;
pthread_rwlock_t rwlock;
void* list;
void* free_keytables;
int destroyed;
} bthread_keytable_pool_t;
Expand Down
7 changes: 2 additions & 5 deletions test/bthread_key_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,12 @@ TEST(KeyTest, set_tls_before_creating_any_bthread) {

struct PoolData {
bthread_key_t key;
PoolData* expected_data;
PoolData* data;
int seq;
int end_seq;
};

static void pool_thread_impl(PoolData* data) {
ASSERT_EQ(data->expected_data, (PoolData*)bthread_getspecific(data->key));
if (NULL == bthread_getspecific(data->key)) {
ASSERT_EQ(0, bthread_setspecific(data->key, data));
}
Expand Down Expand Up @@ -385,19 +384,17 @@ TEST(KeyTest, using_pool) {
ASSERT_EQ(0, bthread_start_urgent(&bth, &attr, pool_thread, &bth_data));
ASSERT_EQ(0, bthread_join(bth, NULL));
ASSERT_EQ(0, bth_data.seq);
ASSERT_EQ(1, bthread_keytable_pool_size(&pool));

PoolData bth2_data = { key, &bth_data, 0, 3 };
bthread_t bth2;
ASSERT_EQ(0, bthread_start_urgent(&bth2, &attr2, pool_thread, &bth2_data));
ASSERT_EQ(0, bthread_join(bth2, NULL));
ASSERT_EQ(0, bth2_data.seq);
ASSERT_EQ(1, bthread_keytable_pool_size(&pool));

ASSERT_EQ(0, bthread_keytable_pool_destroy(&pool));

EXPECT_EQ(bth_data.end_seq, bth_data.seq);
EXPECT_EQ(0, bth2_data.seq);
EXPECT_EQ(bth_data.end_seq, bth2_data.seq);

ASSERT_EQ(0, bthread_key_delete(key));
}
Expand Down
Loading