|
24 | 24 | #include "share_buffer.h" |
25 | 25 | #include <atomic> |
26 | 26 | #include <chrono> |
| 27 | +#include <filesystem> |
27 | 28 | #include <thread> |
28 | 29 | #include <unistd.h> |
29 | 30 | #include "file/file.h" |
@@ -97,21 +98,36 @@ struct ShareBufferHeader { |
97 | 98 | ShareBlockHeader headers[0]; |
98 | 99 | }; |
99 | 100 |
|
100 | | -inline std::string GenShareBufferName(const size_t blockSize, const size_t blockNumber, |
101 | | - const bool ioDirect, const size_t nSharer) |
| 101 | +const inline std::string& ShmPrefix() noexcept |
102 | 102 | { |
103 | | - return fmt::format("uc.buf-{}-{}-{}-{:04x}", blockSize, blockNumber, ioDirect, nSharer); |
| 103 | + static std::string prefix{"uc_shm_pcstore_"}; |
| 104 | + return prefix; |
| 105 | +} |
| 106 | +void CleanUpShmFileExceptMe(const std::string& me) |
| 107 | +{ |
| 108 | + namespace fs = std::filesystem; |
| 109 | + std::string_view prefix = ShmPrefix(); |
| 110 | + fs::path shmDir = "/dev/shm"; |
| 111 | + if (!fs::exists(shmDir)) { return; } |
| 112 | + for (const auto& entry : fs::directory_iterator(shmDir)) { |
| 113 | + const auto& name = entry.path().filename().string(); |
| 114 | + if (entry.is_regular_file() && (name.compare(0, prefix.length(), prefix) == 0) && |
| 115 | + name != me) { |
| 116 | + fs::remove(entry.path()); |
| 117 | + } |
| 118 | + } |
104 | 119 | } |
105 | 120 |
|
106 | 121 | Status ShareBuffer::Setup(const size_t blockSize, const size_t blockNumber, const bool ioDirect, |
107 | | - const size_t nSharer) |
| 122 | + const size_t nSharer, const std::string& uniqueId) |
108 | 123 | { |
109 | 124 | this->blockSize_ = blockSize; |
110 | 125 | this->blockNumber_ = blockNumber; |
111 | 126 | this->ioDirect_ = ioDirect; |
112 | 127 | this->nSharer_ = nSharer; |
113 | 128 | this->addr_ = nullptr; |
114 | | - this->shmName_ = GenShareBufferName(blockSize, blockNumber, ioDirect, nSharer); |
| 129 | + this->shmName_ = ShmPrefix() + uniqueId; |
| 130 | + CleanUpShmFileExceptMe(this->shmName_); |
115 | 131 | auto file = File::Make(this->shmName_); |
116 | 132 | if (!file) { return Status::OutOfMemory(); } |
117 | 133 | auto flags = IFile::OpenFlag::CREATE | IFile::OpenFlag::EXCL | IFile::OpenFlag::READ_WRITE; |
@@ -305,4 +321,4 @@ uintptr_t ShareBuffer::Reader::GetData() |
305 | 321 | return (uintptr_t)header->Data(); |
306 | 322 | } |
307 | 323 |
|
308 | | -} // namespace UC |
| 324 | +} // namespace UC |
0 commit comments