blob: 0ccbd4923f8cb7f858ab6b72dc87d1b5c7ab7202 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * 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>
David Klempner9b60fa32015-02-04 09:48:53 -080039#include <time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/support/log.h>
41#include <grpc/support/sync.h>
42#include <grpc/support/time.h>
43
44void gpr_mu_init(gpr_mu *mu) { GPR_ASSERT(pthread_mutex_init(mu, NULL) == 0); }
45
46void gpr_mu_destroy(gpr_mu *mu) { GPR_ASSERT(pthread_mutex_destroy(mu) == 0); }
47
48void gpr_mu_lock(gpr_mu *mu) { GPR_ASSERT(pthread_mutex_lock(mu) == 0); }
49
50void gpr_mu_unlock(gpr_mu *mu) { GPR_ASSERT(pthread_mutex_unlock(mu) == 0); }
51
52int gpr_mu_trylock(gpr_mu *mu) {
53 int err = pthread_mutex_trylock(mu);
54 GPR_ASSERT(err == 0 || err == EBUSY);
55 return err == 0;
56}
57
58/*----------------------------------------*/
59
60void gpr_cv_init(gpr_cv *cv) { GPR_ASSERT(pthread_cond_init(cv, NULL) == 0); }
61
62void gpr_cv_destroy(gpr_cv *cv) { GPR_ASSERT(pthread_cond_destroy(cv) == 0); }
63
64int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline) {
65 int err = 0;
66 if (gpr_time_cmp(abs_deadline, gpr_inf_future) == 0) {
67 err = pthread_cond_wait(cv, mu);
68 } else {
David Klempner9b60fa32015-02-04 09:48:53 -080069 struct timespec abs_deadline_ts;
70 abs_deadline_ts.tv_sec = abs_deadline.tv_sec;
71 abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
72 err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073 }
74 GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN);
75 return err == ETIMEDOUT;
76}
77
78void gpr_cv_signal(gpr_cv *cv) { GPR_ASSERT(pthread_cond_signal(cv) == 0); }
79
80void gpr_cv_broadcast(gpr_cv *cv) {
81 GPR_ASSERT(pthread_cond_broadcast(cv) == 0);
82}
83
84/*----------------------------------------*/
85
86void gpr_once_init(gpr_once *once, void (*init_function)(void)) {
87 GPR_ASSERT(pthread_once(once, init_function) == 0);
88}
jtattermusch98bffb72014-12-09 12:47:19 -080089
Craig Tiller190d3602015-02-18 09:23:38 -080090#endif /* GRP_POSIX_SYNC */