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/timer_heap.h" |
| 35 |
| 36 #include <string.h> |
| 37 |
| 38 #include <grpc/support/alloc.h> |
| 39 #include <grpc/support/useful.h> |
| 40 |
| 41 /* Adjusts a heap so as to move a hole at position i closer to the root, |
| 42 until a suitable position is found for element t. Then, copies t into that |
| 43 position. This functor is called each time immediately after modifying a |
| 44 value in the underlying container, with the offset of the modified element as |
| 45 its argument. */ |
| 46 static void adjust_upwards(grpc_timer **first, uint32_t i, grpc_timer *t) { |
| 47 while (i > 0) { |
| 48 uint32_t parent = (uint32_t)(((int)i - 1) / 2); |
| 49 if (gpr_time_cmp(first[parent]->deadline, t->deadline) <= 0) break; |
| 50 first[i] = first[parent]; |
| 51 first[i]->heap_index = i; |
| 52 i = parent; |
| 53 } |
| 54 first[i] = t; |
| 55 t->heap_index = i; |
| 56 } |
| 57 |
| 58 /* Adjusts a heap so as to move a hole at position i farther away from the root, |
| 59 until a suitable position is found for element t. Then, copies t into that |
| 60 position. */ |
| 61 static void adjust_downwards(grpc_timer **first, uint32_t i, uint32_t length, |
| 62 grpc_timer *t) { |
| 63 for (;;) { |
| 64 uint32_t left_child = 1u + 2u * i; |
| 65 if (left_child >= length) break; |
| 66 uint32_t right_child = left_child + 1; |
| 67 uint32_t next_i = right_child < length && |
| 68 gpr_time_cmp(first[left_child]->deadline, |
| 69 first[right_child]->deadline) > 0 |
| 70 ? right_child |
| 71 : left_child; |
| 72 if (gpr_time_cmp(t->deadline, first[next_i]->deadline) <= 0) break; |
| 73 first[i] = first[next_i]; |
| 74 first[i]->heap_index = i; |
| 75 i = next_i; |
| 76 } |
| 77 first[i] = t; |
| 78 t->heap_index = i; |
| 79 } |
| 80 |
| 81 #define SHRINK_MIN_ELEMS 8 |
| 82 #define SHRINK_FULLNESS_FACTOR 2 |
| 83 |
| 84 static void maybe_shrink(grpc_timer_heap *heap) { |
| 85 if (heap->timer_count >= 8 && |
| 86 heap->timer_count <= heap->timer_capacity / SHRINK_FULLNESS_FACTOR / 2) { |
| 87 heap->timer_capacity = heap->timer_count * SHRINK_FULLNESS_FACTOR; |
| 88 heap->timers = |
| 89 gpr_realloc(heap->timers, heap->timer_capacity * sizeof(grpc_timer *)); |
| 90 } |
| 91 } |
| 92 |
| 93 static void note_changed_priority(grpc_timer_heap *heap, grpc_timer *timer) { |
| 94 uint32_t i = timer->heap_index; |
| 95 uint32_t parent = (uint32_t)(((int)i - 1) / 2); |
| 96 if (gpr_time_cmp(heap->timers[parent]->deadline, timer->deadline) > 0) { |
| 97 adjust_upwards(heap->timers, i, timer); |
| 98 } else { |
| 99 adjust_downwards(heap->timers, i, heap->timer_count, timer); |
| 100 } |
| 101 } |
| 102 |
| 103 void grpc_timer_heap_init(grpc_timer_heap *heap) { |
| 104 memset(heap, 0, sizeof(*heap)); |
| 105 } |
| 106 |
| 107 void grpc_timer_heap_destroy(grpc_timer_heap *heap) { gpr_free(heap->timers); } |
| 108 |
| 109 int grpc_timer_heap_add(grpc_timer_heap *heap, grpc_timer *timer) { |
| 110 if (heap->timer_count == heap->timer_capacity) { |
| 111 heap->timer_capacity = |
| 112 GPR_MAX(heap->timer_capacity + 1, heap->timer_capacity * 3 / 2); |
| 113 heap->timers = |
| 114 gpr_realloc(heap->timers, heap->timer_capacity * sizeof(grpc_timer *)); |
| 115 } |
| 116 timer->heap_index = heap->timer_count; |
| 117 adjust_upwards(heap->timers, heap->timer_count, timer); |
| 118 heap->timer_count++; |
| 119 return timer->heap_index == 0; |
| 120 } |
| 121 |
| 122 void grpc_timer_heap_remove(grpc_timer_heap *heap, grpc_timer *timer) { |
| 123 uint32_t i = timer->heap_index; |
| 124 if (i == heap->timer_count - 1) { |
| 125 heap->timer_count--; |
| 126 maybe_shrink(heap); |
| 127 return; |
| 128 } |
| 129 heap->timers[i] = heap->timers[heap->timer_count - 1]; |
| 130 heap->timers[i]->heap_index = i; |
| 131 heap->timer_count--; |
| 132 maybe_shrink(heap); |
| 133 note_changed_priority(heap, heap->timers[i]); |
| 134 } |
| 135 |
| 136 int grpc_timer_heap_is_empty(grpc_timer_heap *heap) { |
| 137 return heap->timer_count == 0; |
| 138 } |
| 139 |
| 140 grpc_timer *grpc_timer_heap_top(grpc_timer_heap *heap) { |
| 141 return heap->timers[0]; |
| 142 } |
| 143 |
| 144 void grpc_timer_heap_pop(grpc_timer_heap *heap) { |
| 145 grpc_timer_heap_remove(heap, grpc_timer_heap_top(heap)); |
| 146 } |
OLD | NEW |