GRPC C++ 0.10.0.0

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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 private:
50 ::gpr_mu mu_;
51 template <class mutex>
52 friend class lock_guard;
53 friend class condition_variable;
54 };
55
56 template <class mutex>
57 class lock_guard {
58 public:
59 lock_guard(mutex &mu) : mu_(mu), locked(true) { gpr_mu_lock(&mu.mu_); }
60 ~lock_guard() { unlock_internal(); }
61 protected:
62 void lock_internal() {
63 if (!locked) gpr_mu_lock(&mu_.mu_);
64 locked = true;
65 }
66 void unlock_internal() {
67 if (locked) gpr_mu_unlock(&mu_.mu_);
68 locked = false;
69 }
70 private:
71 mutex &mu_;
72 bool locked;
73 friend class condition_variable;
74 };
75
76 template <class mutex>
77 class unique_lock : public lock_guard<mutex> {
78 public:
79 unique_lock(mutex &mu) : lock_guard<mutex>(mu) { }
80 void lock() { this->lock_internal(); }
81 void unlock() { this->unlock_internal(); }
82 };
83
84 class condition_variable {
85 public:
86 condition_variable() { gpr_cv_init(&cv_); }
87 ~condition_variable() { gpr_cv_destroy(&cv_); }
88 void wait(lock_guard<mutex> &mu) {
89 mu.locked = false;
90 gpr_cv_wait(&cv_, &mu.mu_.mu_, gpr_inf_future(GPR_CLOCK_REALTIME));
91 mu.locked = true;
92 }
93 void notify_one() { gpr_cv_signal(&cv_); }
94 void notify_all() { gpr_cv_broadcast(&cv_); }
95 private:
96 gpr_cv cv_;
97 };
98
99 } // namespace grpc
100
101 #endif // GRPCXX_IMPL_SYNC_NO_CXX11_H
grpc::mutex
Definition: sync_no_cxx11.h:45
grpc::lock_guard::unlock_internal
void unlock_internal()
Definition: sync_no_cxx11.h:66
grpc::lock_guard::lock_internal
void lock_internal()
Definition: sync_no_cxx11.h:62
grpc::condition_variable
Definition: sync_no_cxx11.h:84
grpc::condition_variable::~condition_variable
~condition_variable()
Definition: sync_no_cxx11.h:87
grpc::lock_guard::~lock_guard
~lock_guard()
Definition: sync_no_cxx11.h:60
grpc::condition_variable::notify_all
void notify_all()
Definition: sync_no_cxx11.h:94
grpc::mutex::mutex
mutex()
Definition: sync_no_cxx11.h:47
grpc::condition_variable::notify_one
void notify_one()
Definition: sync_no_cxx11.h:93
grpc::unique_lock::unlock
void unlock()
Definition: sync_no_cxx11.h:81
grpc::lock_guard::lock_guard
lock_guard(mutex &mu)
Definition: sync_no_cxx11.h:59
grpc::unique_lock::lock
void lock()
Definition: sync_no_cxx11.h:80
grpc::mutex::~mutex
~mutex()
Definition: sync_no_cxx11.h:48
grpc::lock_guard
Definition: sync_no_cxx11.h:42
grpc::condition_variable::condition_variable
condition_variable()
Definition: sync_no_cxx11.h:86
grpc::unique_lock
Definition: sync_no_cxx11.h:77
grpc::unique_lock::unique_lock
unique_lock(mutex &mu)
Definition: sync_no_cxx11.h:79
grpc::condition_variable::wait
void wait(lock_guard< mutex > &mu)
Definition: sync_no_cxx11.h:88