diff options
| author | chrislu <chris.lu@gmail.com> | 2022-08-26 17:04:11 -0700 |
|---|---|---|
| committer | chrislu <chris.lu@gmail.com> | 2022-08-26 17:04:11 -0700 |
| commit | 301b49b63f5261595a5117ae12aa9a9e6b3ed74b (patch) | |
| tree | 7923730760acc958f96ae941a12058b9ea1291f6 | |
| parent | 57a46f46a0465db30d6a8906c367da5071a327e7 (diff) | |
| download | seaweedfs-301b49b63f5261595a5117ae12aa9a9e6b3ed74b.tar.xz seaweedfs-301b49b63f5261595a5117ae12aa9a9e6b3ed74b.zip | |
atomic operation
| -rw-r--r-- | weed/mount/page_writer_pattern.go | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/weed/mount/page_writer_pattern.go b/weed/mount/page_writer_pattern.go index 4b4a816ce..d0c3c417c 100644 --- a/weed/mount/page_writer_pattern.go +++ b/weed/mount/page_writer_pattern.go @@ -1,5 +1,7 @@ package mount +import "sync/atomic" + type WriterPattern struct { isSequentialCounter int64 lastWriteStopOffset int64 @@ -20,18 +22,19 @@ func NewWriterPattern(chunkSize int64) *WriterPattern { } func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) { - if rp.lastWriteStopOffset == offset { - if rp.isSequentialCounter < ModeChangeLimit { - rp.isSequentialCounter++ + lastOffset := atomic.SwapInt64(&rp.lastWriteStopOffset, offset+int64(size)) + counter := atomic.LoadInt64(&rp.isSequentialCounter) + if lastOffset == offset { + if counter < ModeChangeLimit { + atomic.AddInt64(&rp.isSequentialCounter, 1) } } else { - if rp.isSequentialCounter > -ModeChangeLimit { - rp.isSequentialCounter-- + if counter > -ModeChangeLimit { + atomic.AddInt64(&rp.isSequentialCounter, -1) } } - rp.lastWriteStopOffset = offset + int64(size) } func (rp *WriterPattern) IsSequentialMode() bool { - return rp.isSequentialCounter >= 0 + return atomic.LoadInt64(&rp.isSequentialCounter) >= 0 } |
