diff --git a/job.go b/job.go index 17b922e..8f8331f 100644 --- a/job.go +++ b/job.go @@ -126,9 +126,30 @@ type writeJob struct { ch chan struct{} } +func newWriteJob(job *baseJob, src *mmap.ReaderAt, needWait bool) *writeJob { + j := &writeJob{ + baseJob: job, + src: src, + } + if needWait { + j.ch = make(chan struct{}) + } + return j +} + func (wj *writeJob) done() { wj.src.Close() - close(wj.ch) + + if wj.ch != nil { + close(wj.ch) + } +} + +func (wj *writeJob) wait() { + if wj.ch == nil { + return + } + <-wj.ch } type Job struct { diff --git a/opt.go b/opt.go index e58e4cc..f6aaeb9 100644 --- a/opt.go +++ b/opt.go @@ -91,6 +91,10 @@ func (o *option) check() error { o.fromDevice.check() o.toDevice.check() + if o.fromDevice.linear || o.toDevice.linear { + o.fromDevice.threads = 1 + o.toDevice.threads = 1 + } if o.logger == nil { o.logger = logrus.StandardLogger() } diff --git a/prepare.go b/prepare.go index b4f891d..ff2aee7 100644 --- a/prepare.go +++ b/prepare.go @@ -45,9 +45,9 @@ func (c *Copyer) prepare(ctx context.Context, indexed <-chan *baseJob) <-chan *w return } - wj := &writeJob{baseJob: job, src: file, ch: make(chan struct{})} + wj := newWriteJob(job, file, c.fromDevice.linear) ch <- wj - <-wj.ch + wj.wait() } } })