blob: d306d43cb4f4c007641b0499f659acc44cb834c2 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
jtattermusch98bffb72014-12-09 12:47:19 -080019#include <grpc/support/port_platform.h>
20
21#ifdef GPR_POSIX_SYNC
22
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080023#include <errno.h>
24#include <grpc/support/log.h>
25#include <grpc/support/sync.h>
26#include <grpc/support/time.h>
Craig Tillerf40df232016-03-25 13:38:14 -070027#include <time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070028#include "src/core/lib/profiling/timers.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080029
Craig Tillerf3dec9c2017-02-21 10:02:31 -080030#ifdef GPR_LOW_LEVEL_COUNTERS
31gpr_atm gpr_mu_locks = 0;
Craig Tiller7f4d30a2017-02-21 10:24:00 -080032gpr_atm gpr_counter_atm_cas = 0;
33gpr_atm gpr_counter_atm_add = 0;
Craig Tillerb038beb2017-02-07 08:28:51 -080034#endif
35
Craig Tiller4782d922017-11-10 09:53:21 -080036void gpr_mu_init(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_init(mu, nullptr) == 0); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037
Craig Tillera82950e2015-09-22 12:33:20 -070038void gpr_mu_destroy(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_destroy(mu) == 0); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
Craig Tiller1f41b6b2015-10-09 15:07:02 -070040void gpr_mu_lock(gpr_mu* mu) {
Craig Tillerf3dec9c2017-02-21 10:02:31 -080041#ifdef GPR_LOW_LEVEL_COUNTERS
42 GPR_ATM_INC_COUNTER(gpr_mu_locks);
Craig Tillerb038beb2017-02-07 08:28:51 -080043#endif
Craig Tiller0ba432d2015-10-09 16:57:11 -070044 GPR_TIMER_BEGIN("gpr_mu_lock", 0);
Craig Tiller1f41b6b2015-10-09 15:07:02 -070045 GPR_ASSERT(pthread_mutex_lock(mu) == 0);
Craig Tiller0ba432d2015-10-09 16:57:11 -070046 GPR_TIMER_END("gpr_mu_lock", 0);
Craig Tiller8910ac62015-10-08 16:49:15 -070047}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
Craig Tiller1f41b6b2015-10-09 15:07:02 -070049void gpr_mu_unlock(gpr_mu* mu) {
Craig Tiller0ba432d2015-10-09 16:57:11 -070050 GPR_TIMER_BEGIN("gpr_mu_unlock", 0);
Craig Tiller1f41b6b2015-10-09 15:07:02 -070051 GPR_ASSERT(pthread_mutex_unlock(mu) == 0);
Craig Tiller0ba432d2015-10-09 16:57:11 -070052 GPR_TIMER_END("gpr_mu_unlock", 0);
Craig Tiller8910ac62015-10-08 16:49:15 -070053}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054
Craig Tillera82950e2015-09-22 12:33:20 -070055int gpr_mu_trylock(gpr_mu* mu) {
Craig Tiller59256032015-11-02 14:19:15 -080056 int err;
57 GPR_TIMER_BEGIN("gpr_mu_trylock", 0);
58 err = pthread_mutex_trylock(mu);
Craig Tillera82950e2015-09-22 12:33:20 -070059 GPR_ASSERT(err == 0 || err == EBUSY);
Craig Tiller59256032015-11-02 14:19:15 -080060 GPR_TIMER_END("gpr_mu_trylock", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061 return err == 0;
62}
63
64/*----------------------------------------*/
65
Craig Tiller4782d922017-11-10 09:53:21 -080066void gpr_cv_init(gpr_cv* cv) { GPR_ASSERT(pthread_cond_init(cv, nullptr) == 0); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067
Craig Tillera82950e2015-09-22 12:33:20 -070068void gpr_cv_destroy(gpr_cv* cv) { GPR_ASSERT(pthread_cond_destroy(cv) == 0); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069
Craig Tillera82950e2015-09-22 12:33:20 -070070int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071 int err = 0;
Craig Tillera82950e2015-09-22 12:33:20 -070072 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-g471dbd82016-01-04 15:25:37 -080078 abs_deadline_ts.tv_sec = (time_t)abs_deadline.tv_sec;
Craig Tillera82950e2015-09-22 12:33:20 -070079 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 Nobleb7ebd3b2014-11-26 16:33:03 -080083 return err == ETIMEDOUT;
84}
85
Craig Tillera82950e2015-09-22 12:33:20 -070086void gpr_cv_signal(gpr_cv* cv) { GPR_ASSERT(pthread_cond_signal(cv) == 0); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087
Craig Tillera82950e2015-09-22 12:33:20 -070088void gpr_cv_broadcast(gpr_cv* cv) {
89 GPR_ASSERT(pthread_cond_broadcast(cv) == 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090}
91
92/*----------------------------------------*/
93
Craig Tillera82950e2015-09-22 12:33:20 -070094void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
95 GPR_ASSERT(pthread_once(once, init_function) == 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080096}
jtattermusch98bffb72014-12-09 12:47:19 -080097
Craig Tiller190d3602015-02-18 09:23:38 -080098#endif /* GRP_POSIX_SYNC */