diff options
Diffstat (limited to 'src/go/pkg/safewriter/writer.go')
-rw-r--r-- | src/go/pkg/safewriter/writer.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/go/pkg/safewriter/writer.go b/src/go/pkg/safewriter/writer.go new file mode 100644 index 000000000..533c1055d --- /dev/null +++ b/src/go/pkg/safewriter/writer.go @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package safewriter + +import ( + "io" + "os" + "sync" +) + +var Stdout = New(os.Stdout) + +func New(w io.Writer) io.Writer { + return &writer{ + mx: &sync.Mutex{}, + w: w, + } +} + +type writer struct { + mx *sync.Mutex + w io.Writer +} + +func (w *writer) Write(p []byte) (n int, err error) { + w.mx.Lock() + n, err = w.w.Write(p) + w.mx.Unlock() + return n, err +} |