Bitcoin
port_win.h
Go to the documentation of this file.
1 // LevelDB Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 //
5 // See port_example.h for documentation for the following types/functions.
6 
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 // * Neither the name of the University of California, Berkeley nor the
16 // names of its contributors may be used to endorse or promote products
17 // derived from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
23 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 
31 #ifndef STORAGE_LEVELDB_PORT_PORT_WIN_H_
32 #define STORAGE_LEVELDB_PORT_PORT_WIN_H_
33 
34 #ifdef _MSC_VER
35 #if !(_MSC_VER >= 1900)
36 #define snprintf _snprintf
37 #endif
38 #define close _close
39 #define fread_unlocked _fread_nolock
40 #ifdef _WIN64
41 #define ssize_t int64_t
42 #else
43 #define ssize_t int32_t
44 #endif
45 #endif
46 
47 #include <string>
48 #include <stdint.h>
49 #ifdef SNAPPY
50 #include <snappy.h>
51 #endif
52 
53 namespace leveldb {
54 namespace port {
55 
56 // Windows is little endian (for now :p)
57 static const bool kLittleEndian = true;
58 
59 class CondVar;
60 
61 class Mutex {
62  public:
63  Mutex();
64  ~Mutex();
65 
66  void Lock();
67  void Unlock();
68  void AssertHeld();
69 
70  private:
71  friend class CondVar;
72  // critical sections are more efficient than mutexes
73  // but they are not recursive and can only be used to synchronize threads within the same process
74  // we use opaque void * to avoid including windows.h in port_win.h
75  void * cs_;
76 
77  // No copying
78  Mutex(const Mutex&);
79  void operator=(const Mutex&);
80 };
81 
82 // the Win32 API offers a dependable condition variable mechanism, but only starting with
83 // Windows 2008 and Vista
84 // no matter what we will implement our own condition variable with a semaphore
85 // implementation as described in a paper written by Andrew D. Birrell in 2003
86 class CondVar {
87  public:
88  explicit CondVar(Mutex* mu);
89  ~CondVar();
90  void Wait();
91  void Signal();
92  void SignalAll();
93  private:
94  Mutex* mu_;
95 
97  long waiting_;
98 
99  void * sem1_;
100  void * sem2_;
101 
102 
103 };
104 
105 class OnceType {
106 public:
107 // OnceType() : init_(false) {}
109  OnceType(bool f) : init_(f) {}
110  void InitOnce(void (*initializer)()) {
111  mutex_.Lock();
112  if (!init_) {
113  init_ = true;
114  initializer();
115  }
116  mutex_.Unlock();
117  }
118 
119 private:
120  bool init_;
122 };
123 
124 #define LEVELDB_ONCE_INIT false
125 extern void InitOnce(port::OnceType*, void (*initializer)());
126 
127 // Storage for a lock-free pointer
128 class AtomicPointer {
129  private:
130  void * rep_;
131  public:
132  AtomicPointer() : rep_(NULL) { }
133  explicit AtomicPointer(void* v);
134  void* Acquire_Load() const;
135 
136  void Release_Store(void* v);
137 
138  void* NoBarrier_Load() const;
139 
140  void NoBarrier_Store(void* v);
141 };
142 
143 inline bool Snappy_Compress(const char* input, size_t length,
144  ::std::string* output) {
145 #ifdef SNAPPY
146  output->resize(snappy::MaxCompressedLength(length));
147  size_t outlen;
148  snappy::RawCompress(input, length, &(*output)[0], &outlen);
149  output->resize(outlen);
150  return true;
151 #endif
152 
153  return false;
154 }
155 
156 inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
157  size_t* result) {
158 #ifdef SNAPPY
159  return snappy::GetUncompressedLength(input, length, result);
160 #else
161  return false;
162 #endif
163 }
164 
165 inline bool Snappy_Uncompress(const char* input, size_t length,
166  char* output) {
167 #ifdef SNAPPY
168  return snappy::RawUncompress(input, length, output);
169 #else
170  return false;
171 #endif
172 }
173 
174 inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
175  return false;
176 }
177 
178 bool HasAcceleratedCRC32C();
179 uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size);
180 
181 }
182 }
183 
184 #endif // STORAGE_LEVELDB_PORT_PORT_WIN_H_
AtomicPointer()
Definition: port_win.h:132
Definition: autocompact_test.cc:11
void AssertHeld()
Definition: port_win.cc:66
bool Snappy_Uncompress(const char *input_data, size_t input_length, char *output)
Definition: port_posix.h:142
CondVar(Mutex *mu)
Definition: port_posix.cc:33
void Unlock()
Definition: port_posix.cc:31
OnceType(const OnceType &once)
Definition: port_win.h:108
static port::OnceType once
Definition: comparator.cc:69
void * rep_
Definition: port_win.h:130
Mutex wait_mtx_
Definition: port_win.h:96
~Mutex()
Definition: port_posix.cc:27
Mutex()
Definition: port_posix.cc:25
long waiting_
Definition: port_win.h:97
f
Definition: linearize-data.py:263
bool Snappy_GetUncompressedLength(const char *input, size_t length, size_t *result)
Definition: port_posix.h:133
Definition: port_win.h:105
bool HasAcceleratedCRC32C()
Definition: port_posix.cc:56
void Signal()
Definition: port_posix.cc:44
void NoBarrier_Store(void *v)
Definition: port_win.cc:143
void Wait()
Definition: port_posix.cc:40
Mutex mutex_
Definition: port_win.h:121
void Release_Store(void *v)
Definition: port_win.cc:135
static const bool kLittleEndian
Definition: port_example.h:21
void Lock()
Definition: port_posix.cc:29
unsigned int uint32_t
Definition: stdint.h:21
bool Snappy_Compress(const char *input, size_t input_length, std::string *output)
void InitOnce(void(*initializer)())
Definition: port_win.h:110
void InitOnce(port::OnceType *, void(*initializer)())
Definition: port_posix.cc:52
bool GetHeapProfile(void(*func)(void *, const char *, int), void *arg)
Definition: port_posix.h:151
OnceType(bool f)
Definition: port_win.h:109
void * Acquire_Load() const
Definition: port_win.cc:129
void * cs_
Definition: port_win.h:75
bool init_
Definition: port_win.h:120
uint32_t AcceleratedCRC32C(uint32_t crc, const char *buf, size_t size)
Definition: port_posix_sse.cc:54
intptr_t OnceType
Definition: port_example.h:69
void SignalAll()
Definition: port_posix.cc:48
void * NoBarrier_Load() const
Definition: port_win.cc:139
void * sem2_
Definition: port_win.h:100
Mutex * mu_
Definition: port_posix.h:113
void * sem1_
Definition: port_win.h:99
Definition: port_example.h:45
~CondVar()
Definition: port_posix.cc:38
void operator=(const Mutex &)
int port
Definition: zmq_sub.py:37
Definition: port_example.h:26
intptr_t rep_
Definition: port_example.h:77