11package timer
22
33import (
4- "container/list"
54 "context"
65 "fmt"
76 "sync/atomic"
87 "time"
8+ "unsafe"
9+
10+ "github.com/antlabs/stl/list"
911)
1012
1113const (
@@ -57,12 +59,13 @@ func newTimeWheel() *timeWheel {
5759func (t * timeWheel ) init () {
5860
5961 for i := 0 ; i < nearSize ; i ++ {
60- t .t1 [i ] = & Time {List : list .New ()}
62+ t .t1 [i ] = newTimeHead ()
63+
6164 }
6265
6366 for i := 0 ; i < 4 ; i ++ {
6467 for j := 0 ; j < levelSize ; j ++ {
65- t.t2Tot5 [i ][j ] = & Time { List : list . New ()}
68+ t.t2Tot5 [i ][j ] = newTimeHead ()
6669 }
6770 }
6871
@@ -161,27 +164,21 @@ func (t *timeWheel) Stop() {
161164// 移动链表
162165func (t * timeWheel ) cascade (levelIndex int , index int ) {
163166
164- tmp := list . New ()
167+ tmp := newTimeHead ()
165168
166169 l := t.t2Tot5 [levelIndex ][index ]
167170 l .Lock ()
168171
169- for e := l .Front (); e != nil ; {
170- next := e .Next ()
171- l .Remove (e )
172-
173- tmp .PushBack (e .Value )
174-
175- e = next
176- }
177-
178- t.t2Tot5 [levelIndex ][index ].Init ()
172+ l .ReplaceInit (& tmp .Head )
179173
180174 l .Unlock ()
181175
182- for e := tmp .Front (); e != nil ; e = e .Next () {
183- t .add (e .Value .(* timeNode ), atomic .LoadUint64 (& t .jiffies ))
184- }
176+ offset := unsafe .Offsetof (tmp .Head )
177+ tmp .ForEachSafe (func (pos * list.Head ) {
178+ node := (* timeNode )(pos .Entry (offset ))
179+ t .add (node , atomic .LoadUint64 (& t .jiffies ))
180+ })
181+
185182}
186183
187184func (t * timeWheel ) moveTot1 (head * Time , index uint64 ) {
@@ -190,15 +187,7 @@ func (t *timeWheel) moveTot1(head *Time, index uint64) {
190187 t1 .Lock ()
191188 defer t1 .Unlock ()
192189
193- for e := t1 .Front (); e != nil ; {
194- next := e .Next ()
195-
196- t1 .Remove (e )
197- head .PushBack (e .Value )
198- e = next
199- }
200-
201- t .t1 [index ].List .Init ()
190+ t1 .ReplaceInit (& head .Head )
202191}
203192
204193// moveAndExec函数功能
@@ -229,31 +218,31 @@ func (t *timeWheel) moveAndExec() {
229218 atomic .AddUint64 (& t .jiffies , 1 )
230219
231220 t .t1 [index ].Lock ()
232- if t .t1 [index ].List . Len () == 0 {
221+ if t .t1 [index ].Len () == 0 {
233222 t .t1 [index ].Unlock ()
234223 return
235224 }
225+
236226 t .t1 [index ].Unlock ()
237227
238- head := Time { List : list . New ()}
239- t .moveTot1 (& head , index )
228+ head := newTimeHead ()
229+ t .moveTot1 (head , index )
240230 // 执行
241231
242- for e := head .Front (); e != nil ; {
243-
244- val := e .Value .(* timeNode )
245- next := e .Next ()
246- head .Remove (e )
247- e = next
232+ offset := unsafe .Offsetof (head .Head )
248233
234+ head .ForEachSafe (func (pos * list.Head ) {
235+ val := (* timeNode )(pos .Entry (offset ))
236+ head .Del (pos )
249237 go val .callback ()
250238
251239 if val .isSchedule {
252240 jiffies := t .jiffies
253241 val .expire = uint64 (t .getExpire (val .userExpire , jiffies ))
254242 t .add (val , jiffies )
255243 }
256- }
244+ })
245+
257246}
258247
259248func (t * timeWheel ) run () {
0 commit comments