diff options
Diffstat (limited to 'pkg/rand')
-rw-r--r-- | pkg/rand/BUILD | 8 | ||||
-rw-r--r-- | pkg/rand/rand_linux.go | 19 |
2 files changed, 22 insertions, 5 deletions
diff --git a/pkg/rand/BUILD b/pkg/rand/BUILD index f4f2001f3..80b8ceb02 100644 --- a/pkg/rand/BUILD +++ b/pkg/rand/BUILD @@ -1,4 +1,4 @@ -load("//tools/go_stateify:defs.bzl", "go_library") +load("//tools:defs.bzl", "go_library") package(licenses = ["notice"]) @@ -8,7 +8,9 @@ go_library( "rand.go", "rand_linux.go", ], - importpath = "gvisor.dev/gvisor/pkg/rand", visibility = ["//:sandbox"], - deps = ["@org_golang_x_sys//unix:go_default_library"], + deps = [ + "//pkg/sync", + "@org_golang_x_sys//unix:go_default_library", + ], ) diff --git a/pkg/rand/rand_linux.go b/pkg/rand/rand_linux.go index 2b92db3e6..fa6a21026 100644 --- a/pkg/rand/rand_linux.go +++ b/pkg/rand/rand_linux.go @@ -17,11 +17,12 @@ package rand import ( + "bufio" "crypto/rand" "io" - "sync" "golang.org/x/sys/unix" + "gvisor.dev/gvisor/pkg/sync" ) // reader implements an io.Reader that returns pseudorandom bytes. @@ -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) { |