From 0c176c6cfb9cfa0c5ec7683363f4a59c1c957b39 Mon Sep 17 00:00:00 2001 From: fumiama Date: Fri, 31 Dec 2021 17:41:48 +0800 Subject: [PATCH] add lock --- syscalls_windows.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/syscalls_windows.go b/syscalls_windows.go index 38d4bcd..b9e1648 100644 --- a/syscalls_windows.go +++ b/syscalls_windows.go @@ -329,6 +329,7 @@ type wintunRWC struct { readwait windows.Handle readbuf []byte rate rateJuggler + mu sync.Mutex isclosed bool } @@ -340,6 +341,8 @@ func (w *wintunRWC) Close() error { func (w *wintunRWC) Write(b []byte) (int, error) { w.rate.update(uint64(len(b))) + w.mu.Lock() + defer w.mu.Unlock() packet, err := w.s.AllocateSendPacket(len(b)) switch err { case nil: @@ -377,6 +380,8 @@ RETRY: } start := nanotime() shouldSpin := atomic.LoadUint64(&w.rate.current) >= spinloopRateThreshold && uint64(start-atomic.LoadInt64(&w.rate.nextStartTime)) <= rateMeasurementGranularity*2 + w.mu.Lock() + defer w.mu.Unlock() for { packet, err := w.s.ReceivePacket() switch err {