1313
1414#include " port/win/port_win.h"
1515
16- #include < assert.h>
1716#include < io.h>
17+ #include " port/port_dirent.h"
18+ #include " port/sys_time.h"
19+
20+ #include < cstdlib>
1821#include < stdio.h>
22+ #include < assert.h>
1923#include < string.h>
2024
21- #include < chrono>
22- #include < cstdlib>
23- #include < exception>
2425#include < memory>
25-
26- #include " port/port_dirent.h"
27- #include " port/sys_time.h"
26+ #include < exception>
27+ #include < chrono>
2828
2929#ifdef ROCKSDB_WINDOWS_UTF8_FILENAMES
3030// utf8 <-> utf16
31- #include < codecvt>
32- #include < locale>
3331#include < string>
32+ #include < locale>
33+ #include < codecvt>
3434#endif
3535
3636#include " logging/logging.h"
3737
38- namespace rocksdb {
38+ namespace ROCKSDB_NAMESPACE {
3939
4040extern const bool kDefaultToAdaptiveMutex = false ;
4141
4242namespace port {
4343
4444#ifdef ROCKSDB_WINDOWS_UTF8_FILENAMES
4545std::string utf16_to_utf8 (const std::wstring& utf16) {
46- std::wstring_convert<std::codecvt_utf8_utf16<wchar_t >, wchar_t > convert;
46+ std::wstring_convert<std::codecvt_utf8_utf16<wchar_t >,wchar_t > convert;
4747 return convert.to_bytes (utf16);
4848}
4949
@@ -63,77 +63,64 @@ void gettimeofday(struct timeval* tv, struct timezone* /* tz */) {
6363
6464 tv->tv_sec = static_cast <long >(secNow.count ());
6565 tv->tv_usec = static_cast <long >(usNow.count () -
66- duration_cast<microseconds>(secNow).count ());
66+ duration_cast<microseconds>(secNow).count ());
6767}
6868
69- Mutex::Mutex (bool adaptive) { ::InitializeCriticalSection (§ion_); }
70-
71- Mutex::~Mutex () { ::DeleteCriticalSection (§ion_); }
72-
73- void Mutex::Lock () {
74- ::EnterCriticalSection (§ion_);
75- #ifndef NDEBUG
76- locked_ = true ;
77- #endif
78- }
79-
80- void Mutex::Unlock () {
81- #ifndef NDEBUG
82- locked_ = false ;
83- #endif
84- ::LeaveCriticalSection (§ion_);
85- }
86-
87- void Mutex::AssertHeld () {
88- #ifndef NDEBUG
89- assert (locked_);
90- #endif
91- }
92-
93- CondVar::CondVar (Mutex* mu) : mu_(mu) { ::InitializeConditionVariable (&cv_); }
69+ Mutex::~Mutex () {}
9470
9571CondVar::~CondVar () {}
9672
9773void CondVar::Wait () {
74+ // Caller must ensure that mutex is held prior to calling this method
75+ std::unique_lock<std::mutex> lk (mu_->getLock (), std::adopt_lock);
9876#ifndef NDEBUG
9977 mu_->locked_ = false ;
10078#endif
101- ::SleepConditionVariableCS (& cv_, &(mu_->section_), INFINITE );
79+ cv_. wait (lk );
10280#ifndef NDEBUG
10381 mu_->locked_ = true ;
10482#endif
83+ // Release ownership of the lock as we don't want it to be unlocked when
84+ // it goes out of scope (as we adopted the lock and didn't lock it ourselves)
85+ lk.release ();
10586}
10687
10788bool CondVar::TimedWait (uint64_t abs_time_us) {
89+
10890 using namespace std ::chrono;
10991
11092 // MSVC++ library implements wait_until in terms of wait_for so
11193 // we need to convert absolute wait into relative wait.
11294 microseconds usAbsTime (abs_time_us);
11395
11496 microseconds usNow (
115- duration_cast<microseconds>(system_clock::now ().time_since_epoch ()));
97+ duration_cast<microseconds>(system_clock::now ().time_since_epoch ()));
11698 microseconds relTimeUs =
117- (usAbsTime > usNow) ? (usAbsTime - usNow) : microseconds::zero ();
118-
119- const BOOL cvStatus = ::SleepConditionVariableCS (
120- &cv_, &(mu_->section_ ),
121- static_cast <DWORD>(duration_cast<milliseconds>(relTimeUs).count ()));
99+ (usAbsTime > usNow) ? (usAbsTime - usNow) : microseconds::zero ();
122100
101+ // Caller must ensure that mutex is held prior to calling this method
102+ std::unique_lock<std::mutex> lk (mu_->getLock (), std::adopt_lock);
103+ #ifndef NDEBUG
104+ mu_->locked_ = false ;
105+ #endif
106+ std::cv_status cvStatus = cv_.wait_for (lk, relTimeUs);
123107#ifndef NDEBUG
124108 mu_->locked_ = true ;
125109#endif
110+ // Release ownership of the lock as we don't want it to be unlocked when
111+ // it goes out of scope (as we adopted the lock and didn't lock it ourselves)
112+ lk.release ();
126113
127- if ((! cvStatus) && ( GetLastError () == ERROR_TIMEOUT) ) {
114+ if (cvStatus == std::cv_status::timeout ) {
128115 return true ;
129116 }
130117
131118 return false ;
132119}
133120
134- void CondVar::Signal () { :: WakeConditionVariable (& cv_); }
121+ void CondVar::Signal () { cv_. notify_one ( ); }
135122
136- void CondVar::SignalAll () { WakeAllConditionVariable (& cv_); }
123+ void CondVar::SignalAll () { cv_. notify_all ( ); }
137124
138125int PhysicalCoreID () { return GetCurrentProcessorNumber (); }
139126
@@ -143,12 +130,13 @@ void InitOnce(OnceType* once, void (*initializer)()) {
143130
144131// Private structure, exposed only by pointer
145132struct DIR {
146- HANDLE handle_;
147- bool firstread_;
133+ HANDLE handle_;
134+ bool firstread_;
148135 RX_WIN32_FIND_DATA data_;
149136 dirent entry_;
150137
151- DIR () : handle_(INVALID_HANDLE_VALUE), firstread_(true ) {}
138+ DIR () : handle_(INVALID_HANDLE_VALUE),
139+ firstread_ (true ) {}
152140
153141 DIR (const DIR&) = delete;
154142 DIR& operator =(const DIR&) = delete ;
@@ -171,19 +159,20 @@ DIR* opendir(const char* name) {
171159
172160 std::unique_ptr<DIR> dir (new DIR);
173161
174- dir->handle_ =
175- RX_FindFirstFileEx ( RX_FN (pattern). c_str (),
176- FindExInfoBasic, // Do not want alternative name
177- &dir-> data_ , FindExSearchNameMatch,
178- NULL , // lpSearchFilter
179- 0 );
162+ dir->handle_ = RX_FindFirstFileEx ( RX_FN (pattern). c_str (),
163+ FindExInfoBasic, // Do not want alternative name
164+ &dir-> data_ ,
165+ FindExSearchNameMatch,
166+ NULL , // lpSearchFilter
167+ 0 );
180168
181169 if (dir->handle_ == INVALID_HANDLE_VALUE) {
182170 return nullptr ;
183171 }
184172
185173 RX_FILESTRING x (dir->data_ .cFileName , RX_FNLEN (dir->data_ .cFileName ));
186- strcpy_s (dir->entry_ .d_name , sizeof (dir->entry_ .d_name ), FN_TO_RX (x).c_str ());
174+ strcpy_s (dir->entry_ .d_name , sizeof (dir->entry_ .d_name ),
175+ FN_TO_RX (x).c_str ());
187176
188177 return dir.release ();
189178}
@@ -206,7 +195,7 @@ struct dirent* readdir(DIR* dirp) {
206195 }
207196
208197 RX_FILESTRING x (dirp->data_ .cFileName , RX_FNLEN (dirp->data_ .cFileName ));
209- strcpy_s (dirp->entry_ .d_name , sizeof (dirp->entry_ .d_name ),
198+ strcpy_s (dirp->entry_ .d_name , sizeof (dirp->entry_ .d_name ),
210199 FN_TO_RX (x).c_str ());
211200
212201 return &dirp->entry_ ;
@@ -222,21 +211,22 @@ int truncate(const char* path, int64_t length) {
222211 errno = EFAULT;
223212 return -1 ;
224213 }
225- return rocksdb ::port::Truncate (path, length);
214+ return ROCKSDB_NAMESPACE ::port::Truncate (path, length);
226215}
227216
228217int Truncate (std::string path, int64_t len) {
218+
229219 if (len < 0 ) {
230220 errno = EINVAL;
231221 return -1 ;
232222 }
233223
234224 HANDLE hFile =
235225 RX_CreateFile (RX_FN (path).c_str (), GENERIC_READ | GENERIC_WRITE,
236- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
237- NULL , // Security attrs
238- OPEN_EXISTING, // Truncate existing file only
239- FILE_ATTRIBUTE_NORMAL, NULL );
226+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
227+ NULL , // Security attrs
228+ OPEN_EXISTING, // Truncate existing file only
229+ FILE_ATTRIBUTE_NORMAL, NULL );
240230
241231 if (INVALID_HANDLE_VALUE == hFile) {
242232 auto lastError = GetLastError ();
@@ -272,5 +262,8 @@ void Crash(const std::string& srcfile, int srcline) {
272262
273263int GetMaxOpenFiles () { return -1 ; }
274264
265+ // Assume 4KB page size
266+ const size_t kPageSize = 4U * 1024U ;
267+
275268} // namespace port
276- } // namespace rocksdb
269+ } // namespace ROCKSDB_NAMESPACE
0 commit comments