GRPC Core  4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sync.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * 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 
34 #ifndef GRPC_SUPPORT_SYNC_H
35 #define GRPC_SUPPORT_SYNC_H
36 
37 #include <grpc/impl/codegen/gpr_types.h> /* for gpr_timespec */
38 #include <grpc/impl/codegen/sync.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
52 GPRAPI void gpr_mu_init(gpr_mu *mu);
53 
56 GPRAPI void gpr_mu_destroy(gpr_mu *mu);
57 
61 GPRAPI void gpr_mu_lock(gpr_mu *mu);
62 
65 GPRAPI void gpr_mu_unlock(gpr_mu *mu);
66 
72 
80 GPRAPI void gpr_cv_init(gpr_cv *cv);
81 
84 GPRAPI void gpr_cv_destroy(gpr_cv *cv);
85 
93 GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline);
94 
99 GPRAPI void gpr_cv_signal(gpr_cv *cv);
100 
102 GPRAPI void gpr_cv_broadcast(gpr_cv *cv);
103 
115 GPRAPI void gpr_once_init(gpr_once *once, void (*init_routine)(void));
116 
126 
130 GPRAPI void gpr_event_set(gpr_event *ev, void *value);
131 
137 GPRAPI void *gpr_event_get(gpr_event *ev);
138 
144 GPRAPI void *gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline);
145 
151 GPRAPI void gpr_ref_init(gpr_refcount *r, int n);
152 
154 GPRAPI void gpr_ref(gpr_refcount *r);
155 
159 
161 GPRAPI void gpr_refn(gpr_refcount *r, int n);
162 
166 
170 
180 GPRAPI void gpr_stats_init(gpr_stats_counter *c, intptr_t n);
181 
183 GPRAPI void gpr_stats_inc(gpr_stats_counter *c, intptr_t inc);
184 
186 GPRAPI intptr_t gpr_stats_read(const gpr_stats_counter *c);
187 
191 #if 0
192 
193 #define N 4
194 
195  typedef struct queue {
196  gpr_cv non_empty; /* Signalled when length becomes non-zero. */
197  gpr_cv non_full; /* Signalled when length becomes non-N. */
198  gpr_mu mu; /* Protects all fields below.
199  (That is, except during initialization or
200  destruction, the fields below should be accessed
201  only by a thread that holds mu.) */
202  int head; /* Index of head of queue 0..N-1. */
203  int length; /* Number of valid elements in queue 0..N. */
204  int elem[N]; /* elem[head .. head+length-1] are queue elements. */
205  } queue;
206 
207  /* Initialize *q. */
208  void queue_init(queue *q) {
209  gpr_mu_init(&q->mu);
210  gpr_cv_init(&q->non_empty);
211  gpr_cv_init(&q->non_full);
212  q->head = 0;
213  q->length = 0;
214  }
215 
216  /* Free storage associated with *q. */
217  void queue_destroy(queue *q) {
218  gpr_mu_destroy(&q->mu);
219  gpr_cv_destroy(&q->non_empty);
220  gpr_cv_destroy(&q->non_full);
221  }
222 
223  /* Wait until there is room in *q, then append x to *q. */
224  void queue_append(queue *q, int x) {
225  gpr_mu_lock(&q->mu);
226  /* To wait for a predicate without a deadline, loop on the negation of the
227  predicate, and use gpr_cv_wait(..., gpr_inf_future) inside the loop
228  to release the lock, wait, and reacquire on each iteration. Code that
229  makes the condition true should use gpr_cv_broadcast() on the
230  corresponding condition variable. The predicate must be on state
231  protected by the lock. */
232  while (q->length == N) {
233  gpr_cv_wait(&q->non_full, &q->mu, gpr_inf_future);
234  }
235  if (q->length == 0) { /* Wake threads blocked in queue_remove(). */
236  /* It's normal to use gpr_cv_broadcast() or gpr_signal() while
237  holding the lock. */
238  gpr_cv_broadcast(&q->non_empty);
239  }
240  q->elem[(q->head + q->length) % N] = x;
241  q->length++;
242  gpr_mu_unlock(&q->mu);
243  }
244 
245  /* If it can be done without blocking, append x to *q and return non-zero.
246  Otherwise return 0. */
247  int queue_try_append(queue *q, int x) {
248  int result = 0;
249  if (gpr_mu_trylock(&q->mu)) {
250  if (q->length != N) {
251  if (q->length == 0) { /* Wake threads blocked in queue_remove(). */
252  gpr_cv_broadcast(&q->non_empty);
253  }
254  q->elem[(q->head + q->length) % N] = x;
255  q->length++;
256  result = 1;
257  }
258  gpr_mu_unlock(&q->mu);
259  }
260  return result;
261  }
262 
263  /* Wait until the *q is non-empty or deadline abs_deadline passes. If the
264  queue is non-empty, remove its head entry, place it in *head, and return
265  non-zero. Otherwise return 0. */
266  int queue_remove(queue *q, int *head, gpr_timespec abs_deadline) {
267  int result = 0;
268  gpr_mu_lock(&q->mu);
269  /* To wait for a predicate with a deadline, loop on the negation of the
270  predicate or until gpr_cv_wait() returns true. Code that makes
271  the condition true should use gpr_cv_broadcast() on the corresponding
272  condition variable. The predicate must be on state protected by the
273  lock. */
274  while (q->length == 0 &&
275  !gpr_cv_wait(&q->non_empty, &q->mu, abs_deadline)) {
276  }
277  if (q->length != 0) { /* Queue is non-empty. */
278  result = 1;
279  if (q->length == N) { /* Wake threads blocked in queue_append(). */
280  gpr_cv_broadcast(&q->non_full);
281  }
282  *head = q->elem[q->head];
283  q->head = (q->head + 1) % N;
284  q->length--;
285  } /* else deadline exceeded */
286  gpr_mu_unlock(&q->mu);
287  return result;
288  }
289 #endif /* 0 */
290 
291 #ifdef __cplusplus
292 }
293 #endif
294 
295 #endif /* GRPC_SUPPORT_SYNC_H */
GPRAPI void gpr_stats_init(gpr_stats_counter *c, intptr_t n)
— Stats counters —
GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)
Atomically release *mu and wait on *cv.
GPRAPI void gpr_event_init(gpr_event *ev)
— One-time event notification —
Definition: sync_generic.h:47
#define GPRAPI
Definition: port_platform.h:416
pthread_once_t gpr_once
Definition: sync_posix.h:43
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
Cause *cv no longer to be initialized, freeing any memory in use.
GPRAPI void gpr_mu_init(gpr_mu *mu)
— Mutex interface —
GPRAPI intptr_t gpr_stats_read(const gpr_stats_counter *c)
Return *c.
GPRAPI void gpr_cv_broadcast(gpr_cv *cv)
Wake all threads waiting on *cv.
Definition: sync_generic.h:50
GPRAPI void gpr_stats_inc(gpr_stats_counter *c, intptr_t inc)
*c += inc.
pthread_cond_t gpr_cv
Definition: sync_posix.h:42
GPRAPI void gpr_ref_init(gpr_refcount *r, int n)
— Reference counting —
Definition: sync_windows.h:39
GPRAPI int gpr_mu_trylock(gpr_mu *mu)
Without blocking, attempt to acquire an exclusive lock on *mu for the calling thread, then return non-zero iff success.
GPRAPI void gpr_event_set(gpr_event *ev, void *value)
Set *ev so that gpr_event_get() and gpr_event_wait() will return value.
GPRAPI void gpr_cv_init(gpr_cv *cv)
— Condition variable interface —
GPRAPI void gpr_ref_non_zero(gpr_refcount *r)
Increment the reference count *r.
GPRAPI int gpr_ref_is_unique(gpr_refcount *r)
Return non-zero iff the reference count of *r is one, and thus is owned by exactly one object...
GPRAPI void * gpr_event_get(gpr_event *ev)
Return the value set by gpr_event_set(ev, ...), or NULL if no such call has completed.
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
The zero time interval.
GPRAPI void gpr_mu_lock(gpr_mu *mu)
Wait until no thread has a lock on *mu, cause the calling thread to own an exclusive lock on *mu...
GPRAPI void gpr_once_init(gpr_once *once, void(*init_routine)(void))
— One-time initialization —
Analogous to struct timespec.
Definition: gpr_types.h:62
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
Release an exclusive lock on *mu held by the calling thread.
GPRAPI void gpr_ref(gpr_refcount *r)
Increment the reference count *r.
GPRAPI void gpr_cv_signal(gpr_cv *cv)
If any threads are waiting on *cv, wake at least one.
GPRAPI void gpr_refn(gpr_refcount *r, int n)
Increment the reference count *r by n.
Definition: sync_generic.h:41
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
Cause *mu no longer to be initialized, freeing any memory in use.
GPRAPI void * gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline)
Wait until *ev is set by gpr_event_set(ev, ...), or abs_deadline is exceeded, then return gpr_event_g...
GPRAPI int gpr_unref(gpr_refcount *r)
Decrement the reference count *r and return non-zero iff it has reached zero.