1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 /* Generic implementation of synchronization primitives. */
20
21 #include <grpc/support/port_platform.h>
22
23 #include <grpc/support/atm.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/sync.h>
26
27 #include <assert.h>
28
29 /* Number of mutexes to allocate for events, to avoid lock contention.
30 Should be a prime. */
31 enum { event_sync_partitions = 31 };
32
33 /* Events are partitioned by address to avoid lock contention. */
34 static struct sync_array_s {
35 gpr_mu mu;
36 gpr_cv cv;
37 } sync_array[event_sync_partitions];
38
39 /* This routine is executed once on first use, via event_once */
40 static gpr_once event_once = GPR_ONCE_INIT;
event_initialize(void)41 static void event_initialize(void) {
42 int i;
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);
46 }
47 }
48
49 /* Hash ev into an element of sync_array[]. */
hash(gpr_event * ev)50 static struct sync_array_s* hash(gpr_event* ev) {
51 return &sync_array[((uintptr_t)ev) % event_sync_partitions];
52 }
53
gpr_event_init(gpr_event * ev)54 void gpr_event_init(gpr_event* ev) {
55 gpr_once_init(&event_once, &event_initialize);
56 ev->state = 0;
57 }
58
gpr_event_set(gpr_event * ev,void * value)59 void gpr_event_set(gpr_event* ev, void* value) {
60 struct sync_array_s* s = hash(ev);
61 gpr_mu_lock(&s->mu);
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);
67 }
68
gpr_event_get(gpr_event * ev)69 void* gpr_event_get(gpr_event* ev) {
70 return (void*)gpr_atm_acq_load(&ev->state);
71 }
72
gpr_event_wait(gpr_event * ev,gpr_timespec abs_deadline)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);
77 gpr_mu_lock(&s->mu);
78 do {
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);
82 }
83 return result;
84 }
85
gpr_ref_init(gpr_refcount * r,int n)86 void gpr_ref_init(gpr_refcount* r, int n) { gpr_atm_rel_store(&r->count, n); }
87
gpr_ref(gpr_refcount * r)88 void gpr_ref(gpr_refcount* r) { gpr_atm_no_barrier_fetch_add(&r->count, 1); }
89
gpr_ref_non_zero(gpr_refcount * r)90 void gpr_ref_non_zero(gpr_refcount* r) {
91 #ifndef NDEBUG
92 gpr_atm prior = gpr_atm_no_barrier_fetch_add(&r->count, 1);
93 assert(prior > 0);
94 #else
95 gpr_ref(r);
96 #endif
97 }
98
gpr_refn(gpr_refcount * r,int n)99 void gpr_refn(gpr_refcount* r, int n) {
100 gpr_atm_no_barrier_fetch_add(&r->count, n);
101 }
102
gpr_unref(gpr_refcount * r)103 int gpr_unref(gpr_refcount* r) {
104 gpr_atm prior = gpr_atm_full_fetch_add(&r->count, -1);
105 GPR_ASSERT(prior > 0);
106 return prior == 1;
107 }
108
gpr_ref_is_unique(gpr_refcount * r)109 int gpr_ref_is_unique(gpr_refcount* r) {
110 return gpr_atm_acq_load(&r->count) == 1;
111 }
112
gpr_stats_init(gpr_stats_counter * c,intptr_t n)113 void gpr_stats_init(gpr_stats_counter* c, intptr_t n) {
114 gpr_atm_rel_store(&c->value, n);
115 }
116
gpr_stats_inc(gpr_stats_counter * c,intptr_t inc)117 void gpr_stats_inc(gpr_stats_counter* c, intptr_t inc) {
118 gpr_atm_no_barrier_fetch_add(&c->value, inc);
119 }
120
gpr_stats_read(const gpr_stats_counter * c)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);
124 }
125