From 27500d529f7fb87eef8812278fd1bbca67bcba72 Mon Sep 17 00:00:00 2001 From: Ian Gudger Date: Thu, 9 Jan 2020 22:00:42 -0800 Subject: New sync package. * Rename syncutil to sync. * Add aliases to sync types. * Replace existing usage of standard library sync package. This will make it easier to swap out synchronization primitives. For example, this will allow us to use primitives from github.com/sasha-s/go-deadlock to check for lock ordering violations. Updates #1472 PiperOrigin-RevId: 289033387 --- pkg/rand/rand_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pkg/rand/rand_linux.go') diff --git a/pkg/rand/rand_linux.go b/pkg/rand/rand_linux.go index 2b92db3e6..0bdad5fad 100644 --- a/pkg/rand/rand_linux.go +++ b/pkg/rand/rand_linux.go @@ -19,9 +19,9 @@ package rand import ( "crypto/rand" "io" - "sync" "golang.org/x/sys/unix" + "gvisor.dev/gvisor/pkg/sync" ) // reader implements an io.Reader that returns pseudorandom bytes. -- cgit v1.2.3 From eddd6ce514e3bbcc08ce9d8435c7dac12715989c Mon Sep 17 00:00:00 2001 From: Bhasker Hariharan Date: Tue, 17 Mar 2020 19:09:24 -0700 Subject: Wrap rand.Reader in a bufio.Reader. rand.Read() results in a syscall to the host on every call instead we can wrap it with a bufio.Reader to buffer and reduce number of syscalls. This is especially important for TCP where every newly created endpoint reads random data to initialize the timestamp offsets for the endpoint. Updates #231 PiperOrigin-RevId: 301501607 --- pkg/rand/rand_linux.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'pkg/rand/rand_linux.go') diff --git a/pkg/rand/rand_linux.go b/pkg/rand/rand_linux.go index 0bdad5fad..1aec96e2b 100644 --- a/pkg/rand/rand_linux.go +++ b/pkg/rand/rand_linux.go @@ -45,12 +45,18 @@ func (r *reader) Read(p []byte) (int, error) { return rand.Read(p) } +// mu protects the global Reader below. +var mu sync.Mutex + // Reader is the default reader. var Reader io.Reader = &reader{} // Read reads from the default reader. func Read(b []byte) (int, error) { - return io.ReadFull(Reader, b) + mu.Lock() + n, err := io.ReadFull(Reader, b) + mu.Unlock() + return n, err } // Init can be called to make sure /dev/urandom is pre-opened on kernels that -- cgit v1.2.3 From c29d4fc59eefefa71526daf030844a4b449c91b9 Mon Sep 17 00:00:00 2001 From: Bhasker Hariharan Date: Wed, 18 Mar 2020 06:35:34 -0700 Subject: Automated rollback of changelist 301501607 PiperOrigin-RevId: 301578043 --- pkg/rand/rand_linux.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'pkg/rand/rand_linux.go') diff --git a/pkg/rand/rand_linux.go b/pkg/rand/rand_linux.go index 1aec96e2b..0bdad5fad 100644 --- a/pkg/rand/rand_linux.go +++ b/pkg/rand/rand_linux.go @@ -45,18 +45,12 @@ func (r *reader) Read(p []byte) (int, error) { return rand.Read(p) } -// mu protects the global Reader below. -var mu sync.Mutex - // Reader is the default reader. var Reader io.Reader = &reader{} // Read reads from the default reader. func Read(b []byte) (int, error) { - mu.Lock() - n, err := io.ReadFull(Reader, b) - mu.Unlock() - return n, err + return io.ReadFull(Reader, b) } // Init can be called to make sure /dev/urandom is pre-opened on kernels that -- cgit v1.2.3 From 1bf2e52bdb5f366b397cb887d4cbdb91dd5e3213 Mon Sep 17 00:00:00 2001 From: Bhasker Hariharan Date: Fri, 20 Mar 2020 17:00:55 -0700 Subject: Actually wrap rand.Reader in bufio.Reader. Updates #231 PiperOrigin-RevId: 302127697 --- pkg/rand/rand_linux.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'pkg/rand/rand_linux.go') diff --git a/pkg/rand/rand_linux.go b/pkg/rand/rand_linux.go index 0bdad5fad..fa6a21026 100644 --- a/pkg/rand/rand_linux.go +++ b/pkg/rand/rand_linux.go @@ -17,6 +17,7 @@ package rand import ( + "bufio" "crypto/rand" "io" @@ -45,8 +46,22 @@ func (r *reader) Read(p []byte) (int, error) { return rand.Read(p) } +// bufferedReader implements a threadsafe buffered io.Reader. +type bufferedReader struct { + mu sync.Mutex + r *bufio.Reader +} + +// Read implements io.Reader.Read. +func (b *bufferedReader) Read(p []byte) (int, error) { + b.mu.Lock() + n, err := b.r.Read(p) + b.mu.Unlock() + return n, err +} + // Reader is the default reader. -var Reader io.Reader = &reader{} +var Reader io.Reader = &bufferedReader{r: bufio.NewReader(&reader{})} // Read reads from the default reader. func Read(b []byte) (int, error) { -- cgit v1.2.3