blob: de0f0484b57f0f1cf091590b03cfc4bd9abf8de0 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
jtattermusch98bffb72014-12-09 12:47:19 -080034#include <grpc/support/port_platform.h>
35
36#ifdef GPR_POSIX_SYNC
37
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <errno.h>
39#include <grpc/support/log.h>
40#include <grpc/support/sync.h>
41#include <grpc/support/time.h>
Craig Tillerf40df232016-03-25 13:38:14 -070042#include <time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070043#include "src/core/lib/profiling/timers.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044
Craig Tillerb038beb2017-02-07 08:28:51 -080045#ifdef GPR_MU_COUNTERS
46gpr_atm grpc_mu_locks = 0;
47#endif
48
Craig Tillera82950e2015-09-22 12:33:20 -070049void gpr_mu_init(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_init(mu, NULL) == 0); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050
Craig Tillera82950e2015-09-22 12:33:20 -070051void gpr_mu_destroy(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_destroy(mu) == 0); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
Craig Tiller1f41b6b2015-10-09 15:07:02 -070053void gpr_mu_lock(gpr_mu* mu) {
Craig Tillerb038beb2017-02-07 08:28:51 -080054#ifdef GPR_MU_COUNTERS
55 gpr_atm_no_barrier_fetch_add(&grpc_mu_locks, 1);
56#endif
Craig Tiller0ba432d2015-10-09 16:57:11 -070057 GPR_TIMER_BEGIN("gpr_mu_lock", 0);
Craig Tiller1f41b6b2015-10-09 15:07:02 -070058 GPR_ASSERT(pthread_mutex_lock(mu) == 0);
Craig Tiller0ba432d2015-10-09 16:57:11 -070059 GPR_TIMER_END("gpr_mu_lock", 0);
Craig Tiller8910ac62015-10-08 16:49:15 -070060}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061
Craig Tiller1f41b6b2015-10-09 15:07:02 -070062void gpr_mu_unlock(gpr_mu* mu) {
Craig Tiller0ba432d2015-10-09 16:57:11 -070063 GPR_TIMER_BEGIN("gpr_mu_unlock", 0);
Craig Tiller1f41b6b2015-10-09 15:07:02 -070064 GPR_ASSERT(pthread_mutex_unlock(mu) == 0);
Craig Tiller0ba432d2015-10-09 16:57:11 -070065 GPR_TIMER_END("gpr_mu_unlock", 0);
Craig Tiller8910ac62015-10-08 16:49:15 -070066}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067
Craig Tillera82950e2015-09-22 12:33:20 -070068int gpr_mu_trylock(gpr_mu* mu) {
Craig Tiller59256032015-11-02 14:19:15 -080069 int err;
70 GPR_TIMER_BEGIN("gpr_mu_trylock", 0);
71 err = pthread_mutex_trylock(mu);
Craig Tillera82950e2015-09-22 12:33:20 -070072 GPR_ASSERT(err == 0 || err == EBUSY);
Craig Tiller59256032015-11-02 14:19:15 -080073 GPR_TIMER_END("gpr_mu_trylock", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074 return err == 0;
75}
76
77/*----------------------------------------*/
78
Craig Tillera82950e2015-09-22 12:33:20 -070079void gpr_cv_init(gpr_cv* cv) { GPR_ASSERT(pthread_cond_init(cv, NULL) == 0); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080
Craig Tillera82950e2015-09-22 12:33:20 -070081void gpr_cv_destroy(gpr_cv* cv) { GPR_ASSERT(pthread_cond_destroy(cv) == 0); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080082
Craig Tillera82950e2015-09-22 12:33:20 -070083int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084 int err = 0;
Craig Tillera82950e2015-09-22 12:33:20 -070085 if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
86 0) {
87 err = pthread_cond_wait(cv, mu);
88 } else {
89 struct timespec abs_deadline_ts;
90 abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
yang-g471dbd82016-01-04 15:25:37 -080091 abs_deadline_ts.tv_sec = (time_t)abs_deadline.tv_sec;
Craig Tillera82950e2015-09-22 12:33:20 -070092 abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
93 err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts);
94 }
95 GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080096 return err == ETIMEDOUT;
97}
98
Craig Tillera82950e2015-09-22 12:33:20 -070099void gpr_cv_signal(gpr_cv* cv) { GPR_ASSERT(pthread_cond_signal(cv) == 0); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100
Craig Tillera82950e2015-09-22 12:33:20 -0700101void gpr_cv_broadcast(gpr_cv* cv) {
102 GPR_ASSERT(pthread_cond_broadcast(cv) == 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103}
104
105/*----------------------------------------*/
106
Craig Tillera82950e2015-09-22 12:33:20 -0700107void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
108 GPR_ASSERT(pthread_once(once, init_function) == 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109}
jtattermusch98bffb72014-12-09 12:47:19 -0800110
Craig Tiller190d3602015-02-18 09:23:38 -0800111#endif /* GRP_POSIX_SYNC */