]>
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
19 // Assume that for everywhere
20 #undef PLATFORM_IS_LITTLE_ENDIAN
21 #define PLATFORM_IS_LITTLE_ENDIAN true
28 #include <condition_variable>
34 #include "port/win/win_thread.h"
36 #include "rocksdb/options.h"
45 #define strcasecmp _stricmp
51 #ifndef _SSIZE_T_DEFINED
52 typedef SSIZE_T ssize_t
;
55 // size_t printf formatting named in the manner of C99 standard formatting
56 // strings such as PRIu64
57 // in fact, we could use that one
58 #ifndef ROCKSDB_PRIszt
59 #define ROCKSDB_PRIszt "Iu"
63 #define __attribute__(A)
65 // Thread local storage on Linux
66 // There is thread_local in C++11
68 #define __thread __declspec(thread)
73 #ifndef PLATFORM_IS_LITTLE_ENDIAN
74 #define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
79 #define PREFETCH(addr, rw, locality)
84 #if defined(_MSC_VER) && (_MSC_VER < 1900)
87 #define snprintf _snprintf
89 #define ROCKSDB_NOEXCEPT
90 // std::numeric_limits<size_t>::max() is not constexpr just yet
91 // therefore, use the same limits
93 // For use at db/file_indexer.h kLevelMaxIndex
94 const uint32_t kMaxUint32
= UINT32_MAX
;
95 const int kMaxInt32
= INT32_MAX
;
96 const int64_t kMaxInt64
= INT64_MAX
;
97 const uint64_t kMaxUint64
= UINT64_MAX
;
100 const size_t kMaxSizet
= UINT64_MAX
;
102 const size_t kMaxSizet
= UINT_MAX
;
105 #else // VS >= 2015 or MinGW
107 #define ROCKSDB_NOEXCEPT noexcept
109 // For use at db/file_indexer.h kLevelMaxIndex
110 const uint32_t kMaxUint32
= std::numeric_limits
<uint32_t>::max();
111 const int kMaxInt32
= std::numeric_limits
<int>::max();
112 const uint64_t kMaxUint64
= std::numeric_limits
<uint64_t>::max();
113 const int64_t kMaxInt64
= std::numeric_limits
<int64_t>::max();
115 const size_t kMaxSizet
= std::numeric_limits
<size_t>::max();
119 const bool kLittleEndian
= true;
126 /* implicit */ Mutex(bool adaptive
= false)
148 // this will assert if the mutex is not locked
149 // it does NOT verify that mutex is held by a calling thread
156 // Mutex is move only with lock ownership transfer
157 Mutex(const Mutex
&) = delete;
158 void operator=(const Mutex
&) = delete;
162 friend class CondVar
;
164 std::mutex
& getLock() {
176 RWMutex() { InitializeSRWLock(&srwLock_
); }
178 void ReadLock() { AcquireSRWLockShared(&srwLock_
); }
180 void WriteLock() { AcquireSRWLockExclusive(&srwLock_
); }
182 void ReadUnlock() { ReleaseSRWLockShared(&srwLock_
); }
184 void WriteUnlock() { ReleaseSRWLockExclusive(&srwLock_
); }
191 // No copying allowed
192 RWMutex(const RWMutex
&);
193 void operator=(const RWMutex
&);
198 explicit CondVar(Mutex
* mu
) : mu_(mu
) {
203 bool TimedWait(uint64_t expiration_time
);
207 // Condition var is not copy/move constructible
208 CondVar(const CondVar
&) = delete;
209 CondVar
& operator=(const CondVar
&) = delete;
211 CondVar(CondVar
&&) = delete;
212 CondVar
& operator=(CondVar
&&) = delete;
215 std::condition_variable cv_
;
219 // Wrapper around the platform efficient
220 // or otherwise preferrable implementation
221 using Thread
= WindowsThread
;
223 // OnceInit type helps emulate
224 // Posix semantics with initialization
225 // adopted in the project
231 OnceType(const Init
&) {}
232 OnceType(const OnceType
&) = delete;
233 OnceType
& operator=(const OnceType
&) = delete;
235 std::once_flag flag_
;
238 #define LEVELDB_ONCE_INIT port::OnceType::Init()
239 extern void InitOnce(OnceType
* once
, void (*initializer
)());
241 #ifndef CACHE_LINE_SIZE
242 #define CACHE_LINE_SIZE 64U
245 #ifdef ROCKSDB_JEMALLOC
246 // Separate inlines so they can be replaced if needed
247 void* jemalloc_aligned_alloc(size_t size
, size_t alignment
) ROCKSDB_NOEXCEPT
;
248 void jemalloc_aligned_free(void* p
) ROCKSDB_NOEXCEPT
;
251 inline void *cacheline_aligned_alloc(size_t size
) {
252 #ifdef ROCKSDB_JEMALLOC
253 return jemalloc_aligned_alloc(size
, CACHE_LINE_SIZE
);
255 return _aligned_malloc(size
, CACHE_LINE_SIZE
);
259 inline void cacheline_aligned_free(void *memblock
) {
260 #ifdef ROCKSDB_JEMALLOC
261 jemalloc_aligned_free(memblock
);
263 _aligned_free(memblock
);
267 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52991 for MINGW32
268 // could not be worked around with by -mno-ms-bitfields
270 #define ALIGN_AS(n) __declspec(align(n))
275 static inline void AsmVolatilePause() {
276 #if defined(_M_IX86) || defined(_M_X64)
279 // it would be nice to get "wfe" on ARM here
282 extern int PhysicalCoreID();
284 // For Thread Local Storage abstraction
285 typedef DWORD pthread_key_t
;
287 inline int pthread_key_create(pthread_key_t
* key
, void (*destructor
)(void*)) {
291 pthread_key_t k
= TlsAlloc();
292 if (TLS_OUT_OF_INDEXES
== k
) {
300 inline int pthread_key_delete(pthread_key_t key
) {
307 inline int pthread_setspecific(pthread_key_t key
, const void* value
) {
308 if (!TlsSetValue(key
, const_cast<void*>(value
))) {
314 inline void* pthread_getspecific(pthread_key_t key
) {
315 void* result
= TlsGetValue(key
);
317 if (GetLastError() != ERROR_SUCCESS
) {
326 // UNIX equiv although errno numbers will be off
327 // using C-runtime to implement. Note, this does not
328 // feel space with zeros in case the file is extended.
329 int truncate(const char* path
, int64_t length
);
330 void Crash(const std::string
& srcfile
, int srcline
);
331 extern int GetMaxOpenFiles();
335 using port::pthread_key_t
;
336 using port::pthread_key_create
;
337 using port::pthread_key_delete
;
338 using port::pthread_setspecific
;
339 using port::pthread_getspecific
;
340 using port::truncate
;
342 } // namespace rocksdb