summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-10-17 18:40:08 +0200
committerGitHub <noreply@github.com>2023-10-17 18:40:08 +0200
commit91ae05e15643b067d46a8fa9593d93c8b89bd1ab (patch)
tree8935251de6b42c3ebf70a2d7d96d946603b8e7e6
parentb82ec03ad0151462f2ff42fd247629fa9737b273 (diff)
parente046d2e730876d5f4ffa0442bd226da76ff1a7dd (diff)
Merge pull request #28984 from vespa-engine/balder/make-cached-size-atomic
Since the cached size can be updated by many threads, it must be an a…
-rw-r--r--vespalib/src/vespa/fastos/linux_file.cpp22
-rw-r--r--vespalib/src/vespa/fastos/linux_file.h8
2 files changed, 17 insertions, 13 deletions
diff --git a/vespalib/src/vespa/fastos/linux_file.cpp b/vespalib/src/vespa/fastos/linux_file.cpp
index b6094a050d9..0f32aa953a8 100644
--- a/vespalib/src/vespa/fastos/linux_file.cpp
+++ b/vespalib/src/vespa/fastos/linux_file.cpp
@@ -202,7 +202,7 @@ FastOS_Linux_File::Write2(const void *buffer, size_t length)
if (writtenNow > 0) {
written += writtenNow;
} else {
- return (written > 0) ? written : writtenNow;;
+ return (written > 0) ? written : writtenNow;
}
}
return written;
@@ -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_relaxed);
}
}
} 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_relaxed);
}
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_relaxed);
}
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..af6e6af51af 100644
--- a/vespalib/src/vespa/fastos/linux_file.h
+++ b/vespalib/src/vespa/fastos/linux_file.h
@@ -10,21 +10,23 @@
#pragma once
#include "unix_file.h"
+#include <atomic>
/**
* This is the Linux implementation of @ref FastOS_File. Most
* methods are inherited from @ref FastOS_UNIX_File.
*/
-class FastOS_Linux_File : public FastOS_UNIX_File
+class FastOS_Linux_File final : 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:
- FastOS_Linux_File (const char *filename = nullptr);
+ FastOS_Linux_File() : FastOS_Linux_File(nullptr) {}
+ explicit FastOS_Linux_File(const char *filename);
~FastOS_Linux_File () override;
bool GetDirectIORestrictions(size_t &memoryAlignment, size_t &transferGranularity, size_t &transferMaximum) override;
bool DirectIOPadding(int64_t offset, size_t length, size_t &padBefore, size_t &padAfter) override;