@@ -7,61 +7,26 @@ package device
77
88import (
99 "sync"
10- )
11-
12- type WaitPool struct {
13- pool sync.Pool
14- cond sync.Cond
15- lock sync.Mutex
16- count uint32 // Get calls not yet Put back
17- max uint32
18- }
1910
20- func NewWaitPool (max uint32 , new func () any ) * WaitPool {
21- p := & WaitPool {pool : sync.Pool {New : new }, max : max }
22- p .cond = sync.Cond {L : & p .lock }
23- return p
24- }
25-
26- func (p * WaitPool ) Get () any {
27- if p .max != 0 {
28- p .lock .Lock ()
29- for p .count >= p .max {
30- p .cond .Wait ()
31- }
32- p .count ++
33- p .lock .Unlock ()
34- }
35- return p .pool .Get ()
36- }
37-
38- func (p * WaitPool ) Put (x any ) {
39- p .pool .Put (x )
40- if p .max == 0 {
41- return
42- }
43- p .lock .Lock ()
44- defer p .lock .Unlock ()
45- p .count --
46- p .cond .Signal ()
47- }
11+ "github.com/tailscale/wireguard-go/waitpool"
12+ )
4813
4914func (device * Device ) PopulatePools () {
50- device .pool .inboundElementsContainer = NewWaitPool (PreallocatedBuffersPerPool , func () any {
15+ device .pool .inboundElementsContainer = waitpool . New (PreallocatedBuffersPerPool , func () any {
5116 s := make ([]* QueueInboundElement , 0 , device .BatchSize ())
5217 return & QueueInboundElementsContainer {elems : s }
5318 })
54- device .pool .outboundElementsContainer = NewWaitPool (PreallocatedBuffersPerPool , func () any {
19+ device .pool .outboundElementsContainer = waitpool . New (PreallocatedBuffersPerPool , func () any {
5520 s := make ([]* QueueOutboundElement , 0 , device .BatchSize ())
5621 return & QueueOutboundElementsContainer {elems : s }
5722 })
58- device .pool .messageBuffers = NewWaitPool (PreallocatedBuffersPerPool , func () any {
23+ device .pool .messageBuffers = waitpool . New (PreallocatedBuffersPerPool , func () any {
5924 return new ([MaxMessageSize ]byte )
6025 })
61- device .pool .inboundElements = NewWaitPool (PreallocatedBuffersPerPool , func () any {
26+ device .pool .inboundElements = waitpool . New (PreallocatedBuffersPerPool , func () any {
6227 return new (QueueInboundElement )
6328 })
64- device .pool .outboundElements = NewWaitPool (PreallocatedBuffersPerPool , func () any {
29+ device .pool .outboundElements = waitpool . New (PreallocatedBuffersPerPool , func () any {
6530 return new (QueueOutboundElement )
6631 })
6732}
0 commit comments