LCOV - code coverage report
Current view: top level - src/core/support - sync.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 45 48 93.8 %
Date: 2015-10-10 Functions: 12 13 92.3 %

          Line data    Source code
       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             : /* Generic implementation of synchronization primitives. */
      35             : 
      36             : #include <grpc/support/log.h>
      37             : #include <grpc/support/sync.h>
      38             : #include <grpc/support/atm.h>
      39             : 
      40             : /* Number of mutexes to allocate for events, to avoid lock contention.
      41             :    Should be a prime. */
      42             : enum { event_sync_partitions = 31 };
      43             : 
      44             : /* Events are partitioned by address to avoid lock contention. */
      45             : static struct sync_array_s {
      46             :   gpr_mu mu;
      47             :   gpr_cv cv;
      48             : } sync_array[event_sync_partitions];
      49             : 
      50             : /* This routine is executed once on first use, via event_once */
      51             : static gpr_once event_once = GPR_ONCE_INIT;
      52          21 : static void event_initialize(void) {
      53             :   int i;
      54         672 :   for (i = 0; i != event_sync_partitions; i++) {
      55         651 :     gpr_mu_init(&sync_array[i].mu);
      56         651 :     gpr_cv_init(&sync_array[i].cv);
      57             :   }
      58          21 : }
      59             : 
      60             : /* Hash ev into an element of sync_array[]. */
      61       20991 : static struct sync_array_s *hash(gpr_event *ev) {
      62       20991 :   return &sync_array[((gpr_uintptr)ev) % event_sync_partitions];
      63             : }
      64             : 
      65         302 : void gpr_event_init(gpr_event *ev) {
      66         302 :   gpr_once_init(&event_once, &event_initialize);
      67         302 :   ev->state = 0;
      68         302 : }
      69             : 
      70         271 : void gpr_event_set(gpr_event *ev, void *value) {
      71         271 :   struct sync_array_s *s = hash(ev);
      72         271 :   gpr_mu_lock(&s->mu);
      73         271 :   GPR_ASSERT(gpr_atm_acq_load(&ev->state) == 0);
      74         271 :   gpr_atm_rel_store(&ev->state, (gpr_atm)value);
      75         271 :   gpr_cv_broadcast(&s->cv);
      76         271 :   gpr_mu_unlock(&s->mu);
      77         270 :   GPR_ASSERT(value != NULL);
      78         270 : }
      79             : 
      80          11 : void *gpr_event_get(gpr_event *ev) {
      81          11 :   return (void *)gpr_atm_acq_load(&ev->state);
      82             : }
      83             : 
      84       20829 : void *gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline) {
      85       20829 :   void *result = (void *)gpr_atm_acq_load(&ev->state);
      86       20829 :   if (result == NULL) {
      87       20720 :     struct sync_array_s *s = hash(ev);
      88       20720 :     gpr_mu_lock(&s->mu);
      89             :     do {
      90       20964 :       result = (void *)gpr_atm_acq_load(&ev->state);
      91       20964 :     } while (result == NULL && !gpr_cv_wait(&s->cv, &s->mu, abs_deadline));
      92       20722 :     gpr_mu_unlock(&s->mu);
      93             :   }
      94       20830 :   return result;
      95             : }
      96             : 
      97     5098212 : void gpr_ref_init(gpr_refcount *r, int n) { gpr_atm_rel_store(&r->count, n); }
      98             : 
      99   101477385 : void gpr_ref(gpr_refcount *r) { gpr_atm_no_barrier_fetch_add(&r->count, 1); }
     100             : 
     101           0 : void gpr_refn(gpr_refcount *r, int n) {
     102           0 :   gpr_atm_no_barrier_fetch_add(&r->count, n);
     103           0 : }
     104             : 
     105   121197854 : int gpr_unref(gpr_refcount *r) {
     106   121197854 :   gpr_atm prior = gpr_atm_full_fetch_add(&r->count, -1);
     107   121197854 :   GPR_ASSERT(prior > 0);
     108   121197854 :   return prior == 1;
     109             : }
     110             : 
     111          50 : void gpr_stats_init(gpr_stats_counter *c, gpr_intptr n) {
     112          50 :   gpr_atm_rel_store(&c->value, n);
     113          50 : }
     114             : 
     115    55167137 : void gpr_stats_inc(gpr_stats_counter *c, gpr_intptr inc) {
     116    55167137 :   gpr_atm_no_barrier_fetch_add(&c->value, inc);
     117    55167137 : }
     118             : 
     119         120 : gpr_intptr gpr_stats_read(const gpr_stats_counter *c) {
     120             :   /* don't need acquire-load, but we have no no-barrier load yet */
     121         120 :   return gpr_atm_acq_load(&c->value);
     122             : }

Generated by: LCOV version 1.10