GRPC Core  6.0.0
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 
22 #include <grpc/impl/codegen/gpr_types.h> /* for gpr_timespec */
23 #include <grpc/impl/codegen/sync.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
37 GPRAPI void gpr_mu_init(gpr_mu* mu);
38 
41 GPRAPI void gpr_mu_destroy(gpr_mu* mu);
42 
46 GPRAPI void gpr_mu_lock(gpr_mu* mu);
47 
50 GPRAPI void gpr_mu_unlock(gpr_mu* mu);
51 
57 
65 GPRAPI void gpr_cv_init(gpr_cv* cv);
66 
69 GPRAPI void gpr_cv_destroy(gpr_cv* cv);
70 
78 GPRAPI int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline);
79 
84 GPRAPI void gpr_cv_signal(gpr_cv* cv);
85 
88 
100 GPRAPI void gpr_once_init(gpr_once* once, void (*init_routine)(void));
101 
111 
115 GPRAPI void gpr_event_set(gpr_event* ev, void* value);
116 
122 GPRAPI void* gpr_event_get(gpr_event* ev);
123 
129 GPRAPI void* gpr_event_wait(gpr_event* ev, gpr_timespec abs_deadline);
130 
136 GPRAPI void gpr_ref_init(gpr_refcount* r, int n);
137 
139 GPRAPI void gpr_ref(gpr_refcount* r);
140 
144 
146 GPRAPI void gpr_refn(gpr_refcount* r, int n);
147 
151 
155 
165 GPRAPI void gpr_stats_init(gpr_stats_counter* c, intptr_t n);
166 
168 GPRAPI void gpr_stats_inc(gpr_stats_counter* c, intptr_t inc);
169 
171 GPRAPI intptr_t gpr_stats_read(const gpr_stats_counter* c);
172 
176 #if 0
177 
178 #define N 4
179 
180  typedef struct queue {
181  gpr_cv non_empty; /* Signalled when length becomes non-zero. */
182  gpr_cv non_full; /* Signalled when length becomes non-N. */
183  gpr_mu mu; /* Protects all fields below.
184  (That is, except during initialization or
185  destruction, the fields below should be accessed
186  only by a thread that holds mu.) */
187  int head; /* Index of head of queue 0..N-1. */
188  int length; /* Number of valid elements in queue 0..N. */
189  int elem[N]; /* elem[head .. head+length-1] are queue elements. */
190  } queue;
191 
192  /* Initialize *q. */
193  void queue_init(queue *q) {
194  gpr_mu_init(&q->mu);
195  gpr_cv_init(&q->non_empty);
196  gpr_cv_init(&q->non_full);
197  q->head = 0;
198  q->length = 0;
199  }
200 
201  /* Free storage associated with *q. */
202  void queue_destroy(queue *q) {
203  gpr_mu_destroy(&q->mu);
204  gpr_cv_destroy(&q->non_empty);
205  gpr_cv_destroy(&q->non_full);
206  }
207 
208  /* Wait until there is room in *q, then append x to *q. */
209  void queue_append(queue *q, int x) {
210  gpr_mu_lock(&q->mu);
211  /* To wait for a predicate without a deadline, loop on the negation of the
212  predicate, and use gpr_cv_wait(..., gpr_inf_future) inside the loop
213  to release the lock, wait, and reacquire on each iteration. Code that
214  makes the condition true should use gpr_cv_broadcast() on the
215  corresponding condition variable. The predicate must be on state
216  protected by the lock. */
217  while (q->length == N) {
218  gpr_cv_wait(&q->non_full, &q->mu, gpr_inf_future);
219  }
220  if (q->length == 0) { /* Wake threads blocked in queue_remove(). */
221  /* It's normal to use gpr_cv_broadcast() or gpr_signal() while
222  holding the lock. */
223  gpr_cv_broadcast(&q->non_empty);
224  }
225  q->elem[(q->head + q->length) % N] = x;
226  q->length++;
227  gpr_mu_unlock(&q->mu);
228  }
229 
230  /* If it can be done without blocking, append x to *q and return non-zero.
231  Otherwise return 0. */
232  int queue_try_append(queue *q, int x) {
233  int result = 0;
234  if (gpr_mu_trylock(&q->mu)) {
235  if (q->length != N) {
236  if (q->length == 0) { /* Wake threads blocked in queue_remove(). */
237  gpr_cv_broadcast(&q->non_empty);
238  }
239  q->elem[(q->head + q->length) % N] = x;
240  q->length++;
241  result = 1;
242  }
243  gpr_mu_unlock(&q->mu);
244  }
245  return result;
246  }
247 
248  /* Wait until the *q is non-empty or deadline abs_deadline passes. If the
249  queue is non-empty, remove its head entry, place it in *head, and return
250  non-zero. Otherwise return 0. */
251  int queue_remove(queue *q, int *head, gpr_timespec abs_deadline) {
252  int result = 0;
253  gpr_mu_lock(&q->mu);
254  /* To wait for a predicate with a deadline, loop on the negation of the
255  predicate or until gpr_cv_wait() returns true. Code that makes
256  the condition true should use gpr_cv_broadcast() on the corresponding
257  condition variable. The predicate must be on state protected by the
258  lock. */
259  while (q->length == 0 &&
260  !gpr_cv_wait(&q->non_empty, &q->mu, abs_deadline)) {
261  }
262  if (q->length != 0) { /* Queue is non-empty. */
263  result = 1;
264  if (q->length == N) { /* Wake threads blocked in queue_append(). */
265  gpr_cv_broadcast(&q->non_full);
266  }
267  *head = q->elem[q->head];
268  q->head = (q->head + 1) % N;
269  q->length--;
270  } /* else deadline exceeded */
271  gpr_mu_unlock(&q->mu);
272  return result;
273  }
274 #endif /* 0 */
275 
276 #ifdef __cplusplus
277 } // extern "C"
278 
279 namespace grpc_core {
280 
281 class mu_guard {
282  public:
283  mu_guard(gpr_mu* mu) : mu_(mu) { gpr_mu_lock(mu); }
284  ~mu_guard() { gpr_mu_unlock(mu_); }
285 
286  mu_guard(const mu_guard&) = delete;
287  mu_guard& operator=(const mu_guard&) = delete;
288 
289  private:
290  gpr_mu* const mu_;
291 };
292 
293 } // namespace grpc_core
294 #endif
295 
296 #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:34
#define GPRAPI
Definition: port_platform.h:466
pthread_once_t gpr_once
Definition: sync_posix.h:28
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:39
GPRAPI void gpr_stats_inc(gpr_stats_counter *c, intptr_t inc)
*c += inc.
pthread_cond_t gpr_cv
Definition: sync_posix.h:27
GPRAPI void gpr_ref_init(gpr_refcount *r, int n)
— Reference counting —
Definition: sync_windows.h:24
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:47
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:26
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.