10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

716 lines
21 KiB

  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 <deque>
  15. #include <memory>
  16. #include <mutex>
  17. #include <sstream>
  18. #include <string>
  19. #include <vector>
  20. #include "leveldb/env.h"
  21. #include "leveldb/slice.h"
  22. #include "port/port.h"
  23. #include "port/thread_annotations.h"
  24. #include "util/env_windows_test_helper.h"
  25. #include "util/logging.h"
  26. #include "util/mutexlock.h"
  27. #include "util/windows_logger.h"
  28. #if defined(DeleteFile)
  29. #undef DeleteFile
  30. #endif // defined(DeleteFile)
  31. namespace leveldb {
  32. namespace {
  33. constexpr const size_t kWritableFileBufferSize = 65536;
  34. // Up to 1000 mmaps for 64-bit binaries; none for 32-bit.
  35. constexpr int kDefaultMmapLimit = sizeof(void*) >= 8 ? 1000 : 0;
  36. // Modified by EnvWindowsTestHelper::SetReadOnlyMMapLimit().
  37. int g_mmap_limit = kDefaultMmapLimit;
  38. std::string GetWindowsErrorMessage(DWORD error_code) {
  39. std::string message;
  40. char* error_text = nullptr;
  41. // Use MBCS version of FormatMessage to match return value.
  42. size_t error_text_size = ::FormatMessageA(
  43. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER |
  44. FORMAT_MESSAGE_IGNORE_INSERTS,
  45. nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  46. reinterpret_cast<char*>(&error_text), 0, nullptr);
  47. if (!error_text) {
  48. return message;
  49. }
  50. message.assign(error_text, error_text_size);
  51. ::LocalFree(error_text);
  52. return message;
  53. }
  54. Status WindowsError(const std::string& context, DWORD error_code) {
  55. if (error_code == ERROR_FILE_NOT_FOUND || error_code == ERROR_PATH_NOT_FOUND)
  56. return Status::NotFound(context, GetWindowsErrorMessage(error_code));
  57. return Status::IOError(context, GetWindowsErrorMessage(error_code));
  58. }
  59. class ScopedHandle {
  60. public:
  61. ScopedHandle(HANDLE handle) : handle_(handle) {}
  62. ScopedHandle(ScopedHandle&& other) noexcept : handle_(other.Release()) {}
  63. ~ScopedHandle() { Close(); }
  64. ScopedHandle& operator=(ScopedHandle&& rhs) noexcept {
  65. if (this != &rhs) handle_ = rhs.Release();
  66. return *this;
  67. }
  68. bool Close() {
  69. if (!is_valid()) {
  70. return true;
  71. }
  72. HANDLE h = handle_;
  73. handle_ = INVALID_HANDLE_VALUE;
  74. return ::CloseHandle(h);
  75. }
  76. bool is_valid() const {
  77. return handle_ != INVALID_HANDLE_VALUE && handle_ != nullptr;
  78. }
  79. HANDLE get() const { return handle_; }
  80. HANDLE Release() {
  81. HANDLE h = handle_;
  82. handle_ = INVALID_HANDLE_VALUE;
  83. return h;
  84. }
  85. private:
  86. HANDLE handle_;
  87. };
  88. // Helper class to limit resource usage to avoid exhaustion.
  89. // Currently used to limit read-only file descriptors and mmap file usage
  90. // so that we do not run out of file descriptors or virtual memory, or run into
  91. // kernel performance problems for very large databases.
  92. class Limiter {
  93. public:
  94. // Limit maximum number of resources to |max_acquires|.
  95. Limiter(int max_acquires) : acquires_allowed_(max_acquires) {}
  96. Limiter(const Limiter&) = delete;
  97. Limiter operator=(const Limiter&) = delete;
  98. // If another resource is available, acquire it and return true.
  99. // Else return false.
  100. bool Acquire() {
  101. int old_acquires_allowed =
  102. acquires_allowed_.fetch_sub(1, std::memory_order_relaxed);
  103. if (old_acquires_allowed > 0)
  104. return true;
  105. acquires_allowed_.fetch_add(1, std::memory_order_relaxed);
  106. return false;
  107. }
  108. // Release a resource acquired by a previous call to Acquire() that returned
  109. // true.
  110. void Release() {
  111. acquires_allowed_.fetch_add(1, std::memory_order_relaxed);
  112. }
  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 fname, ScopedHandle file)
  123. : filename_(fname), file_(std::move(file)) {}
  124. ~WindowsSequentialFile() override {}
  125. Status Read(size_t n, Slice* result, char* scratch) override {
  126. Status s;
  127. DWORD bytes_read;
  128. // DWORD is 32-bit, but size_t could technically be larger. However leveldb
  129. // files are limited to leveldb::Options::max_file_size which is clamped to
  130. // 1<<30 or 1 GiB.
  131. assert(n <= std::numeric_limits<DWORD>::max());
  132. if (!::ReadFile(file_.get(), scratch, static_cast<DWORD>(n), &bytes_read,
  133. nullptr)) {
  134. s = WindowsError(filename_, ::GetLastError());
  135. } else {
  136. *result = Slice(scratch, bytes_read);
  137. }
  138. return s;
  139. }
  140. Status Skip(uint64_t n) override {
  141. LARGE_INTEGER distance;
  142. distance.QuadPart = n;
  143. if (!::SetFilePointerEx(file_.get(), distance, nullptr, FILE_CURRENT)) {
  144. return WindowsError(filename_, ::GetLastError());
  145. }
  146. return Status::OK();
  147. }
  148. private:
  149. std::string filename_;
  150. ScopedHandle file_;
  151. };
  152. class WindowsRandomAccessFile : public RandomAccessFile {
  153. public:
  154. WindowsRandomAccessFile(std::string fname, ScopedHandle handle)
  155. : filename_(fname), handle_(std::move(handle)) {}
  156. ~WindowsRandomAccessFile() override = default;
  157. Status Read(uint64_t offset, size_t n, Slice* result,
  158. char* scratch) const override {
  159. DWORD bytes_read = 0;
  160. OVERLAPPED overlapped = {0};
  161. overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
  162. overlapped.Offset = static_cast<DWORD>(offset);
  163. if (!::ReadFile(handle_.get(), scratch, static_cast<DWORD>(n), &bytes_read,
  164. &overlapped)) {
  165. DWORD error_code = ::GetLastError();
  166. if (error_code != ERROR_HANDLE_EOF) {
  167. *result = Slice(scratch, 0);
  168. return Status::IOError(filename_, GetWindowsErrorMessage(error_code));
  169. }
  170. }
  171. *result = Slice(scratch, bytes_read);
  172. return Status::OK();
  173. }
  174. private:
  175. std::string filename_;
  176. ScopedHandle handle_;
  177. };
  178. class WindowsMmapReadableFile : public RandomAccessFile {
  179. public:
  180. // base[0,length-1] contains the mmapped contents of the file.
  181. WindowsMmapReadableFile(std::string fname, void* base, size_t length,
  182. Limiter* limiter)
  183. : filename_(std::move(fname)),
  184. mmapped_region_(base),
  185. length_(length),
  186. limiter_(limiter) {}
  187. ~WindowsMmapReadableFile() override {
  188. ::UnmapViewOfFile(mmapped_region_);
  189. limiter_->Release();
  190. }
  191. Status Read(uint64_t offset, size_t n, Slice* result,
  192. char* scratch) const override {
  193. Status s;
  194. if (offset + n > length_) {
  195. *result = Slice();
  196. s = WindowsError(filename_, ERROR_INVALID_PARAMETER);
  197. } else {
  198. *result = Slice(reinterpret_cast<char*>(mmapped_region_) + offset, n);
  199. }
  200. return s;
  201. }
  202. private:
  203. std::string filename_;
  204. void* mmapped_region_;
  205. size_t length_;
  206. Limiter* limiter_;
  207. };
  208. class WindowsWritableFile : public WritableFile {
  209. public:
  210. WindowsWritableFile(std::string fname, ScopedHandle handle)
  211. : filename_(std::move(fname)), handle_(std::move(handle)), pos_(0) {}
  212. ~WindowsWritableFile() override = default;
  213. Status Append(const Slice& data) override {
  214. size_t n = data.size();
  215. const char* p = data.data();
  216. // Fit as much as possible into buffer.
  217. size_t copy = std::min(n, kWritableFileBufferSize - pos_);
  218. memcpy(buf_ + pos_, p, copy);
  219. p += copy;
  220. n -= copy;
  221. pos_ += copy;
  222. if (n == 0) {
  223. return Status::OK();
  224. }
  225. // Can't fit in buffer, so need to do at least one write.
  226. Status s = FlushBuffered();
  227. if (!s.ok()) {
  228. return s;
  229. }
  230. // Small writes go to buffer, large writes are written directly.
  231. if (n < kWritableFileBufferSize) {
  232. memcpy(buf_, p, n);
  233. pos_ = n;
  234. return Status::OK();
  235. }
  236. return WriteRaw(p, n);
  237. }
  238. Status Close() override {
  239. Status result = FlushBuffered();
  240. if (!handle_.Close() && result.ok()) {
  241. result = WindowsError(filename_, ::GetLastError());
  242. }
  243. return result;
  244. }
  245. Status Flush() override { return FlushBuffered(); }
  246. Status Sync() override {
  247. // On Windows no need to sync parent directory. It's metadata will be
  248. // updated via the creation of the new file, without an explicit sync.
  249. return FlushBuffered();
  250. }
  251. private:
  252. Status FlushBuffered() {
  253. Status s = WriteRaw(buf_, pos_);
  254. pos_ = 0;
  255. return s;
  256. }
  257. Status WriteRaw(const char* p, size_t n) {
  258. DWORD bytes_written;
  259. if (!::WriteFile(handle_.get(), p, static_cast<DWORD>(n), &bytes_written,
  260. nullptr)) {
  261. return Status::IOError(filename_,
  262. GetWindowsErrorMessage(::GetLastError()));
  263. }
  264. return Status::OK();
  265. }
  266. // buf_[0, pos_-1] contains data to be written to handle_.
  267. const std::string filename_;
  268. ScopedHandle handle_;
  269. char buf_[kWritableFileBufferSize];
  270. size_t pos_;
  271. };
  272. // Lock or unlock the entire file as specified by |lock|. Returns true
  273. // when successful, false upon failure. Caller should call ::GetLastError()
  274. // to determine cause of failure
  275. bool LockOrUnlock(HANDLE handle, bool lock) {
  276. if (lock) {
  277. return ::LockFile(handle,
  278. /*dwFileOffsetLow=*/0, /*dwFileOffsetHigh=*/0,
  279. /*nNumberOfBytesToLockLow=*/MAXDWORD,
  280. /*nNumberOfBytesToLockHigh=*/MAXDWORD);
  281. } else {
  282. return ::UnlockFile(handle,
  283. /*dwFileOffsetLow=*/0, /*dwFileOffsetHigh=*/0,
  284. /*nNumberOfBytesToLockLow=*/MAXDWORD,
  285. /*nNumberOfBytesToLockHigh=*/MAXDWORD);
  286. }
  287. }
  288. class WindowsFileLock : public FileLock {
  289. public:
  290. WindowsFileLock(ScopedHandle handle, std::string name)
  291. : handle_(std::move(handle)), name_(std::move(name)) {}
  292. ScopedHandle& handle() { return handle_; }
  293. const std::string& name() const { return name_; }
  294. private:
  295. ScopedHandle handle_;
  296. std::string name_;
  297. };
  298. class WindowsEnv : public Env {
  299. public:
  300. WindowsEnv();
  301. ~WindowsEnv() override {
  302. static char msg[] = "Destroying Env::Default()\n";
  303. fwrite(msg, 1, sizeof(msg), stderr);
  304. abort();
  305. }
  306. Status NewSequentialFile(const std::string& fname,
  307. SequentialFile** result) override {
  308. *result = nullptr;
  309. DWORD desired_access = GENERIC_READ;
  310. DWORD share_mode = FILE_SHARE_READ;
  311. ScopedHandle handle =
  312. ::CreateFileA(fname.c_str(), desired_access, share_mode, nullptr,
  313. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  314. if (!handle.is_valid()) {
  315. return WindowsError(fname, ::GetLastError());
  316. }
  317. *result = new WindowsSequentialFile(fname, std::move(handle));
  318. return Status::OK();
  319. }
  320. Status NewRandomAccessFile(const std::string& fname,
  321. RandomAccessFile** result) override {
  322. *result = nullptr;
  323. DWORD desired_access = GENERIC_READ;
  324. DWORD share_mode = FILE_SHARE_READ;
  325. DWORD file_flags = FILE_ATTRIBUTE_READONLY;
  326. ScopedHandle handle =
  327. ::CreateFileA(fname.c_str(), desired_access, share_mode, nullptr,
  328. OPEN_EXISTING, file_flags, nullptr);
  329. if (!handle.is_valid()) {
  330. return WindowsError(fname, ::GetLastError());
  331. }
  332. if (!mmap_limiter_.Acquire()) {
  333. *result = new WindowsRandomAccessFile(fname, std::move(handle));
  334. return Status::OK();
  335. }
  336. LARGE_INTEGER file_size;
  337. if (!::GetFileSizeEx(handle.get(), &file_size)) {
  338. return WindowsError(fname, ::GetLastError());
  339. }
  340. ScopedHandle mapping =
  341. ::CreateFileMappingA(handle.get(),
  342. /*security attributes=*/nullptr, PAGE_READONLY,
  343. /*dwMaximumSizeHigh=*/0,
  344. /*dwMaximumSizeLow=*/0, nullptr);
  345. if (mapping.is_valid()) {
  346. void* base = MapViewOfFile(mapping.get(), FILE_MAP_READ, 0, 0, 0);
  347. if (base) {
  348. *result = new WindowsMmapReadableFile(
  349. fname, base, static_cast<size_t>(file_size.QuadPart),
  350. &mmap_limiter_);
  351. return Status::OK();
  352. }
  353. }
  354. Status s = WindowsError(fname, ::GetLastError());
  355. if (!s.ok()) {
  356. mmap_limiter_.Release();
  357. }
  358. return s;
  359. }
  360. Status NewWritableFile(const std::string& fname,
  361. WritableFile** result) override {
  362. DWORD desired_access = GENERIC_WRITE;
  363. DWORD share_mode = 0;
  364. ScopedHandle handle =
  365. ::CreateFileA(fname.c_str(), desired_access, share_mode, nullptr,
  366. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
  367. if (!handle.is_valid()) {
  368. *result = nullptr;
  369. return WindowsError(fname, ::GetLastError());
  370. }
  371. *result = new WindowsWritableFile(fname, std::move(handle));
  372. return Status::OK();
  373. }
  374. Status NewAppendableFile(const std::string& fname,
  375. WritableFile** result) override {
  376. ScopedHandle handle =
  377. ::CreateFileA(fname.c_str(), FILE_APPEND_DATA, 0, nullptr, OPEN_ALWAYS,
  378. FILE_ATTRIBUTE_NORMAL, nullptr);
  379. if (!handle.is_valid()) {
  380. *result = nullptr;
  381. return WindowsError(fname, ::GetLastError());
  382. }
  383. *result = new WindowsWritableFile(fname, std::move(handle));
  384. return Status::OK();
  385. }
  386. bool FileExists(const std::string& fname) override {
  387. return GetFileAttributesA(fname.c_str()) != INVALID_FILE_ATTRIBUTES;
  388. }
  389. Status GetChildren(const std::string& dir,
  390. std::vector<std::string>* result) override {
  391. const std::string find_pattern = dir + "\\*";
  392. WIN32_FIND_DATAA find_data;
  393. HANDLE dir_handle = ::FindFirstFileA(find_pattern.c_str(), &find_data);
  394. if (dir_handle == INVALID_HANDLE_VALUE) {
  395. DWORD last_error = ::GetLastError();
  396. if (last_error == ERROR_FILE_NOT_FOUND) {
  397. return Status::OK();
  398. }
  399. return WindowsError(dir, last_error);
  400. }
  401. do {
  402. char base_name[_MAX_FNAME];
  403. char ext[_MAX_EXT];
  404. if (!_splitpath_s(find_data.cFileName, nullptr, 0, nullptr, 0, base_name,
  405. ARRAYSIZE(base_name), ext, ARRAYSIZE(ext))) {
  406. result->emplace_back(std::string(base_name) + ext);
  407. }
  408. } while (::FindNextFileA(dir_handle, &find_data));
  409. DWORD last_error = ::GetLastError();
  410. ::FindClose(dir_handle);
  411. if (last_error != ERROR_NO_MORE_FILES) {
  412. return WindowsError(dir, last_error);
  413. }
  414. return Status::OK();
  415. }
  416. Status DeleteFile(const std::string& fname) override {
  417. if (!::DeleteFileA(fname.c_str())) {
  418. return WindowsError(fname, ::GetLastError());
  419. }
  420. return Status::OK();
  421. }
  422. Status CreateDir(const std::string& name) override {
  423. if (!::CreateDirectoryA(name.c_str(), nullptr)) {
  424. return WindowsError(name, ::GetLastError());
  425. }
  426. return Status::OK();
  427. }
  428. Status DeleteDir(const std::string& name) override {
  429. if (!::RemoveDirectoryA(name.c_str())) {
  430. return WindowsError(name, ::GetLastError());
  431. }
  432. return Status::OK();
  433. }
  434. Status GetFileSize(const std::string& fname, uint64_t* size) override {
  435. WIN32_FILE_ATTRIBUTE_DATA attrs;
  436. if (!::GetFileAttributesExA(fname.c_str(), GetFileExInfoStandard, &attrs)) {
  437. return WindowsError(fname, ::GetLastError());
  438. }
  439. ULARGE_INTEGER file_size;
  440. file_size.HighPart = attrs.nFileSizeHigh;
  441. file_size.LowPart = attrs.nFileSizeLow;
  442. *size = file_size.QuadPart;
  443. return Status::OK();
  444. }
  445. Status RenameFile(const std::string& src,
  446. const std::string& target) override {
  447. // Try a simple move first. It will only succeed when |to_path| doesn't
  448. // already exist.
  449. if (::MoveFileA(src.c_str(), target.c_str())) {
  450. return Status::OK();
  451. }
  452. DWORD move_error = ::GetLastError();
  453. // Try the full-blown replace if the move fails, as ReplaceFile will only
  454. // succeed when |to_path| does exist. When writing to a network share, we
  455. // may not be able to change the ACLs. Ignore ACL errors then
  456. // (REPLACEFILE_IGNORE_MERGE_ERRORS).
  457. if (::ReplaceFileA(target.c_str(), src.c_str(), nullptr,
  458. REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr)) {
  459. return Status::OK();
  460. }
  461. DWORD replace_error = ::GetLastError();
  462. // In the case of FILE_ERROR_NOT_FOUND from ReplaceFile, it is likely
  463. // that |to_path| does not exist. In this case, the more relevant error
  464. // comes from the call to MoveFile.
  465. if (replace_error == ERROR_FILE_NOT_FOUND ||
  466. replace_error == ERROR_PATH_NOT_FOUND) {
  467. return WindowsError(src, move_error);
  468. } else {
  469. return WindowsError(src, replace_error);
  470. }
  471. }
  472. Status LockFile(const std::string& fname, FileLock** lock) override {
  473. *lock = nullptr;
  474. Status result;
  475. ScopedHandle handle = ::CreateFileA(
  476. fname.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
  477. /*lpSecurityAttributes=*/nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
  478. nullptr);
  479. if (!handle.is_valid()) {
  480. result = WindowsError(fname, ::GetLastError());
  481. } else if (!LockOrUnlock(handle.get(), true)) {
  482. result = WindowsError("lock " + fname, ::GetLastError());
  483. } else {
  484. *lock = new WindowsFileLock(std::move(handle), std::move(fname));
  485. }
  486. return result;
  487. }
  488. Status UnlockFile(FileLock* lock) override {
  489. std::unique_ptr<WindowsFileLock> my_lock(
  490. reinterpret_cast<WindowsFileLock*>(lock));
  491. Status result;
  492. if (!LockOrUnlock(my_lock->handle().get(), false)) {
  493. result = WindowsError("unlock", ::GetLastError());
  494. }
  495. return result;
  496. }
  497. void Schedule(void (*function)(void*), void* arg) override;
  498. void StartThread(void (*function)(void* arg), void* arg) override {
  499. std::thread t(function, arg);
  500. t.detach();
  501. }
  502. Status GetTestDirectory(std::string* result) override {
  503. const char* env = getenv("TEST_TMPDIR");
  504. if (env && env[0] != '\0') {
  505. *result = env;
  506. return Status::OK();
  507. }
  508. char tmp_path[MAX_PATH];
  509. if (!GetTempPathA(ARRAYSIZE(tmp_path), tmp_path)) {
  510. return WindowsError("GetTempPath", ::GetLastError());
  511. }
  512. std::stringstream ss;
  513. ss << tmp_path << "leveldbtest-" << std::this_thread::get_id();
  514. *result = ss.str();
  515. // Directory may already exist
  516. CreateDir(*result);
  517. return Status::OK();
  518. }
  519. Status NewLogger(const std::string& filename, Logger** result) override {
  520. std::FILE* fp = std::fopen(filename.c_str(), "w");
  521. if (fp == nullptr) {
  522. *result = nullptr;
  523. return WindowsError("NewLogger", ::GetLastError());
  524. } else {
  525. *result = new WindowsLogger(fp);
  526. return Status::OK();
  527. }
  528. }
  529. uint64_t NowMicros() override {
  530. // GetSystemTimeAsFileTime typically has a resolution of 10-20 msec.
  531. // TODO(cmumford): Switch to GetSystemTimePreciseAsFileTime which is
  532. // available in Windows 8 and later.
  533. FILETIME ft;
  534. ::GetSystemTimeAsFileTime(&ft);
  535. // Each tick represents a 100-nanosecond intervals since January 1, 1601
  536. // (UTC).
  537. uint64_t num_ticks =
  538. (static_cast<uint64_t>(ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
  539. return num_ticks / 10;
  540. }
  541. void SleepForMicroseconds(int micros) override {
  542. std::this_thread::sleep_for(std::chrono::microseconds(micros));
  543. }
  544. private:
  545. // BGThread() is the body of the background thread
  546. void BGThread();
  547. std::mutex mu_;
  548. std::condition_variable bgsignal_;
  549. bool started_bgthread_;
  550. // Entry per Schedule() call
  551. struct BGItem {
  552. void* arg;
  553. void (*function)(void*);
  554. };
  555. typedef std::deque<BGItem> BGQueue;
  556. BGQueue queue_;
  557. Limiter mmap_limiter_;
  558. };
  559. // Return the maximum number of concurrent mmaps.
  560. int MaxMmaps() {
  561. if (g_mmap_limit >= 0) {
  562. return g_mmap_limit;
  563. }
  564. // Up to 1000 mmaps for 64-bit binaries; none for smaller pointer sizes.
  565. g_mmap_limit = sizeof(void*) >= 8 ? 1000 : 0;
  566. return g_mmap_limit;
  567. }
  568. WindowsEnv::WindowsEnv()
  569. : started_bgthread_(false), mmap_limiter_(MaxMmaps()) {}
  570. void WindowsEnv::Schedule(void (*function)(void*), void* arg) {
  571. std::lock_guard<std::mutex> guard(mu_);
  572. // Start background thread if necessary
  573. if (!started_bgthread_) {
  574. started_bgthread_ = true;
  575. std::thread t(&WindowsEnv::BGThread, this);
  576. t.detach();
  577. }
  578. // If the queue is currently empty, the background thread may currently be
  579. // waiting.
  580. if (queue_.empty()) {
  581. bgsignal_.notify_one();
  582. }
  583. // Add to priority queue
  584. queue_.push_back(BGItem());
  585. queue_.back().function = function;
  586. queue_.back().arg = arg;
  587. }
  588. void WindowsEnv::BGThread() {
  589. while (true) {
  590. // Wait until there is an item that is ready to run
  591. std::unique_lock<std::mutex> lk(mu_);
  592. bgsignal_.wait(lk, [this] { return !queue_.empty(); });
  593. void (*function)(void*) = queue_.front().function;
  594. void* arg = queue_.front().arg;
  595. queue_.pop_front();
  596. lk.unlock();
  597. (*function)(arg);
  598. }
  599. }
  600. } // namespace
  601. static std::once_flag once;
  602. static Env* default_env;
  603. static void InitDefaultEnv() { default_env = new WindowsEnv(); }
  604. void EnvWindowsTestHelper::SetReadOnlyMMapLimit(int limit) {
  605. assert(default_env == nullptr);
  606. g_mmap_limit = limit;
  607. }
  608. Env* Env::Default() {
  609. std::call_once(once, InitDefaultEnv);
  610. return default_env;
  611. }
  612. } // namespace leveldb