diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-10-17 14:11:00 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-10-17 14:11:00 +0000 |
commit | 602844c8e92a1e7839942dbec4c03f81e1ca0ed2 (patch) | |
tree | b0bea2eb7156427c3566b159ea836cbb32007df1 /vespalib | |
parent | f13e5af5e1ff7d19b86afae909a21df1f00e3e50 (diff) |
Since the cached size can be updated by many threads, it must be an atomic since there can be many readers.
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/fastos/linux_file.cpp | 20 | ||||
-rw-r--r-- | vespalib/src/vespa/fastos/linux_file.h | 3 |
2 files changed, 13 insertions, 10 deletions
diff --git a/vespalib/src/vespa/fastos/linux_file.cpp b/vespalib/src/vespa/fastos/linux_file.cpp index b6094a050d9..4348f8ea7e9 100644 --- a/vespalib/src/vespa/fastos/linux_file.cpp +++ b/vespalib/src/vespa/fastos/linux_file.cpp @@ -239,8 +239,8 @@ FastOS_Linux_File::internalWrite2(const void *buffer, size_t length) } if (writeRes > 0) { _filePointer += writeRes; - if (_filePointer > _cachedSize) { - _cachedSize = _filePointer; + if (_filePointer > _cachedSize.load(std::memory_order_relaxed)) { + _cachedSize.store(_filePointer, std::memory_order_release); } } } else { @@ -277,7 +277,7 @@ FastOS_Linux_File::SetSize(int64_t newSize) bool rc = FastOS_UNIX_File::SetSize(newSize); if (rc) { - _cachedSize = newSize; + _cachedSize.store(newSize, std::memory_order_release); } return rc; } @@ -334,19 +334,21 @@ FastOS_Linux_File::DirectIOPadding (int64_t offset, size_t length, size_t &padBe if (padAfter == _directIOFileAlign) { padAfter = 0; } - if (int64_t(offset+length+padAfter) > _cachedSize) { + int64_t fileSize = _cachedSize.load(std::memory_order_relaxed); + if (int64_t(offset+length+padAfter) > fileSize) { // _cachedSize is not really trustworthy, so if we suspect it is not correct, we correct it. // The main reason is that it will not reflect the file being extended by another filedescriptor. - _cachedSize = getSize(); + fileSize = getSize(); + _cachedSize.store(fileSize, std::memory_order_release); } if ((padAfter != 0) && - (static_cast<int64_t>(offset + length + padAfter) > _cachedSize) && - (static_cast<int64_t>(offset + length) <= _cachedSize)) + (static_cast<int64_t>(offset + length + padAfter) > fileSize) && + (static_cast<int64_t>(offset + length) <= fileSize)) { - padAfter = _cachedSize - (offset + length); + padAfter = fileSize - (offset + length); } - if (static_cast<uint64_t>(offset + length + padAfter) <= static_cast<uint64_t>(_cachedSize)) { + if (static_cast<uint64_t>(offset + length + padAfter) <= static_cast<uint64_t>(fileSize)) { return true; } } diff --git a/vespalib/src/vespa/fastos/linux_file.h b/vespalib/src/vespa/fastos/linux_file.h index 1295ce38316..6338c4e7757 100644 --- a/vespalib/src/vespa/fastos/linux_file.h +++ b/vespalib/src/vespa/fastos/linux_file.h @@ -10,6 +10,7 @@ #pragma once #include "unix_file.h" +#include <atomic> /** * This is the Linux implementation of @ref FastOS_File. Most @@ -20,7 +21,7 @@ class FastOS_Linux_File : public FastOS_UNIX_File public: using FastOS_UNIX_File::ReadBuf; protected: - int64_t _cachedSize; + std::atomic<int64_t> _cachedSize; int64_t _filePointer; // Only maintained/used in directio mode public: |