00001 /** @file 00002 00003 Queue of Events sorted by the "at_timeout" field 00004 00005 @section license License 00006 00007 Licensed to the Apache Software Foundation (ASF) under one 00008 or more contributor license agreements. See the NOTICE file 00009 distributed with this work for additional information 00010 regarding copyright ownership. The ASF licenses this file 00011 to you under the Apache License, Version 2.0 (the 00012 "License"); you may not use this file except in compliance 00013 with the License. You may obtain a copy of the License at 00014 00015 http://www.apache.org/licenses/LICENSE-2.0 00016 00017 Unless required by applicable law or agreed to in writing, software 00018 distributed under the License is distributed on an "AS IS" BASIS, 00019 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 See the License for the specific language governing permissions and 00021 limitations under the License. 00022 */ 00023 00024 #ifndef _I_PriorityEventQueue_h_ 00025 #define _I_PriorityEventQueue_h_ 00026 00027 #include "libts.h" 00028 #include "I_Event.h" 00029 00030 00031 // <5ms, 10, 20, 40, 80, 160, 320, 640, 1280, 2560, 5120 00032 #define N_PQ_LIST 10 00033 #define PQ_BUCKET_TIME(_i) (HRTIME_MSECONDS(5) << (_i)) 00034 00035 class EThread; 00036 00037 struct PriorityEventQueue 00038 { 00039 00040 Que(Event, link) after[N_PQ_LIST]; 00041 ink_hrtime last_check_time; 00042 uint32_t last_check_buckets; 00043 00044 void enqueue(Event * e, ink_hrtime now) 00045 { 00046 ink_hrtime t = e->timeout_at - now; 00047 int i = 0; 00048 // equivalent but faster 00049 if (t <= PQ_BUCKET_TIME(3)) 00050 { 00051 if (t <= PQ_BUCKET_TIME(1)) { 00052 if (t <= PQ_BUCKET_TIME(0)) { 00053 i = 0; 00054 } else 00055 { 00056 i = 1; 00057 } 00058 } else { 00059 if (t <= PQ_BUCKET_TIME(2)) { 00060 i = 2; 00061 } else { 00062 i = 3; 00063 } 00064 } 00065 } else { 00066 if (t <= PQ_BUCKET_TIME(7)) { 00067 if (t <= PQ_BUCKET_TIME(5)) { 00068 if (t <= PQ_BUCKET_TIME(4)) { 00069 i = 4; 00070 } else { 00071 i = 5; 00072 } 00073 } else { 00074 if (t <= PQ_BUCKET_TIME(6)) { 00075 i = 6; 00076 } else { 00077 i = 7; 00078 } 00079 } 00080 } else { 00081 if (t <= PQ_BUCKET_TIME(8)) { 00082 i = 8; 00083 } else { 00084 i = 9; 00085 } 00086 } 00087 } 00088 e->in_the_priority_queue = 1; 00089 e->in_heap = i; 00090 after[i].enqueue(e); 00091 } 00092 00093 void remove(Event * e) 00094 { 00095 ink_assert(e->in_the_priority_queue); 00096 e->in_the_priority_queue = 0; 00097 after[e->in_heap].remove(e); 00098 } 00099 00100 Event *dequeue_ready(ink_hrtime t) 00101 { 00102 (void) t; 00103 Event *e = after[0].dequeue(); 00104 if (e) { 00105 ink_assert(e->in_the_priority_queue); 00106 e->in_the_priority_queue = 0; 00107 } 00108 return e; 00109 } 00110 00111 void check_ready(ink_hrtime now, EThread * t); 00112 00113 ink_hrtime earliest_timeout() 00114 { 00115 for (int i = 0; i < N_PQ_LIST; i++) { 00116 if (after[i].head) 00117 return last_check_time + (PQ_BUCKET_TIME(i) / 2); 00118 } 00119 return last_check_time + HRTIME_FOREVER; 00120 } 00121 00122 PriorityEventQueue(); 00123 }; 00124 00125 #endif