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.
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:
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.
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"
43 cs_
= static_cast<void *>(new CRITICAL_SECTION());
44 ::InitializeCriticalSection(static_cast<CRITICAL_SECTION
*>(cs_
));
50 ::DeleteCriticalSection(static_cast<CRITICAL_SECTION
*>(cs_
));
51 delete static_cast<CRITICAL_SECTION
*>(cs_
);
58 ::EnterCriticalSection(static_cast<CRITICAL_SECTION
*>(cs_
));
61 void Mutex::Unlock() {
63 ::LeaveCriticalSection(static_cast<CRITICAL_SECTION
*>(cs_
));
66 void Mutex::AssertHeld() {
71 CondVar::CondVar(Mutex
* mu
) :
74 sem1_(::CreateSemaphore(NULL
, 0, 10000, NULL
)),
75 sem2_(::CreateSemaphore(NULL
, 0, 10000, NULL
)) {
84 void CondVar::Wait() {
94 ::WaitForSingleObject(sem1_
, INFINITE
);
95 ::ReleaseSemaphore(sem2_
, 1, NULL
);
99 void CondVar::Signal() {
104 // finalize handshake
105 ::ReleaseSemaphore(sem1_
, 1, NULL
);
106 ::WaitForSingleObject(sem2_
, INFINITE
);
111 void CondVar::SignalAll() {
113 ::ReleaseSemaphore(sem1_
, waiting_
, NULL
);
114 while(waiting_
> 0) {
116 ::WaitForSingleObject(sem2_
, INFINITE
);
121 AtomicPointer::AtomicPointer(void* v
) {
125 void InitOnce(OnceType
* once
, void (*initializer
)()) {
126 once
->InitOnce(initializer
);
129 void* AtomicPointer::Acquire_Load() const {
131 InterlockedExchangePointer(&p
, rep_
);
135 void AtomicPointer::Release_Store(void* v
) {
136 InterlockedExchangePointer(&rep_
, v
);
139 void* AtomicPointer::NoBarrier_Load() const {
143 void AtomicPointer::NoBarrier_Store(void* v
) {
147 bool HasAcceleratedCRC32C() {
148 #if defined(__x86_64__) || defined(__i386__)
150 __cpuid(cpu_info
, 1);
151 return (cpu_info
[2] & (1 << 20)) != 0;