1 2 /** 3 * Tencent is pleased to support the open source community by making MSEC available. 4 * 5 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. 6 * 7 * Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. You may 9 * obtain a copy of the License at 10 * 11 * https://opensource.org/licenses/GPL-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software distributed under the 14 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 15 * either express or implied. See the License for the specific language governing permissions 16 * and limitations under the License. 17 */ 18 19 20 /** 21 * @file heap_timer.cpp 22 */ 23 24 #include "heap_timer.h" 25 #include "micro_thread.h" 26 27 using namespace NS_MICRO_THREAD; 28 29 30 /** 31 * @brief ���캯�� 32 */ 33 CTimerMng::CTimerMng(uint32_t max_item) 34 { 35 #define TIMER_MIN 100000 36 37 if (max_item < TIMER_MIN) 38 { 39 max_item = TIMER_MIN; 40 } 41 42 _heap = new HeapList(max_item); 43 } 44 45 46 /** 47 * @brief �������� 48 */ 49 CTimerMng::~CTimerMng() 50 { 51 if (_heap) { 52 delete _heap; 53 _heap = NULL; 54 } 55 } 56 57 58 /** 59 * @brief ��ʱ�����ú��� 60 * @param timerable ��ʱ������ 61 * @param interval ��ʱ�ļ�� ms��λ 62 * @return �ɹ�����true, ����ʧ�� 63 */ 64 bool CTimerMng::start_timer(CTimerNotify* timerable, uint32_t interval) 65 { 66 if (!_heap || !timerable) { 67 return false; 68 } 69 70 utime64_t now_ms = MtFrame::Instance()->GetLastClock(); 71 timerable->set_expired_time(now_ms + interval); 72 int32_t ret = _heap->HeapPush(timerable); 73 if (ret < 0) { 74 MTLOG_ERROR("timer start failed(%p), ret(%d)", timerable, ret); 75 return false; 76 } 77 78 return true; 79 } 80 81 /** 82 * @brief ��ʱ��ֹͣ�ӿں��� 83 * @param timerable ��ʱ������ 84 */ 85 void CTimerMng::stop_timer(CTimerNotify* timerable) 86 { 87 if (!_heap || !timerable) { 88 return; 89 } 90 91 _heap->HeapDelete(timerable); 92 return; 93 } 94 95 /** 96 * @brief ��ʱ����ʱ��⺯�� 97 */ 98 void CTimerMng::check_expired() 99 { 100 if (!_heap) { 101 return; 102 } 103 104 utime64_t now = MtFrame::Instance()->GetLastClock(); 105 CTimerNotify* timer = dynamic_cast<CTimerNotify*>(_heap->HeapTop()); 106 while (timer && (timer->get_expired_time() <= now)) 107 { 108 _heap->HeapDelete(timer); 109 timer->timer_notify(); 110 timer = dynamic_cast<CTimerNotify*>(_heap->HeapTop()); 111 } 112 }; 113 114 115 116