Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
c24f7ea7
T
TDengine
项目概览
taosdata
/
TDengine
大约 1 年 前同步成功
通知
1185
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看板
提交
c24f7ea7
编写于
4月 27, 2020
作者:
weixin_48148422
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update defer and rename it to cleanup
上级
4826a185
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
159 addition
and
65 deletion
+159
-65
src/util/inc/exception.h
src/util/inc/exception.h
+65
-51
src/util/src/exception.c
src/util/src/exception.c
+94
-14
未找到文件。
src/util/inc/exception.h
浏览文件 @
c24f7ea7
...
...
@@ -25,73 +25,87 @@ extern "C" {
#endif
/*
* exception handling
* cleanup actions
*/
typedef
struct
SCleanupAction
{
bool
failOnly
;
uint8_t
wrapper
;
uint16_t
reserved
;
void
*
func
;
union
{
void
*
Ptr
;
bool
Bool
;
char
Char
;
int8_t
Int8
;
uint8_t
Uint8
;
int16_t
Int16
;
uint16_t
Uint16
;
int
Int
;
unsigned
int
Uint
;
int32_t
Int32
;
uint32_t
Uint32
;
int64_t
Int64
;
uint64_t
Uint64
;
float
Float
;
double
Double
;
}
arg1
,
arg2
;
}
SCleanupAction
;
void
cleanupPush_void_ptr_ptr
(
bool
failOnly
,
void
*
func
,
void
*
arg1
,
void
*
arg2
);
void
cleanupPush_void_ptr_bool
(
bool
failOnly
,
void
*
func
,
void
*
arg1
,
bool
arg2
);
void
cleanupPush_void_ptr
(
bool
failOnly
,
void
*
func
,
void
*
arg
);
void
cleanupPush_int_int
(
bool
failOnly
,
void
*
func
,
int
arg
);
void
cleanupPush_void
(
bool
failOnly
,
void
*
func
);
int32_t
cleanupGetActionCount
();
void
cleanupExecute
(
bool
failed
,
int32_t
toIndex
);
#define CLEANUP_PUSH_VOID_PTR_PTR( failOnly, func, arg1, arg2 ) cleanupPush_void_ptr_ptr( (failOnly), (void*)(func), (void*)(arg1), (void*)(arg2) )
#define CLEANUP_PUSH_VOID_PTR_BOOL( failOnly, func, arg1, arg2 ) cleanupPush_void_ptr_bool( (failOnly), (void*)(func), (void*)(arg1), (bool)(arg2) )
#define CLEANUP_PUSH_VOID_PTR( failOnly, func, arg ) cleanupPush_void_ptr( (failOnly), (void*)(func), (void*)(arg) )
#define CLEANUP_PUSH_INT_INT( failOnly, func, arg ) cleanupPush_void_ptr( (failOnly), (void*)(func), (int)(arg) )
#define CLEANUP_PUSH_VOID( failOnly, func ) cleanupPush_void( (failOnly), (void*)(func) )
#define CLEANUP_PUSH_FREE( failOnly, arg ) cleanupPush_void_ptr( (failOnly), free, (void*)(arg) )
#define CLEANUP_PUSH_CLOSE( failOnly, arg ) cleanupPush_int_int( (failOnly), close, (int)(arg) )
#define CLEANUP_CREATE_ANCHOR() int32_t cleanupAnchor = cleanupGetActionCount()
#define CLEANUP_EXECUTE( failed ) cleanupExecute( cleanupAnchor, (failed) )
/*
* exception hander registration
*/
typedef
struct
SExceptionNode
{
struct
SExceptionNode
*
prev
;
jmp_buf
jb
;
int
code
;
int32_t
code
;
int32_t
maxCleanupAction
;
int32_t
numCleanupAction
;
SCleanupAction
*
cleanupActions
;
}
SExceptionNode
;
void
exceptionPushNode
(
SExceptionNode
*
node
);
int
exceptionPopNode
();
int
32_t
exceptionPopNode
();
void
exceptionThrow
(
int
code
);
#define THROW( x ) exceptionThrow( (x) )
#define CAUGHT_EXCEPTION() (caught_exception == 1)
#define TRY do { \
SExceptionNode expNode = { 0 }; \
exceptionPushNode( &expNode ); \
int caught_exception = setjmp(expNode.jb); \
if( caught_exception == 0 )
#define TRY(maxCleanupActions) do { \
SExceptionNode exceptionNode = { 0 }; \
SDeferedOperation cleanupActions[maxCleanupActions > 0 ? maxCleanupActions : 1]; \
exceptionNode.maxCleanupAction = maxCleanupActions > 0 ? maxDefered : 1; \
exceptionNode.cleanupActions = cleanupActions; \
int32_t cleanupAnchor = 0; \
exceptionPushNode( &exceptionNode ); \
int caughtException = setjmp( exceptionNode.jb ); \
if( caughtException == 0 )
#define CATCH( code ) int code = exceptionPopNode(); \
if( caught
_
exception == 1 )
if( caught
E
exception == 1 )
#define FINALLY( code ) int code = exceptionPopNode();
#define END_TRY } while( 0 );
/*
* defered operations
*/
typedef
struct
SDeferedOperation
{
void
(
*
wrapper
)(
struct
SDeferedOperation
*
dp
);
void
*
func
;
void
*
arg
;
}
SDeferedOperation
;
void
deferExecute
(
SDeferedOperation
*
operations
,
unsigned
int
numOfOperations
);
void
deferWrapper_void_void
(
SDeferedOperation
*
dp
);
void
deferWrapper_void_ptr
(
SDeferedOperation
*
dp
);
void
deferWrapper_int_int
(
SDeferedOperation
*
dp
);
#define DEFER_INIT( MaxOperations ) unsigned int maxDeferedOperations = MaxOperations, numOfDeferedOperations = 0; \
SDeferedOperation deferedOperations[MaxOperations]
#define DEFER_PUSH( wrapperFunc, deferedFunc, argument ) do { \
assert( numOfDeferedOperations < maxDeferedOperations ); \
SDeferedOperation* dp = deferedOperations + numOfDeferedOperations++; \
dp->wrapper = wrapperFunc; \
dp->func = (void*)deferedFunc; \
dp->arg = (void*)argument; \
} while( 0 )
#define DEFER_POP() do { --numOfDeferedOperations; } while( 0 )
#define DEFER_EXECUTE() do{ \
deferExecute( deferedOperations, numOfDeferedOperations ); \
numOfDeferedOperations = 0; \
} while( 0 )
#define DEFER_PUSH_VOID_PTR( func, arg ) DEFER_PUSH( deferWrapper_void_ptr, func, arg )
#define DEFER_PUSH_INT_INT( func, arg ) DEFER_PUSH( deferWrapper_int_int, func, arg )
#define DEFER_PUSH_VOID_VOID( func ) DEFER_PUSH( deferWrapper_void_void, func, 0 )
#define DEFER_PUSH_FREE( arg ) DEFER_PUSH( deferWrapper_void_ptr, free, arg )
#define DEFER_PUSH_CLOSE( arg ) DEFER_PUSH( deferWrapper_int_int, close, arg )
#define THROW( x ) exceptionThrow( (x) )
#define CAUGHT_EXCEPTION() ((bool)(caughtEexception == 1))
#ifdef __cplusplus
}
...
...
src/util/src/exception.c
浏览文件 @
c24f7ea7
...
...
@@ -8,7 +8,7 @@ void exceptionPushNode( SExceptionNode* node ) {
expList
=
node
;
}
int
exceptionPopNode
()
{
int
32_t
exceptionPopNode
()
{
SExceptionNode
*
node
=
expList
;
expList
=
node
->
prev
;
return
node
->
code
;
...
...
@@ -19,25 +19,105 @@ void exceptionThrow( int code ) {
longjmp
(
expList
->
jb
,
1
);
}
void
deferWrapper_void_ptr
(
SDeferedOperation
*
dp
)
{
void
(
*
func
)(
void
*
)
=
dp
->
func
;
func
(
dp
->
arg
);
static
void
cleanupWrapper_void_ptr_ptr
(
SCleanupAction
*
ca
)
{
void
(
*
func
)(
void
*
,
void
*
)
=
ac
->
func
;
func
(
ca
->
arg1
.
Ptr
,
ca
->
arg2
.
Ptr
);
}
static
void
cleanupWrapper_void_ptr_bool
(
SCleanupAction
*
ca
)
{
void
(
*
func
)(
void
*
,
bool
)
=
ca
->
func
;
func
(
ca
->
arg1
.
Ptr
,
ca
->
arg2
.
Bool
);
}
void
deferWrapper_int_int
(
SDeferedOperation
*
dp
)
{
int
(
*
func
)(
int
)
=
dp
->
func
;
func
(
(
int
)(
intptr_t
)(
dp
->
arg
)
);
static
void
cleanupWrapper_void_ptr
(
SCleanupAction
*
ca
)
{
void
(
*
func
)(
void
*
)
=
ca
->
func
;
func
(
ca
->
arg1
.
Ptr
);
}
void
deferWrapper_void_void
(
SDeferedOperation
*
dp
)
{
void
(
*
func
)()
=
dp
->
func
;
static
void
cleanupWrapper_int_int
(
SCleanupAction
*
ca
)
{
int
(
*
func
)(
int
)
=
ca
->
func
;
func
(
(
int
)(
intptr_t
)(
ca
->
arg1
.
Int
)
);
}
static
void
cleanupWrapper_void_void
(
SCleanupAction
*
ca
)
{
void
(
*
func
)()
=
ca
->
func
;
func
();
}
void
deferExecute
(
SDeferedOperation
*
operations
,
unsigned
int
numOfOperations
)
{
while
(
numOfOperations
>
0
)
{
--
numOfOperations
;
SDeferedOperation
*
dp
=
operations
+
numOfOperations
;
dp
->
wrapper
(
dp
);
static
void
(
*
wrappers
)(
SCleanupAction
*
)[]
=
{
cleanupWrapper_void_ptr_ptr
,
cleanupWrapper_void_ptr_bool
,
cleanupWrapper_void_ptr
,
cleanupWrapper_int_int
,
cleanupWrapper_void_void
,
};
void
cleanupPush_void_ptr_ptr
(
bool
failOnly
,
void
*
func
,
void
*
arg1
,
void
*
arg2
)
{
assert
(
expList
->
numCleanupAction
<
expList
->
maxCleanupAction
);
SCleanupAction
*
ac
=
expList
->
cleanupActions
+
expList
->
numCleanupAction
++
;
ac
->
wrapper
=
0
;
ac
->
failOnly
=
failOnly
;
ac
->
func
=
func
;
ac
->
arg1
.
Ptr
=
arg1
;
ac
->
arg2
.
Ptr
=
arg2
;
}
void
cleanupPush_void_ptr_bool
(
bool
failOnly
,
void
*
func
,
void
*
arg1
,
bool
arg2
)
{
assert
(
expList
->
numCleanupAction
<
expList
->
maxCleanupAction
);
SCleanupAction
*
ac
=
expList
->
cleanupActions
+
expList
->
numCleanupAction
++
;
ac
->
wrapper
=
1
;
ac
->
failOnly
=
failOnly
;
ac
->
func
=
func
;
ac
->
arg1
.
Ptr
=
arg1
;
ac
->
arg2
.
Bool
=
arg2
;
}
void
cleanupPush_void_ptr
(
bool
failOnly
,
void
*
func
,
void
*
arg
)
{
assert
(
expList
->
numCleanupAction
<
expList
->
maxCleanupAction
);
SCleanupAction
*
ac
=
expList
->
cleanupActions
+
expList
->
numCleanupAction
++
;
ac
->
wrapper
=
2
;
ac
->
failOnly
=
failOnly
;
ac
->
func
=
func
;
ac
->
arg1
.
Ptr
=
arg1
;
}
void
cleanupPush_int_int
(
bool
failOnly
,
void
*
func
,
int
arg
)
{
assert
(
expList
->
numCleanupAction
<
expList
->
maxCleanupAction
);
SCleanupAction
*
ac
=
expList
->
cleanupActions
+
expList
->
numCleanupAction
++
;
ac
->
wrapper
=
3
;
ac
->
failOnly
=
failOnly
;
ac
->
func
=
func
;
ac
->
arg1
.
Int
=
arg
;
}
void
cleanupPush_void
(
bool
failOnly
,
void
*
func
)
{
assert
(
expList
->
numCleanupAction
<
expList
->
maxCleanupAction
);
SCleanupAction
*
ac
=
expList
->
cleanupActions
+
expList
->
numCleanupAction
++
;
ac
->
wrapper
=
4
;
ac
->
failOnly
=
failOnly
;
ac
->
func
=
func
;
}
int32
cleanupGetActionCount
()
{
return
expList
->
numCleanupAction
;
}
void
cleanupExecute
(
int32_t
anchor
,
bool
failed
)
{
while
(
expList
->
numCleanupAction
>
anchor
)
{
--
expList
->
numCleanupAction
;
SCleanupAction
*
ac
=
expList
->
cleanupActions
+
expList
->
numCleanupAction
;
if
(
failed
||
!
(
ac
->
failOnly
)
)
ac
->
wrapper
(
ac
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录