00001 /** @file 00002 00003 Queue of Events sorted by the "timeout_at" field impl as binary heap 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 #include "P_EventSystem.h" 00025 00026 PriorityEventQueue::PriorityEventQueue() 00027 { 00028 last_check_time = ink_get_based_hrtime_internal(); 00029 last_check_buckets = last_check_time / PQ_BUCKET_TIME(0); 00030 } 00031 00032 void 00033 PriorityEventQueue::check_ready(ink_hrtime now, EThread * t) 00034 { 00035 int i, j, k = 0; 00036 uint32_t check_buckets = (uint32_t) (now / PQ_BUCKET_TIME(0)); 00037 uint32_t todo_buckets = check_buckets ^ last_check_buckets; 00038 last_check_time = now; 00039 last_check_buckets = check_buckets; 00040 todo_buckets &= ((1 << (N_PQ_LIST - 1)) - 1); 00041 while (todo_buckets) { 00042 k++; 00043 todo_buckets >>= 1; 00044 } 00045 for (i = 1; i <= k; i++) { 00046 Event *e; 00047 Que(Event, link) q = after[i]; 00048 after[i].clear(); 00049 while ((e = q.dequeue()) != NULL) { 00050 if (e->cancelled) { 00051 e->in_the_priority_queue = 0; 00052 e->cancelled = 0; 00053 EVENT_FREE(e, eventAllocator, t); 00054 } else { 00055 ink_hrtime tt = e->timeout_at - now; 00056 for (j = i; j > 0 && tt <= PQ_BUCKET_TIME(j - 1);) 00057 j--; 00058 e->in_heap = j; 00059 after[j].enqueue(e); 00060 } 00061 } 00062 } 00063 }