GRPC C++  0.11.0.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sync_no_cxx11.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * 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
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_IMPL_SYNC_NO_CXX11_H
35 #define GRPCXX_IMPL_SYNC_NO_CXX11_H
36 
37 #include <grpc/support/sync.h>
38 
39 namespace grpc {
40 
41 template <class mutex>
42 class lock_guard;
43 class condition_variable;
44 
45 class mutex {
46  public:
47  mutex() { gpr_mu_init(&mu_); }
48  ~mutex() { gpr_mu_destroy(&mu_); }
49 
50  private:
51  ::gpr_mu mu_;
52  template <class mutex>
53  friend class lock_guard;
54  friend class condition_variable;
55 };
56 
57 template <class mutex>
58 class lock_guard {
59  public:
60  lock_guard(mutex &mu) : mu_(mu), locked(true) { gpr_mu_lock(&mu.mu_); }
62 
63  protected:
64  void lock_internal() {
65  if (!locked) gpr_mu_lock(&mu_.mu_);
66  locked = true;
67  }
68  void unlock_internal() {
69  if (locked) gpr_mu_unlock(&mu_.mu_);
70  locked = false;
71  }
72 
73  private:
74  mutex &mu_;
75  bool locked;
76  friend class condition_variable;
77 };
78 
79 template <class mutex>
80 class unique_lock : public lock_guard<mutex> {
81  public:
83  void lock() { this->lock_internal(); }
84  void unlock() { this->unlock_internal(); }
85 };
86 
88  public:
89  condition_variable() { gpr_cv_init(&cv_); }
90  ~condition_variable() { gpr_cv_destroy(&cv_); }
91  void wait(lock_guard<mutex> &mu) {
92  mu.locked = false;
93  gpr_cv_wait(&cv_, &mu.mu_.mu_, gpr_inf_future(GPR_CLOCK_REALTIME));
94  mu.locked = true;
95  }
96  void notify_one() { gpr_cv_signal(&cv_); }
97  void notify_all() { gpr_cv_broadcast(&cv_); }
98 
99  private:
100  gpr_cv cv_;
101 };
102 
103 } // namespace grpc
104 
105 #endif // GRPCXX_IMPL_SYNC_NO_CXX11_H
Definition: sync_no_cxx11.h:45
void unlock_internal()
Definition: sync_no_cxx11.h:68
void lock_internal()
Definition: sync_no_cxx11.h:64
Definition: sync_no_cxx11.h:87
~condition_variable()
Definition: sync_no_cxx11.h:90
~lock_guard()
Definition: sync_no_cxx11.h:61
void notify_all()
Definition: sync_no_cxx11.h:97
mutex()
Definition: sync_no_cxx11.h:47
void notify_one()
Definition: sync_no_cxx11.h:96
void unlock()
Definition: sync_no_cxx11.h:84
lock_guard(mutex &mu)
Definition: sync_no_cxx11.h:60
void lock()
Definition: sync_no_cxx11.h:83
~mutex()
Definition: sync_no_cxx11.h:48
Definition: sync_no_cxx11.h:42
condition_variable()
Definition: sync_no_cxx11.h:89
Definition: sync_no_cxx11.h:80
unique_lock(mutex &mu)
Definition: sync_no_cxx11.h:82
void wait(lock_guard< mutex > &mu)
Definition: sync_no_cxx11.h:91