Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
jtattermusch | 98bffb7 | 2014-12-09 12:47:19 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_POSIX_SYNC |
| 37 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <errno.h> |
| 39 | #include <grpc/support/log.h> |
| 40 | #include <grpc/support/sync.h> |
| 41 | #include <grpc/support/time.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 42 | #include <time.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 43 | #include "src/core/lib/profiling/timers.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | |
Craig Tiller | f3dec9c | 2017-02-21 10:02:31 -0800 | [diff] [blame^] | 45 | #ifdef GPR_LOW_LEVEL_COUNTERS |
| 46 | gpr_atm gpr_mu_locks = 0; |
| 47 | gpr_atm gpr_counter_rmw = 0; |
Craig Tiller | b038beb | 2017-02-07 08:28:51 -0800 | [diff] [blame] | 48 | #endif |
| 49 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 50 | void gpr_mu_init(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_init(mu, NULL) == 0); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 52 | void gpr_mu_destroy(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_destroy(mu) == 0); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 54 | void gpr_mu_lock(gpr_mu* mu) { |
Craig Tiller | f3dec9c | 2017-02-21 10:02:31 -0800 | [diff] [blame^] | 55 | #ifdef GPR_LOW_LEVEL_COUNTERS |
| 56 | GPR_ATM_INC_COUNTER(gpr_mu_locks); |
Craig Tiller | b038beb | 2017-02-07 08:28:51 -0800 | [diff] [blame] | 57 | #endif |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 58 | GPR_TIMER_BEGIN("gpr_mu_lock", 0); |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 59 | GPR_ASSERT(pthread_mutex_lock(mu) == 0); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 60 | GPR_TIMER_END("gpr_mu_lock", 0); |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 61 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 63 | void gpr_mu_unlock(gpr_mu* mu) { |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 64 | GPR_TIMER_BEGIN("gpr_mu_unlock", 0); |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 65 | GPR_ASSERT(pthread_mutex_unlock(mu) == 0); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 66 | GPR_TIMER_END("gpr_mu_unlock", 0); |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 67 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 69 | int gpr_mu_trylock(gpr_mu* mu) { |
Craig Tiller | 5925603 | 2015-11-02 14:19:15 -0800 | [diff] [blame] | 70 | int err; |
| 71 | GPR_TIMER_BEGIN("gpr_mu_trylock", 0); |
| 72 | err = pthread_mutex_trylock(mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 73 | GPR_ASSERT(err == 0 || err == EBUSY); |
Craig Tiller | 5925603 | 2015-11-02 14:19:15 -0800 | [diff] [blame] | 74 | GPR_TIMER_END("gpr_mu_trylock", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | return err == 0; |
| 76 | } |
| 77 | |
| 78 | /*----------------------------------------*/ |
| 79 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | void gpr_cv_init(gpr_cv* cv) { GPR_ASSERT(pthread_cond_init(cv, NULL) == 0); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | void gpr_cv_destroy(gpr_cv* cv) { GPR_ASSERT(pthread_cond_destroy(cv) == 0); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 84 | int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 85 | int err = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) == |
| 87 | 0) { |
| 88 | err = pthread_cond_wait(cv, mu); |
| 89 | } else { |
| 90 | struct timespec abs_deadline_ts; |
| 91 | abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME); |
yang-g | 471dbd8 | 2016-01-04 15:25:37 -0800 | [diff] [blame] | 92 | abs_deadline_ts.tv_sec = (time_t)abs_deadline.tv_sec; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec; |
| 94 | err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts); |
| 95 | } |
| 96 | GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 97 | return err == ETIMEDOUT; |
| 98 | } |
| 99 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | void gpr_cv_signal(gpr_cv* cv) { GPR_ASSERT(pthread_cond_signal(cv) == 0); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | void gpr_cv_broadcast(gpr_cv* cv) { |
| 103 | GPR_ASSERT(pthread_cond_broadcast(cv) == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /*----------------------------------------*/ |
| 107 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | void gpr_once_init(gpr_once* once, void (*init_function)(void)) { |
| 109 | GPR_ASSERT(pthread_once(once, init_function) == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | } |
jtattermusch | 98bffb7 | 2014-12-09 12:47:19 -0800 | [diff] [blame] | 111 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 112 | #endif /* GRP_POSIX_SYNC */ |