include/grpc/support/sync.h
Go to the documentation of this file.
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 #ifndef GRPC_SUPPORT_SYNC_H
20 #define GRPC_SUPPORT_SYNC_H
21 
23 
24 #include <grpc/impl/codegen/gpr_types.h> /* for gpr_timespec */
25 #include <grpc/impl/codegen/sync.h> // IWYU pragma: export
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
40 
44 
49 
53 
59 
68 
72 
80 GPRAPI int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline);
81 
87 
90 
102 GPRAPI void gpr_once_init(gpr_once* once, void (*init_function)(void));
103 
113 
117 GPRAPI void gpr_event_set(gpr_event* ev, void* value);
118 
124 GPRAPI void* gpr_event_get(gpr_event* ev);
125 
131 GPRAPI void* gpr_event_wait(gpr_event* ev, gpr_timespec abs_deadline);
132 
138 GPRAPI void gpr_ref_init(gpr_refcount* r, int n);
139 
142 
146 
148 GPRAPI void gpr_refn(gpr_refcount* r, int n);
149 
153 
157 
168 
171 
174 
178 #if 0
179 
180 #define N 4
181 
182  typedef struct queue {
183  gpr_cv non_empty; /* Signalled when length becomes non-zero. */
184  gpr_cv non_full; /* Signalled when length becomes non-N. */
185  gpr_mu mu; /* Protects all fields below.
186  (That is, except during initialization or
187  destruction, the fields below should be accessed
188  only by a thread that holds mu.) */
189  int head; /* Index of head of queue 0..N-1. */
190  int length; /* Number of valid elements in queue 0..N. */
191  int elem[N]; /* elem[head .. head+length-1] are queue elements. */
192  } queue;
193 
194  /* Initialize *q. */
195  void queue_init(queue *q) {
196  gpr_mu_init(&q->mu);
197  gpr_cv_init(&q->non_empty);
198  gpr_cv_init(&q->non_full);
199  q->head = 0;
200  q->length = 0;
201  }
202 
203  /* Free storage associated with *q. */
204  void queue_destroy(queue *q) {
205  gpr_mu_destroy(&q->mu);
206  gpr_cv_destroy(&q->non_empty);
207  gpr_cv_destroy(&q->non_full);
208  }
209 
210  /* Wait until there is room in *q, then append x to *q. */
211  void queue_append(queue *q, int x) {
212  gpr_mu_lock(&q->mu);
213  /* To wait for a predicate without a deadline, loop on the negation of the
214  predicate, and use gpr_cv_wait(..., gpr_inf_future) inside the loop
215  to release the lock, wait, and reacquire on each iteration. Code that
216  makes the condition true should use gpr_cv_broadcast() on the
217  corresponding condition variable. The predicate must be on state
218  protected by the lock. */
219  while (q->length == N) {
220  gpr_cv_wait(&q->non_full, &q->mu, gpr_inf_future);
221  }
222  if (q->length == 0) { /* Wake threads blocked in queue_remove(). */
223  /* It's normal to use gpr_cv_broadcast() or gpr_signal() while
224  holding the lock. */
225  gpr_cv_broadcast(&q->non_empty);
226  }
227  q->elem[(q->head + q->length) % N] = x;
228  q->length++;
229  gpr_mu_unlock(&q->mu);
230  }
231 
232  /* If it can be done without blocking, append x to *q and return non-zero.
233  Otherwise return 0. */
234  int queue_try_append(queue *q, int x) {
235  int result = 0;
236  if (gpr_mu_trylock(&q->mu)) {
237  if (q->length != N) {
238  if (q->length == 0) { /* Wake threads blocked in queue_remove(). */
239  gpr_cv_broadcast(&q->non_empty);
240  }
241  q->elem[(q->head + q->length) % N] = x;
242  q->length++;
243  result = 1;
244  }
245  gpr_mu_unlock(&q->mu);
246  }
247  return result;
248  }
249 
250  /* Wait until the *q is non-empty or deadline abs_deadline passes. If the
251  queue is non-empty, remove its head entry, place it in *head, and return
252  non-zero. Otherwise return 0. */
253  int queue_remove(queue *q, int *head, gpr_timespec abs_deadline) {
254  int result = 0;
255  gpr_mu_lock(&q->mu);
256  /* To wait for a predicate with a deadline, loop on the negation of the
257  predicate or until gpr_cv_wait() returns true. Code that makes
258  the condition true should use gpr_cv_broadcast() on the corresponding
259  condition variable. The predicate must be on state protected by the
260  lock. */
261  while (q->length == 0 &&
262  !gpr_cv_wait(&q->non_empty, &q->mu, abs_deadline)) {
263  }
264  if (q->length != 0) { /* Queue is non-empty. */
265  result = 1;
266  if (q->length == N) { /* Wake threads blocked in queue_append(). */
267  gpr_cv_broadcast(&q->non_full);
268  }
269  *head = q->elem[q->head];
270  q->head = (q->head + 1) % N;
271  q->length--;
272  } /* else deadline exceeded */
273  gpr_mu_unlock(&q->mu);
274  return result;
275  }
276 #endif /* 0 */
277 
278 #ifdef __cplusplus
279 } // extern "C"
280 #endif
281 
282 #endif /* GRPC_SUPPORT_SYNC_H */
gpr_cv_signal
GPRAPI void gpr_cv_signal(gpr_cv *cv)
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
gpr_refn
GPRAPI void gpr_refn(gpr_refcount *r, int n)
Definition: sync.cc:99
gpr_event_get
GPRAPI void * gpr_event_get(gpr_event *ev)
Definition: sync.cc:69
inc
static void inc(void *v)
Definition: spinlock_test.cc:125
gpr_once
pthread_once_t gpr_once
Definition: impl/codegen/sync_posix.h:50
gpr_cv
pthread_cond_t gpr_cv
Definition: impl/codegen/sync_posix.h:48
gpr_event_set
GPRAPI void gpr_event_set(gpr_event *ev, void *value)
Definition: sync.cc:59
gpr_stats_read
GPRAPI intptr_t gpr_stats_read(const gpr_stats_counter *c)
Definition: sync.cc:121
queue_init
void queue_init(queue *q)
Definition: sync_test.cc:52
GPRAPI
#define GPRAPI
Definition: impl/codegen/port_platform.h:660
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
gpr_refcount
Definition: impl/codegen/sync_generic.h:39
queue_remove
int queue_remove(queue *q, int *head, gpr_timespec abs_deadline)
Definition: sync_test.cc:111
gpr_stats_counter
Definition: impl/codegen/sync_generic.h:44
queue::elem
int elem[N]
Definition: sync_test.cc:48
gpr_once_init
GPRAPI void gpr_once_init(gpr_once *once, void(*init_function)(void))
gpr_stats_init
GPRAPI void gpr_stats_init(gpr_stats_counter *c, intptr_t n)
Definition: sync.cc:113
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
queue_destroy
void queue_destroy(queue *q)
Definition: sync_test.cc:61
queue::length
int length
Definition: sync_test.cc:47
queue::non_empty
gpr_cv non_empty
Definition: sync_test.cc:40
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
queue
Definition: sync_test.cc:39
gpr_cv_destroy
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
gpr_cv_wait
GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
gpr_event_init
GPRAPI void gpr_event_init(gpr_event *ev)
Definition: sync.cc:54
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
gpr_event_wait
GPRAPI void * gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline)
Definition: sync.cc:73
queue::non_full
gpr_cv non_full
Definition: sync_test.cc:41
gpr_types.h
value
const char * value
Definition: hpack_parser_table.cc:165
queue_try_append
int queue_try_append(queue *q, int x)
Definition: sync_test.cc:92
queue
struct queue queue
once
absl::once_flag once
Definition: bm_opencensus_plugin.cc:38
gpr_event
Definition: impl/codegen/sync_generic.h:31
N
#define N
Definition: sync_test.cc:37
cv
unsigned cv
Definition: cxa_demangle.cpp:4908
queue_append
void queue_append(queue *q, int x)
Definition: sync_test.cc:68
gpr_ref_non_zero
GPRAPI void gpr_ref_non_zero(gpr_refcount *r)
Definition: sync.cc:90
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
queue::head
int head
Definition: sync_test.cc:46
gpr_mu_trylock
GPRAPI int gpr_mu_trylock(gpr_mu *mu)
fix_build_deps.r
r
Definition: fix_build_deps.py:491
gpr_stats_inc
GPRAPI void gpr_stats_inc(gpr_stats_counter *c, intptr_t inc)
Definition: sync.cc:117
absl::str_format_internal::LengthMod::q
@ q
gpr_cv_broadcast
GPRAPI void gpr_cv_broadcast(gpr_cv *cv)
queue::mu
gpr_mu mu
Definition: sync_test.cc:42
gpr_ref_init
GPRAPI void gpr_ref_init(gpr_refcount *r, int n)
Definition: sync.cc:86
gpr_timespec
Definition: gpr_types.h:50
gpr_unref
GPRAPI int gpr_unref(gpr_refcount *r)
Definition: sync.cc:103
sync.h
gpr_ref
GPRAPI void gpr_ref(gpr_refcount *r)
Definition: sync.cc:88
gpr_ref_is_unique
GPRAPI int gpr_ref_is_unique(gpr_refcount *r)
Definition: sync.cc:109
gpr_cv_init
GPRAPI void gpr_cv_init(gpr_cv *cv)
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:28