import (
"errors"
"io"
- "log"
"os"
"sync"
)
type Reader struct {
t *Torrent
responsive bool
- opMu sync.Mutex
+ // Ensure operations that change the position are exclusive, like Read()
+ // and Seek().
+ opMu sync.Mutex
+ // Required when modifying pos and readahead, or reading them without
+ // opMu.
mu sync.Mutex
pos int64
readahead int64
}
b = b[n1:]
n += n1
+ r.mu.Lock()
r.pos += int64(n1)
+ r.mu.Unlock()
}
if r.pos >= r.t.torrent.length {
err = io.EOF
}
func (r *Reader) Seek(off int64, whence int) (ret int64, err error) {
+ r.opMu.Lock()
+ defer r.opMu.Unlock()
+
r.mu.Lock()
switch whence {
case os.SEEK_SET:
}
ret = r.pos
r.mu.Unlock()
+
r.posChanged()
return
}