3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 /* Generic implementation of synchronization primitives. */
21 #include <grpc/support/port_platform.h>
23 #include <grpc/support/atm.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/sync.h>
29 /* Number of mutexes to allocate for events, to avoid lock contention.
31 enum { event_sync_partitions = 31 };
33 /* Events are partitioned by address to avoid lock contention. */
34 static struct sync_array_s {
37 } sync_array[event_sync_partitions];
39 /* This routine is executed once on first use, via event_once */
40 static gpr_once event_once = GPR_ONCE_INIT;
41 static void event_initialize(void) {
43 for (i = 0; i != event_sync_partitions; i++) {
44 gpr_mu_init(&sync_array[i].mu);
45 gpr_cv_init(&sync_array[i].cv);
49 /* Hash ev into an element of sync_array[]. */
50 static struct sync_array_s* hash(gpr_event* ev) {
51 return &sync_array[((uintptr_t)ev) % event_sync_partitions];
54 void gpr_event_init(gpr_event* ev) {
55 gpr_once_init(&event_once, &event_initialize);
59 void gpr_event_set(gpr_event* ev, void* value) {
60 struct sync_array_s* s = hash(ev);
62 GPR_ASSERT(gpr_atm_acq_load(&ev->state) == 0);
63 gpr_atm_rel_store(&ev->state, (gpr_atm)value);
64 gpr_cv_broadcast(&s->cv);
65 gpr_mu_unlock(&s->mu);
66 GPR_ASSERT(value != nullptr);
69 void* gpr_event_get(gpr_event* ev) {
70 return (void*)gpr_atm_acq_load(&ev->state);
73 void* gpr_event_wait(gpr_event* ev, gpr_timespec abs_deadline) {
74 void* result = (void*)gpr_atm_acq_load(&ev->state);
75 if (result == nullptr) {
76 struct sync_array_s* s = hash(ev);
79 result = (void*)gpr_atm_acq_load(&ev->state);
80 } while (result == nullptr && !gpr_cv_wait(&s->cv, &s->mu, abs_deadline));
81 gpr_mu_unlock(&s->mu);
86 void gpr_ref_init(gpr_refcount* r, int n) { gpr_atm_rel_store(&r->count, n); }
88 void gpr_ref(gpr_refcount* r) { gpr_atm_no_barrier_fetch_add(&r->count, 1); }
90 void gpr_ref_non_zero(gpr_refcount* r) {
92 gpr_atm prior = gpr_atm_no_barrier_fetch_add(&r->count, 1);
99 void gpr_refn(gpr_refcount* r, int n) {
100 gpr_atm_no_barrier_fetch_add(&r->count, n);
103 int gpr_unref(gpr_refcount* r) {
104 gpr_atm prior = gpr_atm_full_fetch_add(&r->count, -1);
105 GPR_ASSERT(prior > 0);
109 int gpr_ref_is_unique(gpr_refcount* r) {
110 return gpr_atm_acq_load(&r->count) == 1;
113 void gpr_stats_init(gpr_stats_counter* c, intptr_t n) {
114 gpr_atm_rel_store(&c->value, n);
117 void gpr_stats_inc(gpr_stats_counter* c, intptr_t inc) {
118 gpr_atm_no_barrier_fetch_add(&c->value, inc);
121 intptr_t gpr_stats_read(const gpr_stats_counter* c) {
122 /* don't need acquire-load, but we have no no-barrier load yet */
123 return gpr_atm_acq_load(&c->value);