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 : #include "src/core/iomgr/closure.h"
35 :
36 : #include <grpc/support/alloc.h>
37 :
38 54123283 : void grpc_closure_init(grpc_closure *closure, grpc_iomgr_cb_func cb,
39 : void *cb_arg) {
40 54123283 : closure->cb = cb;
41 54123283 : closure->cb_arg = cb_arg;
42 54123283 : closure->final_data = 0;
43 54123283 : }
44 :
45 51890630 : void grpc_closure_list_add(grpc_closure_list *closure_list,
46 : grpc_closure *closure, int success) {
47 103781260 : if (closure == NULL) return;
48 51856487 : closure->final_data = (success != 0);
49 51856487 : if (closure_list->head == NULL) {
50 33998500 : closure_list->head = closure;
51 : } else {
52 17857987 : closure_list->tail->final_data |= (gpr_uintptr)closure;
53 : }
54 51856487 : closure_list->tail = closure;
55 : }
56 :
57 76845559 : int grpc_closure_list_empty(grpc_closure_list closure_list) {
58 76845559 : return closure_list.head == NULL;
59 : }
60 :
61 447364 : void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst) {
62 447364 : if (src->head == NULL) {
63 877474 : return;
64 : }
65 17233 : if (dst->head == NULL) {
66 17227 : *dst = *src;
67 : } else {
68 6 : dst->tail->final_data |= (gpr_uintptr)src->head;
69 6 : dst->tail = src->tail;
70 : }
71 17233 : src->head = src->tail = NULL;
72 : }
73 :
74 : typedef struct {
75 : grpc_iomgr_cb_func cb;
76 : void *cb_arg;
77 : grpc_closure wrapper;
78 : } wrapped_closure;
79 :
80 2169178 : static void closure_wrapper(grpc_exec_ctx *exec_ctx, void *arg, int success) {
81 2169048 : wrapped_closure *wc = arg;
82 2169178 : grpc_iomgr_cb_func cb = wc->cb;
83 2169178 : void *cb_arg = wc->cb_arg;
84 2169178 : gpr_free(wc);
85 2169393 : cb(exec_ctx, cb_arg, success);
86 2169505 : }
87 :
88 2169473 : grpc_closure *grpc_closure_create(grpc_iomgr_cb_func cb, void *cb_arg) {
89 2169473 : wrapped_closure *wc = gpr_malloc(sizeof(*wc));
90 2169498 : wc->cb = cb;
91 2169498 : wc->cb_arg = cb_arg;
92 2169498 : grpc_closure_init(&wc->wrapper, closure_wrapper, wc);
93 2169483 : return &wc->wrapper;
94 : }
95 :
96 128 : grpc_closure *grpc_closure_next(grpc_closure *closure) {
97 128 : return (grpc_closure *)(closure->final_data & ~(gpr_uintptr)1);
98 : }
|