Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
port_win.cc
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 #include "port/port_win.h"
32 
33 #include <windows.h>
34 #include <cassert>
35 
36 namespace leveldb {
37 namespace port {
38 
39 Mutex::Mutex() :
40  cs_(NULL) {
41  assert(!cs_);
42  cs_ = static_cast<void *>(new CRITICAL_SECTION());
43  ::InitializeCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
44  assert(cs_);
45 }
46 
47 Mutex::~Mutex() {
48  assert(cs_);
49  ::DeleteCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
50  delete static_cast<CRITICAL_SECTION *>(cs_);
51  cs_ = NULL;
52  assert(!cs_);
53 }
54 
55 void Mutex::Lock() {
56  assert(cs_);
57  ::EnterCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
58 }
59 
60 void Mutex::Unlock() {
61  assert(cs_);
62  ::LeaveCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
63 }
64 
65 void Mutex::AssertHeld() {
66  assert(cs_);
67  assert(1);
68 }
69 
70 CondVar::CondVar(Mutex* mu) :
71  waiting_(0),
72  mu_(mu),
73  sem1_(::CreateSemaphore(NULL, 0, 10000, NULL)),
74  sem2_(::CreateSemaphore(NULL, 0, 10000, NULL)) {
75  assert(mu_);
76 }
77 
79  ::CloseHandle(sem1_);
80  ::CloseHandle(sem2_);
81 }
82 
83 void CondVar::Wait() {
84  mu_->AssertHeld();
85 
86  wait_mtx_.Lock();
87  ++waiting_;
88  wait_mtx_.Unlock();
89 
90  mu_->Unlock();
91 
92  // initiate handshake
93  ::WaitForSingleObject(sem1_, INFINITE);
94  ::ReleaseSemaphore(sem2_, 1, NULL);
95  mu_->Lock();
96 }
97 
98 void CondVar::Signal() {
99  wait_mtx_.Lock();
100  if (waiting_ > 0) {
101  --waiting_;
102 
103  // finalize handshake
104  ::ReleaseSemaphore(sem1_, 1, NULL);
105  ::WaitForSingleObject(sem2_, INFINITE);
106  }
107  wait_mtx_.Unlock();
108 }
109 
110 void CondVar::SignalAll() {
111  wait_mtx_.Lock();
112  ::ReleaseSemaphore(sem1_, waiting_, NULL);
113  while(waiting_ > 0) {
114  --waiting_;
115  ::WaitForSingleObject(sem2_, INFINITE);
116  }
117  wait_mtx_.Unlock();
118 }
119 
121  Release_Store(v);
122 }
123 
124 void InitOnce(OnceType* once, void (*initializer)()) {
125  once->InitOnce(initializer);
126 }
127 
129  void * p = NULL;
130  InterlockedExchangePointer(&p, rep_);
131  return p;
132 }
133 
135  InterlockedExchangePointer(&rep_, v);
136 }
137 
139  return rep_;
140 }
141 
143  rep_ = v;
144 }
145 
146 }
147 }
pthread_mutex_t mu_
Definition: port_posix.h:100
void * Acquire_Load() const
Definition: port_win.cc:128
void NoBarrier_Store(void *v)
Definition: port_win.cc:142
void * NoBarrier_Load() const
Definition: port_win.cc:138
void Release_Store(void *v)
Definition: port_win.cc:134
port::Mutex mu_
Definition: db_test.cc:31
void InitOnce(port::OnceType *, void(*initializer)())
Definition: port_posix.cc:49
port::Mutex mu
Definition: db_bench.cc:270
intptr_t OnceType
Definition: port_example.h:69