summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ccw/ccw.go24
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)
+}