Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
adea18c0
T
TDengine
项目概览
慢慢CG
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
adea18c0
编写于
10月 29, 2020
作者:
陶建辉(Jeff)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add API taosIterateRef
上级
c90ec0d7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
115 addition
and
10 deletion
+115
-10
src/util/inc/tref.h
src/util/inc/tref.h
+29
-3
src/util/src/tref.c
src/util/src/tref.c
+69
-6
src/util/tests/trefTest.c
src/util/tests/trefTest.c
+17
-1
未找到文件。
src/util/inc/tref.h
浏览文件 @
adea18c0
...
...
@@ -21,15 +21,41 @@
extern
"C"
{
#endif
int
taosOpenRef
(
int
max
,
void
(
*
fp
)(
void
*
));
// return refId which will be used by other APIs
// open an instance, return refId which will be used by other APIs
int
taosOpenRef
(
int
max
,
void
(
*
fp
)(
void
*
));
// close the Ref instance
void
taosCloseRef
(
int
refId
);
int
taosListRef
();
// return the number of references in system
// add ref, p is the pointer to resource or pointer ID
int
taosAddRef
(
int
refId
,
void
*
p
);
#define taosRemoveRef taosReleaseRef
// acquire ref, p is the pointer to resource or pointer ID
int
taosAcquireRef
(
int
refId
,
void
*
p
);
// release ref, p is the pointer to resource or pinter ID
void
taosReleaseRef
(
int
refId
,
void
*
p
);
#define taosRemoveRef taosReleaseRef
// return the first if p is null, otherwise return the next after p
void
*
taosIterateRef
(
int
refId
,
void
*
p
);
// return the number of references in system
int
taosListRef
();
/* sample code to iterate the refs
void demoIterateRefs(int refId) {
void *p = taosGetRefNext(refId, NULL);
while (p) {
// process P
p = taosGetRefNext(refId, p);
}
}
*/
#ifdef __cplusplus
}
...
...
src/util/src/tref.c
浏览文件 @
adea18c0
...
...
@@ -143,8 +143,6 @@ int taosAddRef(int refId, void *p)
return
TSDB_CODE_REF_INVALID_ID
;
}
uTrace
(
"refId:%d p:%p try to add"
,
refId
,
p
);
pSet
=
tsRefSetList
+
refId
;
taosIncRefCount
(
pSet
);
if
(
pSet
->
state
!=
TSDB_REF_STATE_ACTIVE
)
{
...
...
@@ -203,8 +201,6 @@ int taosAcquireRef(int refId, void *p)
return
TSDB_CODE_REF_INVALID_ID
;
}
uTrace
(
"refId:%d p:%p try to acquire"
,
refId
,
p
);
pSet
=
tsRefSetList
+
refId
;
taosIncRefCount
(
pSet
);
if
(
pSet
->
state
!=
TSDB_REF_STATE_ACTIVE
)
{
...
...
@@ -254,8 +250,6 @@ void taosReleaseRef(int refId, void *p)
return
;
}
uTrace
(
"refId:%d p:%p try to release"
,
refId
,
p
);
pSet
=
tsRefSetList
+
refId
;
if
(
pSet
->
state
==
TSDB_REF_STATE_EMPTY
)
{
uTrace
(
"refId:%d p:%p failed to release, cleaned"
,
refId
,
p
);
...
...
@@ -305,6 +299,75 @@ void taosReleaseRef(int refId, void *p)
if
(
released
)
taosDecRefCount
(
pSet
);
}
// if p is NULL, return the first p in hash list, otherwise, return the next after p
void
*
taosIterateRef
(
int
refId
,
void
*
p
)
{
SRefNode
*
pNode
=
NULL
;
SRefSet
*
pSet
;
if
(
refId
<
0
||
refId
>=
TSDB_REF_OBJECTS
)
{
uTrace
(
"refId:%d p:%p failed to iterate, refId not valid"
,
refId
,
p
);
return
NULL
;
}
pSet
=
tsRefSetList
+
refId
;
taosIncRefCount
(
pSet
);
if
(
pSet
->
state
!=
TSDB_REF_STATE_ACTIVE
)
{
uTrace
(
"refId:%d p:%p failed to iterate, not active"
,
refId
,
p
);
taosDecRefCount
(
pSet
);
return
NULL
;
}
int
hash
=
0
;
if
(
p
)
{
hash
=
taosHashRef
(
pSet
,
p
);
taosLockList
(
pSet
->
lockedBy
+
hash
);
pNode
=
pSet
->
nodeList
[
hash
];
while
(
pNode
)
{
if
(
pNode
->
p
==
p
)
break
;
pNode
=
pNode
->
next
;
}
if
(
pNode
==
NULL
)
{
uError
(
"refId:%d p:%p not there, quit"
,
refId
,
p
);
taosUnlockList
(
pSet
->
lockedBy
+
hash
);
return
NULL
;
}
// p is there
pNode
=
pNode
->
next
;
if
(
pNode
==
NULL
)
{
taosUnlockList
(
pSet
->
lockedBy
+
hash
);
hash
++
;
}
}
if
(
pNode
==
NULL
)
{
for
(;
hash
<
pSet
->
max
;
++
hash
)
{
taosLockList
(
pSet
->
lockedBy
+
hash
);
pNode
=
pSet
->
nodeList
[
hash
];
if
(
pNode
)
break
;
taosUnlockList
(
pSet
->
lockedBy
+
hash
);
}
}
void
*
newP
=
NULL
;
if
(
pNode
)
{
pNode
->
count
++
;
// acquire it
newP
=
pNode
->
p
;
taosUnlockList
(
pSet
->
lockedBy
+
hash
);
uTrace
(
"refId:%d p:%p is returned"
,
refId
,
p
);
}
else
{
uTrace
(
"refId:%d p:%p the list is over"
,
refId
,
p
);
}
if
(
p
)
taosReleaseRef
(
refId
,
p
);
// release the current one
taosDecRefCount
(
pSet
);
return
newP
;
}
int
taosListRef
()
{
SRefSet
*
pSet
;
SRefNode
*
pNode
;
...
...
src/util/tests/trefTest.c
浏览文件 @
adea18c0
...
...
@@ -17,6 +17,19 @@ typedef struct {
void
**
p
;
}
SRefSpace
;
void
iterateRefs
(
int
refId
)
{
int
count
=
0
;
void
*
p
=
taosIterateRef
(
refId
,
NULL
);
while
(
p
)
{
// process P
count
++
;
p
=
taosIterateRef
(
refId
,
p
);
}
printf
(
" %d "
,
count
);
}
void
*
takeRefActions
(
void
*
param
)
{
SRefSpace
*
pSpace
=
(
SRefSpace
*
)
param
;
int
code
,
id
;
...
...
@@ -44,6 +57,9 @@ void *takeRefActions(void *param) {
usleep
(
id
%
5
+
1
);
taosReleaseRef
(
pSpace
->
refId
,
pSpace
->
p
[
id
]);
}
id
=
random
()
%
pSpace
->
refNum
;
iterateRefs
(
id
);
}
for
(
int
i
=
0
;
i
<
pSpace
->
refNum
;
++
i
)
{
...
...
@@ -63,7 +79,7 @@ void *openRefSpace(void *param) {
SRefSpace
*
pSpace
=
(
SRefSpace
*
)
param
;
printf
(
"c"
);
pSpace
->
refId
=
taosOpenRef
(
1000
0
,
myfree
);
pSpace
->
refId
=
taosOpenRef
(
5
0
,
myfree
);
if
(
pSpace
->
refId
<
0
)
{
printf
(
"failed to open ref, reson:%s
\n
"
,
tstrerror
(
pSpace
->
refId
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录