Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 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: |
Nicolas "Pixel" Noble | b092916 | 2015-04-07 01:17:14 +0200 | [diff] [blame^] | 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() { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 63 | if (!locked) gpr_mu_lock(&mu_.mu_); |
| 64 | locked = true; |
| 65 | } |
Nicolas "Pixel" Noble | b092916 | 2015-04-07 01:17:14 +0200 | [diff] [blame^] | 66 | void unlock_internal() { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 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: |
Nicolas "Pixel" Noble | b092916 | 2015-04-07 01:17:14 +0200 | [diff] [blame^] | 79 | unique_lock(mutex &mu) : lock_guard(mu) { } |
| 80 | void lock() { lock_internal(); } |
| 81 | void unlock() { unlock_internal(); } |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 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); |
| 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 |