diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-07-07 23:34:28 +0200 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-07-07 23:34:28 +0200 |
commit | 7a5fbfc41c8c71576e4788c7ba891979c6f5f1a8 (patch) | |
tree | d72f36377a2362104deb71f342172812459b2347 /skate/xio/util.go | |
parent | 9b089b324d48e6c5d02d7f70adb585cde263f1e4 (diff) | |
parent | 9ea69942a54f1c2e13f058ba35279af3612add1b (diff) | |
download | refcat-7a5fbfc41c8c71576e4788c7ba891979c6f5f1a8.tar.gz refcat-7a5fbfc41c8c71576e4788c7ba891979c6f5f1a8.zip |
fix merge conflict
Diffstat (limited to 'skate/xio/util.go')
-rw-r--r-- | skate/xio/util.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/skate/xio/util.go b/skate/xio/util.go index 4fc8905..49f38a3 100644 --- a/skate/xio/util.go +++ b/skate/xio/util.go @@ -6,8 +6,28 @@ import ( "io" "os" "strings" + "sync" ) +// SingleWriter makes any writer thread safe. +type SingleWriter struct { + sync.Mutex + w io.Writer +} + +// NewSingleWriter returns an io.Writer that can be safely accessed by multiple +// goroutines. +func NewSingleWriter(w io.Writer) *SingleWriter { + return &SingleWriter{w: w} +} + +// Write wraps the underlying writer and gives exclusive access. +func (w *SingleWriter) Write(p []byte) (n int, err error) { + w.Lock() + defer w.Unlock() + return w.w.Write(p) +} + // OpenTwo opens two files. The caller needs to check for a single error only. func OpenTwo(f0, f1 string) (g0, g1 *os.File, err error) { if g0, err = os.Open(f0); err != nil { |