Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

796 wiersze
25 KiB

Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
4 lat temu
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
4 lat temu
  1. // Copyright (c) 2018 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. // Prevent Windows headers from defining min/max macros and instead
  5. // use STL.
  6. #ifndef NOMINMAX
  7. #define NOMINMAX
  8. #endif // ifndef NOMINMAX
  9. #include <windows.h>
  10. #include <algorithm>
  11. #include <atomic>
  12. #include <chrono>
  13. #include <condition_variable>
  14. #include <cstddef>
  15. #include <cstdint>
  16. #include <cstdlib>
  17. #include <cstring>
  18. #include <memory>
  19. #include <mutex>
  20. #include <queue>
  21. #include <sstream>
  22. #include <string>
  23. #include <vector>
  24. #include "leveldb/env.h"
  25. #include "leveldb/slice.h"
  26. #include "port/port.h"
  27. #include "port/thread_annotations.h"
  28. #include "util/env_windows_test_helper.h"
  29. #include "util/logging.h"
  30. #include "util/mutexlock.h"
  31. #include "util/windows_logger.h"
  32. namespace leveldb {
  33. namespace {
  34. constexpr const size_t kWritableFileBufferSize = 65536;
  35. // Up to 1000 mmaps for 64-bit binaries; none for 32-bit.
  36. constexpr int kDefaultMmapLimit = (sizeof(void*) >= 8) ? 1000 : 0;
  37. // Can be set by by EnvWindowsTestHelper::SetReadOnlyMMapLimit().
  38. int g_mmap_limit = kDefaultMmapLimit;
  39. std::string GetWindowsErrorMessage(DWORD error_code) {
  40. std::string message;
  41. char* error_text = nullptr;
  42. // Use MBCS version of FormatMessage to match return value.
  43. size_t error_text_size = ::FormatMessageA(
  44. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER |
  45. FORMAT_MESSAGE_IGNORE_INSERTS,
  46. nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  47. reinterpret_cast<char*>(&error_text), 0, nullptr);
  48. if (!error_text) {
  49. return message;
  50. }
  51. message.assign(error_text, error_text_size);
  52. ::LocalFree(error_text);
  53. return message;
  54. }
  55. Status WindowsError(const std::string& context, DWORD error_code) {
  56. if (error_code == ERROR_FILE_NOT_FOUND || error_code == ERROR_PATH_NOT_FOUND)
  57. return Status::NotFound(context, GetWindowsErrorMessage(error_code));
  58. return Status::IOError(context, GetWindowsErrorMessage(error_code));
  59. }
  60. class ScopedHandle {
  61. public:
  62. ScopedHandle(HANDLE handle) : handle_(handle) {}
  63. ScopedHandle(const ScopedHandle&) = delete;
  64. ScopedHandle(ScopedHandle&& other) noexcept : handle_(other.Release()) {}
  65. ~ScopedHandle() { Close(); }
  66. ScopedHandle& operator=(const ScopedHandle&) = delete;
  67. ScopedHandle& operator=(ScopedHandle&& rhs) noexcept {
  68. if (this != &rhs) handle_ = rhs.Release();
  69. return *this;
  70. }
  71. bool Close() {
  72. if (!is_valid()) {
  73. return true;
  74. }
  75. HANDLE h = handle_;
  76. handle_ = INVALID_HANDLE_VALUE;
  77. return ::CloseHandle(h);
  78. }
  79. bool is_valid() const {
  80. return handle_ != INVALID_HANDLE_VALUE && handle_ != nullptr;
  81. }
  82. HANDLE get() const { return handle_; }
  83. HANDLE Release() {
  84. HANDLE h = handle_;
  85. handle_ = INVALID_HANDLE_VALUE;
  86. return h;
  87. }
  88. private:
  89. HANDLE handle_;
  90. };
  91. // Helper class to limit resource usage to avoid exhaustion.
  92. // Currently used to limit read-only file descriptors and mmap file usage
  93. // so that we do not run out of file descriptors or virtual memory, or run into
  94. // kernel performance problems for very large databases.
  95. class Limiter {
  96. public:
  97. // Limit maximum number of resources to |max_acquires|.
  98. Limiter(int max_acquires) : acquires_allowed_(max_acquires) {}
  99. Limiter(const Limiter&) = delete;
  100. Limiter operator=(const Limiter&) = delete;
  101. // If another resource is available, acquire it and return true.
  102. // Else return false.
  103. bool Acquire() {
  104. int old_acquires_allowed =
  105. acquires_allowed_.fetch_sub(1, std::memory_order_relaxed);
  106. if (old_acquires_allowed > 0) return true;
  107. acquires_allowed_.fetch_add(1, std::memory_order_relaxed);
  108. return false;
  109. }
  110. // Release a resource acquired by a previous call to Acquire() that returned
  111. // true.
  112. void Release() { acquires_allowed_.fetch_add(1, std::memory_order_relaxed); }
  113. private:
  114. // The number of available resources.
  115. //
  116. // This is a counter and is not tied to the invariants of any other class, so
  117. // it can be operated on safely using std::memory_order_relaxed.
  118. std::atomic<int> acquires_allowed_;
  119. };
  120. class WindowsSequentialFile : public SequentialFile {
  121. public:
  122. WindowsSequentialFile(std::string filename, ScopedHandle handle)
  123. : handle_(std::move(handle)), filename_(std::move(filename)) {}
  124. ~WindowsSequentialFile() override {}
  125. Status Read(size_t n, Slice* result, char* scratch) override {
  126. DWORD bytes_read;
  127. // DWORD is 32-bit, but size_t could technically be larger. However leveldb
  128. // files are limited to leveldb::Options::max_file_size which is clamped to
  129. // 1<<30 or 1 GiB.
  130. assert(n <= std::numeric_limits<DWORD>::max());
  131. if (!::ReadFile(handle_.get(), scratch, static_cast<DWORD>(n), &bytes_read,
  132. nullptr)) {
  133. return WindowsError(filename_, ::GetLastError());
  134. }
  135. *result = Slice(scratch, bytes_read);
  136. return Status::OK();
  137. }
  138. Status Skip(uint64_t n) override {
  139. LARGE_INTEGER distance;
  140. distance.QuadPart = n;
  141. if (!::SetFilePointerEx(handle_.get(), distance, nullptr, FILE_CURRENT)) {
  142. return WindowsError(filename_, ::GetLastError());
  143. }
  144. return Status::OK();
  145. }
  146. private:
  147. const ScopedHandle handle_;
  148. const std::string filename_;
  149. };
  150. class WindowsRandomAccessFile : public RandomAccessFile {
  151. public:
  152. WindowsRandomAccessFile(std::string filename, ScopedHandle handle)
  153. : handle_(std::move(handle)), filename_(std::move(filename)) {}
  154. ~WindowsRandomAccessFile() override = default;
  155. Status Read(uint64_t offset, size_t n, Slice* result,
  156. char* scratch) const override {
  157. DWORD bytes_read = 0;
  158. OVERLAPPED overlapped = {0};
  159. overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
  160. overlapped.Offset = static_cast<DWORD>(offset);
  161. if (!::ReadFile(handle_.get(), scratch, static_cast<DWORD>(n), &bytes_read,
  162. &overlapped)) {
  163. DWORD error_code = ::GetLastError();
  164. if (error_code != ERROR_HANDLE_EOF) {
  165. *result = Slice(scratch, 0);
  166. return Status::IOError(filename_, GetWindowsErrorMessage(error_code));
  167. }
  168. }
  169. *result = Slice(scratch, bytes_read);
  170. return Status::OK();
  171. }
  172. private:
  173. const ScopedHandle handle_;
  174. const std::string filename_;
  175. };
  176. class WindowsMmapReadableFile : public RandomAccessFile {
  177. public:
  178. // base[0,length-1] contains the mmapped contents of the file.
  179. WindowsMmapReadableFile(std::string filename, char* mmap_base, size_t length,
  180. Limiter* mmap_limiter)
  181. : mmap_base_(mmap_base),
  182. length_(length),
  183. mmap_limiter_(mmap_limiter),
  184. filename_(std::move(filename)) {}
  185. ~WindowsMmapReadableFile() override {
  186. ::UnmapViewOfFile(mmap_base_);
  187. mmap_limiter_->Release();
  188. }
  189. Status Read(uint64_t offset, size_t n, Slice* result,
  190. char* scratch) const override {
  191. if (offset + n > length_) {
  192. *result = Slice();
  193. return WindowsError(filename_, ERROR_INVALID_PARAMETER);
  194. }
  195. *result = Slice(mmap_base_ + offset, n);
  196. return Status::OK();
  197. }
  198. private:
  199. char* const mmap_base_;
  200. const size_t length_;
  201. Limiter* const mmap_limiter_;
  202. const std::string filename_;
  203. };
  204. class WindowsWritableFile : public WritableFile {
  205. public:
  206. WindowsWritableFile(std::string filename, ScopedHandle handle)
  207. : pos_(0), handle_(std::move(handle)), filename_(std::move(filename)) {}
  208. ~WindowsWritableFile() override = default;
  209. Status Append(const Slice& data) override {
  210. size_t write_size = data.size();
  211. const char* write_data = data.data();
  212. // Fit as much as possible into buffer.
  213. size_t copy_size = std::min(write_size, kWritableFileBufferSize - pos_);
  214. std::memcpy(buf_ + pos_, write_data, copy_size);
  215. write_data += copy_size;
  216. write_size -= copy_size;
  217. pos_ += copy_size;
  218. if (write_size == 0) {
  219. return Status::OK();
  220. }
  221. // Can't fit in buffer, so need to do at least one write.
  222. Status status = FlushBuffer();
  223. if (!status.ok()) {
  224. return status;
  225. }
  226. // Small writes go to buffer, large writes are written directly.
  227. if (write_size < kWritableFileBufferSize) {
  228. std::memcpy(buf_, write_data, write_size);
  229. pos_ = write_size;
  230. return Status::OK();
  231. }
  232. return WriteUnbuffered(write_data, write_size);
  233. }
  234. Status Close() override {
  235. Status status = FlushBuffer();
  236. if (!handle_.Close() && status.ok()) {
  237. status = WindowsError(filename_, ::GetLastError());
  238. }
  239. return status;
  240. }
  241. Status Flush() override { return FlushBuffer(); }
  242. Status Sync() override {
  243. // On Windows no need to sync parent directory. Its metadata will be updated
  244. // via the creation of the new file, without an explicit sync.
  245. Status status = FlushBuffer();
  246. if (!status.ok()) {
  247. return status;
  248. }
  249. if (!::FlushFileBuffers(handle_.get())) {
  250. return Status::IOError(filename_,
  251. GetWindowsErrorMessage(::GetLastError()));
  252. }
  253. return Status::OK();
  254. }
  255. private:
  256. Status FlushBuffer() {
  257. Status status = WriteUnbuffered(buf_, pos_);
  258. pos_ = 0;
  259. return status;
  260. }
  261. Status WriteUnbuffered(const char* data, size_t size) {
  262. DWORD bytes_written;
  263. if (!::WriteFile(handle_.get(), data, static_cast<DWORD>(size),
  264. &bytes_written, nullptr)) {
  265. return Status::IOError(filename_,
  266. GetWindowsErrorMessage(::GetLastError()));
  267. }
  268. return Status::OK();
  269. }
  270. // buf_[0, pos_-1] contains data to be written to handle_.
  271. char buf_[kWritableFileBufferSize];
  272. size_t pos_;
  273. ScopedHandle handle_;
  274. const std::string filename_;
  275. };
  276. // Lock or unlock the entire file as specified by |lock|. Returns true
  277. // when successful, false upon failure. Caller should call ::GetLastError()
  278. // to determine cause of failure
  279. bool LockOrUnlock(HANDLE handle, bool lock) {
  280. if (lock) {
  281. return ::LockFile(handle,
  282. /*dwFileOffsetLow=*/0, /*dwFileOffsetHigh=*/0,
  283. /*nNumberOfBytesToLockLow=*/MAXDWORD,
  284. /*nNumberOfBytesToLockHigh=*/MAXDWORD);
  285. } else {
  286. return ::UnlockFile(handle,
  287. /*dwFileOffsetLow=*/0, /*dwFileOffsetHigh=*/0,
  288. /*nNumberOfBytesToLockLow=*/MAXDWORD,
  289. /*nNumberOfBytesToLockHigh=*/MAXDWORD);
  290. }
  291. }
  292. class WindowsFileLock : public FileLock {
  293. public:
  294. WindowsFileLock(ScopedHandle handle, std::string filename)
  295. : handle_(std::move(handle)), filename_(std::move(filename)) {}
  296. const ScopedHandle& handle() const { return handle_; }
  297. const std::string& filename() const { return filename_; }
  298. private:
  299. const ScopedHandle handle_;
  300. const std::string filename_;
  301. };
  302. class WindowsEnv : public Env {
  303. public:
  304. WindowsEnv();
  305. ~WindowsEnv() override {
  306. static const char msg[] =
  307. "WindowsEnv singleton destroyed. Unsupported behavior!\n";
  308. std::fwrite(msg, 1, sizeof(msg), stderr);
  309. std::abort();
  310. }
  311. Status NewSequentialFile(const std::string& filename,
  312. SequentialFile** result) override {
  313. *result = nullptr;
  314. DWORD desired_access = GENERIC_READ;
  315. DWORD share_mode = FILE_SHARE_READ;
  316. ScopedHandle handle = ::CreateFileA(
  317. filename.c_str(), desired_access, share_mode,
  318. /*lpSecurityAttributes=*/nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  319. /*hTemplateFile=*/nullptr);
  320. if (!handle.is_valid()) {
  321. return WindowsError(filename, ::GetLastError());
  322. }
  323. *result = new WindowsSequentialFile(filename, std::move(handle));
  324. return Status::OK();
  325. }
  326. Status NewRandomAccessFile(const std::string& filename,
  327. RandomAccessFile** result) override {
  328. *result = nullptr;
  329. DWORD desired_access = GENERIC_READ;
  330. DWORD share_mode = FILE_SHARE_READ;
  331. ScopedHandle handle =
  332. ::CreateFileA(filename.c_str(), desired_access, share_mode,
  333. /*lpSecurityAttributes=*/nullptr, OPEN_EXISTING,
  334. FILE_ATTRIBUTE_READONLY,
  335. /*hTemplateFile=*/nullptr);
  336. if (!handle.is_valid()) {
  337. return WindowsError(filename, ::GetLastError());
  338. }
  339. if (!mmap_limiter_.Acquire()) {
  340. *result = new WindowsRandomAccessFile(filename, std::move(handle));
  341. return Status::OK();
  342. }
  343. LARGE_INTEGER file_size;
  344. Status status;
  345. if (!::GetFileSizeEx(handle.get(), &file_size)) {
  346. mmap_limiter_.Release();
  347. return WindowsError(filename, ::GetLastError());
  348. }
  349. ScopedHandle mapping =
  350. ::CreateFileMappingA(handle.get(),
  351. /*security attributes=*/nullptr, PAGE_READONLY,
  352. /*dwMaximumSizeHigh=*/0,
  353. /*dwMaximumSizeLow=*/0,
  354. /*lpName=*/nullptr);
  355. if (mapping.is_valid()) {
  356. void* mmap_base = ::MapViewOfFile(mapping.get(), FILE_MAP_READ,
  357. /*dwFileOffsetHigh=*/0,
  358. /*dwFileOffsetLow=*/0,
  359. /*dwNumberOfBytesToMap=*/0);
  360. if (mmap_base) {
  361. *result = new WindowsMmapReadableFile(
  362. filename, reinterpret_cast<char*>(mmap_base),
  363. static_cast<size_t>(file_size.QuadPart), &mmap_limiter_);
  364. return Status::OK();
  365. }
  366. }
  367. mmap_limiter_.Release();
  368. return WindowsError(filename, ::GetLastError());
  369. }
  370. Status NewWritableFile(const std::string& filename,
  371. WritableFile** result) override {
  372. DWORD desired_access = GENERIC_WRITE;
  373. DWORD share_mode = 0; // Exclusive access.
  374. ScopedHandle handle = ::CreateFileA(
  375. filename.c_str(), desired_access, share_mode,
  376. /*lpSecurityAttributes=*/nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
  377. /*hTemplateFile=*/nullptr);
  378. if (!handle.is_valid()) {
  379. *result = nullptr;
  380. return WindowsError(filename, ::GetLastError());
  381. }
  382. *result = new WindowsWritableFile(filename, std::move(handle));
  383. return Status::OK();
  384. }
  385. Status NewAppendableFile(const std::string& filename,
  386. WritableFile** result) override {
  387. DWORD desired_access = FILE_APPEND_DATA;
  388. DWORD share_mode = 0; // Exclusive access.
  389. ScopedHandle handle = ::CreateFileA(
  390. filename.c_str(), desired_access, share_mode,
  391. /*lpSecurityAttributes=*/nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
  392. /*hTemplateFile=*/nullptr);
  393. if (!handle.is_valid()) {
  394. *result = nullptr;
  395. return WindowsError(filename, ::GetLastError());
  396. }
  397. *result = new WindowsWritableFile(filename, std::move(handle));
  398. return Status::OK();
  399. }
  400. bool FileExists(const std::string& filename) override {
  401. return GetFileAttributesA(filename.c_str()) != INVALID_FILE_ATTRIBUTES;
  402. }
  403. Status GetChildren(const std::string& directory_path,
  404. std::vector<std::string>* result) override {
  405. const std::string find_pattern = directory_path + "\\*";
  406. WIN32_FIND_DATAA find_data;
  407. HANDLE dir_handle = ::FindFirstFileA(find_pattern.c_str(), &find_data);
  408. if (dir_handle == INVALID_HANDLE_VALUE) {
  409. DWORD last_error = ::GetLastError();
  410. if (last_error == ERROR_FILE_NOT_FOUND) {
  411. return Status::OK();
  412. }
  413. return WindowsError(directory_path, last_error);
  414. }
  415. do {
  416. char base_name[_MAX_FNAME];
  417. char ext[_MAX_EXT];
  418. if (!_splitpath_s(find_data.cFileName, nullptr, 0, nullptr, 0, base_name,
  419. ARRAYSIZE(base_name), ext, ARRAYSIZE(ext))) {
  420. result->emplace_back(std::string(base_name) + ext);
  421. }
  422. } while (::FindNextFileA(dir_handle, &find_data));
  423. DWORD last_error = ::GetLastError();
  424. ::FindClose(dir_handle);
  425. if (last_error != ERROR_NO_MORE_FILES) {
  426. return WindowsError(directory_path, last_error);
  427. }
  428. return Status::OK();
  429. }
  430. Status RemoveFile(const std::string& filename) override {
  431. if (!::DeleteFileA(filename.c_str())) {
  432. return WindowsError(filename, ::GetLastError());
  433. }
  434. return Status::OK();
  435. }
  436. Status CreateDir(const std::string& dirname) override {
  437. if (!::CreateDirectoryA(dirname.c_str(), nullptr)) {
  438. return WindowsError(dirname, ::GetLastError());
  439. }
  440. return Status::OK();
  441. }
  442. Status RemoveDir(const std::string& dirname) override {
  443. if (!::RemoveDirectoryA(dirname.c_str())) {
  444. return WindowsError(dirname, ::GetLastError());
  445. }
  446. return Status::OK();
  447. }
  448. Status GetFileSize(const std::string& filename, uint64_t* size) override {
  449. WIN32_FILE_ATTRIBUTE_DATA file_attributes;
  450. if (!::GetFileAttributesExA(filename.c_str(), GetFileExInfoStandard,
  451. &file_attributes)) {
  452. return WindowsError(filename, ::GetLastError());
  453. }
  454. ULARGE_INTEGER file_size;
  455. file_size.HighPart = file_attributes.nFileSizeHigh;
  456. file_size.LowPart = file_attributes.nFileSizeLow;
  457. *size = file_size.QuadPart;
  458. return Status::OK();
  459. }
  460. Status RenameFile(const std::string& from, const std::string& to) override {
  461. // Try a simple move first. It will only succeed when |to| doesn't already
  462. // exist.
  463. if (::MoveFileA(from.c_str(), to.c_str())) {
  464. return Status::OK();
  465. }
  466. DWORD move_error = ::GetLastError();
  467. // Try the full-blown replace if the move fails, as ReplaceFile will only
  468. // succeed when |to| does exist. When writing to a network share, we may not
  469. // be able to change the ACLs. Ignore ACL errors then
  470. // (REPLACEFILE_IGNORE_MERGE_ERRORS).
  471. if (::ReplaceFileA(to.c_str(), from.c_str(), /*lpBackupFileName=*/nullptr,
  472. REPLACEFILE_IGNORE_MERGE_ERRORS,
  473. /*lpExclude=*/nullptr, /*lpReserved=*/nullptr)) {
  474. return Status::OK();
  475. }
  476. DWORD replace_error = ::GetLastError();
  477. // In the case of FILE_ERROR_NOT_FOUND from ReplaceFile, it is likely that
  478. // |to| does not exist. In this case, the more relevant error comes from the
  479. // call to MoveFile.
  480. if (replace_error == ERROR_FILE_NOT_FOUND ||
  481. replace_error == ERROR_PATH_NOT_FOUND) {
  482. return WindowsError(from, move_error);
  483. } else {
  484. return WindowsError(from, replace_error);
  485. }
  486. }
  487. Status LockFile(const std::string& filename, FileLock** lock) override {
  488. *lock = nullptr;
  489. Status result;
  490. ScopedHandle handle = ::CreateFileA(
  491. filename.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
  492. /*lpSecurityAttributes=*/nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
  493. nullptr);
  494. if (!handle.is_valid()) {
  495. result = WindowsError(filename, ::GetLastError());
  496. } else if (!LockOrUnlock(handle.get(), true)) {
  497. result = WindowsError("lock " + filename, ::GetLastError());
  498. } else {
  499. *lock = new WindowsFileLock(std::move(handle), filename);
  500. }
  501. return result;
  502. }
  503. Status UnlockFile(FileLock* lock) override {
  504. WindowsFileLock* windows_file_lock =
  505. reinterpret_cast<WindowsFileLock*>(lock);
  506. if (!LockOrUnlock(windows_file_lock->handle().get(), false)) {
  507. return WindowsError("unlock " + windows_file_lock->filename(),
  508. ::GetLastError());
  509. }
  510. delete windows_file_lock;
  511. return Status::OK();
  512. }
  513. void Schedule(void (*background_work_function)(void* background_work_arg),
  514. void* background_work_arg) override;
  515. void StartThread(void (*thread_main)(void* thread_main_arg),
  516. void* thread_main_arg) override {
  517. std::thread new_thread(thread_main, thread_main_arg);
  518. new_thread.detach();
  519. }
  520. Status GetTestDirectory(std::string* result) override {
  521. const char* env = getenv("TEST_TMPDIR");
  522. if (env && env[0] != '\0') {
  523. *result = env;
  524. return Status::OK();
  525. }
  526. char tmp_path[MAX_PATH];
  527. if (!GetTempPathA(ARRAYSIZE(tmp_path), tmp_path)) {
  528. return WindowsError("GetTempPath", ::GetLastError());
  529. }
  530. std::stringstream ss;
  531. ss << tmp_path << "leveldbtest-" << std::this_thread::get_id();
  532. *result = ss.str();
  533. // Directory may already exist
  534. CreateDir(*result);
  535. return Status::OK();
  536. }
  537. Status NewLogger(const std::string& filename, Logger** result) override {
  538. std::FILE* fp = std::fopen(filename.c_str(), "w");
  539. if (fp == nullptr) {
  540. *result = nullptr;
  541. return WindowsError(filename, ::GetLastError());
  542. } else {
  543. *result = new WindowsLogger(fp);
  544. return Status::OK();
  545. }
  546. }
  547. uint64_t NowMicros() override {
  548. // GetSystemTimeAsFileTime typically has a resolution of 10-20 msec.
  549. // TODO(cmumford): Switch to GetSystemTimePreciseAsFileTime which is
  550. // available in Windows 8 and later.
  551. FILETIME ft;
  552. ::GetSystemTimeAsFileTime(&ft);
  553. // Each tick represents a 100-nanosecond intervals since January 1, 1601
  554. // (UTC).
  555. uint64_t num_ticks =
  556. (static_cast<uint64_t>(ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
  557. return num_ticks / 10;
  558. }
  559. void SleepForMicroseconds(int micros) override {
  560. std::this_thread::sleep_for(std::chrono::microseconds(micros));
  561. }
  562. private:
  563. void BackgroundThreadMain();
  564. static void BackgroundThreadEntryPoint(WindowsEnv* env) {
  565. env->BackgroundThreadMain();
  566. }
  567. // Stores the work item data in a Schedule() call.
  568. //
  569. // Instances are constructed on the thread calling Schedule() and used on the
  570. // background thread.
  571. //
  572. // This structure is thread-safe beacuse it is immutable.
  573. struct BackgroundWorkItem {
  574. explicit BackgroundWorkItem(void (*function)(void* arg), void* arg)
  575. : function(function), arg(arg) {}
  576. void (*const function)(void*);
  577. void* const arg;
  578. };
  579. port::Mutex background_work_mutex_;
  580. port::CondVar background_work_cv_ GUARDED_BY(background_work_mutex_);
  581. bool started_background_thread_ GUARDED_BY(background_work_mutex_);
  582. std::queue<BackgroundWorkItem> background_work_queue_
  583. GUARDED_BY(background_work_mutex_);
  584. Limiter mmap_limiter_; // Thread-safe.
  585. };
  586. // Return the maximum number of concurrent mmaps.
  587. int MaxMmaps() { return g_mmap_limit; }
  588. WindowsEnv::WindowsEnv()
  589. : background_work_cv_(&background_work_mutex_),
  590. started_background_thread_(false),
  591. mmap_limiter_(MaxMmaps()) {}
  592. void WindowsEnv::Schedule(
  593. void (*background_work_function)(void* background_work_arg),
  594. void* background_work_arg) {
  595. background_work_mutex_.Lock();
  596. // Start the background thread, if we haven't done so already.
  597. if (!started_background_thread_) {
  598. started_background_thread_ = true;
  599. std::thread background_thread(WindowsEnv::BackgroundThreadEntryPoint, this);
  600. background_thread.detach();
  601. }
  602. // If the queue is empty, the background thread may be waiting for work.
  603. if (background_work_queue_.empty()) {
  604. background_work_cv_.Signal();
  605. }
  606. background_work_queue_.emplace(background_work_function, background_work_arg);
  607. background_work_mutex_.Unlock();
  608. }
  609. void WindowsEnv::BackgroundThreadMain() {
  610. while (true) {
  611. background_work_mutex_.Lock();
  612. // Wait until there is work to be done.
  613. while (background_work_queue_.empty()) {
  614. background_work_cv_.Wait();
  615. }
  616. assert(!background_work_queue_.empty());
  617. auto background_work_function = background_work_queue_.front().function;
  618. void* background_work_arg = background_work_queue_.front().arg;
  619. background_work_queue_.pop();
  620. background_work_mutex_.Unlock();
  621. background_work_function(background_work_arg);
  622. }
  623. }
  624. // Wraps an Env instance whose destructor is never created.
  625. //
  626. // Intended usage:
  627. // using PlatformSingletonEnv = SingletonEnv<PlatformEnv>;
  628. // void ConfigurePosixEnv(int param) {
  629. // PlatformSingletonEnv::AssertEnvNotInitialized();
  630. // // set global configuration flags.
  631. // }
  632. // Env* Env::Default() {
  633. // static PlatformSingletonEnv default_env;
  634. // return default_env.env();
  635. // }
  636. template <typename EnvType>
  637. class SingletonEnv {
  638. public:
  639. SingletonEnv() {
  640. #if !defined(NDEBUG)
  641. env_initialized_.store(true, std::memory_order::memory_order_relaxed);
  642. #endif // !defined(NDEBUG)
  643. static_assert(sizeof(env_storage_) >= sizeof(EnvType),
  644. "env_storage_ will not fit the Env");
  645. static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType),
  646. "env_storage_ does not meet the Env's alignment needs");
  647. new (&env_storage_) EnvType();
  648. }
  649. ~SingletonEnv() = default;
  650. SingletonEnv(const SingletonEnv&) = delete;
  651. SingletonEnv& operator=(const SingletonEnv&) = delete;
  652. Env* env() { return reinterpret_cast<Env*>(&env_storage_); }
  653. static void AssertEnvNotInitialized() {
  654. #if !defined(NDEBUG)
  655. assert(!env_initialized_.load(std::memory_order::memory_order_relaxed));
  656. #endif // !defined(NDEBUG)
  657. }
  658. private:
  659. typename std::aligned_storage<sizeof(EnvType), alignof(EnvType)>::type
  660. env_storage_;
  661. #if !defined(NDEBUG)
  662. static std::atomic<bool> env_initialized_;
  663. #endif // !defined(NDEBUG)
  664. };
  665. #if !defined(NDEBUG)
  666. template <typename EnvType>
  667. std::atomic<bool> SingletonEnv<EnvType>::env_initialized_;
  668. #endif // !defined(NDEBUG)
  669. using WindowsDefaultEnv = SingletonEnv<WindowsEnv>;
  670. } // namespace
  671. void EnvWindowsTestHelper::SetReadOnlyMMapLimit(int limit) {
  672. WindowsDefaultEnv::AssertEnvNotInitialized();
  673. g_mmap_limit = limit;
  674. }
  675. Env* Env::Default() {
  676. static WindowsDefaultEnv env_container;
  677. return env_container.env();
  678. }
  679. } // namespace leveldb