summaryrefslogtreecommitdiff
path: root/ccw/ccw.go
blob: ea207e50473bfe3976181c247e5e6c83422f8122 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)
}