1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef HIVIEW_BASE_EVENT_PRIORITY_QUEUE_H 16 #define HIVIEW_BASE_EVENT_PRIORITY_QUEUE_H 17 #include <queue> 18 namespace OHOS { 19 namespace HiviewDFX { 20 using Task = std::function<void()>; 21 template<typename T> 22 class EventPriorityQueue : public std::priority_queue<T, std::vector<T>> { 23 public: remove(uint64_t seq)24 bool remove(uint64_t seq) 25 { 26 auto it = std::find_if(this->c.begin(), this->c.end(), [seq](T event) { 27 return event.seq == seq; 28 }); 29 if (it != this->c.end()) { 30 this->c.erase(it); 31 std::make_heap(this->c.begin(), this->c.end(), this->comp); 32 return true; 33 } else { 34 return false; 35 }; 36 }; 37 ShrinkIfNeedLocked()38 void ShrinkIfNeedLocked() 39 { 40 if ((this->c.capacity() / this->c.size()) > 10) { // 10 times, begin to shrink 41 this->c.shrink_to_fit(); 42 } 43 } 44 }; 45 } // namespace HiviewDFX 46 } // namespace OHOS 47 #endif // HIVIEW_BASE_EVENT_LOOP_H