1
0
mirror of https://github.com/fumiama/water.git synced 2026-06-30 00:40:26 +08:00
This commit is contained in:
fumiama
2021-12-31 17:41:48 +08:00
parent 80ce193460
commit 0c176c6cfb

View File

@@ -329,6 +329,7 @@ type wintunRWC struct {
readwait windows.Handle readwait windows.Handle
readbuf []byte readbuf []byte
rate rateJuggler rate rateJuggler
mu sync.Mutex
isclosed bool isclosed bool
} }
@@ -340,6 +341,8 @@ func (w *wintunRWC) Close() error {
func (w *wintunRWC) Write(b []byte) (int, error) { func (w *wintunRWC) Write(b []byte) (int, error) {
w.rate.update(uint64(len(b))) w.rate.update(uint64(len(b)))
w.mu.Lock()
defer w.mu.Unlock()
packet, err := w.s.AllocateSendPacket(len(b)) packet, err := w.s.AllocateSendPacket(len(b))
switch err { switch err {
case nil: case nil:
@@ -377,6 +380,8 @@ RETRY:
} }
start := nanotime() start := nanotime()
shouldSpin := atomic.LoadUint64(&w.rate.current) >= spinloopRateThreshold && uint64(start-atomic.LoadInt64(&w.rate.nextStartTime)) <= rateMeasurementGranularity*2 shouldSpin := atomic.LoadUint64(&w.rate.current) >= spinloopRateThreshold && uint64(start-atomic.LoadInt64(&w.rate.nextStartTime)) <= rateMeasurementGranularity*2
w.mu.Lock()
defer w.mu.Unlock()
for { for {
packet, err := w.s.ReceivePacket() packet, err := w.s.ReceivePacket()
switch err { switch err {