34 #ifndef GRPC_SUPPORT_SYNC_H
35 #define GRPC_SUPPORT_SYNC_H
56 #include <grpc/support/port_platform.h>
57 #include <grpc/support/sync_generic.h>
59 #if defined(GPR_POSIX_SYNC)
60 #include <grpc/support/sync_posix.h>
61 #elif defined(GPR_WIN32)
62 #include <grpc/support/sync_win32.h>
63 #elif !defined(GPR_CUSTOM_SYNC)
64 #error Unable to determine platform for sync
67 #include <grpc/support/time.h>
68 #include <grpc/support/cancellable_platform.h>
82 void gpr_mu_init(
gpr_mu *mu);
86 void gpr_mu_destroy(
gpr_mu *mu);
91 void gpr_mu_lock(
gpr_mu *mu);
95 void gpr_mu_unlock(
gpr_mu *mu);
101 int gpr_mu_trylock(
gpr_mu *mu);
110 void gpr_cv_init(gpr_cv *cv);
114 void gpr_cv_destroy(gpr_cv *cv);
133 void gpr_cv_signal(gpr_cv *cv);
136 void gpr_cv_broadcast(gpr_cv *cv);
171 void gpr_once_init(gpr_once *once,
void (*init_routine)(
void));
186 void gpr_event_set(
gpr_event *ev,
void *value);
248 typedef struct queue {
261 void queue_init(queue *q) {
263 gpr_cv_init(&q->non_empty);
264 gpr_cv_init(&q->non_full);
270 void queue_destroy(queue *q) {
271 gpr_mu_destroy(&q->mu);
272 gpr_cv_destroy(&q->non_empty);
273 gpr_cv_destroy(&q->non_full);
277 void queue_append(queue *q,
int x) {
285 while (q->length == N) {
286 gpr_cv_wait(&q->non_full, &q->mu, gpr_inf_future);
288 if (q->length == 0) {
291 gpr_cv_broadcast(&q->non_empty);
293 q->elem[(q->head + q->length) % N] = x;
295 gpr_mu_unlock(&q->mu);
300 int queue_try_append(queue *q,
int x) {
302 if (gpr_mu_trylock(&q->mu)) {
303 if (q->length != N) {
304 if (q->length == 0) {
305 gpr_cv_broadcast(&q->non_empty);
307 q->elem[(q->head + q->length) % N] = x;
311 gpr_mu_unlock(&q->mu);
319 int queue_remove(queue *q,
int *head,
gpr_timespec abs_deadline) {
327 while (q->length == 0 &&
328 !gpr_cv_wait(&q->non_empty, &q->mu, abs_deadline)) {
330 if (q->length != 0) {
332 if (q->length == N) {
333 gpr_cv_broadcast(&q->non_full);
335 *head = q->elem[q->head];
336 q->head = (q->head + 1) % N;
339 gpr_mu_unlock(&q->mu);
Definition: sync_generic.h:49
Definition: sync_generic.h:54
Definition: cancellable_platform.h:50
Definition: sync_win32.h:41
Definition: sync_generic.h:41