blob: 769142e425d22706dfb6bf6c791fd499b248a130 [file] [log] [blame]
ctiller3bf466f2014-12-19 16:21:57 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
ctiller3bf466f2014-12-19 16:21:57 -08004 * 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/alarm_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. */
Craig Tillera82950e2015-09-22 12:33:20 -070046static void adjust_upwards(grpc_alarm **first, gpr_uint32 i, grpc_alarm *t) {
47 while (i > 0) {
48 gpr_uint32 parent = (gpr_uint32)(((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 }
ctiller3bf466f2014-12-19 16:21:57 -080054 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. */
Craig Tillera82950e2015-09-22 12:33:20 -070061static void adjust_downwards(grpc_alarm **first, gpr_uint32 i,
62 gpr_uint32 length, grpc_alarm *t) {
63 for (;;) {
64 gpr_uint32 left_child = 1u + 2u * i;
65 gpr_uint32 right_child;
66 gpr_uint32 next_i;
67 if (left_child >= length) break;
68 right_child = left_child + 1;
69 next_i = right_child < length &&
70 gpr_time_cmp(first[left_child]->deadline,
71 first[right_child]->deadline) < 0
72 ? right_child
73 : left_child;
74 if (gpr_time_cmp(t->deadline, first[next_i]->deadline) >= 0) break;
75 first[i] = first[next_i];
76 first[i]->heap_index = i;
77 i = next_i;
78 }
ctiller3bf466f2014-12-19 16:21:57 -080079 first[i] = t;
80 t->heap_index = i;
81}
82
83#define SHRINK_MIN_ELEMS 8
84#define SHRINK_FULLNESS_FACTOR 2
85
Craig Tillera82950e2015-09-22 12:33:20 -070086static void maybe_shrink(grpc_alarm_heap *heap) {
87 if (heap->alarm_count >= 8 &&
88 heap->alarm_count <= heap->alarm_capacity / SHRINK_FULLNESS_FACTOR / 2) {
89 heap->alarm_capacity = heap->alarm_count * SHRINK_FULLNESS_FACTOR;
90 heap->alarms =
91 gpr_realloc(heap->alarms, heap->alarm_capacity * sizeof(grpc_alarm *));
92 }
ctiller3bf466f2014-12-19 16:21:57 -080093}
94
Craig Tillera82950e2015-09-22 12:33:20 -070095static void note_changed_priority(grpc_alarm_heap *heap, grpc_alarm *alarm) {
Craig Tiller3121fd42015-09-10 09:56:20 -070096 gpr_uint32 i = alarm->heap_index;
Craig Tillera82950e2015-09-22 12:33:20 -070097 gpr_uint32 parent = (gpr_uint32)(((int)i - 1) / 2);
98 if (gpr_time_cmp(heap->alarms[parent]->deadline, alarm->deadline) < 0) {
99 adjust_upwards(heap->alarms, i, alarm);
100 } else {
101 adjust_downwards(heap->alarms, i, heap->alarm_count, alarm);
102 }
ctiller3bf466f2014-12-19 16:21:57 -0800103}
104
Craig Tillera82950e2015-09-22 12:33:20 -0700105void grpc_alarm_heap_init(grpc_alarm_heap *heap) {
106 memset(heap, 0, sizeof(*heap));
ctiller3bf466f2014-12-19 16:21:57 -0800107}
108
Craig Tillera82950e2015-09-22 12:33:20 -0700109void grpc_alarm_heap_destroy(grpc_alarm_heap *heap) { gpr_free(heap->alarms); }
ctiller3bf466f2014-12-19 16:21:57 -0800110
Craig Tillera82950e2015-09-22 12:33:20 -0700111int grpc_alarm_heap_add(grpc_alarm_heap *heap, grpc_alarm *alarm) {
112 if (heap->alarm_count == heap->alarm_capacity) {
113 heap->alarm_capacity =
114 GPR_MAX(heap->alarm_capacity + 1, heap->alarm_capacity * 3 / 2);
115 heap->alarms =
116 gpr_realloc(heap->alarms, heap->alarm_capacity * sizeof(grpc_alarm *));
117 }
ctiller3bf466f2014-12-19 16:21:57 -0800118 alarm->heap_index = heap->alarm_count;
Craig Tillera82950e2015-09-22 12:33:20 -0700119 adjust_upwards(heap->alarms, heap->alarm_count, alarm);
ctiller3bf466f2014-12-19 16:21:57 -0800120 heap->alarm_count++;
121 return alarm->heap_index == 0;
122}
123
Craig Tillera82950e2015-09-22 12:33:20 -0700124void grpc_alarm_heap_remove(grpc_alarm_heap *heap, grpc_alarm *alarm) {
Craig Tiller3121fd42015-09-10 09:56:20 -0700125 gpr_uint32 i = alarm->heap_index;
Craig Tillera82950e2015-09-22 12:33:20 -0700126 if (i == heap->alarm_count - 1) {
127 heap->alarm_count--;
128 maybe_shrink(heap);
129 return;
130 }
ctiller3bf466f2014-12-19 16:21:57 -0800131 heap->alarms[i] = heap->alarms[heap->alarm_count - 1];
132 heap->alarms[i]->heap_index = i;
133 heap->alarm_count--;
Craig Tillera82950e2015-09-22 12:33:20 -0700134 maybe_shrink(heap);
135 note_changed_priority(heap, heap->alarms[i]);
ctiller3bf466f2014-12-19 16:21:57 -0800136}
137
Craig Tillera82950e2015-09-22 12:33:20 -0700138int grpc_alarm_heap_is_empty(grpc_alarm_heap *heap) {
ctiller3bf466f2014-12-19 16:21:57 -0800139 return heap->alarm_count == 0;
140}
141
Craig Tillera82950e2015-09-22 12:33:20 -0700142grpc_alarm *grpc_alarm_heap_top(grpc_alarm_heap *heap) {
ctiller3bf466f2014-12-19 16:21:57 -0800143 return heap->alarms[0];
144}
145
Craig Tillera82950e2015-09-22 12:33:20 -0700146void grpc_alarm_heap_pop(grpc_alarm_heap *heap) {
147 grpc_alarm_heap_remove(heap, grpc_alarm_heap_top(heap));
Craig Tiller190d3602015-02-18 09:23:38 -0800148}