Skip to content

Commit 8d57f32

Browse files
Changed atomic types to avoid alignment problems on 32bit platforms (#26)
1 parent c35e241 commit 8d57f32

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

time_wheel.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (t *timeWheel) cascade(levelIndex int, index int) {
176176
l.ReplaceInit(&tmp.Head)
177177

178178
// 每次链表的元素被移动走,都修改version
179-
atomic.AddUint64(&l.version, 1)
179+
l.version.Add(1)
180180
l.Unlock()
181181

182182
offset := unsafe.Offsetof(tmp.Head)
@@ -220,7 +220,7 @@ func (t *timeWheel) moveAndExec() {
220220
head := newTimeHead(0, 0)
221221
t1 := t.t1[index]
222222
t1.ReplaceInit(&head.Head)
223-
atomic.AddUint64(&t1.version, 1)
223+
t1.version.Add(1)
224224
t.t1[index].Unlock()
225225

226226
// 执行,链表中的定时器
@@ -230,7 +230,7 @@ func (t *timeWheel) moveAndExec() {
230230
val := (*timeNode)(pos.Entry(offset))
231231
head.Del(pos)
232232

233-
if atomic.LoadUint32(&val.stop) == haveStop {
233+
if val.stop.Load() == haveStop {
234234
return
235235
}
236236

time_wheel_node.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ type Time struct {
2727
// level 在near盘子里就是1, 在T2ToTt[0]盘子里就是2起步
2828
// index 就是各自盘子的索引值
2929
// seq 自增id
30-
version uint64
30+
version atomic.Uint64
3131
}
3232

3333
func newTimeHead(level uint64, index uint64) *Time {
3434
head := &Time{}
35-
head.version = genVersionHeight(level, index)
35+
head.version.Store(genVersionHeight(level, index))
3636
head.Init()
3737
return head
3838
}
@@ -44,23 +44,23 @@ func genVersionHeight(level uint64, index uint64) uint64 {
4444
func (t *Time) lockPushBack(node *timeNode, level uint64, index uint64) {
4545
t.Lock()
4646
defer t.Unlock()
47-
if atomic.LoadUint32(&node.stop) == haveStop {
47+
if node.stop.Load() == haveStop {
4848
return
4949
}
5050

5151
t.AddTail(&node.Head)
5252
atomic.StorePointer(&node.list, unsafe.Pointer(t))
5353
//更新节点的version信息
54-
atomic.StoreUint64(&node.version, atomic.LoadUint64(&t.version))
54+
node.version.Store(t.version.Load())
5555
}
5656

5757
type timeNode struct {
5858
expire uint64
5959
userExpire time.Duration
6060
callback func()
61-
stop uint32
61+
stop atomic.Uint32
6262
list unsafe.Pointer //存放表头信息
63-
version uint64 //保存节点版本信息
63+
version atomic.Uint64 //保存节点版本信息
6464
isSchedule bool
6565
root *timeWheel
6666
list.Head
@@ -78,15 +78,15 @@ type timeNode struct {
7878
// 2和3.2状态会是没有锁保护下的操作,会有数据竞争
7979
func (t *timeNode) Stop() bool {
8080

81-
atomic.StoreUint32(&t.stop, haveStop)
81+
t.stop.Store(haveStop)
8282

8383
// 使用版本号算法让timeNode知道自己是否被移动了
8484
// timeNode的version和表头的version一样表示没有被移动可以直接删除
8585
// 如果不一样,可能在第2或者3.2状态,使用惰性删除
8686
cpyList := (*Time)(atomic.LoadPointer(&t.list))
8787
cpyList.Lock()
8888
defer cpyList.Unlock()
89-
if atomic.LoadUint64(&t.version) != atomic.LoadUint64(&cpyList.version) {
89+
if t.version.Load() != cpyList.version.Load() {
9090
return false
9191
}
9292

0 commit comments

Comments
 (0)