1313
1414#include " port/win/port_win.h"
1515
16+ #include < assert.h>
1617#include < io.h>
17- #include " port/port_dirent.h"
18- #include " port/sys_time.h"
19-
20- #include < cstdlib>
2118#include < stdio.h>
22- #include < assert.h>
2319#include < string.h>
2420
25- #include < memory>
26- #include < exception>
2721#include < chrono>
22+ #include < cstdlib>
23+ #include < exception>
24+ #include < memory>
25+
26+ #include " port/port_dirent.h"
27+ #include " port/sys_time.h"
2828
2929#ifdef ROCKSDB_WINDOWS_UTF8_FILENAMES
3030// utf8 <-> utf16
31- #include < string>
32- #include < locale>
3331#include < codecvt>
32+ #include < locale>
33+ #include < string>
3434#endif
3535
3636#include " logging/logging.h"
@@ -43,7 +43,7 @@ namespace 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,64 +63,77 @@ 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 ());
67+ }
68+
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
6791}
6892
69- Mutex::~ Mutex( ) {}
93+ CondVar::CondVar ( Mutex* mu) : mu_(mu ) { :: InitializeConditionVariable (&cv_); }
7094
7195CondVar::~CondVar () {}
7296
7397void 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);
7698#ifndef NDEBUG
7799 mu_->locked_ = false ;
78100#endif
79- cv_. wait (lk );
101+ ::SleepConditionVariableCS (& cv_, &(mu_->section_), INFINITE );
80102#ifndef NDEBUG
81103 mu_->locked_ = true ;
82104#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 ();
86105}
87106
88107bool CondVar::TimedWait (uint64_t abs_time_us) {
89-
90108 using namespace std ::chrono;
91109
92110 // MSVC++ library implements wait_until in terms of wait_for so
93111 // we need to convert absolute wait into relative wait.
94112 microseconds usAbsTime (abs_time_us);
95113
96114 microseconds usNow (
97- duration_cast<microseconds>(system_clock::now ().time_since_epoch ()));
115+ duration_cast<microseconds>(system_clock::now ().time_since_epoch ()));
98116 microseconds relTimeUs =
99- (usAbsTime > usNow) ? (usAbsTime - usNow) : microseconds::zero ();
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 ()));
100122
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);
107123#ifndef NDEBUG
108124 mu_->locked_ = true ;
109125#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 ();
113126
114- if (cvStatus == std::cv_status::timeout ) {
127+ if ((! cvStatus) && ( GetLastError () == ERROR_TIMEOUT) ) {
115128 return true ;
116129 }
117130
118131 return false ;
119132}
120133
121- void CondVar::Signal () { cv_. notify_one ( ); }
134+ void CondVar::Signal () { :: WakeConditionVariable (&cv_ ); }
122135
123- void CondVar::SignalAll () { cv_. notify_all ( ); }
136+ void CondVar::SignalAll () { WakeAllConditionVariable (&cv_ ); }
124137
125138int PhysicalCoreID () { return GetCurrentProcessorNumber (); }
126139
@@ -130,13 +143,12 @@ void InitOnce(OnceType* once, void (*initializer)()) {
130143
131144// Private structure, exposed only by pointer
132145struct DIR {
133- HANDLE handle_;
134- bool firstread_;
146+ HANDLE handle_;
147+ bool firstread_;
135148 RX_WIN32_FIND_DATA data_;
136149 dirent entry_;
137150
138- DIR () : handle_(INVALID_HANDLE_VALUE),
139- firstread_ (true ) {}
151+ DIR () : handle_(INVALID_HANDLE_VALUE), firstread_(true ) {}
140152
141153 DIR (const DIR&) = delete ;
142154 DIR& operator =(const DIR&) = delete ;
@@ -159,20 +171,19 @@ DIR* opendir(const char* name) {
159171
160172 std::unique_ptr<DIR> dir (new DIR);
161173
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 );
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 );
168180
169181 if (dir->handle_ == INVALID_HANDLE_VALUE) {
170182 return nullptr ;
171183 }
172184
173185 RX_FILESTRING x (dir->data_ .cFileName , RX_FNLEN (dir->data_ .cFileName ));
174- strcpy_s (dir->entry_ .d_name , sizeof (dir->entry_ .d_name ),
175- FN_TO_RX (x).c_str ());
186+ strcpy_s (dir->entry_ .d_name , sizeof (dir->entry_ .d_name ), FN_TO_RX (x).c_str ());
176187
177188 return dir.release ();
178189}
@@ -195,7 +206,7 @@ struct dirent* readdir(DIR* dirp) {
195206 }
196207
197208 RX_FILESTRING x (dirp->data_ .cFileName , RX_FNLEN (dirp->data_ .cFileName ));
198- strcpy_s (dirp->entry_ .d_name , sizeof (dirp->entry_ .d_name ),
209+ strcpy_s (dirp->entry_ .d_name , sizeof (dirp->entry_ .d_name ),
199210 FN_TO_RX (x).c_str ());
200211
201212 return &dirp->entry_ ;
@@ -215,18 +226,17 @@ int truncate(const char* path, int64_t length) {
215226}
216227
217228int Truncate (std::string path, int64_t len) {
218-
219229 if (len < 0 ) {
220230 errno = EINVAL;
221231 return -1 ;
222232 }
223233
224234 HANDLE hFile =
225235 RX_CreateFile (RX_FN (path).c_str (), GENERIC_READ | GENERIC_WRITE,
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 );
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 );
230240
231241 if (INVALID_HANDLE_VALUE == hFile) {
232242 auto lastError = GetLastError ();
0 commit comments