diff options
| author | Will Hawkins <[email protected]> | 2022-03-15 21:06:52 -0400 |
|---|---|---|
| committer | Will Hawkins <[email protected]> | 2022-03-15 21:06:52 -0400 |
| commit | 9e9e8b992963aa949340fafc4b2fc6835857a1da (patch) | |
| tree | 9575685345344f9b99651d02b2eb2d8471cad3bd /ccw/ccw.go | |
| parent | 75ccf8f64787831a14d0211a605636c34deef489 (diff) | |
Add a helper for doing concurrent writes to files.
Diffstat (limited to 'ccw/ccw.go')
| -rw-r--r-- | ccw/ccw.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/ccw/ccw.go b/ccw/ccw.go new file mode 100644 index 0000000..ea207e5 --- /dev/null +++ b/ccw/ccw.go @@ -0,0 +1,24 @@ +package ccw + +import ( + "os" + "sync" +) + +type ConcurrentWriter struct { + lock sync.Mutex + file *os.File +} + +func NewConcurrentFileWriter(file *os.File) *ConcurrentWriter { + return &ConcurrentWriter{sync.Mutex{}, file} +} + +func (ccw *ConcurrentWriter) Write(p []byte) (n int, err error) { + ccw.lock.Lock() + defer func() { + ccw.file.Sync() + ccw.lock.Unlock() + }() + return ccw.file.Write(p) +} |
