OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 void grpc_closure_init(grpc_closure *closure, grpc_iomgr_cb_func cb, |
| 39 void *cb_arg) { |
| 40 closure->cb = cb; |
| 41 closure->cb_arg = cb_arg; |
| 42 closure->final_data = 0; |
| 43 } |
| 44 |
| 45 void grpc_closure_list_add(grpc_closure_list *closure_list, |
| 46 grpc_closure *closure, bool success) { |
| 47 if (closure == NULL) return; |
| 48 closure->final_data = (success != 0); |
| 49 if (closure_list->head == NULL) { |
| 50 closure_list->head = closure; |
| 51 } else { |
| 52 closure_list->tail->final_data |= (uintptr_t)closure; |
| 53 } |
| 54 closure_list->tail = closure; |
| 55 } |
| 56 |
| 57 bool grpc_closure_list_empty(grpc_closure_list closure_list) { |
| 58 return closure_list.head == NULL; |
| 59 } |
| 60 |
| 61 void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst) { |
| 62 if (src->head == NULL) { |
| 63 return; |
| 64 } |
| 65 if (dst->head == NULL) { |
| 66 *dst = *src; |
| 67 } else { |
| 68 dst->tail->final_data |= (uintptr_t)src->head; |
| 69 dst->tail = src->tail; |
| 70 } |
| 71 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 static void closure_wrapper(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
| 81 wrapped_closure *wc = arg; |
| 82 grpc_iomgr_cb_func cb = wc->cb; |
| 83 void *cb_arg = wc->cb_arg; |
| 84 gpr_free(wc); |
| 85 cb(exec_ctx, cb_arg, success); |
| 86 } |
| 87 |
| 88 grpc_closure *grpc_closure_create(grpc_iomgr_cb_func cb, void *cb_arg) { |
| 89 wrapped_closure *wc = gpr_malloc(sizeof(*wc)); |
| 90 wc->cb = cb; |
| 91 wc->cb_arg = cb_arg; |
| 92 grpc_closure_init(&wc->wrapper, closure_wrapper, wc); |
| 93 return &wc->wrapper; |
| 94 } |
| 95 |
| 96 grpc_closure *grpc_closure_next(grpc_closure *closure) { |
| 97 return (grpc_closure *)(closure->final_data & ~(uintptr_t)1); |
| 98 } |
OLD | NEW |