Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
051d6284
T
TDengine
项目概览
taosdata
/
TDengine
大约 1 年 前同步成功
通知
1184
Star
22015
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
051d6284
编写于
4月 11, 2020
作者:
S
slguan
提交者:
GitHub
4月 11, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1586 from taosdata/enhance/ihash
Enhance/ihash
上级
8e67dae1
1a5b1c56
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
53 addition
and
29 deletion
+53
-29
src/util/src/ihash.c
src/util/src/ihash.c
+53
-29
未找到文件。
src/util/src/ihash.c
浏览文件 @
051d6284
...
...
@@ -16,7 +16,7 @@
#include "os.h"
typedef
struct
_str_node_t
{
uint64_t
key
;
uint64_t
key
;
struct
_str_node_t
*
prev
;
struct
_str_node_t
*
next
;
char
data
[];
...
...
@@ -24,10 +24,10 @@ typedef struct _str_node_t {
typedef
struct
{
IHashNode
**
hashList
;
int64_t
*
lockedBy
;
int32_t
maxSessions
;
int32_t
dataSize
;
int32_t
(
*
hashFp
)(
void
*
,
uint64_t
key
);
pthread_mutex_t
mutex
;
}
IHashObj
;
int32_t
taosHashInt
(
void
*
handle
,
uint64_t
key
)
{
...
...
@@ -36,6 +36,9 @@ int32_t taosHashInt(void *handle, uint64_t key) {
return
hash
;
}
static
void
taosLockIntHash
(
IHashObj
*
pObj
,
int
hash
);
static
void
taosUnlockIntHash
(
IHashObj
*
pObj
,
int
hash
);
char
*
taosAddIntHash
(
void
*
handle
,
uint64_t
key
,
char
*
pData
)
{
int32_t
hash
;
IHashNode
*
pNode
;
...
...
@@ -50,7 +53,7 @@ char *taosAddIntHash(void *handle, uint64_t key, char *pData) {
if
(
pNode
==
NULL
)
return
NULL
;
pthread_mutex_lock
(
&
pObj
->
mutex
);
taosLockIntHash
(
pObj
,
hash
);
pNode
->
key
=
key
;
if
(
pData
!=
NULL
)
{
...
...
@@ -62,7 +65,7 @@ char *taosAddIntHash(void *handle, uint64_t key, char *pData) {
if
(
pObj
->
hashList
[
hash
]
!=
0
)
(
pObj
->
hashList
[
hash
])
->
prev
=
pNode
;
pObj
->
hashList
[
hash
]
=
pNode
;
pthread_mutex_unlock
(
&
pObj
->
mutex
);
taosUnlockIntHash
(
pObj
,
hash
);
return
(
char
*
)
pNode
->
data
;
}
...
...
@@ -77,7 +80,7 @@ void taosDeleteIntHash(void *handle, uint64_t key) {
hash
=
(
*
(
pObj
->
hashFp
))(
pObj
,
key
);
pthread_mutex_lock
(
&
pObj
->
mutex
);
taosLockIntHash
(
pObj
,
hash
);
pNode
=
pObj
->
hashList
[
hash
];
while
(
pNode
)
{
...
...
@@ -100,7 +103,7 @@ void taosDeleteIntHash(void *handle, uint64_t key) {
free
(
pNode
);
}
pthread_mutex_unlock
(
&
pObj
->
mutex
);
taosUnlockIntHash
(
pObj
,
hash
);
}
char
*
taosGetIntHashData
(
void
*
handle
,
uint64_t
key
)
{
...
...
@@ -113,7 +116,7 @@ char *taosGetIntHashData(void *handle, uint64_t key) {
hash
=
(
*
pObj
->
hashFp
)(
pObj
,
key
);
pthread_mutex_lock
(
&
pObj
->
mutex
);
taosLockIntHash
(
pObj
,
hash
);
pNode
=
pObj
->
hashList
[
hash
];
...
...
@@ -125,7 +128,7 @@ char *taosGetIntHashData(void *handle, uint64_t key) {
pNode
=
pNode
->
next
;
}
pthread_mutex_unlock
(
&
pObj
->
mutex
);
taosUnlockIntHash
(
pObj
,
hash
);
if
(
pNode
)
return
pNode
->
data
;
...
...
@@ -152,7 +155,12 @@ void *taosInitIntHash(int32_t maxSessions, int32_t dataSize, int32_t (*fp)(void
}
memset
(
pObj
->
hashList
,
0
,
sizeof
(
IHashNode
*
)
*
(
size_t
)
maxSessions
);
pthread_mutex_init
(
&
pObj
->
mutex
,
NULL
);
pObj
->
lockedBy
=
(
int64_t
*
)
calloc
(
sizeof
(
int64_t
),
maxSessions
);
if
(
pObj
->
lockedBy
==
NULL
)
{
free
(
pObj
);
free
(
pObj
->
hashList
);
pObj
=
NULL
;
}
return
pObj
;
}
...
...
@@ -164,26 +172,25 @@ void taosCleanUpIntHash(void *handle) {
pObj
=
(
IHashObj
*
)
handle
;
if
(
pObj
==
NULL
||
pObj
->
maxSessions
<=
0
)
return
;
pthread_mutex_lock
(
&
pObj
->
mutex
);
if
(
pObj
->
hashList
)
{
for
(
int32_t
i
=
0
;
i
<
pObj
->
maxSessions
;
++
i
)
{
taosLockIntHash
(
pObj
,
i
);
pNode
=
pObj
->
hashList
[
i
];
while
(
pNode
)
{
pNext
=
pNode
->
next
;
free
(
pNode
);
pNode
=
pNext
;
}
taosUnlockIntHash
(
pObj
,
i
);
}
free
(
pObj
->
hashList
);
}
pthread_mutex_unlock
(
&
pObj
->
mutex
);
pthread_mutex_destroy
(
&
pObj
->
mutex
);
memset
(
pObj
,
0
,
sizeof
(
IHashObj
));
free
(
pObj
->
lockedBy
);
free
(
pObj
);
}
...
...
@@ -194,10 +201,10 @@ void taosCleanUpIntHashWithFp(void *handle, void (*fp)(char *)) {
pObj
=
(
IHashObj
*
)
handle
;
if
(
pObj
==
NULL
||
pObj
->
maxSessions
<=
0
)
return
;
pthread_mutex_lock
(
&
pObj
->
mutex
);
if
(
pObj
->
hashList
)
{
for
(
int
i
=
0
;
i
<
pObj
->
maxSessions
;
++
i
)
{
taosLockIntHash
(
pObj
,
i
);
pNode
=
pObj
->
hashList
[
i
];
while
(
pNode
)
{
pNext
=
pNode
->
next
;
...
...
@@ -205,15 +212,13 @@ void taosCleanUpIntHashWithFp(void *handle, void (*fp)(char *)) {
free
(
pNode
);
pNode
=
pNext
;
}
taosUnlockIntHash
(
pObj
,
i
);
}
free
(
pObj
->
hashList
);
}
pthread_mutex_unlock
(
&
pObj
->
mutex
);
pthread_mutex_destroy
(
&
pObj
->
mutex
);
memset
(
pObj
,
0
,
sizeof
(
IHashObj
));
free
(
pObj
);
}
...
...
@@ -225,20 +230,20 @@ void taosVisitIntHashWithFp(void *handle, int (*fp)(char *, void *), void *param
pObj
=
(
IHashObj
*
)
handle
;
if
(
pObj
==
NULL
||
pObj
->
maxSessions
<=
0
)
return
;
pthread_mutex_lock
(
&
pObj
->
mutex
);
if
(
pObj
->
hashList
)
{
for
(
int
i
=
0
;
i
<
pObj
->
maxSessions
;
++
i
)
{
taosLockIntHash
(
pObj
,
i
);
pNode
=
pObj
->
hashList
[
i
];
while
(
pNode
)
{
pNext
=
pNode
->
next
;
(
*
fp
)(
pNode
->
data
,
param
);
pNode
=
pNext
;
}
taosUnlockIntHash
(
pObj
,
i
);
}
}
pthread_mutex_unlock
(
&
pObj
->
mutex
);
}
int32_t
taosGetIntHashSize
(
void
*
handle
)
{
...
...
@@ -249,19 +254,38 @@ int32_t taosGetIntHashSize(void *handle) {
pObj
=
(
IHashObj
*
)
handle
;
if
(
pObj
==
NULL
||
pObj
->
maxSessions
<=
0
)
return
0
;
pthread_mutex_lock
(
&
pObj
->
mutex
);
if
(
pObj
->
hashList
)
{
for
(
int
i
=
0
;
i
<
pObj
->
maxSessions
;
++
i
)
{
taosLockIntHash
(
pObj
,
i
);
pNode
=
pObj
->
hashList
[
i
];
while
(
pNode
)
{
pNext
=
pNode
->
next
;
num
++
;
pNode
=
pNext
;
}
taosUnlockIntHash
(
pObj
,
i
);
}
}
pthread_mutex_unlock
(
&
pObj
->
mutex
);
return
num
;
}
\ No newline at end of file
}
static
void
taosLockIntHash
(
IHashObj
*
pObj
,
int
hash
)
{
int64_t
tid
=
taosGetPthreadId
();
int
i
=
0
;
while
(
atomic_val_compare_exchange_64
(
&
(
pObj
->
lockedBy
[
hash
]),
0
,
tid
)
!=
0
)
{
if
(
++
i
%
1000
==
0
)
{
sched_yield
();
}
}
}
static
void
taosUnlockIntHash
(
IHashObj
*
pObj
,
int
hash
)
{
int64_t
tid
=
taosGetPthreadId
();
if
(
atomic_val_compare_exchange_64
(
&
(
pObj
->
lockedBy
[
hash
]),
tid
,
0
)
!=
tid
)
{
assert
(
false
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录