Use MakeUnique<Db>(...)
[bitcoinplatinum.git] / src / leveldb / port / port_win.cc
blob1be9e8d5b0b41ea458b42bb17f2e3a18eefac315
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.
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.
31 #include "port/port_win.h"
33 #include <windows.h>
34 #include <cassert>
35 #include <intrin.h>
37 namespace leveldb {
38 namespace port {
40 Mutex::Mutex() :
41 cs_(NULL) {
42 assert(!cs_);
43 cs_ = static_cast<void *>(new CRITICAL_SECTION());
44 ::InitializeCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
45 assert(cs_);
48 Mutex::~Mutex() {
49 assert(cs_);
50 ::DeleteCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
51 delete static_cast<CRITICAL_SECTION *>(cs_);
52 cs_ = NULL;
53 assert(!cs_);
56 void Mutex::Lock() {
57 assert(cs_);
58 ::EnterCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
61 void Mutex::Unlock() {
62 assert(cs_);
63 ::LeaveCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
66 void Mutex::AssertHeld() {
67 assert(cs_);
68 assert(1);
71 CondVar::CondVar(Mutex* mu) :
72 waiting_(0),
73 mu_(mu),
74 sem1_(::CreateSemaphore(NULL, 0, 10000, NULL)),
75 sem2_(::CreateSemaphore(NULL, 0, 10000, NULL)) {
76 assert(mu_);
79 CondVar::~CondVar() {
80 ::CloseHandle(sem1_);
81 ::CloseHandle(sem2_);
84 void CondVar::Wait() {
85 mu_->AssertHeld();
87 wait_mtx_.Lock();
88 ++waiting_;
89 wait_mtx_.Unlock();
91 mu_->Unlock();
93 // initiate handshake
94 ::WaitForSingleObject(sem1_, INFINITE);
95 ::ReleaseSemaphore(sem2_, 1, NULL);
96 mu_->Lock();
99 void CondVar::Signal() {
100 wait_mtx_.Lock();
101 if (waiting_ > 0) {
102 --waiting_;
104 // finalize handshake
105 ::ReleaseSemaphore(sem1_, 1, NULL);
106 ::WaitForSingleObject(sem2_, INFINITE);
108 wait_mtx_.Unlock();
111 void CondVar::SignalAll() {
112 wait_mtx_.Lock();
113 ::ReleaseSemaphore(sem1_, waiting_, NULL);
114 while(waiting_ > 0) {
115 --waiting_;
116 ::WaitForSingleObject(sem2_, INFINITE);
118 wait_mtx_.Unlock();
121 AtomicPointer::AtomicPointer(void* v) {
122 Release_Store(v);
125 void InitOnce(OnceType* once, void (*initializer)()) {
126 once->InitOnce(initializer);
129 void* AtomicPointer::Acquire_Load() const {
130 void * p = NULL;
131 InterlockedExchangePointer(&p, rep_);
132 return p;
135 void AtomicPointer::Release_Store(void* v) {
136 InterlockedExchangePointer(&rep_, v);
139 void* AtomicPointer::NoBarrier_Load() const {
140 return rep_;
143 void AtomicPointer::NoBarrier_Store(void* v) {
144 rep_ = v;
147 bool HasAcceleratedCRC32C() {
148 #if defined(__x86_64__) || defined(__i386__)
149 int cpu_info[4];
150 __cpuid(cpu_info, 1);
151 return (cpu_info[2] & (1 << 20)) != 0;
152 #else
153 return false;
154 #endif