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 <assert.h>
22 #include <grpc/support/atm.h>
23 #include <grpc/support/port_platform.h>
24 #include <grpc/support/sync.h>
25
26 #include "absl/log/check.h"
27
28 // Number of mutexes to allocate for events, to avoid lock contention.
29 // Should be a prime.
30 enum { event_sync_partitions = 31 };
31
32 // Events are partitioned by address to avoid lock contention.
33 static struct sync_array_s {
34 gpr_mu mu;
35 gpr_cv cv;
36 } sync_array[event_sync_partitions];
37
38 // This routine is executed once on first use, via event_once
39 static gpr_once event_once = GPR_ONCE_INIT;
event_initialize(void)40 static void event_initialize(void) {
41 int i;
42 for (i = 0; i != event_sync_partitions; i++) {
43 gpr_mu_init(&sync_array[i].mu);
44 gpr_cv_init(&sync_array[i].cv);
45 }
46 }
47
48 // Hash ev into an element of sync_array[].
hash(gpr_event * ev)49 static struct sync_array_s* hash(gpr_event* ev) {
50 return &sync_array[reinterpret_cast<uintptr_t>(ev) % event_sync_partitions];
51 }
52
gpr_event_init(gpr_event * ev)53 void gpr_event_init(gpr_event* ev) {
54 gpr_once_init(&event_once, &event_initialize);
55 ev->state = 0;
56 }
57
gpr_event_set(gpr_event * ev,void * value)58 void gpr_event_set(gpr_event* ev, void* value) {
59 struct sync_array_s* s = hash(ev);
60 gpr_mu_lock(&s->mu);
61 CHECK_EQ(gpr_atm_acq_load(&ev->state), 0);
62 gpr_atm_rel_store(&ev->state, (gpr_atm)value);
63 gpr_cv_broadcast(&s->cv);
64 gpr_mu_unlock(&s->mu);
65 CHECK_NE(value, nullptr);
66 }
67
gpr_event_get(gpr_event * ev)68 void* gpr_event_get(gpr_event* ev) {
69 return reinterpret_cast<void*>(gpr_atm_acq_load(&ev->state));
70 }
71
gpr_event_wait(gpr_event * ev,gpr_timespec abs_deadline)72 void* gpr_event_wait(gpr_event* ev, gpr_timespec abs_deadline) {
73 void* result = reinterpret_cast<void*>(gpr_atm_acq_load(&ev->state));
74 if (result == nullptr) {
75 struct sync_array_s* s = hash(ev);
76 gpr_mu_lock(&s->mu);
77 do {
78 result = reinterpret_cast<void*>(gpr_atm_acq_load(&ev->state));
79 } while (result == nullptr && !gpr_cv_wait(&s->cv, &s->mu, abs_deadline));
80 gpr_mu_unlock(&s->mu);
81 }
82 return result;
83 }
84
gpr_ref_init(gpr_refcount * r,int n)85 void gpr_ref_init(gpr_refcount* r, int n) { gpr_atm_rel_store(&r->count, n); }
86
gpr_ref(gpr_refcount * r)87 void gpr_ref(gpr_refcount* r) { gpr_atm_no_barrier_fetch_add(&r->count, 1); }
88
gpr_ref_non_zero(gpr_refcount * r)89 void gpr_ref_non_zero(gpr_refcount* r) {
90 #ifndef NDEBUG
91 gpr_atm prior = gpr_atm_no_barrier_fetch_add(&r->count, 1);
92 assert(prior > 0);
93 #else
94 gpr_ref(r);
95 #endif
96 }
97
gpr_refn(gpr_refcount * r,int n)98 void gpr_refn(gpr_refcount* r, int n) {
99 gpr_atm_no_barrier_fetch_add(&r->count, n);
100 }
101
gpr_unref(gpr_refcount * r)102 int gpr_unref(gpr_refcount* r) {
103 gpr_atm prior = gpr_atm_full_fetch_add(&r->count, -1);
104 CHECK_GT(prior, 0);
105 return prior == 1;
106 }
107
gpr_ref_is_unique(gpr_refcount * r)108 int gpr_ref_is_unique(gpr_refcount* r) {
109 return gpr_atm_acq_load(&r->count) == 1;
110 }
111
gpr_stats_init(gpr_stats_counter * c,intptr_t n)112 void gpr_stats_init(gpr_stats_counter* c, intptr_t n) {
113 gpr_atm_rel_store(&c->value, n);
114 }
115
gpr_stats_inc(gpr_stats_counter * c,intptr_t inc)116 void gpr_stats_inc(gpr_stats_counter* c, intptr_t inc) {
117 gpr_atm_no_barrier_fetch_add(&c->value, inc);
118 }
119
gpr_stats_read(const gpr_stats_counter * c)120 intptr_t gpr_stats_read(const gpr_stats_counter* c) {
121 // don't need acquire-load, but we have no no-barrier load yet
122 return gpr_atm_acq_load(&c->value);
123 }
124