Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yitter
雪花ID全家桶(SnowFlake IdGenerator)
提交
1f13dca4
雪
雪花ID全家桶(SnowFlake IdGenerator)
项目概览
yitter
/
雪花ID全家桶(SnowFlake IdGenerator)
11 个月 前同步成功
通知
201
Star
3
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
雪
雪花ID全家桶(SnowFlake IdGenerator)
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1f13dca4
编写于
6月 15, 2022
作者:
微
微希夷
提交者:
yitter
6月 15, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
!18 !optimize cpu used
* !optimize 时间追平漂移量时,损耗一点时间(微妙级别)降低CPU占用
上级
5e243405
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
32 addition
and
20 deletion
+32
-20
PHP/src/snowflake/snowflake.c
PHP/src/snowflake/snowflake.c
+32
-20
未找到文件。
PHP/src/snowflake/snowflake.c
浏览文件 @
1f13dca4
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
#else
#else
#include <unistd.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/time.h>
#include <time.h>
#endif
#endif
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
...
@@ -11,19 +12,19 @@
...
@@ -11,19 +12,19 @@
#include "spinlock.h"
#include "spinlock.h"
// static void EndOverCostAction(uint64_t useTimeTick, snowflake *flake);
// static void EndOverCostAction(uint64_t useTimeTick, snowflake *flake);
static
inline
uint64_t
NextOverCostId
(
snowflake
*
flake
);
static
inline
uint64_t
NextOverCostId
(
snowflake
*
flake
);
static
inline
uint64_t
NextNormalId
(
snowflake
*
flake
);
static
inline
uint64_t
NextNormalId
(
snowflake
*
flake
);
static
inline
uint64_t
GetCurrentTimeTick
(
snowflake
*
flake
);
static
inline
uint64_t
GetCurrentTimeTick
(
snowflake
*
flake
);
static
inline
uint64_t
GetNextTimeTick
(
snowflake
*
flake
);
static
inline
uint64_t
GetNextTimeTick
(
snowflake
*
flake
);
static
inline
uint64_t
CalcId
(
snowflake
*
flake
);
static
inline
uint64_t
CalcId
(
snowflake
*
flake
);
static
inline
uint64_t
CalcTurnBackId
(
snowflake
*
flake
);
static
inline
uint64_t
CalcTurnBackId
(
snowflake
*
flake
);
static
inline
uint64_t
GetSysCurrentTime
();
static
inline
uint64_t
GetSysCurrentTime
();
int
ncpu
;
int
ncpu
;
uint16_t
spin
=
2048
;
uint16_t
spin
=
2048
;
uint32_t
pid
=
0
;
uint32_t
pid
=
0
;
void
Config
(
snowflake
*
flake
)
void
Config
(
snowflake
*
flake
)
{
{
if
(
pid
==
0
)
if
(
pid
==
0
)
{
{
...
@@ -132,7 +133,7 @@ void Config(snowflake* flake)
...
@@ -132,7 +133,7 @@ void Config(snowflake* flake)
// }
// }
// }
// }
static
inline
uint64_t
NextOverCostId
(
snowflake
*
flake
)
static
inline
uint64_t
NextOverCostId
(
snowflake
*
flake
)
{
{
uint64_t
currentTimeTick
=
GetCurrentTimeTick
(
flake
);
uint64_t
currentTimeTick
=
GetCurrentTimeTick
(
flake
);
if
(
currentTimeTick
>
flake
->
_LastTimeTick
)
if
(
currentTimeTick
>
flake
->
_LastTimeTick
)
...
@@ -169,7 +170,7 @@ static inline uint64_t NextOverCostId(snowflake* flake)
...
@@ -169,7 +170,7 @@ static inline uint64_t NextOverCostId(snowflake* flake)
return
CalcId
(
flake
);
return
CalcId
(
flake
);
}
}
static
inline
uint64_t
NextNormalId
(
snowflake
*
flake
)
static
inline
uint64_t
NextNormalId
(
snowflake
*
flake
)
{
{
uint64_t
currentTimeTick
=
GetCurrentTimeTick
(
flake
);
uint64_t
currentTimeTick
=
GetCurrentTimeTick
(
flake
);
if
(
currentTimeTick
<
flake
->
_LastTimeTick
)
if
(
currentTimeTick
<
flake
->
_LastTimeTick
)
...
@@ -221,39 +222,50 @@ static inline uint64_t GetSysCurrentTime()
...
@@ -221,39 +222,50 @@ static inline uint64_t GetSysCurrentTime()
gettimeofday
(
&
t
,
NULL
);
gettimeofday
(
&
t
,
NULL
);
return
(
uint64_t
)(
t
.
tv_sec
*
1000
+
t
.
tv_usec
/
1000
);
return
(
uint64_t
)(
t
.
tv_sec
*
1000
+
t
.
tv_usec
/
1000
);
#endif
#endif
}
}
static
inline
uint64_t
GetCurrentTimeTick
(
snowflake
*
flake
)
static
inline
uint64_t
GetCurrentTimeTick
(
snowflake
*
flake
)
{
{
return
GetSysCurrentTime
()
-
flake
->
BaseTime
;
return
GetSysCurrentTime
()
-
flake
->
BaseTime
;
}
}
static
inline
uint64_t
GetNextTimeTick
(
snowflake
*
flake
)
static
inline
uint64_t
GetNextTimeTick
(
snowflake
*
flake
)
{
{
uint64_t
tempTimeTicker
=
GetCurrentTimeTick
(
flake
);
uint64_t
tempTimeTicker
=
GetCurrentTimeTick
(
flake
);
while
(
tempTimeTicker
<=
flake
->
_LastTimeTick
)
struct
timespec
delay
;
delay
.
tv_sec
=
0
;
delay
.
tv_nsec
=
500000
;
while
(
1
)
{
{
tempTimeTicker
=
GetCurrentTimeTick
(
flake
);
tempTimeTicker
=
GetCurrentTimeTick
(
flake
);
if
(
tempTimeTicker
>
flake
->
_LastTimeTick
)
{
break
;
}
#ifdef WIN32
SwitchToThread
();
#else
nanosleep
(
&
delay
,
NULL
);
#endif
}
}
return
tempTimeTicker
;
return
tempTimeTicker
;
}
}
static
inline
uint64_t
CalcId
(
snowflake
*
flake
)
static
inline
uint64_t
CalcId
(
snowflake
*
flake
)
{
{
uint64_t
result
=
(
flake
->
_LastTimeTick
<<
flake
->
_TimestampShift
)
+
(
flake
->
WorkerId
<<
flake
->
SeqBitLength
)
+
(
flake
->
_CurrentSeqNumber
);
uint64_t
result
=
(
flake
->
_LastTimeTick
<<
flake
->
_TimestampShift
)
+
(
flake
->
WorkerId
<<
flake
->
SeqBitLength
)
+
(
flake
->
_CurrentSeqNumber
);
flake
->
_CurrentSeqNumber
++
;
flake
->
_CurrentSeqNumber
++
;
return
result
;
return
result
;
}
}
static
inline
uint64_t
CalcTurnBackId
(
snowflake
*
flake
)
static
inline
uint64_t
CalcTurnBackId
(
snowflake
*
flake
)
{
{
uint64_t
result
=
(
flake
->
_LastTimeTick
<<
flake
->
_TimestampShift
)
+
(
flake
->
WorkerId
<<
flake
->
SeqBitLength
)
+
(
flake
->
_TurnBackTimeTick
);
uint64_t
result
=
(
flake
->
_LastTimeTick
<<
flake
->
_TimestampShift
)
+
(
flake
->
WorkerId
<<
flake
->
SeqBitLength
)
+
(
flake
->
_TurnBackTimeTick
);
flake
->
_TurnBackTimeTick
--
;
flake
->
_TurnBackTimeTick
--
;
return
result
;
return
result
;
}
}
static
inline
uint64_t
NextSonwId
(
snowflake
*
flake
)
static
inline
uint64_t
NextSonwId
(
snowflake
*
flake
)
{
{
uint64_t
currentTimeTick
=
GetCurrentTimeTick
(
flake
);
uint64_t
currentTimeTick
=
GetCurrentTimeTick
(
flake
);
if
(
flake
->
_LastTimeTick
==
currentTimeTick
)
if
(
flake
->
_LastTimeTick
==
currentTimeTick
)
...
@@ -273,12 +285,12 @@ static inline uint64_t NextSonwId(snowflake* flake)
...
@@ -273,12 +285,12 @@ static inline uint64_t NextSonwId(snowflake* flake)
return
(
uint64_t
)((
currentTimeTick
<<
flake
->
_TimestampShift
)
|
(
flake
->
WorkerId
<<
flake
->
SeqBitLength
)
|
flake
->
_CurrentSeqNumber
);
return
(
uint64_t
)((
currentTimeTick
<<
flake
->
_TimestampShift
)
|
(
flake
->
WorkerId
<<
flake
->
SeqBitLength
)
|
flake
->
_CurrentSeqNumber
);
}
}
static
inline
uint64_t
GetId
(
snowflake
*
flake
)
static
inline
uint64_t
GetId
(
snowflake
*
flake
)
{
{
return
flake
->
Method
==
1
?
(
flake
->
_IsOverCost
!=
0
?
NextOverCostId
(
flake
)
:
NextNormalId
(
flake
))
:
NextSonwId
(
flake
);
return
flake
->
Method
==
1
?
(
flake
->
_IsOverCost
!=
0
?
NextOverCostId
(
flake
)
:
NextNormalId
(
flake
))
:
NextSonwId
(
flake
);
}
}
uint64_t
NextId
(
snowflake
*
flake
)
uint64_t
NextId
(
snowflake
*
flake
)
{
{
spin_lock
(
&
flake
->
_Lock
,
pid
);
spin_lock
(
&
flake
->
_Lock
,
pid
);
uint64_t
id
=
GetId
(
flake
);
uint64_t
id
=
GetId
(
flake
);
...
@@ -286,9 +298,9 @@ uint64_t NextId(snowflake* flake)
...
@@ -286,9 +298,9 @@ uint64_t NextId(snowflake* flake)
return
id
;
return
id
;
}
}
uint64_t
*
NextNumId
(
snowflake
*
flake
,
uint32_t
num
)
uint64_t
*
NextNumId
(
snowflake
*
flake
,
uint32_t
num
)
{
{
uint64_t
*
arr
=
(
uint64_t
*
)
malloc
(
sizeof
(
uint64_t
)
*
num
);
uint64_t
*
arr
=
(
uint64_t
*
)
malloc
(
sizeof
(
uint64_t
)
*
num
);
spin_lock
(
&
flake
->
_Lock
,
pid
);
spin_lock
(
&
flake
->
_Lock
,
pid
);
uint32_t
i
;
uint32_t
i
;
for
(
i
=
0
;
i
<
num
;
i
++
)
for
(
i
=
0
;
i
<
num
;
i
++
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录