Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
47ae0423
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
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看板
提交
47ae0423
编写于
9月 05, 2019
作者:
S
slguan
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of
https://github.com/taosdata/TDengine
上级
dbe69d68
eb5ce731
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
59 addition
and
19 deletion
+59
-19
src/util/src/ttimer.c
src/util/src/ttimer.c
+59
-19
未找到文件。
src/util/src/ttimer.c
浏览文件 @
47ae0423
...
@@ -78,8 +78,13 @@ typedef struct {
...
@@ -78,8 +78,13 @@ typedef struct {
int
count
;
int
count
;
}
tmr_list_t
;
}
tmr_list_t
;
#define TMR_CTRL_STAGE_UNUSED 0
#define TMR_CTRL_STAGE_IN_USE 1
#define TMR_CTRL_STAGE_CLEANNING_UP 2
typedef
struct
_tmr_ctrl_t
{
typedef
struct
_tmr_ctrl_t
{
void
*
signature
;
int
stage
;
/* life cycle stage of this tmr ctrl */
pthread_mutex_t
mutex
;
/* mutex to protect critical resource */
pthread_mutex_t
mutex
;
/* mutex to protect critical resource */
int
resolution
;
/* resolution in mseconds */
int
resolution
;
/* resolution in mseconds */
int
numOfPeriods
;
/* total number of periods */
int
numOfPeriods
;
/* total number of periods */
...
@@ -110,15 +115,37 @@ void *taosTimerLoopFunc(int signo) {
...
@@ -110,15 +115,37 @@ void *taosTimerLoopFunc(int signo) {
for
(
int
i
=
1
;
i
<
maxNumOfTmrCtrl
;
++
i
)
{
for
(
int
i
=
1
;
i
<
maxNumOfTmrCtrl
;
++
i
)
{
pCtrl
=
tmrCtrl
+
i
;
pCtrl
=
tmrCtrl
+
i
;
if
(
pCtrl
->
signature
)
{
// save 'stage' to a local variable so that all later code can
count
++
;
// use the same 'stage'. acquire semantic is required to ensure
// 'stage' is load before other 'pCtrl' fields.
int
stage
=
__atomic_load_n
(
&
pCtrl
->
stage
,
__ATOMIC_ACQUIRE
);
if
(
stage
==
TMR_CTRL_STAGE_IN_USE
)
{
pCtrl
->
ticks
++
;
pCtrl
->
ticks
++
;
if
(
pCtrl
->
ticks
>=
pCtrl
->
maxTicks
)
{
if
(
pCtrl
->
ticks
>=
pCtrl
->
maxTicks
)
{
taosTmrProcessList
(
pCtrl
);
taosTmrProcessList
(
pCtrl
);
pCtrl
->
ticks
=
0
;
pCtrl
->
ticks
=
0
;
}
}
if
(
count
>=
numOfTmrCtrl
)
break
;
}
else
if
(
stage
==
TMR_CTRL_STAGE_CLEANNING_UP
)
{
__atomic_store_n
(
&
pCtrl
->
stage
,
TMR_CTRL_STAGE_UNUSED
,
__ATOMIC_RELEASE
);
pthread_mutex_destroy
(
&
pCtrl
->
mutex
);
tfree
(
pCtrl
->
tmrList
);
tmrMemPoolCleanUp
(
pCtrl
->
poolHandle
);
// decrease 'numOfTmrCtrl', need to be atomic but relaxed semantic is fine
// because we don't need (unable to guarantee either) an accurate counter.
int
num
=
__atomic_add_fetch
(
&
numOfTmrCtrl
,
-
1
,
__ATOMIC_RELAXED
);
tmrTrace
(
"%s is cleaned up, numOfTmrCtrls:%d"
,
pCtrl
->
label
,
num
);
// return 'id' to the poool and then this timer controller can be reused,
// this must be the last step.
taosFreeId
(
tmrIdPool
,
pCtrl
->
tmrCtrlId
);
}
else
{
continue
;
}
}
if
(
++
count
>=
__atomic_load_n
(
&
numOfTmrCtrl
,
__ATOMIC_RELAXED
))
break
;
}
}
return
NULL
;
return
NULL
;
...
@@ -206,9 +233,6 @@ void *taosTmrInit(int maxNumOfTmrs, int resolution, int longest, char *label) {
...
@@ -206,9 +233,6 @@ void *taosTmrInit(int maxNumOfTmrs, int resolution, int longest, char *label) {
}
}
pCtrl
=
tmrCtrl
+
tmrCtrlId
;
pCtrl
=
tmrCtrl
+
tmrCtrlId
;
tfree
(
pCtrl
->
tmrList
);
tmrMemPoolCleanUp
(
pCtrl
->
poolHandle
);
memset
(
pCtrl
,
0
,
sizeof
(
tmr_ctrl_t
));
memset
(
pCtrl
,
0
,
sizeof
(
tmr_ctrl_t
));
pCtrl
->
tmrCtrlId
=
tmrCtrlId
;
pCtrl
->
tmrCtrlId
=
tmrCtrlId
;
...
@@ -217,18 +241,25 @@ void *taosTmrInit(int maxNumOfTmrs, int resolution, int longest, char *label) {
...
@@ -217,18 +241,25 @@ void *taosTmrInit(int maxNumOfTmrs, int resolution, int longest, char *label) {
if
((
pCtrl
->
poolHandle
=
tmrMemPoolInit
(
maxNumOfTmrs
+
10
,
sizeof
(
tmr_obj_t
)))
==
NULL
)
{
if
((
pCtrl
->
poolHandle
=
tmrMemPoolInit
(
maxNumOfTmrs
+
10
,
sizeof
(
tmr_obj_t
)))
==
NULL
)
{
tmrError
(
"%s failed to allocate mem pool"
,
label
);
tmrError
(
"%s failed to allocate mem pool"
,
label
);
t
mrMemPoolCleanUp
(
pCtrl
->
poolHandle
);
t
aosFreeId
(
tmrIdPool
,
tmrCtrlId
);
return
NULL
;
return
NULL
;
}
}
if
(
resolution
<
MSECONDS_PER_TICK
)
resolution
=
MSECONDS_PER_TICK
;
if
(
resolution
<
MSECONDS_PER_TICK
)
resolution
=
MSECONDS_PER_TICK
;
pCtrl
->
resolution
=
resolution
;
pCtrl
->
resolution
=
resolution
;
pCtrl
->
maxTicks
=
resolution
/
MSECONDS_PER_TICK
;
pCtrl
->
maxTicks
=
resolution
/
MSECONDS_PER_TICK
;
pCtrl
->
ticks
=
rand
()
/
pCtrl
->
maxTicks
;
pCtrl
->
ticks
=
rand
()
%
pCtrl
->
maxTicks
;
pCtrl
->
numOfPeriods
=
longest
/
resolution
;
pCtrl
->
numOfPeriods
=
longest
/
resolution
;
if
(
pCtrl
->
numOfPeriods
<
10
)
pCtrl
->
numOfPeriods
=
10
;
if
(
pCtrl
->
numOfPeriods
<
10
)
pCtrl
->
numOfPeriods
=
10
;
pCtrl
->
tmrList
=
(
tmr_list_t
*
)
malloc
(
sizeof
(
tmr_list_t
)
*
pCtrl
->
numOfPeriods
);
pCtrl
->
tmrList
=
(
tmr_list_t
*
)
malloc
(
sizeof
(
tmr_list_t
)
*
pCtrl
->
numOfPeriods
);
if
(
pCtrl
->
tmrList
==
NULL
)
{
tmrError
(
"%s failed to allocate tmrList"
,
label
);
tmrMemPoolCleanUp
(
pCtrl
->
poolHandle
);
taosFreeId
(
tmrIdPool
,
tmrCtrlId
);
return
NULL
;
}
for
(
int
i
=
0
;
i
<
pCtrl
->
numOfPeriods
;
i
++
)
{
for
(
int
i
=
0
;
i
<
pCtrl
->
numOfPeriods
;
i
++
)
{
pCtrl
->
tmrList
[
i
].
head
=
NULL
;
pCtrl
->
tmrList
[
i
].
head
=
NULL
;
pCtrl
->
tmrList
[
i
].
count
=
0
;
pCtrl
->
tmrList
[
i
].
count
=
0
;
...
@@ -236,12 +267,20 @@ void *taosTmrInit(int maxNumOfTmrs, int resolution, int longest, char *label) {
...
@@ -236,12 +267,20 @@ void *taosTmrInit(int maxNumOfTmrs, int resolution, int longest, char *label) {
if
(
pthread_mutex_init
(
&
pCtrl
->
mutex
,
NULL
)
<
0
)
{
if
(
pthread_mutex_init
(
&
pCtrl
->
mutex
,
NULL
)
<
0
)
{
tmrError
(
"%s failed to create the mutex, reason:%s"
,
label
,
strerror
(
errno
));
tmrError
(
"%s failed to create the mutex, reason:%s"
,
label
,
strerror
(
errno
));
taosTmrCleanUp
(
pCtrl
);
free
(
pCtrl
->
tmrList
);
tmrMemPoolCleanUp
(
pCtrl
->
poolHandle
);
taosFreeId
(
tmrIdPool
,
tmrCtrlId
);
return
NULL
;
return
NULL
;
}
}
pCtrl
->
signature
=
pCtrl
;
// set 'stage' to 'in use' to mark the completion of initialization,
numOfTmrCtrl
++
;
// release semantic is required to ensure all operations prior this
// are visible to other threads first.
__atomic_store_n
(
&
pCtrl
->
stage
,
TMR_CTRL_STAGE_IN_USE
,
__ATOMIC_RELEASE
);
// increase 'numOfTmrCtrl', need to be atomic but relaxed semantic is fine
__atomic_add_fetch
(
&
numOfTmrCtrl
,
1
,
__ATOMIC_RELAXED
);
tmrTrace
(
"%s timer ctrl is initialized, index:%d"
,
label
,
tmrCtrlId
);
tmrTrace
(
"%s timer ctrl is initialized, index:%d"
,
label
,
tmrCtrlId
);
return
pCtrl
;
return
pCtrl
;
}
}
...
@@ -293,12 +332,14 @@ void taosTmrProcessList(tmr_ctrl_t *pCtrl) {
...
@@ -293,12 +332,14 @@ void taosTmrProcessList(tmr_ctrl_t *pCtrl) {
void
taosTmrCleanUp
(
void
*
handle
)
{
void
taosTmrCleanUp
(
void
*
handle
)
{
tmr_ctrl_t
*
pCtrl
=
(
tmr_ctrl_t
*
)
handle
;
tmr_ctrl_t
*
pCtrl
=
(
tmr_ctrl_t
*
)
handle
;
if
(
pCtrl
==
NULL
||
pCtrl
->
signature
!=
pCtrl
)
return
;
if
(
pCtrl
==
NULL
)
return
;
pCtrl
->
signature
=
NULL
;
// set 'stage' to 'cleanning up' if it is 'in use' atomically,
taosFreeId
(
tmrIdPool
,
pCtrl
->
tmrCtrlId
);
// actual cleanning up will be done in 'taosTimerLoopFunc'.
numOfTmrCtrl
--
;
int
oldStage
=
TMR_CTRL_STAGE_IN_USE
;
tmrTrace
(
"%s is cleaned up, numOfTmrs:%d"
,
pCtrl
->
label
,
numOfTmrCtrl
);
__atomic_compare_exchange_n
(
&
pCtrl
->
stage
,
&
oldStage
,
TMR_CTRL_STAGE_CLEANNING_UP
,
false
,
__ATOMIC_ACQ_REL
,
__ATOMIC_ACQUIRE
);
}
}
tmr_h
taosTmrStart
(
void
(
*
fp
)(
void
*
,
void
*
),
int
mseconds
,
void
*
param1
,
void
*
handle
)
{
tmr_h
taosTmrStart
(
void
(
*
fp
)(
void
*
,
void
*
),
int
mseconds
,
void
*
param1
,
void
*
handle
)
{
...
@@ -538,7 +579,6 @@ mpool_h tmrMemPoolInit(int numOfBlock, int blockSize) {
...
@@ -538,7 +579,6 @@ mpool_h tmrMemPoolInit(int numOfBlock, int blockSize) {
pool_p
->
blockSize
=
blockSize
;
pool_p
->
blockSize
=
blockSize
;
pool_p
->
numOfBlock
=
numOfBlock
;
pool_p
->
numOfBlock
=
numOfBlock
;
pool_p
->
pool
=
(
char
*
)
malloc
(
blockSize
*
numOfBlock
);
pool_p
->
pool
=
(
char
*
)
malloc
(
blockSize
*
numOfBlock
);
memset
(
pool_p
->
pool
,
0
,
blockSize
*
numOfBlock
);
pool_p
->
freeList
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
numOfBlock
);
pool_p
->
freeList
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
numOfBlock
);
if
(
pool_p
->
pool
==
NULL
||
pool_p
->
freeList
==
NULL
)
{
if
(
pool_p
->
pool
==
NULL
||
pool_p
->
freeList
==
NULL
)
{
...
@@ -596,6 +636,6 @@ void tmrMemPoolCleanUp(mpool_h handle) {
...
@@ -596,6 +636,6 @@ void tmrMemPoolCleanUp(mpool_h handle) {
if
(
pool_p
->
pool
)
free
(
pool_p
->
pool
);
if
(
pool_p
->
pool
)
free
(
pool_p
->
pool
);
if
(
pool_p
->
freeList
)
free
(
pool_p
->
freeList
);
if
(
pool_p
->
freeList
)
free
(
pool_p
->
freeList
);
memset
(
&
pool_p
,
0
,
sizeof
(
pool_p
));
memset
(
pool_p
,
0
,
sizeof
(
*
pool_p
));
free
(
pool_p
);
free
(
pool_p
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录