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"
42 cs_ = static_cast<void *>(new CRITICAL_SECTION());
43 ::InitializeCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
49 ::DeleteCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
50 delete static_cast<CRITICAL_SECTION *>(cs_);
57 ::EnterCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
60 void Mutex::Unlock() {
62 ::LeaveCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
65 void Mutex::AssertHeld() {
70 CondVar::CondVar(Mutex* mu) :
73 sem1_(::CreateSemaphore(NULL, 0, 10000, NULL)),
74 sem2_(::CreateSemaphore(NULL, 0, 10000, NULL)) {
83 void CondVar::Wait() {
93 ::WaitForSingleObject(sem1_, INFINITE);
94 ::ReleaseSemaphore(sem2_, 1, NULL);
98 void CondVar::Signal() {
103 // finalize handshake
104 ::ReleaseSemaphore(sem1_, 1, NULL);
105 ::WaitForSingleObject(sem2_, INFINITE);
110 void CondVar::SignalAll() {
112 ::ReleaseSemaphore(sem1_, waiting_, NULL);
113 while(waiting_ > 0) {
115 ::WaitForSingleObject(sem2_, INFINITE);
120 AtomicPointer::AtomicPointer(void* v) {
124 void InitOnce(OnceType* once, void (*initializer)()) {
125 once->InitOnce(initializer);
128 void* AtomicPointer::Acquire_Load() const {
130 InterlockedExchangePointer(&p, rep_);
134 void AtomicPointer::Release_Store(void* v) {
135 InterlockedExchangePointer(&rep_, v);
138 void* AtomicPointer::NoBarrier_Load() const {
142 void AtomicPointer::NoBarrier_Store(void* v) {