Exemplo n.º 1
0
/* Public API */
void grpc_resource_quota_resize(grpc_resource_quota *resource_quota,
                                size_t size) {
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  rq_resize_args *a = gpr_malloc(sizeof(*a));
  a->resource_quota = grpc_resource_quota_internal_ref(resource_quota);
  a->size = (int64_t)size;
  grpc_closure_init(&a->closure, rq_resize, a);
  grpc_combiner_execute(&exec_ctx, resource_quota->combiner, &a->closure,
                        GRPC_ERROR_NONE, false);
  grpc_exec_ctx_finish(&exec_ctx);
}
Exemplo n.º 2
0
void grpc_resource_user_finish_reclamation(grpc_exec_ctx *exec_ctx,
                                           grpc_resource_user *resource_user) {
  if (grpc_resource_quota_trace) {
    gpr_log(GPR_DEBUG, "RQ %s %s: reclamation complete",
            resource_user->resource_quota->name, resource_user->name);
  }
  grpc_combiner_execute(
      exec_ctx, resource_user->resource_quota->combiner,
      &resource_user->resource_quota->rq_reclamation_done_closure,
      GRPC_ERROR_NONE, false);
}
Exemplo n.º 3
0
static void ru_unref_by(grpc_exec_ctx *exec_ctx,
                        grpc_resource_user *resource_user, gpr_atm amount) {
  GPR_ASSERT(amount > 0);
  gpr_atm old = gpr_atm_full_fetch_add(&resource_user->refs, -amount);
  GPR_ASSERT(old >= amount);
  if (old == amount) {
    grpc_combiner_execute(exec_ctx, resource_user->resource_quota->combiner,
                          &resource_user->destroy_closure, GRPC_ERROR_NONE,
                          false);
  }
}
Exemplo n.º 4
0
void grpc_resource_user_post_reclaimer(grpc_exec_ctx *exec_ctx,
                                       grpc_resource_user *resource_user,
                                       bool destructive,
                                       grpc_closure *closure) {
  GPR_ASSERT(resource_user->reclaimers[destructive] == NULL);
  if (gpr_atm_acq_load(&resource_user->shutdown) > 0) {
    grpc_exec_ctx_sched(exec_ctx, closure, GRPC_ERROR_CANCELLED, NULL);
    return;
  }
  resource_user->reclaimers[destructive] = closure;
  grpc_combiner_execute(exec_ctx, resource_user->resource_quota->combiner,
                        &resource_user->post_reclaimer_closure[destructive],
                        GRPC_ERROR_NONE, false);
}
Exemplo n.º 5
0
void grpc_resource_user_shutdown(grpc_exec_ctx *exec_ctx,
                                 grpc_resource_user *resource_user,
                                 grpc_closure *on_done) {
  gpr_mu_lock(&resource_user->mu);
  GPR_ASSERT(gpr_atm_no_barrier_load(&resource_user->on_done_destroy_closure) ==
             0);
  gpr_atm_no_barrier_store(&resource_user->on_done_destroy_closure,
                           (gpr_atm)on_done);
  if (resource_user->allocated == 0) {
    grpc_combiner_execute(exec_ctx, resource_user->resource_quota->combiner,
                          &resource_user->destroy_closure, GRPC_ERROR_NONE,
                          false);
  }
  gpr_mu_unlock(&resource_user->mu);
}
Exemplo n.º 6
0
void grpc_resource_user_free(grpc_exec_ctx *exec_ctx,
                             grpc_resource_user *resource_user, size_t size) {
  gpr_mu_lock(&resource_user->mu);
  bool was_zero_or_negative = resource_user->free_pool <= 0;
  resource_user->free_pool += (int64_t)size;
  if (grpc_resource_quota_trace) {
    gpr_log(GPR_DEBUG, "RQ %s %s: free %" PRIdPTR "; free_pool -> %" PRId64,
            resource_user->resource_quota->name, resource_user->name, size,
            resource_user->free_pool);
  }
  bool is_bigger_than_zero = resource_user->free_pool > 0;
  if (is_bigger_than_zero && was_zero_or_negative &&
      !resource_user->added_to_free_pool) {
    resource_user->added_to_free_pool = true;
    grpc_combiner_execute(exec_ctx, resource_user->resource_quota->combiner,
                          &resource_user->add_to_free_pool_closure,
                          GRPC_ERROR_NONE, false);
  }
  gpr_mu_unlock(&resource_user->mu);
  ru_unref_by(exec_ctx, resource_user, (gpr_atm)size);
}
Exemplo n.º 7
0
void grpc_resource_user_alloc(grpc_exec_ctx *exec_ctx,
                              grpc_resource_user *resource_user, size_t size,
                              grpc_closure *optional_on_done) {
  gpr_mu_lock(&resource_user->mu);
  grpc_closure *on_done_destroy = (grpc_closure *)gpr_atm_no_barrier_load(
      &resource_user->on_done_destroy_closure);
  if (on_done_destroy != NULL) {
    /* already shutdown */
    if (grpc_resource_quota_trace) {
      gpr_log(GPR_DEBUG, "RQ %s %s: alloc %" PRIdPTR " after shutdown",
              resource_user->resource_quota->name, resource_user->name, size);
    }
    grpc_exec_ctx_sched(
        exec_ctx, optional_on_done,
        GRPC_ERROR_CREATE("Buffer pool user is already shutdown"), NULL);
    gpr_mu_unlock(&resource_user->mu);
    return;
  }
  resource_user->allocated += (int64_t)size;
  resource_user->free_pool -= (int64_t)size;
  if (grpc_resource_quota_trace) {
    gpr_log(GPR_DEBUG, "RQ %s %s: alloc %" PRIdPTR "; allocated -> %" PRId64
                       ", free_pool -> %" PRId64,
            resource_user->resource_quota->name, resource_user->name, size,
            resource_user->allocated, resource_user->free_pool);
  }
  if (resource_user->free_pool < 0) {
    grpc_closure_list_append(&resource_user->on_allocated, optional_on_done,
                             GRPC_ERROR_NONE);
    if (!resource_user->allocating) {
      resource_user->allocating = true;
      grpc_combiner_execute(exec_ctx, resource_user->resource_quota->combiner,
                            &resource_user->allocate_closure, GRPC_ERROR_NONE,
                            false);
    }
  } else {
    grpc_exec_ctx_sched(exec_ctx, optional_on_done, GRPC_ERROR_NONE, NULL);
  }
  gpr_mu_unlock(&resource_user->mu);
}