Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
jtattermusch | 98bffb7 | 2014-12-09 12:47:19 -0800 | [diff] [blame] | 19 | #include <grpc/support/port_platform.h> |
| 20 | |
| 21 | #ifdef GPR_POSIX_SYNC |
| 22 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <grpc/support/log.h> |
| 25 | #include <grpc/support/sync.h> |
| 26 | #include <grpc/support/time.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 27 | #include <time.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 28 | #include "src/core/lib/profiling/timers.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 29 | |
Craig Tiller | f3dec9c | 2017-02-21 10:02:31 -0800 | [diff] [blame] | 30 | #ifdef GPR_LOW_LEVEL_COUNTERS |
| 31 | gpr_atm gpr_mu_locks = 0; |
Craig Tiller | 7f4d30a | 2017-02-21 10:24:00 -0800 | [diff] [blame] | 32 | gpr_atm gpr_counter_atm_cas = 0; |
| 33 | gpr_atm gpr_counter_atm_add = 0; |
Craig Tiller | b038beb | 2017-02-07 08:28:51 -0800 | [diff] [blame] | 34 | #endif |
| 35 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 36 | 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] | 37 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 38 | 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] | 39 | |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 40 | void gpr_mu_lock(gpr_mu* mu) { |
Craig Tiller | f3dec9c | 2017-02-21 10:02:31 -0800 | [diff] [blame] | 41 | #ifdef GPR_LOW_LEVEL_COUNTERS |
| 42 | GPR_ATM_INC_COUNTER(gpr_mu_locks); |
Craig Tiller | b038beb | 2017-02-07 08:28:51 -0800 | [diff] [blame] | 43 | #endif |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 44 | GPR_TIMER_BEGIN("gpr_mu_lock", 0); |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 45 | GPR_ASSERT(pthread_mutex_lock(mu) == 0); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 46 | GPR_TIMER_END("gpr_mu_lock", 0); |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 47 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 49 | void gpr_mu_unlock(gpr_mu* mu) { |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 50 | GPR_TIMER_BEGIN("gpr_mu_unlock", 0); |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 51 | GPR_ASSERT(pthread_mutex_unlock(mu) == 0); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 52 | GPR_TIMER_END("gpr_mu_unlock", 0); |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 53 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 55 | int gpr_mu_trylock(gpr_mu* mu) { |
Craig Tiller | 5925603 | 2015-11-02 14:19:15 -0800 | [diff] [blame] | 56 | int err; |
| 57 | GPR_TIMER_BEGIN("gpr_mu_trylock", 0); |
| 58 | err = pthread_mutex_trylock(mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 59 | GPR_ASSERT(err == 0 || err == EBUSY); |
Craig Tiller | 5925603 | 2015-11-02 14:19:15 -0800 | [diff] [blame] | 60 | GPR_TIMER_END("gpr_mu_trylock", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | return err == 0; |
| 62 | } |
| 63 | |
| 64 | /*----------------------------------------*/ |
| 65 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 66 | 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] | 67 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | 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] | 69 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 70 | 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] | 71 | int err = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) == |
| 73 | 0) { |
| 74 | err = pthread_cond_wait(cv, mu); |
| 75 | } else { |
| 76 | struct timespec abs_deadline_ts; |
| 77 | abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME); |
yang-g | 471dbd8 | 2016-01-04 15:25:37 -0800 | [diff] [blame] | 78 | abs_deadline_ts.tv_sec = (time_t)abs_deadline.tv_sec; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 79 | abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec; |
| 80 | err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts); |
| 81 | } |
| 82 | GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | return err == ETIMEDOUT; |
| 84 | } |
| 85 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | 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] | 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | void gpr_cv_broadcast(gpr_cv* cv) { |
| 89 | GPR_ASSERT(pthread_cond_broadcast(cv) == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /*----------------------------------------*/ |
| 93 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 94 | void gpr_once_init(gpr_once* once, void (*init_function)(void)) { |
| 95 | GPR_ASSERT(pthread_once(once, init_function) == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 96 | } |
jtattermusch | 98bffb7 | 2014-12-09 12:47:19 -0800 | [diff] [blame] | 97 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 98 | #endif /* GRP_POSIX_SYNC */ |