]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/port/win/port_win.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
10 // See port_example.h for documentation for the following types/functions.
14 // Always want minimum headers
15 #ifndef WIN32_LEAN_AND_MEAN
16 #define WIN32_LEAN_AND_MEAN
24 #include <condition_variable>
30 #include "port/win/win_thread.h"
32 #include "rocksdb/options.h"
41 #define strcasecmp _stricmp
47 #ifndef _SSIZE_T_DEFINED
48 typedef SSIZE_T ssize_t
;
51 // size_t printf formatting named in the manner of C99 standard formatting
52 // strings such as PRIu64
53 // in fact, we could use that one
54 #ifndef ROCKSDB_PRIszt
55 #define ROCKSDB_PRIszt "Iu"
59 #define __attribute__(A)
61 // Thread local storage on Linux
62 // There is thread_local in C++11
64 #define __thread __declspec(thread)
69 namespace ROCKSDB_NAMESPACE
{
71 #define PREFETCH(addr, rw, locality)
73 extern const bool kDefaultToAdaptiveMutex
;
78 #if defined(_MSC_VER) && (_MSC_VER < 1900)
81 #define snprintf _snprintf
83 #define ROCKSDB_NOEXCEPT
84 // std::numeric_limits<size_t>::max() is not constexpr just yet
85 // therefore, use the same limits
87 // For use at db/file_indexer.h kLevelMaxIndex
88 const uint32_t kMaxUint32
= UINT32_MAX
;
89 const int kMaxInt32
= INT32_MAX
;
90 const int kMinInt32
= INT32_MIN
;
91 const int64_t kMaxInt64
= INT64_MAX
;
92 const int64_t kMinInt64
= INT64_MIN
;
93 const uint64_t kMaxUint64
= UINT64_MAX
;
96 const size_t kMaxSizet
= UINT64_MAX
;
98 const size_t kMaxSizet
= UINT_MAX
;
101 #else // VS >= 2015 or MinGW
103 #define ROCKSDB_NOEXCEPT noexcept
105 // For use at db/file_indexer.h kLevelMaxIndex
106 const uint32_t kMaxUint32
= std::numeric_limits
<uint32_t>::max();
107 const int kMaxInt32
= std::numeric_limits
<int>::max();
108 const int kMinInt32
= std::numeric_limits
<int>::min();
109 const uint64_t kMaxUint64
= std::numeric_limits
<uint64_t>::max();
110 const int64_t kMaxInt64
= std::numeric_limits
<int64_t>::max();
111 const int64_t kMinInt64
= std::numeric_limits
<int64_t>::min();
113 const size_t kMaxSizet
= std::numeric_limits
<size_t>::max();
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
127 /* implicit */ Mutex(bool adaptive
= kDefaultToAdaptiveMutex
)
149 // this will assert if the mutex is not locked
150 // it does NOT verify that mutex is held by a calling thread
157 // Mutex is move only with lock ownership transfer
158 Mutex(const Mutex
&) = delete;
159 void operator=(const Mutex
&) = delete;
163 friend class CondVar
;
165 std::mutex
& getLock() {
177 RWMutex() { InitializeSRWLock(&srwLock_
); }
178 // No copying allowed
179 RWMutex(const RWMutex
&) = delete;
180 void operator=(const RWMutex
&) = delete;
182 void ReadLock() { AcquireSRWLockShared(&srwLock_
); }
184 void WriteLock() { AcquireSRWLockExclusive(&srwLock_
); }
186 void ReadUnlock() { ReleaseSRWLockShared(&srwLock_
); }
188 void WriteUnlock() { ReleaseSRWLockExclusive(&srwLock_
); }
199 explicit CondVar(Mutex
* mu
) : mu_(mu
) {
204 bool TimedWait(uint64_t expiration_time
);
208 // Condition var is not copy/move constructible
209 CondVar(const CondVar
&) = delete;
210 CondVar
& operator=(const CondVar
&) = delete;
212 CondVar(CondVar
&&) = delete;
213 CondVar
& operator=(CondVar
&&) = delete;
216 std::condition_variable cv_
;
220 // Wrapper around the platform efficient
221 // or otherwise preferrable implementation
222 using Thread
= WindowsThread
;
224 // OnceInit type helps emulate
225 // Posix semantics with initialization
226 // adopted in the project
232 OnceType(const Init
&) {}
233 OnceType(const OnceType
&) = delete;
234 OnceType
& operator=(const OnceType
&) = delete;
236 std::once_flag flag_
;
239 #define LEVELDB_ONCE_INIT port::OnceType::Init()
240 extern void InitOnce(OnceType
* once
, void (*initializer
)());
242 #ifndef CACHE_LINE_SIZE
243 #define CACHE_LINE_SIZE 64U
246 #ifdef ROCKSDB_JEMALLOC
247 // Separate inlines so they can be replaced if needed
248 void* jemalloc_aligned_alloc(size_t size
, size_t alignment
) ROCKSDB_NOEXCEPT
;
249 void jemalloc_aligned_free(void* p
) ROCKSDB_NOEXCEPT
;
252 inline void *cacheline_aligned_alloc(size_t size
) {
253 #ifdef ROCKSDB_JEMALLOC
254 return jemalloc_aligned_alloc(size
, CACHE_LINE_SIZE
);
256 return _aligned_malloc(size
, CACHE_LINE_SIZE
);
260 inline void cacheline_aligned_free(void *memblock
) {
261 #ifdef ROCKSDB_JEMALLOC
262 jemalloc_aligned_free(memblock
);
264 _aligned_free(memblock
);
268 extern const size_t kPageSize
;
270 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52991 for MINGW32
271 // could not be worked around with by -mno-ms-bitfields
273 #define ALIGN_AS(n) __declspec(align(n))
278 static inline void AsmVolatilePause() {
279 #if defined(_M_IX86) || defined(_M_X64)
282 // it would be nice to get "wfe" on ARM here
285 extern int PhysicalCoreID();
287 // For Thread Local Storage abstraction
288 typedef DWORD pthread_key_t
;
290 inline int pthread_key_create(pthread_key_t
* key
, void (*destructor
)(void*)) {
294 pthread_key_t k
= TlsAlloc();
295 if (TLS_OUT_OF_INDEXES
== k
) {
303 inline int pthread_key_delete(pthread_key_t key
) {
310 inline int pthread_setspecific(pthread_key_t key
, const void* value
) {
311 if (!TlsSetValue(key
, const_cast<void*>(value
))) {
317 inline void* pthread_getspecific(pthread_key_t key
) {
318 void* result
= TlsGetValue(key
);
320 if (GetLastError() != ERROR_SUCCESS
) {
329 // UNIX equiv although errno numbers will be off
330 // using C-runtime to implement. Note, this does not
331 // feel space with zeros in case the file is extended.
332 int truncate(const char* path
, int64_t length
);
333 int Truncate(std::string path
, int64_t length
);
334 void Crash(const std::string
& srcfile
, int srcline
);
335 extern int GetMaxOpenFiles();
336 std::string
utf16_to_utf8(const std::wstring
& utf16
);
337 std::wstring
utf8_to_utf16(const std::string
& utf8
);
342 #ifdef ROCKSDB_WINDOWS_UTF8_FILENAMES
344 #define RX_FILESTRING std::wstring
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)
347 #define RX_FNLEN(a) ::wcslen(a)
349 #define RX_DeleteFile DeleteFileW
350 #define RX_CreateFile CreateFileW
351 #define RX_CreateFileMapping CreateFileMappingW
352 #define RX_GetFileAttributesEx GetFileAttributesExW
353 #define RX_FindFirstFileEx FindFirstFileExW
354 #define RX_FindNextFile FindNextFileW
355 #define RX_WIN32_FIND_DATA WIN32_FIND_DATAW
356 #define RX_CreateDirectory CreateDirectoryW
357 #define RX_RemoveDirectory RemoveDirectoryW
358 #define RX_GetFileAttributesEx GetFileAttributesExW
359 #define RX_MoveFileEx MoveFileExW
360 #define RX_CreateHardLink CreateHardLinkW
361 #define RX_PathIsRelative PathIsRelativeW
362 #define RX_GetCurrentDirectory GetCurrentDirectoryW
363 #define RX_GetDiskFreeSpaceEx GetDiskFreeSpaceExW
367 #define RX_FILESTRING std::string
369 #define FN_TO_RX(a) a
370 #define RX_FNLEN(a) strlen(a)
372 #define RX_DeleteFile DeleteFileA
373 #define RX_CreateFile CreateFileA
374 #define RX_CreateFileMapping CreateFileMappingA
375 #define RX_GetFileAttributesEx GetFileAttributesExA
376 #define RX_FindFirstFileEx FindFirstFileExA
377 #define RX_CreateDirectory CreateDirectoryA
378 #define RX_FindNextFile FindNextFileA
379 #define RX_WIN32_FIND_DATA WIN32_FIND_DATA
380 #define RX_CreateDirectory CreateDirectoryA
381 #define RX_RemoveDirectory RemoveDirectoryA
382 #define RX_GetFileAttributesEx GetFileAttributesExA
383 #define RX_MoveFileEx MoveFileExA
384 #define RX_CreateHardLink CreateHardLinkA
385 #define RX_PathIsRelative PathIsRelativeA
386 #define RX_GetCurrentDirectory GetCurrentDirectoryA
387 #define RX_GetDiskFreeSpaceEx GetDiskFreeSpaceExA
391 using port::pthread_key_t
;
392 using port::pthread_key_create
;
393 using port::pthread_key_delete
;
394 using port::pthread_setspecific
;
395 using port::pthread_getspecific
;
396 using port::truncate
;
398 } // namespace ROCKSDB_NAMESPACE