sync.c 3.3 KB

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