Skip to content

Commit b719bfc

Browse files
committed
Rollback EA Win Port fix
1 parent d1b148c commit b719bfc

File tree

2 files changed

+101
-84
lines changed

2 files changed

+101
-84
lines changed

port/win/port_win.cc

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,37 @@
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

4040
extern const bool kDefaultToAdaptiveMutex = false;
4141

4242
namespace port {
4343

4444
#ifdef ROCKSDB_WINDOWS_UTF8_FILENAMES
4545
std::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(&section_); }
70-
71-
Mutex::~Mutex() { ::DeleteCriticalSection(&section_); }
72-
73-
void Mutex::Lock() {
74-
::EnterCriticalSection(&section_);
75-
#ifndef NDEBUG
76-
locked_ = true;
77-
#endif
78-
}
79-
80-
void Mutex::Unlock() {
81-
#ifndef NDEBUG
82-
locked_ = false;
83-
#endif
84-
::LeaveCriticalSection(&section_);
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

9571
CondVar::~CondVar() {}
9672

9773
void 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

10788
bool 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

138125
int PhysicalCoreID() { return GetCurrentProcessorNumber(); }
139126

@@ -143,12 +130,13 @@ void InitOnce(OnceType* once, void (*initializer)()) {
143130

144131
// Private structure, exposed only by pointer
145132
struct 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

228217
int 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

273263
int 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

port/win/port_win.h

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
#define WIN32_LEAN_AND_MEAN
1717
#endif
1818

19-
// Assume that for everywhere
20-
#undef PLATFORM_IS_LITTLE_ENDIAN
21-
#define PLATFORM_IS_LITTLE_ENDIAN true
22-
2319
#include <windows.h>
2420
#include <string>
2521
#include <string.h>
@@ -70,11 +66,7 @@ typedef SSIZE_T ssize_t;
7066

7167
#endif
7268

73-
#ifndef PLATFORM_IS_LITTLE_ENDIAN
74-
#define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
75-
#endif
76-
77-
namespace rocksdb {
69+
namespace ROCKSDB_NAMESPACE {
7870

7971
#define PREFETCH(addr, rw, locality)
8072

@@ -122,31 +114,59 @@ const size_t kMaxSizet = std::numeric_limits<size_t>::max();
122114

123115
#endif //_MSC_VER
124116

125-
const bool kLittleEndian = true;
117+
// "Windows is designed to run on little-endian computer architectures."
118+
// https://docs.microsoft.com/en-us/windows/win32/sysinfo/registry-value-types
119+
constexpr bool kLittleEndian = true;
120+
#undef PLATFORM_IS_LITTLE_ENDIAN
126121

127122
class CondVar;
128123

129124
class Mutex {
130125
public:
131-
/* implicit */ Mutex(bool adaptive = false);
126+
127+
/* implicit */ Mutex(bool adaptive = kDefaultToAdaptiveMutex)
128+
#ifndef NDEBUG
129+
: locked_(false)
130+
#endif
131+
{ }
132+
132133
~Mutex();
133134

134-
void Lock();
135-
void Unlock();
135+
void Lock() {
136+
mutex_.lock();
137+
#ifndef NDEBUG
138+
locked_ = true;
139+
#endif
140+
}
141+
142+
void Unlock() {
143+
#ifndef NDEBUG
144+
locked_ = false;
145+
#endif
146+
mutex_.unlock();
147+
}
136148

137149
// this will assert if the mutex is not locked
138150
// it does NOT verify that mutex is held by a calling thread
139-
void AssertHeld();
151+
void AssertHeld() {
152+
#ifndef NDEBUG
153+
assert(locked_);
154+
#endif
155+
}
140156

141157
// Mutex is move only with lock ownership transfer
142158
Mutex(const Mutex&) = delete;
143159
void operator=(const Mutex&) = delete;
144160

145161
private:
162+
146163
friend class CondVar;
147164

148-
CRITICAL_SECTION section_;
165+
std::mutex& getLock() {
166+
return mutex_;
167+
}
149168

169+
std::mutex mutex_;
150170
#ifndef NDEBUG
151171
bool locked_;
152172
#endif
@@ -176,7 +196,9 @@ class RWMutex {
176196

177197
class CondVar {
178198
public:
179-
explicit CondVar(Mutex* mu);
199+
explicit CondVar(Mutex* mu) : mu_(mu) {
200+
}
201+
180202
~CondVar();
181203
void Wait();
182204
bool TimedWait(uint64_t expiration_time);
@@ -191,7 +213,7 @@ class CondVar {
191213
CondVar& operator=(CondVar&&) = delete;
192214

193215
private:
194-
CONDITION_VARIABLE cv_;
216+
std::condition_variable cv_;
195217
Mutex* mu_;
196218
};
197219

@@ -243,6 +265,8 @@ inline void cacheline_aligned_free(void *memblock) {
243265
#endif
244266
}
245267

268+
extern const size_t kPageSize;
269+
246270
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52991 for MINGW32
247271
// could not be worked around with by -mno-ms-bitfields
248272
#ifndef __MINGW32__
@@ -318,8 +342,8 @@ std::wstring utf8_to_utf16(const std::string& utf8);
318342
#ifdef ROCKSDB_WINDOWS_UTF8_FILENAMES
319343

320344
#define RX_FILESTRING std::wstring
321-
#define RX_FN(a) rocksdb::port::utf8_to_utf16(a)
322-
#define FN_TO_RX(a) rocksdb::port::utf16_to_utf8(a)
345+
#define RX_FN(a) ROCKSDB_NAMESPACE::port::utf8_to_utf16(a)
346+
#define FN_TO_RX(a) ROCKSDB_NAMESPACE::port::utf16_to_utf8(a)
323347
#define RX_FNLEN(a) ::wcslen(a)
324348

325349
#define RX_DeleteFile DeleteFileW
@@ -371,4 +395,4 @@ using port::pthread_setspecific;
371395
using port::pthread_getspecific;
372396
using port::truncate;
373397

374-
} // namespace rocksdb
398+
} // namespace ROCKSDB_NAMESPACE

0 commit comments

Comments
 (0)